Skip to content

Commit 3417a4a

Browse files
authored
Unrolled build for rust-lang#139046
Rollup merge of rust-lang#139046 - nnethercote:hir-Lifetime-better, r=lcnr Improve `Lifetime::suggestion` r? ``@lcnr``
2 parents f97b3c6 + d42edee commit 3417a4a

File tree

5 files changed

+104
-31
lines changed

5 files changed

+104
-31
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
5555
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5656
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5757
use rustc_hir::{
58-
self as hir, ConstArg, GenericArg, HirId, ItemLocalMap, LangItem, LifetimeSource,
59-
LifetimeSyntax, ParamName, TraitCandidate,
58+
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LangItem,
59+
LifetimeSource, LifetimeSyntax, ParamName, TraitCandidate,
6060
};
6161
use rustc_index::{Idx, IndexSlice, IndexVec};
6262
use rustc_macros::extension;
@@ -1087,7 +1087,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10871087
match arg {
10881088
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(
10891089
lt,
1090-
LifetimeSource::Path { with_angle_brackets: true },
1090+
LifetimeSource::Path { angle_brackets: hir::AngleBrackets::Full },
10911091
lt.ident.into(),
10921092
)),
10931093
ast::GenericArg::Type(ty) => {
@@ -1779,13 +1779,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17791779
&mut self,
17801780
id: NodeId,
17811781
span: Span,
1782-
with_angle_brackets: bool,
1782+
angle_brackets: AngleBrackets,
17831783
) -> &'hir hir::Lifetime {
17841784
self.new_named_lifetime(
17851785
id,
17861786
id,
17871787
Ident::new(kw::UnderscoreLifetime, span),
1788-
LifetimeSource::Path { with_angle_brackets },
1788+
LifetimeSource::Path { angle_brackets },
17891789
LifetimeSyntax::Hidden,
17901790
)
17911791
}

compiler/rustc_ast_lowering/src/path.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -432,27 +432,31 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
432432

433433
// Note: these spans are used for diagnostics when they can't be inferred.
434434
// See rustc_resolve::late::lifetimes::LifetimeContext::add_missing_lifetime_specifiers_label
435-
let (elided_lifetime_span, with_angle_brackets) = if generic_args.span.is_empty() {
436-
// If there are no brackets, use the identifier span.
435+
let (elided_lifetime_span, angle_brackets) = if generic_args.span.is_empty() {
436+
// No brackets, e.g. `Path`: use an empty span just past the end of the identifier.
437437
// HACK: we use find_ancestor_inside to properly suggest elided spans in paths
438438
// originating from macros, since the segment's span might be from a macro arg.
439-
(segment_ident_span.find_ancestor_inside(path_span).unwrap_or(path_span), false)
440-
} else if generic_args.is_empty() {
441-
// If there are brackets, but not generic arguments, then use the opening bracket
442-
(generic_args.span.with_hi(generic_args.span.lo() + BytePos(1)), true)
439+
(
440+
segment_ident_span.find_ancestor_inside(path_span).unwrap_or(path_span),
441+
hir::AngleBrackets::Missing,
442+
)
443443
} else {
444-
// Else use an empty span right after the opening bracket.
445-
(generic_args.span.with_lo(generic_args.span.lo() + BytePos(1)).shrink_to_lo(), true)
444+
// Brackets, e.g. `Path<>` or `Path<T>`: use an empty span just after the `<`.
445+
(
446+
generic_args.span.with_lo(generic_args.span.lo() + BytePos(1)).shrink_to_lo(),
447+
if generic_args.is_empty() {
448+
hir::AngleBrackets::Empty
449+
} else {
450+
hir::AngleBrackets::Full
451+
},
452+
)
446453
};
447454

448455
generic_args.args.insert_many(
449456
0,
450457
(start..end).map(|id| {
451-
let l = self.lower_lifetime_hidden_in_path(
452-
id,
453-
elided_lifetime_span,
454-
with_angle_brackets,
455-
);
458+
let l =
459+
self.lower_lifetime_hidden_in_path(id, elided_lifetime_span, angle_brackets);
456460
GenericArg::Lifetime(l)
457461
}),
458462
);

compiler/rustc_hir/src/hir.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,24 @@ use crate::def_id::{DefId, LocalDefIdMap};
3535
pub(crate) use crate::hir_id::{HirId, ItemLocalId, ItemLocalMap, OwnerId};
3636
use crate::intravisit::{FnKind, VisitorExt};
3737

38+
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable_Generic)]
39+
pub enum AngleBrackets {
40+
/// E.g. `Path`.
41+
Missing,
42+
/// E.g. `Path<>`.
43+
Empty,
44+
/// E.g. `Path<T>`.
45+
Full,
46+
}
47+
3848
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable_Generic)]
3949
pub enum LifetimeSource {
4050
/// E.g. `&Type`, `&'_ Type`, `&'a Type`, `&mut Type`, `&'_ mut Type`, `&'a mut Type`
4151
Reference,
4252

43-
/// E.g. `ContainsLifetime`, `ContainsLifetime<'_>`, `ContainsLifetime<'a>`
44-
Path {
45-
/// - true for `ContainsLifetime<'_>`, `ContainsLifetime<'a>`,
46-
/// `ContainsLifetime<'_, T>`, `ContainsLifetime<'a, T>`
47-
/// - false for `ContainsLifetime`
48-
with_angle_brackets: bool,
49-
},
53+
/// E.g. `ContainsLifetime`, `ContainsLifetime<>`, `ContainsLifetime<'_>`,
54+
/// `ContainsLifetime<'a>`
55+
Path { angle_brackets: AngleBrackets },
5056

5157
/// E.g. `impl Trait + '_`, `impl Trait + 'a`
5258
OutlivesBound,
@@ -304,12 +310,17 @@ impl Lifetime {
304310
(Named | Anonymous, _) => (self.ident.span, format!("{new_lifetime}")),
305311

306312
// The user wrote `Path<T>`, and omitted the `'_,`.
307-
(Hidden, Path { with_angle_brackets: true }) => {
313+
(Hidden, Path { angle_brackets: AngleBrackets::Full }) => {
308314
(self.ident.span, format!("{new_lifetime}, "))
309315
}
310316

317+
// The user wrote `Path<>`, and omitted the `'_`..
318+
(Hidden, Path { angle_brackets: AngleBrackets::Empty }) => {
319+
(self.ident.span, format!("{new_lifetime}"))
320+
}
321+
311322
// The user wrote `Path` and omitted the `<'_>`.
312-
(Hidden, Path { with_angle_brackets: false }) => {
323+
(Hidden, Path { angle_brackets: AngleBrackets::Missing }) => {
313324
(self.ident.span.shrink_to_hi(), format!("<{new_lifetime}>"))
314325
}
315326

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

+11
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ mod alone_in_path {
4949
//~| ERROR missing lifetime specifier
5050
}
5151

52+
mod alone_in_path2 {
53+
trait Foo<'a> { fn next(&mut self) -> Option<&'a ()>; }
54+
55+
fn f(_: impl Foo<>) {}
56+
//~^ ERROR anonymous lifetimes in `impl Trait` are unstable
57+
58+
fn g(mut x: impl Foo<>) -> Option<&()> { x.next() }
59+
//~^ ERROR anonymous lifetimes in `impl Trait` are unstable
60+
//~| ERROR missing lifetime specifier
61+
}
62+
5263
mod in_path {
5364
trait Foo<'a, T> { fn next(&mut self) -> Option<&'a T>; }
5465

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

+51-4
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,28 @@ 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:58:41
111+
--> $DIR/impl-trait-missing-lifetime-gated.rs:58:39
112+
|
113+
LL | fn g(mut x: impl Foo<>) -> Option<&()> { x.next() }
114+
| ^ expected named lifetime parameter
115+
|
116+
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
117+
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
118+
|
119+
LL | fn g(mut x: impl Foo<>) -> Option<&'static ()> { x.next() }
120+
| +++++++
121+
help: consider introducing a named lifetime parameter
122+
|
123+
LL | fn g<'a>(mut x: impl Foo<>) -> Option<&'a ()> { x.next() }
124+
| ++++ ++
125+
help: alternatively, you might want to return an owned value
126+
|
127+
LL - fn g(mut x: impl Foo<>) -> Option<&()> { x.next() }
128+
LL + fn g(mut x: impl Foo<>) -> Option<()> { x.next() }
129+
|
130+
131+
error[E0106]: missing lifetime specifier
132+
--> $DIR/impl-trait-missing-lifetime-gated.rs:69:41
112133
|
113134
LL | fn g(mut x: impl Foo<()>) -> Option<&()> { x.next() }
114135
| ^ expected named lifetime parameter
@@ -129,7 +150,7 @@ LL + fn g(mut x: impl Foo<()>) -> Option<()> { x.next() }
129150
|
130151

131152
warning: elided lifetime has a name
132-
--> $DIR/impl-trait-missing-lifetime-gated.rs:64:57
153+
--> $DIR/impl-trait-missing-lifetime-gated.rs:75:57
133154
|
134155
LL | fn resolved_anonymous<'a, T: 'a>(f: impl Fn(&'a str) -> &T) {
135156
| -- lifetime `'a` declared here ^ this elided lifetime gets resolved as `'a`
@@ -219,6 +240,32 @@ LL | fn g<'a>(mut x: impl Foo<'a>) -> Option<&()> { x.next() }
219240
error[E0658]: anonymous lifetimes in `impl Trait` are unstable
220241
--> $DIR/impl-trait-missing-lifetime-gated.rs:55:22
221242
|
243+
LL | fn f(_: impl Foo<>) {}
244+
| ^ expected named lifetime parameter
245+
|
246+
= help: add `#![feature(anonymous_lifetime_in_impl_trait)]` to the crate attributes to enable
247+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
248+
help: consider introducing a named lifetime parameter
249+
|
250+
LL | fn f<'a>(_: impl Foo<'a>) {}
251+
| ++++ ++
252+
253+
error[E0658]: anonymous lifetimes in `impl Trait` are unstable
254+
--> $DIR/impl-trait-missing-lifetime-gated.rs:58:26
255+
|
256+
LL | fn g(mut x: impl Foo<>) -> Option<&()> { x.next() }
257+
| ^ expected named lifetime parameter
258+
|
259+
= help: add `#![feature(anonymous_lifetime_in_impl_trait)]` to the crate attributes to enable
260+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
261+
help: consider introducing a named lifetime parameter
262+
|
263+
LL | fn g<'a>(mut x: impl Foo<'a>) -> Option<&()> { x.next() }
264+
| ++++ ++
265+
266+
error[E0658]: anonymous lifetimes in `impl Trait` are unstable
267+
--> $DIR/impl-trait-missing-lifetime-gated.rs:66:22
268+
|
222269
LL | fn f(_: impl Foo<()>) {}
223270
| ^ expected named lifetime parameter
224271
|
@@ -230,7 +277,7 @@ LL | fn f<'a>(_: impl Foo<'a, ()>) {}
230277
| ++++ +++
231278

232279
error[E0658]: anonymous lifetimes in `impl Trait` are unstable
233-
--> $DIR/impl-trait-missing-lifetime-gated.rs:58:26
280+
--> $DIR/impl-trait-missing-lifetime-gated.rs:69:26
234281
|
235282
LL | fn g(mut x: impl Foo<()>) -> Option<&()> { x.next() }
236283
| ^ expected named lifetime parameter
@@ -242,7 +289,7 @@ help: consider introducing a named lifetime parameter
242289
LL | fn g<'a>(mut x: impl Foo<'a, ()>) -> Option<&()> { x.next() }
243290
| ++++ +++
244291

245-
error: aborting due to 14 previous errors; 1 warning emitted
292+
error: aborting due to 17 previous errors; 1 warning emitted
246293

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

0 commit comments

Comments
 (0)