Skip to content

Commit 99d9ccd

Browse files
committed
Improve output of argument anonymous borrow missing annotation involving opaque return type
Go from ``` error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements --> file8.rs:22:5 | 22 | / move || { 23 | | *dest = g.get(); 24 | | } | |_____^ | note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the function body at 18:1... --> file8.rs:18:1 | 18 | / fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a 19 | | where 20 | | G: Get<T> 21 | | { ... | 24 | | } 25 | | } | |_^ note: ...so that the types are compatible --> file8.rs:22:5 | 22 | / move || { //~ ERROR cannot infer an appropriate lifetime 23 | | *dest = g.get(); 24 | | } | |_____^ = note: expected `&mut T` found `&mut T` note: but, the lifetime must be valid for the lifetime `'a` as defined on the function body at 18:8... --> file8.rs:18:8 | 18 | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a | ^^ note: ...so that return value is valid for the call --> file8.rs:18:45 | 18 | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a | ^^^^^^^^^^^^^^^^^^^^^^^ ``` to ``` error[E0621]: explicit lifetime required in the type of `dest` --> file8.rs:18:45 | 18 | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a | ------ ^^^^^^^^^^^^^^^^^^^^^^^ lifetime `'a` required | | | help: add explicit lifetime `'a` to the type of `dest`: `&'a mut T` ```
1 parent f49ebbb commit 99d9ccd

File tree

7 files changed

+37
-76
lines changed

7 files changed

+37
-76
lines changed

src/librustc_infer/infer/error_reporting/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,14 +1917,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
19171917
"...",
19181918
);
19191919

1920+
debug!("report_sub_sup_conflict: var_origin={:?}", var_origin);
1921+
debug!("report_sub_sup_conflict: sub_region={:?}", sub_region);
1922+
debug!("report_sub_sup_conflict: sub_origin={:?}", sub_origin);
1923+
debug!("report_sub_sup_conflict: sup_region={:?}", sup_region);
1924+
debug!("report_sub_sup_conflict: sup_origin={:?}", sup_origin);
1925+
19201926
if let (&infer::Subtype(ref sup_trace), &infer::Subtype(ref sub_trace)) =
19211927
(&sup_origin, &sub_origin)
19221928
{
1923-
debug!("report_sub_sup_conflict: var_origin={:?}", var_origin);
1924-
debug!("report_sub_sup_conflict: sub_region={:?}", sub_region);
1925-
debug!("report_sub_sup_conflict: sub_origin={:?}", sub_origin);
1926-
debug!("report_sub_sup_conflict: sup_region={:?}", sup_region);
1927-
debug!("report_sub_sup_conflict: sup_origin={:?}", sup_origin);
19281929
debug!("report_sub_sup_conflict: sup_trace={:?}", sup_trace);
19291930
debug!("report_sub_sup_conflict: sub_trace={:?}", sub_trace);
19301931
debug!("report_sub_sup_conflict: sup_trace.values={:?}", sup_trace.values);

src/librustc_infer/infer/error_reporting/nice_region_error/named_anon_conflict.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,22 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
7474
}
7575

7676
if let Some((_, fndecl)) = self.find_anon_type(anon, &br) {
77-
if self.is_return_type_anon(scope_def_id, br, fndecl).is_some()
78-
|| self.is_self_anon(is_first, scope_def_id)
79-
{
77+
let return_type_anon = self.is_return_type_anon(scope_def_id, br, fndecl);
78+
let is_self_anon = self.is_self_anon(is_first, scope_def_id);
79+
debug!(
80+
"try_report_named_anon_conflict: fndecl {:?} {:?} {}",
81+
fndecl, return_type_anon, is_self_anon
82+
);
83+
if is_self_anon {
84+
// We used to check for `return_type_anon.is_some()` here. Removing that improves
85+
// some diagnostics, but we might have to readd the check if there are regressions
86+
// in the wild.
8087
return None;
8188
}
8289
if let FnRetTy::Return(ty) = &fndecl.output {
90+
debug!("try_report_named_anon_conflict: ret ty {:?}", ty);
8391
if let (TyKind::Def(_, _), ty::ReStatic) = (&ty.kind, sub) {
92+
debug!("try_report_named_anon_conflict: impl Trait + 'static");
8493
// This is an impl Trait return that evaluates de need of 'static.
8594
// We handle this case better in `static_impl_trait`.
8695
return None;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ where
5555
}
5656

5757
// After applying suggestion for `qux`:
58-
// FIXME: we should suggest be suggesting to change `dest` to `&'a mut T`.
5958
fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
59+
//~^ ERROR explicit lifetime required in the type of `dest`
6060
where
6161
G: Get<T>
6262
{
63-
move || { //~ ERROR cannot infer an appropriate lifetime
63+
move || {
6464
*dest = g.get();
6565
}
6666
}

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

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -87,44 +87,13 @@ help: consider introducing an explicit lifetime bound to unify the type paramete
8787
LL | fn qux<'b, 'a, G: 'b + 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'b
8888
| ^^^ ^^^^^^^ ^^^^
8989

90-
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
91-
--> $DIR/missing-lifetimes-in-signature.rs:63:5
92-
|
93-
LL | / move || {
94-
LL | | *dest = g.get();
95-
LL | | }
96-
| |_____^
97-
|
98-
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the function body at 59:1...
99-
--> $DIR/missing-lifetimes-in-signature.rs:59:1
100-
|
101-
LL | / fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
102-
LL | | where
103-
LL | | G: Get<T>
104-
LL | | {
105-
... |
106-
LL | | }
107-
LL | | }
108-
| |_^
109-
note: ...so that the types are compatible
110-
--> $DIR/missing-lifetimes-in-signature.rs:63:5
111-
|
112-
LL | / move || {
113-
LL | | *dest = g.get();
114-
LL | | }
115-
| |_____^
116-
= note: expected `&mut T`
117-
found `&mut T`
118-
note: but, the lifetime must be valid for the lifetime `'a` as defined on the function body at 59:8...
119-
--> $DIR/missing-lifetimes-in-signature.rs:59:8
120-
|
121-
LL | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
122-
| ^^
123-
note: ...so that return value is valid for the call
124-
--> $DIR/missing-lifetimes-in-signature.rs:59:45
90+
error[E0621]: explicit lifetime required in the type of `dest`
91+
--> $DIR/missing-lifetimes-in-signature.rs:58:45
12592
|
12693
LL | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
127-
| ^^^^^^^^^^^^^^^^^^^^^^^
94+
| ------ ^^^^^^^^^^^^^^^^^^^^^^^ lifetime `'a` required
95+
| |
96+
| help: add explicit lifetime `'a` to the type of `dest`: `&'a mut T`
12897

12998
error[E0309]: the parameter type `G` may not live long enough
13099
--> $DIR/missing-lifetimes-in-signature.rs:69:44
@@ -142,5 +111,5 @@ LL | fn bak<'a, G, T>(g: G, dest: &'a mut T) -> impl FnOnce() + 'a
142111

143112
error: aborting due to 6 previous errors
144113

145-
Some errors have detailed explanations: E0261, E0309, E0495.
114+
Some errors have detailed explanations: E0261, E0309, E0621.
146115
For more information about an error, try `rustc --explain E0261`.
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
error: lifetime may not live long enough
1+
error[E0621]: explicit lifetime required in the type of `items`
22
--> $DIR/dyn-trait-underscore.rs:8:5
33
|
44
LL | fn a<T>(items: &[T]) -> Box<dyn Iterator<Item=&T>> {
5-
| - let's call the lifetime of this reference `'1`
5+
| ---- help: add explicit lifetime `'static` to the type of `items`: `&'static [T]`
66
LL | // ^^^^^^^^^^^^^^^^^^^^^ bound *here* defaults to `'static`
77
LL | Box::new(items.iter())
8-
| ^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static`
8+
| ^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required
99

1010
error: aborting due to previous error
1111

12+
For more information about this error, try `rustc --explain E0621`.

src/test/ui/underscore-lifetime/dyn-trait-underscore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
fn a<T>(items: &[T]) -> Box<dyn Iterator<Item=&T>> {
77
// ^^^^^^^^^^^^^^^^^^^^^ bound *here* defaults to `'static`
8-
Box::new(items.iter()) //~ ERROR cannot infer an appropriate lifetime
8+
Box::new(items.iter()) //~ ERROR explicit lifetime required in the type of `items`
99
}
1010

1111
fn b<T>(items: &[T]) -> Box<dyn Iterator<Item=&T> + '_> {
Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
1-
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
2-
--> $DIR/dyn-trait-underscore.rs:8:20
3-
|
4-
LL | Box::new(items.iter())
5-
| ^^^^
6-
|
7-
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the function body at 6:1...
8-
--> $DIR/dyn-trait-underscore.rs:6:1
9-
|
10-
LL | / fn a<T>(items: &[T]) -> Box<dyn Iterator<Item=&T>> {
11-
LL | | // ^^^^^^^^^^^^^^^^^^^^^ bound *here* defaults to `'static`
12-
LL | | Box::new(items.iter())
13-
LL | | }
14-
| |_^
15-
note: ...so that reference does not outlive borrowed content
16-
--> $DIR/dyn-trait-underscore.rs:8:14
17-
|
18-
LL | Box::new(items.iter())
19-
| ^^^^^
20-
= note: but, the lifetime must be valid for the static lifetime...
21-
note: ...so that the expression is assignable
1+
error[E0621]: explicit lifetime required in the type of `items`
222
--> $DIR/dyn-trait-underscore.rs:8:5
233
|
4+
LL | fn a<T>(items: &[T]) -> Box<dyn Iterator<Item=&T>> {
5+
| ---- help: add explicit lifetime `'static` to the type of `items`: `&'static [T]`
6+
LL | // ^^^^^^^^^^^^^^^^^^^^^ bound *here* defaults to `'static`
247
LL | Box::new(items.iter())
25-
| ^^^^^^^^^^^^^^^^^^^^^^
26-
= note: expected `std::boxed::Box<(dyn std::iter::Iterator<Item = &T> + 'static)>`
27-
found `std::boxed::Box<dyn std::iter::Iterator<Item = &T>>`
8+
| ^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required
289

2910
error: aborting due to previous error
3011

31-
For more information about this error, try `rustc --explain E0495`.
12+
For more information about this error, try `rustc --explain E0621`.

0 commit comments

Comments
 (0)