Skip to content

Commit 3c4ecc9

Browse files
committed
Display secondary span for E0053 for Sort TypeErrors
1 parent 7ac11ca commit 3c4ecc9

File tree

4 files changed

+70
-23
lines changed

4 files changed

+70
-23
lines changed

src/librustc/hir/map/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,13 @@ impl<'ast> Map<'ast> {
569569
}
570570
}
571571

572+
pub fn expect_impl_item(&self, id: NodeId) -> &'ast ImplItem {
573+
match self.find(id) {
574+
Some(NodeImplItem(item)) => item,
575+
_ => bug!("expected impl item, found {}", self.node_to_string(id))
576+
}
577+
}
578+
572579
pub fn expect_trait_item(&self, id: NodeId) -> &'ast TraitItem {
573580
match self.find(id) {
574581
Some(NodeTraitItem(item)) => item,

src/librustc/infer/error_reporting.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
523523
pub fn note_type_err(&self,
524524
diag: &mut DiagnosticBuilder<'tcx>,
525525
origin: TypeOrigin,
526+
secondary_span: Option<(Span, String)>,
526527
values: Option<ValuePairs<'tcx>>,
527528
terr: &TypeError<'tcx>)
528529
{
@@ -553,6 +554,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
553554
}
554555

555556
diag.span_label(span, &terr);
557+
if let Some((sp, msg)) = secondary_span {
558+
diag.span_label(sp, &msg);
559+
}
556560

557561
self.note_error_origin(diag, &origin);
558562
self.check_and_note_conflicting_crates(diag, terr, span);
@@ -569,7 +573,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
569573
self.tcx.sess, trace.origin.span(), E0308,
570574
"{}", trace.origin.as_failure_str()
571575
);
572-
self.note_type_err(&mut diag, trace.origin, Some(trace.values), terr);
576+
self.note_type_err(&mut diag, trace.origin, None, Some(trace.values), terr);
573577
diag
574578
}
575579

src/librustc/traits/error_reporting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
161161
self.tcx.sess, origin.span(), E0271,
162162
"type mismatch resolving `{}`", predicate
163163
);
164-
self.note_type_err(&mut diag, origin, values, err);
164+
self.note_type_err(&mut diag, origin, None, values, err);
165165
self.note_obligation_cause(&mut diag, obligation);
166166
diag.emit();
167167
});

src/librustc_typeck/check/compare_method.rs

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ use middle::free_region::FreeRegionMap;
1212
use rustc::infer::{self, InferOk, TypeOrigin};
1313
use rustc::ty;
1414
use rustc::traits::{self, Reveal};
15-
use rustc::ty::error::ExpectedFound;
15+
use rustc::ty::error::{ExpectedFound, TypeError};
1616
use rustc::ty::subst::{Subst, Substs};
17-
use rustc::hir::map::Node;
18-
use rustc::hir::{ImplItemKind, TraitItem_};
17+
use rustc::hir::{ImplItemKind, TraitItem_, Ty_};
1918

2019
use syntax::ast;
2120
use syntax_pos::Span;
@@ -300,6 +299,7 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
300299
impl_m_span,
301300
impl_m_body_id,
302301
&impl_sig);
302+
let impl_args = impl_sig.inputs.clone();
303303
let impl_fty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
304304
unsafety: impl_m.fty.unsafety,
305305
abi: impl_m.fty.abi,
@@ -318,6 +318,7 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
318318
impl_m_span,
319319
impl_m_body_id,
320320
&trait_sig);
321+
let trait_args = trait_sig.inputs.clone();
321322
let trait_fty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
322323
unsafety: trait_m.fty.unsafety,
323324
abi: trait_m.fty.abi,
@@ -331,16 +332,54 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
331332
impl_fty,
332333
trait_fty);
333334

335+
let impl_m_iter = match tcx.map.expect_impl_item(impl_m_node_id).node {
336+
ImplItemKind::Method(ref impl_m_sig, _) => impl_m_sig.decl.inputs.iter(),
337+
_ => bug!("{:?} is not a method", impl_m)
338+
};
339+
340+
let (impl_err_span, trait_err_span) = match terr {
341+
TypeError::Sorts(ExpectedFound { expected, found }) => {
342+
if let Some(trait_m_node_id) = tcx.map.as_local_node_id(trait_m.def_id) {
343+
let trait_m_iter = match tcx.map.expect_trait_item(trait_m_node_id).node {
344+
TraitItem_::MethodTraitItem(ref trait_m_sig, _) =>
345+
trait_m_sig.decl.inputs.iter(),
346+
_ => bug!("{:?} is not a MethodTraitItem", trait_m)
347+
};
348+
let impl_iter = impl_args.iter();
349+
let trait_iter = trait_args.iter();
350+
let arg_idx = impl_iter.zip(trait_iter)
351+
.position(|(impl_arg_ty, trait_arg_ty)| {
352+
*impl_arg_ty == found && *trait_arg_ty == expected
353+
}).unwrap();
354+
impl_m_iter.zip(trait_m_iter)
355+
.nth(arg_idx)
356+
.map(|(impl_arg, trait_arg)|
357+
(impl_arg.ty.span, Some(trait_arg.ty.span)))
358+
.unwrap_or(
359+
(origin.span(), tcx.map.span_if_local(trait_m.def_id)))
360+
} else {
361+
(origin.span(), tcx.map.span_if_local(trait_m.def_id))
362+
}
363+
}
364+
_ => (origin.span(), tcx.map.span_if_local(trait_m.def_id))
365+
};
366+
367+
let origin = TypeOrigin::MethodCompatCheck(impl_err_span);
368+
334369
let mut diag = struct_span_err!(
335370
tcx.sess, origin.span(), E0053,
336371
"method `{}` has an incompatible type for trait", trait_m.name
337372
);
373+
338374
infcx.note_type_err(
339-
&mut diag, origin,
375+
&mut diag,
376+
origin,
377+
trait_err_span.map(|sp| (sp, format!("original trait requirement"))),
340378
Some(infer::ValuePairs::Types(ExpectedFound {
341-
expected: trait_fty,
342-
found: impl_fty
343-
})), &terr
379+
expected: trait_fty,
380+
found: impl_fty
381+
})),
382+
&terr
344383
);
345384
diag.emit();
346385
return
@@ -487,12 +526,9 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
487526
trait_ty);
488527

489528
// Locate the Span containing just the type of the offending impl
490-
if let Some(impl_trait_node) = tcx.map.get_if_local(impl_c.def_id) {
491-
if let Node::NodeImplItem(impl_trait_item) = impl_trait_node {
492-
if let ImplItemKind::Const(ref ty, _) = impl_trait_item.node {
493-
origin = TypeOrigin::Misc(ty.span);
494-
}
495-
}
529+
match tcx.map.expect_impl_item(impl_c_node_id).node {
530+
ImplItemKind::Const(ref ty, _) => origin = TypeOrigin::Misc(ty.span),
531+
_ => bug!("{:?} is not a impl const", impl_c)
496532
}
497533

498534
let mut diag = struct_span_err!(
@@ -502,16 +538,16 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
502538
);
503539

504540
// Add a label to the Span containing just the type of the item
505-
if let Some(orig_trait_node) = tcx.map.get_if_local(trait_c.def_id) {
506-
if let Node::NodeTraitItem(orig_trait_item) = orig_trait_node {
507-
if let TraitItem_::ConstTraitItem(ref ty, _) = orig_trait_item.node {
508-
diag.span_label(ty.span, &format!("original trait requirement"));
509-
}
510-
}
511-
}
541+
let trait_c_node_id = tcx.map.as_local_node_id(trait_c.def_id).unwrap();
542+
let trait_c_span = match tcx.map.expect_trait_item(trait_c_node_id).node {
543+
TraitItem_::ConstTraitItem(ref ty, _) => ty.span,
544+
_ => bug!("{:?} is not a trait const", trait_c)
545+
};
512546

513547
infcx.note_type_err(
514-
&mut diag, origin,
548+
&mut diag,
549+
origin,
550+
Some((trait_c_span, format!("original trait requirement"))),
515551
Some(infer::ValuePairs::Types(ExpectedFound {
516552
expected: trait_ty,
517553
found: impl_ty

0 commit comments

Comments
 (0)