Skip to content

Commit

Permalink
Rollup merge of #74665 - smmalis37:issue-62200, r=davidtwco
Browse files Browse the repository at this point in the history
Don't ICE on unconstrained anonymous lifetimes inside associated types.

Fixes #62200. The change here is inspired (copied) by how this case is handled on bare fns at https://github.com/rust-lang/rust/blob/e8b55a4ad230ebec762fdfc4f241ba98a98560af/src/librustc_typeck/astconv.rs#L3083-L3106.
  • Loading branch information
JohnTitor authored Jul 24, 2020
2 parents a02aecb + bbaab63 commit 1f6d5ce
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
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`.

0 comments on commit 1f6d5ce

Please sign in to comment.