Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak presentation on lifetime trait mismatch #47791

Merged
merged 3 commits into from
Feb 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 48 additions & 23 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,25 +175,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
ty::ReEarlyBound(_) |
ty::ReFree(_) => {
let scope = region.free_region_binding_scope(self);
let prefix = match *region {
ty::ReEarlyBound(ref br) => {
format!("the lifetime {} as defined on", br.name)
}
ty::ReFree(ref fr) => {
match fr.bound_region {
ty::BrAnon(idx) => {
format!("the anonymous lifetime #{} defined on", idx + 1)
}
ty::BrFresh(_) => "an anonymous lifetime defined on".to_owned(),
_ => {
format!("the lifetime {} as defined on",
fr.bound_region)
}
}
}
_ => bug!()
};

let node = self.hir.as_local_node_id(scope)
.unwrap_or(DUMMY_NODE_ID);
let unknown;
Expand All @@ -218,7 +199,26 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
&unknown
}
};
let (msg, opt_span) = explain_span(self, tag, self.hir.span(node));
let (prefix, span) = match *region {
ty::ReEarlyBound(ref br) => {
(format!("the lifetime {} as defined on", br.name),
self.sess.codemap().def_span(self.hir.span(node)))
}
ty::ReFree(ref fr) => {
match fr.bound_region {
ty::BrAnon(idx) => {
(format!("the anonymous lifetime #{} defined on", idx + 1),
self.hir.span(node))
}
ty::BrFresh(_) => ("an anonymous lifetime defined on".to_owned(),
self.hir.span(node)),
_ => (format!("the lifetime {} as defined on", fr.bound_region),
self.sess.codemap().def_span(self.hir.span(node))),
}
}
_ => bug!()
};
let (msg, opt_span) = explain_span(self, tag, span);
(format!("{} {}", prefix, msg), opt_span)
}

Expand Down Expand Up @@ -807,7 +807,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
}
};

let span = cause.span;
let span = cause.span(&self.tcx);

diag.span_label(span, terr.to_string());
if let Some((sp, msg)) = secondary_span {
Expand Down Expand Up @@ -842,7 +842,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
"did you mean `{}(/* fields */)`?",
self.tcx.item_path_str(def_id)
);
diag.span_label(cause.span, message);
diag.span_label(span, message);
}
}
}
Expand Down Expand Up @@ -870,7 +870,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
trace,
terr);

let span = trace.cause.span;
let span = trace.cause.span(&self.tcx);
let failure_code = trace.cause.as_failure_code(terr);
let mut diag = match failure_code {
FailureCode::Error0317(failure_str) => {
Expand Down Expand Up @@ -1075,6 +1075,31 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
sup_region,
"...");

match (&sup_origin, &sub_origin) {
(&infer::Subtype(ref sup_trace), &infer::Subtype(ref sub_trace)) => {
if let (Some((sup_expected, sup_found)),
Some((sub_expected, sub_found))) = (self.values_str(&sup_trace.values),
self.values_str(&sub_trace.values)) {
if sub_expected == sup_expected && sub_found == sup_found {
self.tcx.note_and_explain_region(
region_scope_tree,
&mut err,
"...but the lifetime must also be valid for ",
sub_region,
"...",
);
err.note(&format!("...so that the {}:\nexpected {}\n found {}",
sup_trace.cause.as_requirement_str(),
sup_expected.content(),
sup_found.content()));
err.emit();
return;
}
}
}
_ => {}
}

self.note_region_origin(&mut err, &sup_origin);

self.tcx.note_and_explain_region(region_scope_tree, &mut err,
Expand Down
10 changes: 4 additions & 6 deletions src/librustc/infer/error_reporting/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
if let Some((expected, found)) = self.values_str(&trace.values) {
let expected = expected.content();
let found = found.content();
// FIXME: do we want a "the" here?
err.span_note(trace.cause.span,
&format!("...so that {} (expected {}, found {})",
trace.cause.as_requirement_str(),
expected,
found));
err.note(&format!("...so that the {}:\nexpected {}\n found {}",
trace.cause.as_requirement_str(),
expected,
found));
} else {
// FIXME: this really should be handled at some earlier stage. Our
// handling of region checking when type errors are present is
Expand Down
13 changes: 13 additions & 0 deletions src/librustc/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ pub struct ObligationCause<'tcx> {
pub code: ObligationCauseCode<'tcx>
}

impl<'tcx> ObligationCause<'tcx> {
pub fn span<'a, 'gcx>(&self, tcx: &TyCtxt<'a, 'gcx, 'tcx>) -> Span {
match self.code {
ObligationCauseCode::CompareImplMethodObligation { .. } |
ObligationCauseCode::MainFunctionType |
ObligationCauseCode::StartFunctionType => {
tcx.sess.codemap().def_span(self.span)
}
_ => self.span,
}
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ObligationCauseCode<'tcx> {
/// Not well classified or should be obvious from span.
Expand Down
37 changes: 23 additions & 14 deletions src/librustc_typeck/check/compare_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub fn compare_impl_method<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
debug!("compare_impl_method(impl_trait_ref={:?})",
impl_trait_ref);

let impl_m_span = tcx.sess.codemap().def_span(impl_m_span);

if let Err(ErrorReported) = compare_self_type(tcx,
impl_m,
impl_m_span,
Expand Down Expand Up @@ -186,6 +188,7 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
check_region_bounds_on_impl_method(tcx,
impl_m_span,
impl_m,
trait_m,
&trait_m_generics,
&impl_m_generics,
trait_to_skol_substs)?;
Expand Down Expand Up @@ -310,7 +313,7 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
};

let mut diag = struct_span_err!(tcx.sess,
cause.span,
cause.span(&tcx),
E0053,
"method `{}` has an incompatible type for trait",
trait_m.name);
Expand Down Expand Up @@ -346,10 +349,12 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
fn check_region_bounds_on_impl_method<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
span: Span,
impl_m: &ty::AssociatedItem,
trait_m: &ty::AssociatedItem,
trait_generics: &ty::Generics,
impl_generics: &ty::Generics,
trait_to_skol_substs: &Substs<'tcx>)
-> Result<(), ErrorReported> {
let span = tcx.sess.codemap().def_span(span);
let trait_params = &trait_generics.regions[..];
let impl_params = &impl_generics.regions[..];

Expand All @@ -371,14 +376,18 @@ fn check_region_bounds_on_impl_method<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
// are zero. Since I don't quite know how to phrase things at
// the moment, give a kind of vague error message.
if trait_params.len() != impl_params.len() {
struct_span_err!(tcx.sess,
span,
E0195,
"lifetime parameters or bounds on method `{}` do not match the \
trait declaration",
impl_m.name)
.span_label(span, "lifetimes do not match trait")
.emit();
let mut err = struct_span_err!(tcx.sess,
span,
E0195,
"lifetime parameters or bounds on method `{}` do not match \
the trait declaration",
impl_m.name);
err.span_label(span, "lifetimes do not match method in trait");
if let Some(sp) = tcx.hir.span_if_local(trait_m.def_id) {
err.span_label(tcx.sess.codemap().def_span(sp),
"lifetimes in impl do not match this method in trait");
}
err.emit();
return Err(ErrorReported);
}

Expand Down Expand Up @@ -424,9 +433,9 @@ fn extract_spans_for_error_reporting<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a
}).map(|(ref impl_arg, ref trait_arg)| {
(impl_arg.span, Some(trait_arg.span))
})
.unwrap_or_else(|| (cause.span, tcx.hir.span_if_local(trait_m.def_id)))
.unwrap_or_else(|| (cause.span(&tcx), tcx.hir.span_if_local(trait_m.def_id)))
} else {
(cause.span, tcx.hir.span_if_local(trait_m.def_id))
(cause.span(&tcx), tcx.hir.span_if_local(trait_m.def_id))
}
}
TypeError::Sorts(ExpectedFound { .. }) => {
Expand Down Expand Up @@ -459,14 +468,14 @@ fn extract_spans_for_error_reporting<'a, 'gcx, 'tcx>(infcx: &infer::InferCtxt<'a
{
(impl_m_output.span(), Some(trait_m_output.span()))
} else {
(cause.span, tcx.hir.span_if_local(trait_m.def_id))
(cause.span(&tcx), tcx.hir.span_if_local(trait_m.def_id))
}
})
} else {
(cause.span, tcx.hir.span_if_local(trait_m.def_id))
(cause.span(&tcx), tcx.hir.span_if_local(trait_m.def_id))
}
}
_ => (cause.span, tcx.hir.span_if_local(trait_m.def_id)),
_ => (cause.span(&tcx), tcx.hir.span_if_local(trait_m.def_id)),
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/compile-fail/E0195.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@

trait Trait {
fn bar<'a,'b:'a>(x: &'a str, y: &'b str);
//~^ NOTE lifetimes in impl do not match this method in trait
}

struct Foo;

impl Trait for Foo {
fn bar<'a,'b>(x: &'a str, y: &'b str) { //~ ERROR E0195
//~^ lifetimes do not match trait
//~^ NOTE lifetimes do not match method in trait
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/test/compile-fail/issue-16048.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

trait NoLifetime {
fn get<'p, T : Test<'p>>(&self) -> T;
//~^ NOTE lifetimes in impl do not match this method in trait
}

trait Test<'p> {
Expand All @@ -28,8 +29,8 @@ impl<'a> Test<'a> for Foo<'a> {

impl<'a> NoLifetime for Foo<'a> {
fn get<'p, T : Test<'a>>(&self) -> T {
//~^ ERROR E0195
//~| lifetimes do not match trait
//~^ ERROR E0195
//~| NOTE lifetimes do not match method in trait
return *self as T;
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/test/ui/associated-const-impl-wrong-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ error[E0308]: mismatched types
note: the lifetime 'a as defined on the impl at 17:1...
--> $DIR/associated-const-impl-wrong-lifetime.rs:17:1
|
17 | / impl<'a> Foo for &'a () {
18 | | const NAME: &'a str = "unit";
19 | | //~^ ERROR mismatched types [E0308]
20 | | }
| |_^
17 | impl<'a> Foo for &'a () {
| ^^^^^^^^^^^^^^^^^^^^^^^
= note: ...does not necessarily outlive the static lifetime

error: aborting due to previous error
Expand Down
48 changes: 48 additions & 0 deletions src/test/ui/borrowck/regions-bound-missing-bound-in-impl.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
error[E0195]: lifetime parameters or bounds on method `no_bound` do not match the trait declaration
--> $DIR/regions-bound-missing-bound-in-impl.rs:28:5
|
20 | fn no_bound<'b>(self, b: Inv<'b>);
| ---------------------------------- lifetimes in impl do not match this method in trait
...
28 | fn no_bound<'b:'a>(self, b: Inv<'b>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetimes do not match method in trait

error[E0195]: lifetime parameters or bounds on method `has_bound` do not match the trait declaration
--> $DIR/regions-bound-missing-bound-in-impl.rs:32:5
|
21 | fn has_bound<'b:'a>(self, b: Inv<'b>);
| -------------------------------------- lifetimes in impl do not match this method in trait
...
32 | fn has_bound<'b>(self, b: Inv<'b>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetimes do not match method in trait

error[E0308]: method not compatible with trait
--> $DIR/regions-bound-missing-bound-in-impl.rs:36:5
|
36 | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected type `fn(&'a isize, Inv<'c>, Inv<'c>, Inv<'d>)`
found type `fn(&'a isize, Inv<'_>, Inv<'c>, Inv<'d>)`
note: the lifetime 'c as defined on the method body at 36:5...
--> $DIR/regions-bound-missing-bound-in-impl.rs:36:5
|
36 | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...does not necessarily outlive the lifetime 'c as defined on the method body at 36:5
--> $DIR/regions-bound-missing-bound-in-impl.rs:36:5
|
36 | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0276]: impl has stricter requirements than trait
--> $DIR/regions-bound-missing-bound-in-impl.rs:53:5
|
24 | fn another_bound<'x: 'a>(self, x: Inv<'x>, y: Inv<'t>);
| ------------------------------------------------------- definition of `another_bound` from trait
...
53 | fn another_bound<'x: 't>(self, x: Inv<'x>, y: Inv<'t>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl has extra requirement `'x: 't`

error: aborting due to 4 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,8 @@ note: the anonymous lifetime #2 defined on the body at 47:29...
note: ...does not necessarily outlive the lifetime 'x as defined on the function body at 42:1
--> $DIR/expect-region-supply-region.rs:42:1
|
42 | / fn expect_bound_supply_named<'x>() {
43 | | let mut f: Option<&u32> = None;
44 | |
45 | | // Here we give a type annotation that `x` should be free. We get
... |
54 | | });
55 | | }
| |_^
42 | fn expect_bound_supply_named<'x>() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
--> $DIR/expect-region-supply-region.rs:47:33
Expand All @@ -61,14 +55,8 @@ error[E0308]: mismatched types
note: the lifetime 'x as defined on the function body at 42:1...
--> $DIR/expect-region-supply-region.rs:42:1
|
42 | / fn expect_bound_supply_named<'x>() {
43 | | let mut f: Option<&u32> = None;
44 | |
45 | | // Here we give a type annotation that `x` should be free. We get
... |
54 | | });
55 | | }
| |_^
42 | fn expect_bound_supply_named<'x>() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...does not necessarily outlive the anonymous lifetime #2 defined on the body at 47:29
--> $DIR/expect-region-supply-region.rs:47:29
|
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/impl-trait/trait_type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0053]: method `fmt` has an incompatible type for trait
--> $DIR/trait_type.rs:17:4
|
17 | fn fmt(&self, x: &str) -> () { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ in mutability
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ in mutability
|
= note: expected type `fn(&MyType, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`
found type `fn(&MyType, &str)`
Expand All @@ -19,7 +19,7 @@ error[E0186]: method `fmt` has a `&self` declaration in the trait, but not in th
--> $DIR/trait_type.rs:27:4
|
27 | fn fmt() -> () { }
| ^^^^^^^^^^^^^^^^^^ expected `&self` in impl
| ^^^^^^^^^^^^^^ expected `&self` in impl
|
= note: `fmt` from trait: `fn(&Self, &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error>`

Expand Down
Loading