Skip to content

Commit 1f6d5ce

Browse files
authored
Rollup merge of #74665 - smmalis37:issue-62200, r=davidtwco
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.
2 parents a02aecb + bbaab63 commit 1f6d5ce

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

src/librustc_typeck/astconv.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -1485,28 +1485,33 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14851485
debug!("late_bound_in_ty = {:?}", late_bound_in_ty);
14861486
for br in late_bound_in_ty.difference(&late_bound_in_trait_ref) {
14871487
let br_name = match *br {
1488-
ty::BrNamed(_, name) => name,
1489-
_ => {
1490-
span_bug!(
1491-
binding.span,
1492-
"anonymous bound region {:?} in binding but not trait ref",
1493-
br
1494-
);
1495-
}
1488+
ty::BrNamed(_, name) => format!("lifetime `{}`", name),
1489+
_ => "an anonymous lifetime".to_string(),
14961490
};
14971491
// FIXME: point at the type params that don't have appropriate lifetimes:
14981492
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
14991493
// ---- ---- ^^^^^^^
1500-
struct_span_err!(
1494+
let mut err = struct_span_err!(
15011495
tcx.sess,
15021496
binding.span,
15031497
E0582,
1504-
"binding for associated type `{}` references lifetime `{}`, \
1498+
"binding for associated type `{}` references {}, \
15051499
which does not appear in the trait input types",
15061500
binding.item_name,
15071501
br_name
1508-
)
1509-
.emit();
1502+
);
1503+
1504+
if let ty::BrAnon(_) = *br {
1505+
// The only way for an anonymous lifetime to wind up
1506+
// in the return type but **also** be unconstrained is
1507+
// if it only appears in "associated types" in the
1508+
// input. See #62200 for an example. In this case,
1509+
// though we can easily give a hint that ought to be
1510+
// relevant.
1511+
err.note("lifetimes appearing in an associated type are not considered constrained");
1512+
}
1513+
1514+
err.emit();
15101515
}
15111516
}
15121517
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct S {}
2+
3+
trait T<'a> {
4+
type A;
5+
}
6+
7+
impl T<'_> for S {
8+
type A = u32;
9+
}
10+
11+
fn foo(x: impl Fn(<S as T<'_>>::A) -> <S as T<'_>>::A) {}
12+
//~^ ERROR binding for associated type `Output` references an anonymous lifetime
13+
//~^^ NOTE lifetimes appearing in an associated type are not considered constrained
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0582]: binding for associated type `Output` references an anonymous lifetime, which does not appear in the trait input types
2+
--> $DIR/issue-62200.rs:11:39
3+
|
4+
LL | fn foo(x: impl Fn(<S as T<'_>>::A) -> <S as T<'_>>::A) {}
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: lifetimes appearing in an associated type are not considered constrained
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0582`.

0 commit comments

Comments
 (0)