Skip to content

Commit d6d6cb7

Browse files
authored
Rollup merge of #103416 - compiler-errors:rpit-named, r=cjgillot
Name the `impl Trait` in region bound suggestions Slightly more descriptive message
2 parents d0d9c86 + 8f8f74d commit d6d6cb7

13 files changed

+33
-34
lines changed

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ pub fn suggest_new_region_bound(
286286
) {
287287
debug!("try_report_static_impl_trait: fn_return={:?}", fn_returns);
288288
// FIXME: account for the need of parens in `&(dyn Trait + '_)`
289-
let consider = "consider changing the";
290-
let declare = "to declare that the";
289+
let consider = "consider changing";
290+
let declare = "to declare that";
291291
let explicit = format!("you can add an explicit `{}` lifetime bound", lifetime_name);
292292
let explicit_static =
293293
arg.map(|arg| format!("explicit `'static` bound to the lifetime of {}", arg));
@@ -305,6 +305,10 @@ pub fn suggest_new_region_bound(
305305
return;
306306
};
307307

308+
// Get the identity type for this RPIT
309+
let did = item_id.def_id.to_def_id();
310+
let ty = tcx.mk_opaque(did, ty::InternalSubsts::identity_for_item(tcx, did));
311+
308312
if let Some(span) = opaque
309313
.bounds
310314
.iter()
@@ -321,7 +325,7 @@ pub fn suggest_new_region_bound(
321325
if let Some(explicit_static) = &explicit_static {
322326
err.span_suggestion_verbose(
323327
span,
324-
&format!("{} `impl Trait`'s {}", consider, explicit_static),
328+
&format!("{consider} `{ty}`'s {explicit_static}"),
325329
&lifetime_name,
326330
Applicability::MaybeIncorrect,
327331
);
@@ -351,12 +355,7 @@ pub fn suggest_new_region_bound(
351355
} else {
352356
err.span_suggestion_verbose(
353357
fn_return.span.shrink_to_hi(),
354-
&format!(
355-
"{declare} `impl Trait` {captures}, {explicit}",
356-
declare = declare,
357-
captures = captures,
358-
explicit = explicit,
359-
),
358+
&format!("{declare} `{ty}` {captures}, {explicit}",),
360359
&plus_lt,
361360
Applicability::MaybeIncorrect,
362361
);
@@ -367,7 +366,7 @@ pub fn suggest_new_region_bound(
367366
err.span_suggestion_verbose(
368367
fn_return.span.shrink_to_hi(),
369368
&format!(
370-
"{declare} trait object {captures}, {explicit}",
369+
"{declare} the trait object {captures}, {explicit}",
371370
declare = declare,
372371
captures = captures,
373372
explicit = explicit,
@@ -384,7 +383,7 @@ pub fn suggest_new_region_bound(
384383
if let Some(explicit_static) = &explicit_static {
385384
err.span_suggestion_verbose(
386385
lt.span,
387-
&format!("{} trait object's {}", consider, explicit_static),
386+
&format!("{} the trait object's {}", consider, explicit_static),
388387
&lifetime_name,
389388
Applicability::MaybeIncorrect,
390389
);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LL | | (a, b)
2525
LL | | }
2626
| |_^
2727
|
28-
help: to declare that the `impl Trait` captures `'b`, you can add an explicit `'b` lifetime bound
28+
help: to declare that `impl Trait<'a>` captures `'b`, you can add an explicit `'b` lifetime bound
2929
|
3030
LL | async fn async_ret_impl_trait1<'a, 'b>(a: &'a u8, b: &'b u8) -> impl Trait<'a> + 'b {
3131
| ++++

src/test/ui/impl-trait/hidden-lifetimes.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a {
66
LL | x
77
| ^
88
|
9-
help: to declare that the `impl Trait` captures `'b`, you can add an explicit `'b` lifetime bound
9+
help: to declare that `impl Swap` captures `'b`, you can add an explicit `'b` lifetime bound
1010
|
1111
LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a + 'b {
1212
| ++++
@@ -19,7 +19,7 @@ LL | fn hide_rc_refcell<'a, 'b: 'a, T: 'static>(x: Rc<RefCell<&'b T>>) -> impl S
1919
LL | x
2020
| ^
2121
|
22-
help: to declare that the `impl Trait` captures `'b`, you can add an explicit `'b` lifetime bound
22+
help: to declare that `impl Swap` captures `'b`, you can add an explicit `'b` lifetime bound
2323
|
2424
LL | fn hide_rc_refcell<'a, 'b: 'a, T: 'static>(x: Rc<RefCell<&'b T>>) -> impl Swap + 'a + 'b {
2525
| ++++

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | fn upper_bounds<'a, 'b, 'c, 'd, 'e>(a: Ordinary<'a>, b: Ordinary<'b>) -> im
77
LL | if condition() { a } else { b }
88
| ^
99
|
10-
help: to declare that the `impl Trait` captures `'b`, you can add an explicit `'b` lifetime bound
10+
help: to declare that `impl Trait<'d, 'e>` captures `'b`, you can add an explicit `'b` lifetime bound
1111
|
1212
LL | fn upper_bounds<'a, 'b, 'c, 'd, 'e>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'d, 'e> + 'b
1313
| ++++

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | fn upper_bounds<'a, 'b>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'a,
77
LL | if condition() { a } else { b }
88
| ^
99
|
10-
help: to declare that the `impl Trait` captures `'b`, you can add an explicit `'b` lifetime bound
10+
help: to declare that `impl Trait<'a, 'b>` captures `'b`, you can add an explicit `'b` lifetime bound
1111
|
1212
LL | fn upper_bounds<'a, 'b>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'a, 'b> + 'b
1313
| ++++

src/test/ui/impl-trait/must_outlive_least_region_or_bound.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn elided(x: &i32) -> impl Copy { x }
66
| |
77
| hidden type `&i32` captures the anonymous lifetime defined here
88
|
9-
help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound
9+
help: to declare that `impl Copy` captures `'_`, you can add an explicit `'_` lifetime bound
1010
|
1111
LL | fn elided(x: &i32) -> impl Copy + '_ { x }
1212
| ++++
@@ -19,7 +19,7 @@ LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
1919
| |
2020
| hidden type `&'a i32` captures the lifetime `'a` as defined here
2121
|
22-
help: to declare that the `impl Trait` captures `'a`, you can add an explicit `'a` lifetime bound
22+
help: to declare that `impl Copy` captures `'a`, you can add an explicit `'a` lifetime bound
2323
|
2424
LL | fn explicit<'a>(x: &'a i32) -> impl Copy + 'a { x }
2525
| ++++
@@ -32,7 +32,7 @@ LL | fn elided2(x: &i32) -> impl Copy + 'static { x }
3232
| |
3333
| let's call the lifetime of this reference `'1`
3434
|
35-
help: consider changing the `impl Trait`'s explicit `'static` bound to the lifetime of argument `x`
35+
help: consider changing `impl Copy`'s explicit `'static` bound to the lifetime of argument `x`
3636
|
3737
LL | fn elided2(x: &i32) -> impl Copy + '_ { x }
3838
| ~~
@@ -47,7 +47,7 @@ error: lifetime may not live long enough
4747
LL | fn explicit2<'a>(x: &'a i32) -> impl Copy + 'static { x }
4848
| -- lifetime `'a` defined here ^ returning this value requires that `'a` must outlive `'static`
4949
|
50-
help: consider changing the `impl Trait`'s explicit `'static` bound to the lifetime of argument `x`
50+
help: consider changing `impl Copy`'s explicit `'static` bound to the lifetime of argument `x`
5151
|
5252
LL | fn explicit2<'a>(x: &'a i32) -> impl Copy + 'a { x }
5353
| ~~
@@ -76,7 +76,7 @@ help: to declare that the trait object captures data from argument `x`, you can
7676
|
7777
LL | fn elided5(x: &i32) -> (Box<dyn Debug + '_>, impl Debug) { (Box::new(x), x) }
7878
| ++++
79-
help: to declare that the `impl Trait` captures data from argument `x`, you can add an explicit `'_` lifetime bound
79+
help: to declare that `impl Debug` captures data from argument `x`, you can add an explicit `'_` lifetime bound
8080
|
8181
LL | fn elided5(x: &i32) -> (Box<dyn Debug>, impl Debug + '_) { (Box::new(x), x) }
8282
| ++++
@@ -87,7 +87,7 @@ error: lifetime may not live long enough
8787
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
8888
| -- lifetime `'a` defined here ^ returning this value requires that `'a` must outlive `'static`
8989
|
90-
help: consider changing the `impl Trait`'s explicit `'static` bound to the lifetime of argument `x`
90+
help: consider changing `impl LifetimeTrait<'a>`'s explicit `'static` bound to the lifetime of argument `x`
9191
|
9292
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'a { x }
9393
| ~~
@@ -104,7 +104,7 @@ LL | fn move_lifetime_into_fn<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Fn(&'a u32
104104
LL | move |_| println!("{}", y)
105105
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
106106
|
107-
help: to declare that the `impl Trait` captures `'b`, you can add an explicit `'b` lifetime bound
107+
help: to declare that `impl Fn(&'a u32)` captures `'b`, you can add an explicit `'b` lifetime bound
108108
|
109109
LL | fn move_lifetime_into_fn<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Fn(&'a u32) + 'b {
110110
| ++++

src/test/ui/impl-trait/region-escape-via-bound.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | fn foo<'x, 'y>(x: Cell<&'x u32>) -> impl Trait<'y>
77
LL | x
88
| ^
99
|
10-
help: to declare that the `impl Trait` captures `'x`, you can add an explicit `'x` lifetime bound
10+
help: to declare that `impl Trait<'y>` captures `'x`, you can add an explicit `'x` lifetime bound
1111
|
1212
LL | fn foo<'x, 'y>(x: Cell<&'x u32>) -> impl Trait<'y> + 'x
1313
| ++++

src/test/ui/impl-trait/static-return-lifetime-infered.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> {
66
LL | self.x.iter().map(|a| a.0)
77
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
88
|
9-
help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound
9+
help: to declare that `impl Iterator<Item = u32>` captures `'_`, you can add an explicit `'_` lifetime bound
1010
|
1111
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + '_ {
1212
| ++++
@@ -19,7 +19,7 @@ LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> {
1919
LL | self.x.iter().map(|a| a.0)
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
2121
|
22-
help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound
22+
help: to declare that `impl Iterator<Item = u32>` captures `'_`, you can add an explicit `'_` lifetime bound
2323
|
2424
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + '_ {
2525
| ++++
@@ -32,7 +32,7 @@ LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
3232
LL | self.x.iter().map(|a| a.0)
3333
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
3434
|
35-
help: to declare that the `impl Trait` captures `'a`, you can add an explicit `'a` lifetime bound
35+
help: to declare that `impl Iterator<Item = u32>` captures `'a`, you can add an explicit `'a` lifetime bound
3636
|
3737
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + 'a {
3838
| ++++
@@ -45,7 +45,7 @@ LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
4545
LL | self.x.iter().map(|a| a.0)
4646
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
4747
|
48-
help: to declare that the `impl Trait` captures `'a`, you can add an explicit `'a` lifetime bound
48+
help: to declare that `impl Iterator<Item = u32>` captures `'a`, you can add an explicit `'a` lifetime bound
4949
|
5050
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + 'a {
5151
| ++++

src/test/ui/nll/ty-outlives/impl-trait-captures.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn foo<'a, T>(x: &T) -> impl Foo<'a> {
66
LL | x
77
| ^
88
|
9-
help: to declare that the `impl Trait` captures `ReFree(DefId(0:8 ~ impl_trait_captures[1afc]::foo), BrNamed(DefId(0:12 ~ impl_trait_captures[1afc]::foo::'_), '_))`, you can add an explicit `ReFree(DefId(0:8 ~ impl_trait_captures[1afc]::foo), BrNamed(DefId(0:12 ~ impl_trait_captures[1afc]::foo::'_), '_))` lifetime bound
9+
help: to declare that `Opaque(DefId(0:13 ~ impl_trait_captures[1afc]::foo::{opaque#0}), [ReEarlyBound(0, 'a), T, ReEarlyBound(2, 'a)])` captures `ReFree(DefId(0:8 ~ impl_trait_captures[1afc]::foo), BrNamed(DefId(0:12 ~ impl_trait_captures[1afc]::foo::'_), '_))`, you can add an explicit `ReFree(DefId(0:8 ~ impl_trait_captures[1afc]::foo), BrNamed(DefId(0:12 ~ impl_trait_captures[1afc]::foo::'_), '_))` lifetime bound
1010
|
1111
LL | fn foo<'a, T>(x: &T) -> impl Foo<'a> + ReFree(DefId(0:8 ~ impl_trait_captures[1afc]::foo), BrNamed(DefId(0:12 ~ impl_trait_captures[1afc]::foo::'_), '_)) {
1212
| ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait-async.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | async fn f(self: Pin<&Self>) -> impl Clone { self }
66
| |
77
| hidden type `Pin<&Foo>` captures the anonymous lifetime defined here
88
|
9-
help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound
9+
help: to declare that `impl Clone` captures `'_`, you can add an explicit `'_` lifetime bound
1010
|
1111
LL | async fn f(self: Pin<&Self>) -> impl Clone + '_ { self }
1212
| ++++

src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn f(self: Pin<&Self>) -> impl Clone { self }
66
| |
77
| hidden type `Pin<&Foo>` captures the anonymous lifetime defined here
88
|
9-
help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound
9+
help: to declare that `impl Clone` captures `'_`, you can add an explicit `'_` lifetime bound
1010
|
1111
LL | fn f(self: Pin<&Self>) -> impl Clone + '_ { self }
1212
| ++++

src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LL | | *dest = g.get();
1818
LL | | }
1919
| |_____^
2020
|
21-
help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound
21+
help: to declare that `impl FnOnce()` captures `'_`, you can add an explicit `'_` lifetime bound
2222
|
2323
LL | fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
2424
| ++++

src/test/ui/suggestions/lifetimes/trait-object-nested-in-impl-trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | | remaining: self.0.iter(),
1010
LL | | }
1111
| |_________^ returning this value requires that `'1` must outlive `'static`
1212
|
13-
help: to declare that the `impl Trait` captures data from argument `self`, you can add an explicit `'_` lifetime bound
13+
help: to declare that `impl Iterator<Item = Box<(dyn Foo + 'static)>>` captures data from argument `self`, you can add an explicit `'_` lifetime bound
1414
|
1515
LL | fn iter(&self) -> impl Iterator<Item = Box<dyn Foo>> + '_ {
1616
| ++++
@@ -65,7 +65,7 @@ LL | | remaining: self.0.iter(),
6565
LL | | }
6666
| |_________^ returning this value requires that `'a` must outlive `'static`
6767
|
68-
help: to declare that the `impl Trait` captures data from argument `self`, you can add an explicit `'a` lifetime bound
68+
help: to declare that `impl Iterator<Item = Box<(dyn Foo + 'static)>>` captures data from argument `self`, you can add an explicit `'a` lifetime bound
6969
|
7070
LL | fn iter<'a>(&'a self) -> impl Iterator<Item = Box<dyn Foo>> + 'a {
7171
| ++++

0 commit comments

Comments
 (0)