Skip to content

Commit 4b23a22

Browse files
Label opaque type for 'captures lifetime' error message
1 parent 13471d3 commit 4b23a22

21 files changed

+88
-30
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,20 @@ pub fn unexpected_hidden_region_diagnostic<'tcx>(
281281
span: Span,
282282
hidden_ty: Ty<'tcx>,
283283
hidden_region: ty::Region<'tcx>,
284-
opaque_ty: ty::OpaqueTypeKey<'tcx>,
284+
opaque_ty_key: ty::OpaqueTypeKey<'tcx>,
285285
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
286-
let opaque_ty = tcx.mk_opaque(opaque_ty.def_id.to_def_id(), opaque_ty.substs);
286+
let opaque_ty = tcx.mk_opaque(opaque_ty_key.def_id.to_def_id(), opaque_ty_key.substs);
287+
287288
let mut err = struct_span_err!(
288289
tcx.sess,
289290
span,
290291
E0700,
291292
"hidden type for `{opaque_ty}` captures lifetime that does not appear in bounds",
292293
);
293294

295+
let opaque_ty_span = tcx.def_span(opaque_ty_key.def_id);
296+
err.span_label(opaque_ty_span, "opaque type defined here");
297+
294298
// Explain the region we are capturing.
295299
match *hidden_region {
296300
ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReStatic => {

tests/ui/async-await/multiple-lifetimes/ret-impl-trait-one.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ error[E0700]: hidden type for `impl Trait<'a>` captures lifetime that does not a
1717
--> $DIR/ret-impl-trait-one.rs:16:80
1818
|
1919
LL | async fn async_ret_impl_trait1<'a, 'b>(a: &'a u8, b: &'b u8) -> impl Trait<'a> {
20-
| ____________________________________--__________________________________________^
21-
| | |
20+
| ____________________________________--___________________________--------------_^
21+
| | | |
22+
| | | opaque type defined here
2223
| | hidden type `(&'a u8, &'b u8)` captures the lifetime `'b` as defined here
2324
LL | |
2425
LL | | (a, b)

tests/ui/impl-trait/hidden-lifetimes.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0700]: hidden type for `impl Swap + 'a` captures lifetime that does not a
22
--> $DIR/hidden-lifetimes.rs:29:5
33
|
44
LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a {
5-
| -- hidden type `&'a mut &'b T` captures the lifetime `'b` as defined here
5+
| -- -------------- opaque type defined here
6+
| |
7+
| hidden type `&'a mut &'b T` captures the lifetime `'b` as defined here
68
LL | x
79
| ^
810
|
@@ -15,7 +17,9 @@ error[E0700]: hidden type for `impl Swap + 'a` captures lifetime that does not a
1517
--> $DIR/hidden-lifetimes.rs:46:5
1618
|
1719
LL | fn hide_rc_refcell<'a, 'b: 'a, T: 'static>(x: Rc<RefCell<&'b T>>) -> impl Swap + 'a {
18-
| -- hidden type `Rc<RefCell<&'b T>>` captures the lifetime `'b` as defined here
20+
| -- -------------- opaque type defined here
21+
| |
22+
| hidden type `Rc<RefCell<&'b T>>` captures the lifetime `'b` as defined here
1923
LL | x
2024
| ^
2125
|

tests/ui/impl-trait/impl-fn-hrtb-bounds-2.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0700]: hidden type for `impl Debug` captures lifetime that does not appear in bounds
22
--> $DIR/impl-fn-hrtb-bounds-2.rs:5:9
33
|
4+
LL | fn a() -> impl Fn(&u8) -> impl Debug {
5+
| ---------- opaque type defined here
46
LL | |x| x
57
| --- ^
68
| |

tests/ui/impl-trait/multiple-lifetimes/error-handling-2.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0700]: hidden type for `E<'b, 'c>` captures lifetime that does not appear in bounds
22
--> $DIR/error-handling-2.rs:22:5
33
|
4+
LL | type E<'a, 'b> = impl Sized;
5+
| ---------- opaque type defined here
6+
LL |
47
LL | fn foo<'a: 'b, 'b, 'c>(x: &'static i32, mut y: &'a i32) -> E<'b, 'c> {
58
| -- hidden type `*mut &'a i32` captures the lifetime `'a` as defined here
69
...

tests/ui/impl-trait/multiple-lifetimes/ordinary-bounds-unrelated.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0700]: hidden type for `impl Trait<'d, 'e>` captures lifetime that does n
22
--> $DIR/ordinary-bounds-unrelated.rs:28:33
33
|
44
LL | fn upper_bounds<'a, 'b, 'c, 'd, 'e>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'d, 'e>
5-
| -- hidden type `Ordinary<'b>` captures the lifetime `'b` as defined here
5+
| -- ------------------ opaque type defined here
6+
| |
7+
| hidden type `Ordinary<'b>` captures the lifetime `'b` as defined here
68
...
79
LL | if condition() { a } else { b }
810
| ^

tests/ui/impl-trait/multiple-lifetimes/ordinary-bounds-unsuited.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0700]: hidden type for `impl Trait<'a, 'b>` captures lifetime that does n
22
--> $DIR/ordinary-bounds-unsuited.rs:31:33
33
|
44
LL | fn upper_bounds<'a, 'b>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'a, 'b>
5-
| -- hidden type `Ordinary<'b>` captures the lifetime `'b` as defined here
5+
| -- ------------------ opaque type defined here
6+
| |
7+
| hidden type `Ordinary<'b>` captures the lifetime `'b` as defined here
68
...
79
LL | if condition() { a } else { b }
810
| ^

tests/ui/impl-trait/must_outlive_least_region_or_bound.stderr

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ error[E0700]: hidden type for `impl Copy` captures lifetime that does not appear
22
--> $DIR/must_outlive_least_region_or_bound.rs:3:35
33
|
44
LL | fn elided(x: &i32) -> impl Copy { x }
5-
| ---- ^
6-
| |
5+
| ---- --------- ^
6+
| | |
7+
| | opaque type defined here
78
| hidden type `&i32` captures the anonymous lifetime defined here
89
|
910
help: to declare that `impl Copy` captures `'_`, you can add an explicit `'_` lifetime bound
@@ -15,8 +16,9 @@ error[E0700]: hidden type for `impl Copy` captures lifetime that does not appear
1516
--> $DIR/must_outlive_least_region_or_bound.rs:6:44
1617
|
1718
LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
18-
| -- ^
19-
| |
19+
| -- --------- ^
20+
| | |
21+
| | opaque type defined here
2022
| hidden type `&'a i32` captures the lifetime `'a` as defined here
2123
|
2224
help: to declare that `impl Copy` captures `'a`, you can add an explicit `'a` lifetime bound
@@ -100,7 +102,9 @@ error[E0700]: hidden type for `impl Fn(&'a u32)` captures lifetime that does not
100102
--> $DIR/must_outlive_least_region_or_bound.rs:38:5
101103
|
102104
LL | fn move_lifetime_into_fn<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Fn(&'a u32) {
103-
| -- hidden type `[closure@$DIR/must_outlive_least_region_or_bound.rs:38:5: 38:13]` captures the lifetime `'b` as defined here
105+
| -- ---------------- opaque type defined here
106+
| |
107+
| hidden type `[closure@$DIR/must_outlive_least_region_or_bound.rs:38:5: 38:13]` captures the lifetime `'b` as defined here
104108
LL | move |_| println!("{}", y)
105109
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
106110
|

tests/ui/impl-trait/nested-return-type4.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0700]: hidden type for `impl Future<Output = impl Sized>` captures lifeti
22
--> $DIR/nested-return-type4.rs:4:5
33
|
44
LL | fn test<'s: 's>(s: &'s str) -> impl std::future::Future<Output = impl Sized> {
5-
| -- hidden type `[async block@$DIR/nested-return-type4.rs:4:5: 4:31]` captures the lifetime `'s` as defined here
5+
| -- --------------------------------------------- opaque type defined here
6+
| |
7+
| hidden type `[async block@$DIR/nested-return-type4.rs:4:5: 4:31]` captures the lifetime `'s` as defined here
68
LL | async move { let _s = s; }
79
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
810
|

tests/ui/impl-trait/region-escape-via-bound.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0700]: hidden type for `impl Trait<'y>` captures lifetime that does not a
22
--> $DIR/region-escape-via-bound.rs:17:5
33
|
44
LL | fn foo<'x, 'y>(x: Cell<&'x u32>) -> impl Trait<'y>
5-
| -- hidden type `Cell<&'x u32>` captures the lifetime `'x` as defined here
5+
| -- -------------- opaque type defined here
6+
| |
7+
| hidden type `Cell<&'x u32>` captures the lifetime `'x` as defined here
68
...
79
LL | x
810
| ^

tests/ui/impl-trait/static-return-lifetime-infered.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0700]: hidden type for `impl Iterator<Item = u32>` captures lifetime that
22
--> $DIR/static-return-lifetime-infered.rs:7:9
33
|
44
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> {
5-
| ----- hidden type `Map<std::slice::Iter<'_, (u32, u32)>, [closure@$DIR/static-return-lifetime-infered.rs:7:27: 7:30]>` captures the anonymous lifetime defined here
5+
| ----- ----------------------- opaque type defined here
6+
| |
7+
| hidden type `Map<std::slice::Iter<'_, (u32, u32)>, [closure@$DIR/static-return-lifetime-infered.rs:7:27: 7:30]>` captures the anonymous lifetime defined here
68
LL | self.x.iter().map(|a| a.0)
79
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
810
|
@@ -15,7 +17,9 @@ error[E0700]: hidden type for `impl Iterator<Item = u32>` captures lifetime that
1517
--> $DIR/static-return-lifetime-infered.rs:11:9
1618
|
1719
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
18-
| -- hidden type `Map<std::slice::Iter<'a, (u32, u32)>, [closure@$DIR/static-return-lifetime-infered.rs:11:27: 11:30]>` captures the lifetime `'a` as defined here
20+
| -- ----------------------- opaque type defined here
21+
| |
22+
| hidden type `Map<std::slice::Iter<'a, (u32, u32)>, [closure@$DIR/static-return-lifetime-infered.rs:11:27: 11:30]>` captures the lifetime `'a` as defined here
1923
LL | self.x.iter().map(|a| a.0)
2024
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
2125
|

tests/ui/lifetimes/issue-105227.stderr

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0700]: hidden type for `impl Iterator<Item = char>` captures lifetime tha
22
--> $DIR/issue-105227.rs:7:5
33
|
44
LL | fn chars0(v :(& str, &str)) -> impl Iterator<Item = char> {
5-
| ----- hidden type `std::iter::Chain<Chars<'_>, Chars<'_>>` captures the anonymous lifetime defined here
5+
| ----- -------------------------- opaque type defined here
6+
| |
7+
| hidden type `std::iter::Chain<Chars<'_>, Chars<'_>>` captures the anonymous lifetime defined here
68
LL |
79
LL | v.0.chars().chain(v.1.chars())
810
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -16,7 +18,9 @@ error[E0700]: hidden type for `impl Iterator<Item = char>` captures lifetime tha
1618
--> $DIR/issue-105227.rs:13:5
1719
|
1820
LL | fn chars1(v0 : & str, v1 : &str) -> impl Iterator<Item = char> {
19-
| ----- hidden type `std::iter::Chain<Chars<'_>, Chars<'_>>` captures the anonymous lifetime defined here
21+
| ----- -------------------------- opaque type defined here
22+
| |
23+
| hidden type `std::iter::Chain<Chars<'_>, Chars<'_>>` captures the anonymous lifetime defined here
2024
LL |
2125
LL | v0.chars().chain(v1.chars())
2226
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -31,7 +35,10 @@ error[E0700]: hidden type for `impl Iterator<Item = char>` captures lifetime tha
3135
|
3236
LL | fn chars2<'b>(v0 : &str, v1 : &'_ str, v2 : &'b str) ->
3337
| ---- hidden type `std::iter::Chain<Chars<'_>, Chars<'_>>` captures the anonymous lifetime defined here
34-
...
38+
LL |
39+
LL | (impl Iterator<Item = char>, &'b str)
40+
| -------------------------- opaque type defined here
41+
LL | {
3542
LL | (v0.chars().chain(v1.chars()), v2)
3643
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3744
|

tests/ui/nll/issue-73159-rpit-static.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error[E0700]: hidden type for `impl Iterator<Item = u8>` captures lifetime that
44
LL | impl<'a> Foo<'a> {
55
| -- hidden type `Copied<std::slice::Iter<'a, u8>>` captures the lifetime `'a` as defined here
66
LL | fn make_it(&self) -> impl Iterator<Item = u8> {
7+
| ------------------------ opaque type defined here
78
LL | self.0.iter().copied()
89
| ^^^^^^^^^^^^^^^^^^^^^^
910

tests/ui/nll/member-constraints/min-choice-reject-ambiguous.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ error[E0700]: hidden type for `impl Cap<'b> + Cap<'c>` captures lifetime that do
2424
--> $DIR/min-choice-reject-ambiguous.rs:39:5
2525
|
2626
LL | fn test_ambiguous<'a, 'b, 'c>(s: &'a u8) -> impl Cap<'b> + Cap<'c>
27-
| -- hidden type `&'a u8` captures the lifetime `'a` as defined here
27+
| -- ---------------------- opaque type defined here
28+
| |
29+
| hidden type `&'a u8` captures the lifetime `'a` as defined here
2830
...
2931
LL | s
3032
| ^

tests/ui/nll/member-constraints/nested-impl-trait-fail.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0700]: hidden type for `impl IntoIterator<Item = impl Cap<'a> + Cap<'b>>`
22
--> $DIR/nested-impl-trait-fail.rs:17:5
33
|
44
LL | fn fail_early_bound<'s, 'a, 'b>(a: &'s u8) -> impl IntoIterator<Item = impl Cap<'a> + Cap<'b>>
5-
| -- hidden type `[&'s u8; 1]` captures the lifetime `'s` as defined here
5+
| -- ------------------------------------------------ opaque type defined here
6+
| |
7+
| hidden type `[&'s u8; 1]` captures the lifetime `'s` as defined here
68
...
79
LL | [a]
810
| ^^^
@@ -20,7 +22,9 @@ error[E0700]: hidden type for `impl Cap<'a> + Cap<'b>` captures lifetime that do
2022
--> $DIR/nested-impl-trait-fail.rs:17:5
2123
|
2224
LL | fn fail_early_bound<'s, 'a, 'b>(a: &'s u8) -> impl IntoIterator<Item = impl Cap<'a> + Cap<'b>>
23-
| -- hidden type `&'s u8` captures the lifetime `'s` as defined here
25+
| -- ---------------------- opaque type defined here
26+
| |
27+
| hidden type `&'s u8` captures the lifetime `'s` as defined here
2428
...
2529
LL | [a]
2630
| ^^^
@@ -40,6 +44,8 @@ error[E0700]: hidden type for `impl IntoIterator<Item = impl Cap<'a> + Cap<'b>>`
4044
LL | fn fail_late_bound<'s, 'a, 'b>(
4145
| -- hidden type `[&'s u8; 1]` captures the lifetime `'s` as defined here
4246
...
47+
LL | ) -> impl IntoIterator<Item = impl Cap<'a> + Cap<'b>> {
48+
| ------------------------------------------------ opaque type defined here
4349
LL | [a]
4450
| ^^^
4551
|
@@ -58,6 +64,8 @@ error[E0700]: hidden type for `impl Cap<'a> + Cap<'b>` captures lifetime that do
5864
LL | fn fail_late_bound<'s, 'a, 'b>(
5965
| -- hidden type `&'s u8` captures the lifetime `'s` as defined here
6066
...
67+
LL | ) -> impl IntoIterator<Item = impl Cap<'a> + Cap<'b>> {
68+
| ---------------------- opaque type defined here
6169
LL | [a]
6270
| ^^^
6371
|

tests/ui/nll/ty-outlives/impl-trait-captures.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0700]: hidden type for `Opaque(DefId(0:13 ~ impl_trait_captures[1afc]::fo
22
--> $DIR/impl-trait-captures.rs:11:5
33
|
44
LL | fn foo<'a, T>(x: &T) -> impl Foo<'a> {
5-
| -- hidden type `&ReFree(DefId(0:8 ~ impl_trait_captures[1afc]::foo), BrNamed(DefId(0:12 ~ impl_trait_captures[1afc]::foo::'_), '_)) T` captures the anonymous lifetime defined here
5+
| -- ------------ opaque type defined here
6+
| |
7+
| hidden type `&ReFree(DefId(0:8 ~ impl_trait_captures[1afc]::foo), BrNamed(DefId(0:12 ~ impl_trait_captures[1afc]::foo::'_), '_)) T` captures the anonymous lifetime defined here
68
LL | x
79
| ^
810
|

tests/ui/self/arbitrary_self_types_pin_lifetime_impl_trait-async.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ error[E0700]: hidden type for `impl Clone` captures lifetime that does not appea
22
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait-async.rs:8:48
33
|
44
LL | async fn f(self: Pin<&Self>) -> impl Clone { self }
5-
| ----- ^^^^^^^^
6-
| |
5+
| ----- ---------- ^^^^^^^^
6+
| | |
7+
| | opaque type defined here
78
| hidden type `Pin<&Foo>` captures the anonymous lifetime defined here
89
|
910
help: to declare that `impl Clone` captures `'_`, you can add an explicit `'_` lifetime bound

tests/ui/self/arbitrary_self_types_pin_lifetime_impl_trait.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ error[E0700]: hidden type for `impl Clone` captures lifetime that does not appea
22
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait.rs:6:44
33
|
44
LL | fn f(self: Pin<&Self>) -> impl Clone { self }
5-
| ----- ^^^^
6-
| |
5+
| ----- ---------- ^^^^
6+
| | |
7+
| | opaque type defined here
78
| hidden type `Pin<&Foo>` captures the anonymous lifetime defined here
89
|
910
help: to declare that `impl Clone` captures `'_`, you can add an explicit `'_` lifetime bound

tests/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ error[E0700]: hidden type for `impl FnOnce()` captures lifetime that does not ap
1010
--> $DIR/missing-lifetimes-in-signature.rs:19:5
1111
|
1212
LL | fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce()
13-
| ------ hidden type `[closure@$DIR/missing-lifetimes-in-signature.rs:19:5: 19:12]` captures the anonymous lifetime defined here
13+
| ------ ------------- opaque type defined here
14+
| |
15+
| hidden type `[closure@$DIR/missing-lifetimes-in-signature.rs:19:5: 19:12]` captures the anonymous lifetime defined here
1416
...
1517
LL | / move || {
1618
LL | |

tests/ui/type-alias-impl-trait/imply_bounds_from_bounds_param.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0700]: hidden type for `impl PlusOne` captures lifetime that does not app
22
--> $DIR/imply_bounds_from_bounds_param.rs:24:5
33
|
44
LL | fn test<'a>(y: &'a mut i32) -> impl PlusOne {
5-
| -- hidden type `<&'a mut i32 as Callable>::Output` captures the lifetime `'a` as defined here
5+
| -- ------------ opaque type defined here
6+
| |
7+
| hidden type `<&'a mut i32 as Callable>::Output` captures the lifetime `'a` as defined here
68
LL | <&'a mut i32 as Callable>::call(y)
79
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
810
|

tests/ui/type-alias-impl-trait/missing_lifetime_bound.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0700]: hidden type for `Opaque<'a, T>` captures lifetime that does not appear in bounds
22
--> $DIR/missing_lifetime_bound.rs:4:47
33
|
4+
LL | type Opaque<'a, T> = impl Sized;
5+
| ---------- opaque type defined here
46
LL | fn defining<'a, T>(x: &'a i32) -> Opaque<T> { x }
57
| -- ^
68
| |

0 commit comments

Comments
 (0)