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

Don't ICE on unconstrained anonymous lifetimes inside associated types. #74665

Merged
merged 2 commits into from
Jul 24, 2020
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
29 changes: 17 additions & 12 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1485,28 +1485,33 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
debug!("late_bound_in_ty = {:?}", late_bound_in_ty);
for br in late_bound_in_ty.difference(&late_bound_in_trait_ref) {
let br_name = match *br {
ty::BrNamed(_, name) => name,
_ => {
span_bug!(
binding.span,
"anonymous bound region {:?} in binding but not trait ref",
br
);
}
ty::BrNamed(_, name) => format!("lifetime `{}`", name),
_ => "an anonymous lifetime".to_string(),
};
// FIXME: point at the type params that don't have appropriate lifetimes:
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
// ---- ---- ^^^^^^^
struct_span_err!(
let mut err = struct_span_err!(
tcx.sess,
binding.span,
E0582,
"binding for associated type `{}` references lifetime `{}`, \
"binding for associated type `{}` references {}, \
which does not appear in the trait input types",
binding.item_name,
br_name
)
.emit();
);

if let ty::BrAnon(_) = *br {
// The only way for an anonymous lifetime to wind up
// in the return type but **also** be unconstrained is
// if it only appears in "associated types" in the
// input. See #62200 for an example. In this case,
// though we can easily give a hint that ought to be
// relevant.
err.note("lifetimes appearing in an associated type are not considered constrained");
}

err.emit();
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/associated-types/issue-62200.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
struct S {}

trait T<'a> {
type A;
}

impl T<'_> for S {
type A = u32;
}

fn foo(x: impl Fn(<S as T<'_>>::A) -> <S as T<'_>>::A) {}
//~^ ERROR binding for associated type `Output` references an anonymous lifetime
//~^^ NOTE lifetimes appearing in an associated type are not considered constrained

fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/associated-types/issue-62200.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0582]: binding for associated type `Output` references an anonymous lifetime, which does not appear in the trait input types
--> $DIR/issue-62200.rs:11:39
|
LL | fn foo(x: impl Fn(<S as T<'_>>::A) -> <S as T<'_>>::A) {}
| ^^^^^^^^^^^^^^^
|
= note: lifetimes appearing in an associated type are not considered constrained

error: aborting due to previous error

For more information about this error, try `rustc --explain E0582`.