Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f0d3817

Browse files
committedMar 3, 2025
Do not recover missing lifetime with random in-scope lifetime
1 parent daf5985 commit f0d3817

18 files changed

+33
-168
lines changed
 

‎compiler/rustc_resolve/src/late/diagnostics.rs

-6
Original file line numberDiff line numberDiff line change
@@ -3515,12 +3515,6 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
35153515
}
35163516
}
35173517
}
3518-
3519-
// Record as using the suggested resolution.
3520-
let (_, (_, res)) = in_scope_lifetimes[0];
3521-
for &lt in &lifetime_refs {
3522-
self.r.lifetimes_res_map.insert(lt.id, res);
3523-
}
35243518
}
35253519
_ => {
35263520
let lifetime_spans: Vec<_> =

‎tests/ui/async-await/issues/issue-63388-2.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ impl Xyz {
1212
) -> &dyn Foo //~ ERROR missing lifetime specifier
1313
{
1414
foo
15-
//~^ ERROR explicit lifetime required in the type of `foo` [E0621]
1615
}
1716
}
1817

‎tests/ui/async-await/issues/issue-63388-2.stderr

+2-12
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@ help: consider using the `'a` lifetime
1212
LL | ) -> &'a dyn Foo
1313
| ++
1414

15-
error[E0621]: explicit lifetime required in the type of `foo`
16-
--> $DIR/issue-63388-2.rs:14:9
17-
|
18-
LL | foo: &dyn Foo, bar: &'a dyn Foo
19-
| -------- help: add explicit lifetime `'a` to the type of `foo`: `&'a (dyn Foo + 'a)`
20-
...
21-
LL | foo
22-
| ^^^ lifetime `'a` required
23-
24-
error: aborting due to 2 previous errors
15+
error: aborting due to 1 previous error
2516

26-
Some errors have detailed explanations: E0106, E0621.
27-
For more information about an error, try `rustc --explain E0106`.
17+
For more information about this error, try `rustc --explain E0106`.

‎tests/ui/impl-trait/in-trait/opaque-and-lifetime-mismatch.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ trait Foo {
88

99
impl Foo for () {
1010
fn bar() -> i32 {
11-
//~^ ERROR method `bar` has an incompatible type for trait
12-
//~| ERROR method `bar` has an incompatible return type for trait
1311
0
1412
}
1513
}

‎tests/ui/impl-trait/in-trait/opaque-and-lifetime-mismatch.stderr

+7-35
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | fn bar() -> Wrapper<'static, impl Sized>;
1111
| ++++++++
1212

1313
error[E0106]: missing lifetime specifier
14-
--> $DIR/opaque-and-lifetime-mismatch.rs:18:24
14+
--> $DIR/opaque-and-lifetime-mismatch.rs:16:24
1515
|
1616
LL | fn foo() -> Wrapper<impl Sized>;
1717
| ^ expected named lifetime parameter
@@ -23,7 +23,7 @@ LL | fn foo() -> Wrapper<'static, impl Sized>;
2323
| ++++++++
2424

2525
error[E0106]: missing lifetime specifier
26-
--> $DIR/opaque-and-lifetime-mismatch.rs:24:24
26+
--> $DIR/opaque-and-lifetime-mismatch.rs:22:24
2727
|
2828
LL | fn foo() -> Wrapper<impl Sized> {
2929
| ^ expected named lifetime parameter
@@ -49,7 +49,7 @@ LL | struct Wrapper<'rom>(&'rom ());
4949
| ^^^^^^^
5050

5151
error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied
52-
--> $DIR/opaque-and-lifetime-mismatch.rs:18:17
52+
--> $DIR/opaque-and-lifetime-mismatch.rs:16:17
5353
|
5454
LL | fn foo() -> Wrapper<impl Sized>;
5555
| ^^^^^^^ ---------- help: remove the unnecessary generic argument
@@ -62,36 +62,8 @@ note: struct defined here, with 0 generic parameters
6262
LL | struct Wrapper<'rom>(&'rom ());
6363
| ^^^^^^^
6464

65-
error[E0053]: method `bar` has an incompatible return type for trait
66-
--> $DIR/opaque-and-lifetime-mismatch.rs:10:17
67-
|
68-
LL | fn bar() -> i32 {
69-
| ^^^
70-
| |
71-
| expected `Wrapper<'static>`, found `i32`
72-
| return type in trait
73-
74-
error[E0053]: method `bar` has an incompatible type for trait
75-
--> $DIR/opaque-and-lifetime-mismatch.rs:10:17
76-
|
77-
LL | fn bar() -> i32 {
78-
| ^^^ expected `Wrapper<'static>`, found `i32`
79-
|
80-
note: type in trait
81-
--> $DIR/opaque-and-lifetime-mismatch.rs:4:17
82-
|
83-
LL | fn bar() -> Wrapper<impl Sized>;
84-
| ^^^^^^^^^^^^^^^^^^^
85-
= note: expected signature `fn() -> Wrapper<'static>`
86-
found signature `fn() -> i32`
87-
help: change the output type to match the trait
88-
|
89-
LL - fn bar() -> i32 {
90-
LL + fn bar() -> Wrapper<'static> {
91-
|
92-
9365
error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied
94-
--> $DIR/opaque-and-lifetime-mismatch.rs:24:17
66+
--> $DIR/opaque-and-lifetime-mismatch.rs:22:17
9567
|
9668
LL | fn foo() -> Wrapper<impl Sized> {
9769
| ^^^^^^^ ---------- help: remove the unnecessary generic argument
@@ -104,7 +76,7 @@ note: struct defined here, with 0 generic parameters
10476
LL | struct Wrapper<'rom>(&'rom ());
10577
| ^^^^^^^
10678

107-
error: aborting due to 8 previous errors
79+
error: aborting due to 6 previous errors
10880

109-
Some errors have detailed explanations: E0053, E0106, E0107.
110-
For more information about an error, try `rustc --explain E0053`.
81+
Some errors have detailed explanations: E0106, E0107.
82+
For more information about an error, try `rustc --explain E0106`.

‎tests/ui/impl-trait/opaque-used-in-extraneous-argument.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ error[E0061]: this function takes 0 arguments but 1 argument was supplied
7575
--> $DIR/opaque-used-in-extraneous-argument.rs:20:5
7676
|
7777
LL | open_parent(&old_path)
78-
| ^^^^^^^^^^^ --------- unexpected argument of type `&impl Fn<{type error}> + FnOnce<{type error}, Output = {type error}> + 'static`
78+
| ^^^^^^^^^^^ --------- unexpected argument of type `&impl Fn<{type error}> + FnOnce<{type error}, Output = {type error}> + '_`
7979
|
8080
note: function defined here
8181
--> $DIR/opaque-used-in-extraneous-argument.rs:12:4

‎tests/ui/impl-trait/precise-capturing/bad-lifetimes.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
fn no_elided_lt() -> impl Sized + use<'_> {}
22
//~^ ERROR missing lifetime specifier
3-
//~| ERROR expected lifetime parameter in `use<...>` precise captures list, found `'_`
43

54
fn static_lt() -> impl Sized + use<'static> {}
65
//~^ ERROR expected lifetime parameter in `use<...>` precise captures list, found `'static`

‎tests/ui/impl-trait/precise-capturing/bad-lifetimes.stderr

+3-9
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,20 @@ LL + fn no_elided_lt() -> impl Sized + use<'static> {}
1212
|
1313

1414
error[E0261]: use of undeclared lifetime name `'missing`
15-
--> $DIR/bad-lifetimes.rs:8:37
15+
--> $DIR/bad-lifetimes.rs:7:37
1616
|
1717
LL | fn missing_lt() -> impl Sized + use<'missing> {}
1818
| - ^^^^^^^^ undeclared lifetime
1919
| |
2020
| help: consider introducing lifetime `'missing` here: `<'missing>`
2121

22-
error: expected lifetime parameter in `use<...>` precise captures list, found `'_`
23-
--> $DIR/bad-lifetimes.rs:1:39
24-
|
25-
LL | fn no_elided_lt() -> impl Sized + use<'_> {}
26-
| ^^
27-
2822
error: expected lifetime parameter in `use<...>` precise captures list, found `'static`
29-
--> $DIR/bad-lifetimes.rs:5:36
23+
--> $DIR/bad-lifetimes.rs:4:36
3024
|
3125
LL | fn static_lt() -> impl Sized + use<'static> {}
3226
| ^^^^^^^
3327

34-
error: aborting due to 4 previous errors
28+
error: aborting due to 3 previous errors
3529

3630
Some errors have detailed explanations: E0106, E0261.
3731
For more information about an error, try `rustc --explain E0106`.

‎tests/ui/issues/issue-13497.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ fn read_lines_borrowed1() -> Vec<
33
> {
44
let rawLines: Vec<String> = vec!["foo ".to_string(), " bar".to_string()];
55
rawLines.iter().map(|l| l.trim()).collect()
6-
//~^ ERROR: cannot return value referencing
76
}
87

98
fn main() {}

‎tests/ui/issues/issue-13497.stderr

+2-12
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ LL - &str
1515
LL + String
1616
|
1717

18-
error[E0515]: cannot return value referencing local variable `rawLines`
19-
--> $DIR/issue-13497.rs:5:5
20-
|
21-
LL | rawLines.iter().map(|l| l.trim()).collect()
22-
| --------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23-
| |
24-
| returns a value referencing data owned by the current function
25-
| `rawLines` is borrowed here
26-
27-
error: aborting due to 2 previous errors
18+
error: aborting due to 1 previous error
2819

29-
Some errors have detailed explanations: E0106, E0515.
30-
For more information about an error, try `rustc --explain E0106`.
20+
For more information about this error, try `rustc --explain E0106`.

‎tests/ui/lifetimes/issue-26638.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ fn parse_type(iter: Box<dyn Iterator<Item=&str>+'static>) -> &str { iter.next()
33

44
fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter() }
55
//~^ ERROR missing lifetime specifier [E0106]
6-
//~| ERROR mismatched types
76
//~| ERROR function takes 1 argument but 0 arguments were supplied
87

98
fn parse_type_3() -> &str { unimplemented!() }

‎tests/ui/lifetimes/issue-26638.stderr

+3-14
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ LL + fn parse_type_2(iter: fn(&u8)->&u8) -> String { iter() }
3232
|
3333

3434
error[E0106]: missing lifetime specifier
35-
--> $DIR/issue-26638.rs:9:22
35+
--> $DIR/issue-26638.rs:8:22
3636
|
3737
LL | fn parse_type_3() -> &str { unimplemented!() }
3838
| ^ expected named lifetime parameter
@@ -59,18 +59,7 @@ help: provide the argument
5959
LL | fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter(/* &u8 */) }
6060
| +++++++++
6161

62-
error[E0308]: mismatched types
63-
--> $DIR/issue-26638.rs:4:47
64-
|
65-
LL | fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter() }
66-
| ---- ^^^^^^ expected `&str`, found `&u8`
67-
| |
68-
| expected `&'static str` because of return type
69-
|
70-
= note: expected reference `&'static str`
71-
found reference `&u8`
72-
73-
error: aborting due to 5 previous errors
62+
error: aborting due to 4 previous errors
7463

75-
Some errors have detailed explanations: E0061, E0106, E0308.
64+
Some errors have detailed explanations: E0061, E0106.
7665
For more information about an error, try `rustc --explain E0061`.

‎tests/ui/suggestions/impl-trait-missing-lifetime-gated.rs

-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ mod elided {
1818
// But that lifetime does not participate in resolution.
1919
async fn i(mut x: impl Iterator<Item = &()>) -> Option<&()> { x.next() }
2020
//~^ ERROR missing lifetime specifier
21-
//~| ERROR lifetime may not live long enough
2221
}
2322

2423
mod underscore {
@@ -37,7 +36,6 @@ mod underscore {
3736
// But that lifetime does not participate in resolution.
3837
async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
3938
//~^ ERROR missing lifetime specifier
40-
//~| ERROR lifetime may not live long enough
4139
}
4240

4341
mod alone_in_path {

‎tests/ui/suggestions/impl-trait-missing-lifetime-gated.stderr

+12-28
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ LL + async fn i(mut x: impl Iterator<Item = &()>) -> Option<()> { x.next() }
4141
|
4242

4343
error[E0106]: missing lifetime specifier
44-
--> $DIR/impl-trait-missing-lifetime-gated.rs:28:58
44+
--> $DIR/impl-trait-missing-lifetime-gated.rs:27:58
4545
|
4646
LL | fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
4747
| ^^ expected named lifetime parameter
@@ -64,7 +64,7 @@ LL + fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<()> { x.next() }
6464
|
6565

6666
error[E0106]: missing lifetime specifier
67-
--> $DIR/impl-trait-missing-lifetime-gated.rs:38:64
67+
--> $DIR/impl-trait-missing-lifetime-gated.rs:37:64
6868
|
6969
LL | async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
7070
| ^^ expected named lifetime parameter
@@ -87,7 +87,7 @@ LL + async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<()> { x.next(
8787
|
8888

8989
error[E0106]: missing lifetime specifier
90-
--> $DIR/impl-trait-missing-lifetime-gated.rs:49:37
90+
--> $DIR/impl-trait-missing-lifetime-gated.rs:47:37
9191
|
9292
LL | fn g(mut x: impl Foo) -> Option<&()> { x.next() }
9393
| ^ expected named lifetime parameter
@@ -108,7 +108,7 @@ LL + fn g(mut x: impl Foo) -> Option<()> { x.next() }
108108
|
109109

110110
error[E0106]: missing lifetime specifier
111-
--> $DIR/impl-trait-missing-lifetime-gated.rs:60:41
111+
--> $DIR/impl-trait-missing-lifetime-gated.rs:58:41
112112
|
113113
LL | fn g(mut x: impl Foo<()>) -> Option<&()> { x.next() }
114114
| ^ expected named lifetime parameter
@@ -129,7 +129,7 @@ LL + fn g(mut x: impl Foo<()>) -> Option<()> { x.next() }
129129
|
130130

131131
warning: elided lifetime has a name
132-
--> $DIR/impl-trait-missing-lifetime-gated.rs:66:57
132+
--> $DIR/impl-trait-missing-lifetime-gated.rs:64:57
133133
|
134134
LL | fn resolved_anonymous<'a, T: 'a>(f: impl Fn(&'a str) -> &T) {
135135
| -- lifetime `'a` declared here ^ this elided lifetime gets resolved as `'a`
@@ -162,16 +162,8 @@ help: consider introducing a named lifetime parameter
162162
LL | fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&()> { x.next() }
163163
| ++++ ++
164164

165-
error: lifetime may not live long enough
166-
--> $DIR/impl-trait-missing-lifetime-gated.rs:19:67
167-
|
168-
LL | async fn i(mut x: impl Iterator<Item = &()>) -> Option<&()> { x.next() }
169-
| ----------------------------------------------------------- ^^^^^^^^ returning this value requires that `'1` must outlive `'static`
170-
| |
171-
| return type `impl Future<Output = Option<&'static ()>>` contains a lifetime `'1`
172-
173165
error[E0658]: anonymous lifetimes in `impl Trait` are unstable
174-
--> $DIR/impl-trait-missing-lifetime-gated.rs:25:35
166+
--> $DIR/impl-trait-missing-lifetime-gated.rs:24:35
175167
|
176168
LL | fn f(_: impl Iterator<Item = &'_ ()>) {}
177169
| ^^ expected named lifetime parameter
@@ -185,7 +177,7 @@ LL + fn f<'a>(_: impl Iterator<Item = &'a ()>) {}
185177
|
186178

187179
error[E0658]: anonymous lifetimes in `impl Trait` are unstable
188-
--> $DIR/impl-trait-missing-lifetime-gated.rs:28:39
180+
--> $DIR/impl-trait-missing-lifetime-gated.rs:27:39
189181
|
190182
LL | fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
191183
| ^^ expected named lifetime parameter
@@ -198,16 +190,8 @@ LL - fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next()
198190
LL + fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'_ ()> { x.next() }
199191
|
200192

201-
error: lifetime may not live long enough
202-
--> $DIR/impl-trait-missing-lifetime-gated.rs:38:73
203-
|
204-
LL | async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
205-
| ----------------------------------------------------------------- ^^^^^^^^ returning this value requires that `'1` must outlive `'static`
206-
| |
207-
| return type `impl Future<Output = Option<&'static ()>>` contains a lifetime `'1`
208-
209193
error[E0658]: anonymous lifetimes in `impl Trait` are unstable
210-
--> $DIR/impl-trait-missing-lifetime-gated.rs:46:18
194+
--> $DIR/impl-trait-missing-lifetime-gated.rs:44:18
211195
|
212196
LL | fn f(_: impl Foo) {}
213197
| ^^^ expected named lifetime parameter
@@ -220,7 +204,7 @@ LL | fn f<'a>(_: impl Foo<'a>) {}
220204
| ++++ ++++
221205

222206
error[E0658]: anonymous lifetimes in `impl Trait` are unstable
223-
--> $DIR/impl-trait-missing-lifetime-gated.rs:49:22
207+
--> $DIR/impl-trait-missing-lifetime-gated.rs:47:22
224208
|
225209
LL | fn g(mut x: impl Foo) -> Option<&()> { x.next() }
226210
| ^^^ expected named lifetime parameter
@@ -233,7 +217,7 @@ LL | fn g<'a>(mut x: impl Foo<'a>) -> Option<&()> { x.next() }
233217
| ++++ ++++
234218

235219
error[E0658]: anonymous lifetimes in `impl Trait` are unstable
236-
--> $DIR/impl-trait-missing-lifetime-gated.rs:57:22
220+
--> $DIR/impl-trait-missing-lifetime-gated.rs:55:22
237221
|
238222
LL | fn f(_: impl Foo<()>) {}
239223
| ^ expected named lifetime parameter
@@ -246,7 +230,7 @@ LL | fn f<'a>(_: impl Foo<'a, ()>) {}
246230
| ++++ +++
247231

248232
error[E0658]: anonymous lifetimes in `impl Trait` are unstable
249-
--> $DIR/impl-trait-missing-lifetime-gated.rs:60:26
233+
--> $DIR/impl-trait-missing-lifetime-gated.rs:58:26
250234
|
251235
LL | fn g(mut x: impl Foo<()>) -> Option<&()> { x.next() }
252236
| ^ expected named lifetime parameter
@@ -258,7 +242,7 @@ help: consider introducing a named lifetime parameter
258242
LL | fn g<'a>(mut x: impl Foo<'a, ()>) -> Option<&()> { x.next() }
259243
| ++++ +++
260244

261-
error: aborting due to 16 previous errors; 1 warning emitted
245+
error: aborting due to 14 previous errors; 1 warning emitted
262246

263247
Some errors have detailed explanations: E0106, E0658.
264248
For more information about an error, try `rustc --explain E0106`.

‎tests/ui/suggestions/impl-trait-missing-lifetime.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ fn f(_: impl Iterator<Item = &'_ ()>) {}
88
// But that lifetime does not participate in resolution.
99
fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
1010
//~^ ERROR missing lifetime specifier
11-
//~| ERROR lifetime may not live long enough
1211

1312
// This is understood as `fn foo<'_1>(_: impl Iterator<Item = &'_1 ()>) {}`.
1413
async fn h(_: impl Iterator<Item = &'_ ()>) {}
1514

1615
// But that lifetime does not participate in resolution.
1716
async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
1817
//~^ ERROR missing lifetime specifier
19-
//~| ERROR lifetime may not live long enough
2018

2119
fn main() {}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.