]> git.proxmox.com Git - rustc.git/blobdiff - src/librustc_typeck/check/cast.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / librustc_typeck / check / cast.rs
index ff100c261f14aaf6444eac232e14760bf9618e1e..981e6d2c47eef18c6886ecd24a5399f842080d3c 100644 (file)
 use super::FnCtxt;
 
 use crate::hir::def_id::DefId;
-use crate::lint;
 use crate::type_error_struct;
-use crate::util::common::ErrorReported;
-use rustc::middle::lang_items;
-use rustc::session::Session;
-use rustc::ty::adjustment::AllowTwoPhase;
-use rustc::ty::cast::{CastKind, CastTy};
-use rustc::ty::error::TypeError;
-use rustc::ty::subst::SubstsRef;
-use rustc::ty::{self, Ty, TypeAndMut, TypeFoldable};
 use rustc_ast::ast;
-use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
+use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorReported};
 use rustc_hir as hir;
-use rustc_infer::traits;
-use rustc_infer::traits::error_reporting::report_object_safety_error;
+use rustc_hir::lang_items;
+use rustc_middle::ty::adjustment::AllowTwoPhase;
+use rustc_middle::ty::cast::{CastKind, CastTy};
+use rustc_middle::ty::error::TypeError;
+use rustc_middle::ty::subst::SubstsRef;
+use rustc_middle::ty::{self, Ty, TypeAndMut, TypeFoldable};
+use rustc_session::lint;
+use rustc_session::Session;
 use rustc_span::Span;
+use rustc_trait_selection::traits;
+use rustc_trait_selection::traits::error_reporting::report_object_safety_error;
 
 /// Reifies a cast check to be checked once we have full type information for
 /// a function context.
@@ -526,8 +525,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
     /// can return Ok and create type errors in the fcx rather than returning
     /// directly. coercion-cast is handled in check instead of here.
     fn do_check(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError> {
-        use rustc::ty::cast::CastTy::*;
-        use rustc::ty::cast::IntTy::*;
+        use rustc_middle::ty::cast::CastTy::*;
+        use rustc_middle::ty::cast::IntTy::*;
 
         let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))
         {
@@ -537,7 +536,10 @@ impl<'a, 'tcx> CastCheck<'tcx> {
                 match self.expr_ty.kind {
                     ty::FnDef(..) => {
                         // Attempt a coercion to a fn pointer type.
-                        let f = self.expr_ty.fn_sig(fcx.tcx);
+                        let f = fcx.normalize_associated_types_in(
+                            self.expr.span,
+                            &self.expr_ty.fn_sig(fcx.tcx),
+                        );
                         let res = fcx.try_coerce(
                             self.expr,
                             self.expr_ty,
@@ -562,8 +564,9 @@ impl<'a, 'tcx> CastCheck<'tcx> {
                                 ty::Int(_)
                                 | ty::Uint(_)
                                 | ty::Float(_)
-                                | ty::Infer(ty::InferTy::IntVar(_))
-                                | ty::Infer(ty::InferTy::FloatVar(_)) => Err(CastError::NeedDeref),
+                                | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(_)) => {
+                                    Err(CastError::NeedDeref)
+                                }
                                 _ => Err(CastError::NeedViaPtr),
                             },
                             // array-ptr-cast
@@ -581,7 +584,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
 
         match (t_from, t_cast) {
             // These types have invariants! can't cast into them.
-            (_, Int(CEnum)) | (_, FnPtr) => Err(CastError::NonScalar),
+            (_, Int(CEnum) | FnPtr) => Err(CastError::NonScalar),
 
             // * -> Bool
             (_, Int(Bool)) => Err(CastError::CastToBool),
@@ -591,16 +594,11 @@ impl<'a, 'tcx> CastCheck<'tcx> {
             (_, Int(Char)) => Err(CastError::CastToChar),
 
             // prim -> float,ptr
-            (Int(Bool), Float) | (Int(CEnum), Float) | (Int(Char), Float) => {
-                Err(CastError::NeedViaInt)
-            }
+            (Int(Bool) | Int(CEnum) | Int(Char), Float) => Err(CastError::NeedViaInt),
 
-            (Int(Bool), Ptr(_))
-            | (Int(CEnum), Ptr(_))
-            | (Int(Char), Ptr(_))
-            | (Ptr(_), Float)
-            | (FnPtr, Float)
-            | (Float, Ptr(_)) => Err(CastError::IllegalCast),
+            (Int(Bool) | Int(CEnum) | Int(Char) | Float, Ptr(_)) | (Ptr(_) | FnPtr, Float) => {
+                Err(CastError::IllegalCast)
+            }
 
             // ptr -> *
             (Ptr(m_e), Ptr(m_c)) => self.check_ptr_ptr_cast(fcx, m_e, m_c), // ptr-ptr-cast
@@ -613,11 +611,9 @@ impl<'a, 'tcx> CastCheck<'tcx> {
 
             // prim -> prim
             (Int(CEnum), Int(_)) => Ok(CastKind::EnumCast),
-            (Int(Char), Int(_)) | (Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
+            (Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
 
-            (Int(_), Int(_)) | (Int(_), Float) | (Float, Int(_)) | (Float, Float) => {
-                Ok(CastKind::NumericCast)
-            }
+            (Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
         }
     }