]> git.lizzy.rs Git - rust.git/commitdiff
Display secondary span for E0053 for Sort TypeErrors
authorKeith Yeung <kungfukeith11@gmail.com>
Wed, 10 Aug 2016 08:43:12 +0000 (01:43 -0700)
committerKeith Yeung <kungfukeith11@gmail.com>
Wed, 17 Aug 2016 22:12:51 +0000 (15:12 -0700)
src/librustc/hir/map/mod.rs
src/librustc/infer/error_reporting.rs
src/librustc/traits/error_reporting.rs
src/librustc_typeck/check/compare_method.rs

index 7e82a4a05a76461216731761e4ed6b1c9b33284e..04031fabc58667ecde8602b9fd83f9adf6760d4e 100644 (file)
@@ -569,6 +569,13 @@ pub fn expect_item(&self, id: NodeId) -> &'ast Item {
         }
     }
 
+    pub fn expect_impl_item(&self, id: NodeId) -> &'ast ImplItem {
+        match self.find(id) {
+            Some(NodeImplItem(item)) => item,
+            _ => bug!("expected impl item, found {}", self.node_to_string(id))
+        }
+    }
+
     pub fn expect_trait_item(&self, id: NodeId) -> &'ast TraitItem {
         match self.find(id) {
             Some(NodeTraitItem(item)) => item,
index 9a6375719c1bcd406138ce002a37e941c8bdc178..1e053d6bfdab22b3f57d2c47c3a9135db7407692 100644 (file)
@@ -523,6 +523,7 @@ fn note_error_origin(&self,
     pub fn note_type_err(&self,
                          diag: &mut DiagnosticBuilder<'tcx>,
                          origin: TypeOrigin,
+                         secondary_span: Option<(Span, String)>,
                          values: Option<ValuePairs<'tcx>>,
                          terr: &TypeError<'tcx>)
     {
@@ -553,6 +554,9 @@ pub fn note_type_err(&self,
         }
 
         diag.span_label(span, &terr);
+        if let Some((sp, msg)) = secondary_span {
+            diag.span_label(sp, &msg);
+        }
 
         self.note_error_origin(diag, &origin);
         self.check_and_note_conflicting_crates(diag, terr, span);
@@ -569,7 +573,7 @@ pub fn report_and_explain_type_error(&self,
             self.tcx.sess, trace.origin.span(), E0308,
             "{}", trace.origin.as_failure_str()
         );
-        self.note_type_err(&mut diag, trace.origin, Some(trace.values), terr);
+        self.note_type_err(&mut diag, trace.origin, None, Some(trace.values), terr);
         diag
     }
 
index 33db0053cda13547a9b33dd3a71cac10f6f958ad..d6f263fcebeb088f739bc5d3f55e164dc2e2df4c 100644 (file)
@@ -161,7 +161,7 @@ fn report_projection_error(&self,
                 self.tcx.sess, origin.span(), E0271,
                 "type mismatch resolving `{}`", predicate
             );
-            self.note_type_err(&mut diag, origin, values, err);
+            self.note_type_err(&mut diag, origin, None, values, err);
             self.note_obligation_cause(&mut diag, obligation);
             diag.emit();
         });
index 6bcf21563cb98c919e907e2e4f431660f6cb7b86..93badd981300622d5bbc3e39a98411440fe6fa80 100644 (file)
 use rustc::infer::{self, InferOk, TypeOrigin};
 use rustc::ty;
 use rustc::traits::{self, Reveal};
-use rustc::ty::error::ExpectedFound;
+use rustc::ty::error::{ExpectedFound, TypeError};
 use rustc::ty::subst::{Subst, Substs};
-use rustc::hir::map::Node;
-use rustc::hir::{ImplItemKind, TraitItem_};
+use rustc::hir::{ImplItemKind, TraitItem_, Ty_};
 
 use syntax::ast;
 use syntax_pos::Span;
@@ -300,6 +299,7 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
                                                  impl_m_span,
                                                  impl_m_body_id,
                                                  &impl_sig);
+        let impl_args = impl_sig.inputs.clone();
         let impl_fty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
             unsafety: impl_m.fty.unsafety,
             abi: impl_m.fty.abi,
@@ -318,6 +318,7 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
                                                  impl_m_span,
                                                  impl_m_body_id,
                                                  &trait_sig);
+        let trait_args = trait_sig.inputs.clone();
         let trait_fty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
             unsafety: trait_m.fty.unsafety,
             abi: trait_m.fty.abi,
@@ -331,16 +332,54 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
                    impl_fty,
                    trait_fty);
 
+            let impl_m_iter = match tcx.map.expect_impl_item(impl_m_node_id).node {
+                ImplItemKind::Method(ref impl_m_sig, _) => impl_m_sig.decl.inputs.iter(),
+                _ => bug!("{:?} is not a method", impl_m)
+            };
+
+            let (impl_err_span, trait_err_span) = match terr {
+                TypeError::Sorts(ExpectedFound { expected, found }) => {
+                    if let Some(trait_m_node_id) = tcx.map.as_local_node_id(trait_m.def_id) {
+                        let trait_m_iter = match tcx.map.expect_trait_item(trait_m_node_id).node {
+                            TraitItem_::MethodTraitItem(ref trait_m_sig, _) =>
+                                trait_m_sig.decl.inputs.iter(),
+                            _ => bug!("{:?} is not a MethodTraitItem", trait_m)
+                        };
+                        let impl_iter = impl_args.iter();
+                        let trait_iter = trait_args.iter();
+                        let arg_idx = impl_iter.zip(trait_iter)
+                                               .position(|(impl_arg_ty, trait_arg_ty)| {
+                                                *impl_arg_ty == found && *trait_arg_ty == expected
+                                               }).unwrap();
+                        impl_m_iter.zip(trait_m_iter)
+                                   .nth(arg_idx)
+                                   .map(|(impl_arg, trait_arg)|
+                                        (impl_arg.ty.span, Some(trait_arg.ty.span)))
+                                   .unwrap_or(
+                                    (origin.span(), tcx.map.span_if_local(trait_m.def_id)))
+                    } else {
+                        (origin.span(), tcx.map.span_if_local(trait_m.def_id))
+                    }
+                }
+                _ => (origin.span(), tcx.map.span_if_local(trait_m.def_id))
+            };
+
+            let origin = TypeOrigin::MethodCompatCheck(impl_err_span);
+
             let mut diag = struct_span_err!(
                 tcx.sess, origin.span(), E0053,
                 "method `{}` has an incompatible type for trait", trait_m.name
             );
+
             infcx.note_type_err(
-                &mut diag, origin,
+                &mut diag,
+                origin,
+                trait_err_span.map(|sp| (sp, format!("original trait requirement"))),
                 Some(infer::ValuePairs::Types(ExpectedFound {
-                    expected: trait_fty,
-                    found: impl_fty
-                })), &terr
+                     expected: trait_fty,
+                     found: impl_fty
+                })),
+                &terr
             );
             diag.emit();
             return
@@ -487,12 +526,9 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
                    trait_ty);
 
             // Locate the Span containing just the type of the offending impl
-            if let Some(impl_trait_node) = tcx.map.get_if_local(impl_c.def_id) {
-                if let Node::NodeImplItem(impl_trait_item) = impl_trait_node {
-                    if let ImplItemKind::Const(ref ty, _) = impl_trait_item.node {
-                        origin = TypeOrigin::Misc(ty.span);
-                    }
-                }
+            match tcx.map.expect_impl_item(impl_c_node_id).node {
+                ImplItemKind::Const(ref ty, _) => origin = TypeOrigin::Misc(ty.span),
+                _ => bug!("{:?} is not a impl const", impl_c)
             }
 
             let mut diag = struct_span_err!(
@@ -502,16 +538,16 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
             );
 
             // Add a label to the Span containing just the type of the item
-            if let Some(orig_trait_node) = tcx.map.get_if_local(trait_c.def_id) {
-                if let Node::NodeTraitItem(orig_trait_item) = orig_trait_node {
-                    if let TraitItem_::ConstTraitItem(ref ty, _) = orig_trait_item.node {
-                        diag.span_label(ty.span, &format!("original trait requirement"));
-                    }
-                }
-            }
+            let trait_c_node_id = tcx.map.as_local_node_id(trait_c.def_id).unwrap();
+            let trait_c_span = match tcx.map.expect_trait_item(trait_c_node_id).node {
+                TraitItem_::ConstTraitItem(ref ty, _) => ty.span,
+                _ => bug!("{:?} is not a trait const", trait_c)
+            };
 
             infcx.note_type_err(
-                &mut diag, origin,
+                &mut diag,
+                origin,
+                Some((trait_c_span, format!("original trait requirement"))),
                 Some(infer::ValuePairs::Types(ExpectedFound {
                     expected: trait_ty,
                     found: impl_ty