Skip to content

Commit 5f3357c

Browse files
Resolve const lifetimes to static in trait too
1 parent 805397c commit 5f3357c

8 files changed

+69
-124
lines changed

compiler/rustc_resolve/src/late.rs

+34-22
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ enum LifetimeRibKind {
312312

313313
/// Resolves elided lifetimes to `'static` if there are no other lifetimes in scope,
314314
/// otherwise give a warning that the previous behavior of introducing a new early-bound
315-
/// lifetime is a bug and will be removed.
316-
StaticIfNoLifetimeInScope(NodeId),
315+
/// lifetime is a bug and will be removed (if `emit_lint` is enabled).
316+
StaticIfNoLifetimeInScope { lint_id: NodeId, emit_lint: bool },
317317

318318
/// Signal we cannot find which should be the anonymous lifetime.
319319
ElisionFailure,
@@ -1213,7 +1213,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
12131213
}
12141214
LifetimeRibKind::AnonymousCreateParameter { .. }
12151215
| LifetimeRibKind::AnonymousReportError
1216-
| LifetimeRibKind::StaticIfNoLifetimeInScope(_)
1216+
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. }
12171217
| LifetimeRibKind::Elided(_)
12181218
| LifetimeRibKind::ElisionFailure
12191219
| LifetimeRibKind::ConcreteAnonConst(_)
@@ -1581,7 +1581,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15811581
// lifetime would be illegal.
15821582
LifetimeRibKind::Item
15831583
| LifetimeRibKind::AnonymousReportError
1584-
| LifetimeRibKind::StaticIfNoLifetimeInScope(_)
1584+
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. }
15851585
| LifetimeRibKind::ElisionFailure => Some(LifetimeUseSet::Many),
15861586
// An anonymous lifetime is legal here, and bound to the right
15871587
// place, go ahead.
@@ -1644,7 +1644,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
16441644
| LifetimeRibKind::Generics { .. }
16451645
| LifetimeRibKind::ElisionFailure
16461646
| LifetimeRibKind::AnonymousReportError
1647-
| LifetimeRibKind::StaticIfNoLifetimeInScope(_) => {}
1647+
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. } => {}
16481648
}
16491649
}
16501650

@@ -1678,7 +1678,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
16781678
self.record_lifetime_res(lifetime.id, res, elision_candidate);
16791679
return;
16801680
}
1681-
LifetimeRibKind::StaticIfNoLifetimeInScope(node_id) => {
1681+
LifetimeRibKind::StaticIfNoLifetimeInScope { lint_id: node_id, emit_lint } => {
16821682
let mut lifetimes_in_scope = vec![];
16831683
for rib in &self.lifetime_ribs[..i] {
16841684
lifetimes_in_scope.extend(rib.bindings.iter().map(|(ident, _)| ident.span));
@@ -1696,7 +1696,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
16961696
elision_candidate,
16971697
);
16981698
return;
1699-
} else {
1699+
} else if emit_lint {
17001700
self.r.lint_buffer.buffer_lint(
17011701
lint::builtin::ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
17021702
node_id,
@@ -1925,7 +1925,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
19251925
// impl Foo for std::cell::Ref<u32> // note lack of '_
19261926
// async fn foo(_: std::cell::Ref<u32>) { ... }
19271927
LifetimeRibKind::AnonymousCreateParameter { report_in_path: true, .. }
1928-
| LifetimeRibKind::StaticIfNoLifetimeInScope(_) => {
1928+
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. } => {
19291929
let sess = self.r.tcx.sess;
19301930
let subdiag = rustc_errors::elided_lifetime_in_path_suggestion(
19311931
sess.source_map(),
@@ -2859,19 +2859,27 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
28592859
kind: LifetimeBinderKind::ConstItem,
28602860
},
28612861
|this| {
2862-
this.visit_generics(generics);
2863-
this.visit_ty(ty);
2864-
2865-
// Only impose the restrictions of `ConstRibKind` for an
2866-
// actual constant expression in a provided default.
2867-
if let Some(expr) = expr {
2868-
// We allow arbitrary const expressions inside of associated consts,
2869-
// even if they are potentially not const evaluatable.
2870-
//
2871-
// Type parameters can already be used and as associated consts are
2872-
// not used as part of the type system, this is far less surprising.
2873-
this.resolve_const_body(expr, None);
2874-
}
2862+
this.with_lifetime_rib(
2863+
LifetimeRibKind::StaticIfNoLifetimeInScope {
2864+
lint_id: item.id,
2865+
emit_lint: false,
2866+
},
2867+
|this| {
2868+
this.visit_generics(generics);
2869+
this.visit_ty(ty);
2870+
2871+
// Only impose the restrictions of `ConstRibKind` for an
2872+
// actual constant expression in a provided default.
2873+
if let Some(expr) = expr {
2874+
// We allow arbitrary const expressions inside of associated consts,
2875+
// even if they are potentially not const evaluatable.
2876+
//
2877+
// Type parameters can already be used and as associated consts are
2878+
// not used as part of the type system, this is far less surprising.
2879+
this.resolve_const_body(expr, None);
2880+
}
2881+
},
2882+
)
28752883
},
28762884
);
28772885
}
@@ -3052,7 +3060,11 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
30523060
},
30533061
|this| {
30543062
this.with_lifetime_rib(
3055-
LifetimeRibKind::StaticIfNoLifetimeInScope(item.id),
3063+
LifetimeRibKind::StaticIfNoLifetimeInScope {
3064+
lint_id: item.id,
3065+
// In impls, it's not a hard error yet due to backcompat.
3066+
emit_lint: true,
3067+
},
30563068
|this| {
30573069
// If this is a trait impl, ensure the const
30583070
// exists in trait

tests/ui/consts/static-default-lifetime/elided-lifetime.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ impl Foo<'_> {
99
}
1010

1111
trait Bar {
12-
const STATIC: &'static str;
13-
// TODO^
12+
const STATIC: &str;
1413
}
1514

1615
impl Bar for Foo<'_> {

tests/ui/consts/static-default-lifetime/elided-lifetime.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ LL | const STATIC: &'static str = "";
2222
| +++++++
2323

2424
error: `&` without an explicit lifetime name cannot be used here
25-
--> $DIR/elided-lifetime.rs:17:19
25+
--> $DIR/elided-lifetime.rs:16:19
2626
|
2727
LL | const STATIC: &str = "";
2828
| ^
2929
|
3030
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3131
= note: for more information, see issue #115010 <https://github.com/rust-lang/rust/issues/115010>
3232
note: cannot automatically infer `'static` because of other lifetimes in scope
33-
--> $DIR/elided-lifetime.rs:16:18
33+
--> $DIR/elided-lifetime.rs:15:18
3434
|
3535
LL | impl Bar for Foo<'_> {
3636
| ^^
@@ -40,15 +40,15 @@ LL | const STATIC: &'static str = "";
4040
| +++++++
4141

4242
error[E0308]: const not compatible with trait
43-
--> $DIR/elided-lifetime.rs:17:5
43+
--> $DIR/elided-lifetime.rs:16:5
4444
|
4545
LL | const STATIC: &str = "";
4646
| ^^^^^^^^^^^^^^^^^^ lifetime mismatch
4747
|
4848
= note: expected reference `&'static _`
4949
found reference `&_`
5050
note: the anonymous lifetime as defined here...
51-
--> $DIR/elided-lifetime.rs:16:18
51+
--> $DIR/elided-lifetime.rs:15:18
5252
|
5353
LL | impl Bar for Foo<'_> {
5454
| ^^

tests/ui/consts/static-default-lifetime/generic-associated-const.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ impl A {
1212

1313
trait Trait {
1414
const GAC_TYPE<T>: &str = "";
15-
//~^ ERROR missing lifetime specifier
1615
const GAC_LIFETIME<'a>: &str = "";
1716
//~^ ERROR missing lifetime specifier
1817
}

tests/ui/consts/static-default-lifetime/generic-associated-const.stderr

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
error[E0106]: missing lifetime specifier
2-
--> $DIR/generic-associated-const.rs:14:24
3-
|
4-
LL | const GAC_TYPE<T>: &str = "";
5-
| ^ expected named lifetime parameter
6-
|
7-
help: consider introducing a named lifetime parameter
8-
|
9-
LL | const GAC_TYPE<'a, T>: &'a str = "";
10-
| +++ ++
11-
12-
error[E0106]: missing lifetime specifier
13-
--> $DIR/generic-associated-const.rs:16:29
2+
--> $DIR/generic-associated-const.rs:15:29
143
|
154
LL | const GAC_LIFETIME<'a>: &str = "";
165
| ^ expected named lifetime parameter
@@ -52,6 +41,6 @@ help: use the `'static` lifetime
5241
LL | const GAC_LIFETIME<'a>: &'static str = "";
5342
| +++++++
5443

55-
error: aborting due to 3 previous errors; 1 warning emitted
44+
error: aborting due to 2 previous errors; 1 warning emitted
5645

5746
For more information about this error, try `rustc --explain E0106`.
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,25 @@
1-
error[E0106]: missing lifetime specifier
2-
--> $DIR/missing-lifetime-in-assoc-const-type.rs:6:14
3-
|
4-
LL | const A: &str = "";
5-
| ^ expected named lifetime parameter
6-
|
7-
help: consider introducing a named lifetime parameter
8-
|
9-
LL ~ trait ZstAssert<'a>: Sized {
10-
LL ~ const A: &'a str = "";
11-
|
12-
13-
error[E0106]: missing lifetime specifier
1+
error[E0726]: implicit elided lifetime not allowed here
142
--> $DIR/missing-lifetime-in-assoc-const-type.rs:7:14
153
|
164
LL | const B: S = S { s: &() };
17-
| ^ expected named lifetime parameter
18-
|
19-
help: consider introducing a named lifetime parameter
20-
|
21-
LL ~ trait ZstAssert<'a>: Sized {
22-
LL | const A: &str = "";
23-
LL ~ const B: S<'a> = S { s: &() };
5+
| ^ expected lifetime parameter
246
|
25-
26-
error[E0106]: missing lifetime specifier
27-
--> $DIR/missing-lifetime-in-assoc-const-type.rs:8:15
28-
|
29-
LL | const C: &'_ str = "";
30-
| ^^ expected named lifetime parameter
31-
|
32-
help: consider introducing a named lifetime parameter
33-
|
34-
LL ~ trait ZstAssert<'a>: Sized {
35-
LL | const A: &str = "";
36-
LL | const B: S = S { s: &() };
37-
LL ~ const C: &'a str = "";
7+
help: indicate the anonymous lifetime
388
|
9+
LL | const B: S<'_> = S { s: &() };
10+
| ++++
3911

40-
error[E0106]: missing lifetime specifiers
12+
error[E0726]: implicit elided lifetime not allowed here
4113
--> $DIR/missing-lifetime-in-assoc-const-type.rs:9:14
4214
|
4315
LL | const D: T = T { a: &(), b: &() };
44-
| ^ expected 2 lifetime parameters
45-
|
46-
help: consider introducing a named lifetime parameter
16+
| ^ expected lifetime parameters
4717
|
48-
LL ~ trait ZstAssert<'a>: Sized {
49-
LL | const A: &str = "";
50-
LL | const B: S = S { s: &() };
51-
LL | const C: &'_ str = "";
52-
LL ~ const D: T<'a, 'a> = T { a: &(), b: &() };
18+
help: indicate the anonymous lifetimes
5319
|
20+
LL | const D: T<'_, '_> = T { a: &(), b: &() };
21+
| ++++++++
5422

55-
error: aborting due to 4 previous errors
23+
error: aborting due to 2 previous errors
5624

57-
For more information about this error, try `rustc --explain E0106`.
25+
For more information about this error, try `rustc --explain E0726`.
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,25 @@
1-
error[E0106]: missing lifetime specifier
2-
--> $DIR/missing-lifetime-in-assoc-const-type.rs:6:14
3-
|
4-
LL | const A: &str = "";
5-
| ^ expected named lifetime parameter
6-
|
7-
help: consider introducing a named lifetime parameter
8-
|
9-
LL | const A<'a>: &'a str = "";
10-
| ++++ ++
11-
12-
error[E0106]: missing lifetime specifier
1+
error[E0726]: implicit elided lifetime not allowed here
132
--> $DIR/missing-lifetime-in-assoc-const-type.rs:7:14
143
|
154
LL | const B: S = S { s: &() };
16-
| ^ expected named lifetime parameter
17-
|
18-
help: consider introducing a named lifetime parameter
19-
|
20-
LL | const B<'a>: S<'a> = S { s: &() };
21-
| ++++ ++++
22-
23-
error[E0106]: missing lifetime specifier
24-
--> $DIR/missing-lifetime-in-assoc-const-type.rs:8:15
25-
|
26-
LL | const C: &'_ str = "";
27-
| ^^ expected named lifetime parameter
5+
| ^ expected lifetime parameter
286
|
29-
help: consider introducing a named lifetime parameter
7+
help: indicate the anonymous lifetime
308
|
31-
LL | const C<'a>: &'a str = "";
32-
| ++++ ~~
9+
LL | const B: S<'_> = S { s: &() };
10+
| ++++
3311

34-
error[E0106]: missing lifetime specifiers
12+
error[E0726]: implicit elided lifetime not allowed here
3513
--> $DIR/missing-lifetime-in-assoc-const-type.rs:9:14
3614
|
3715
LL | const D: T = T { a: &(), b: &() };
38-
| ^ expected 2 lifetime parameters
16+
| ^ expected lifetime parameters
3917
|
40-
help: consider introducing a named lifetime parameter
18+
help: indicate the anonymous lifetimes
4119
|
42-
LL | const D<'a>: T<'a, 'a> = T { a: &(), b: &() };
43-
| ++++ ++++++++
20+
LL | const D: T<'_, '_> = T { a: &(), b: &() };
21+
| ++++++++
4422

45-
error: aborting due to 4 previous errors
23+
error: aborting due to 2 previous errors
4624

47-
For more information about this error, try `rustc --explain E0106`.
25+
For more information about this error, try `rustc --explain E0726`.

tests/ui/suggestions/missing-lifetime-in-assoc-const-type.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#![cfg_attr(generic_const_items, feature(generic_const_items), allow(incomplete_features))]
44

55
trait ZstAssert: Sized {
6-
const A: &str = ""; //~ ERROR missing lifetime specifier
7-
const B: S = S { s: &() }; //~ ERROR missing lifetime specifier
8-
const C: &'_ str = ""; //~ ERROR missing lifetime specifier
9-
const D: T = T { a: &(), b: &() }; //~ ERROR missing lifetime specifier
6+
const A: &str = "";
7+
const B: S = S { s: &() }; //~ ERROR implicit elided lifetime not allowed here
8+
const C: &'_ str = "";
9+
const D: T = T { a: &(), b: &() }; //~ ERROR implicit elided lifetime not allowed here
1010
}
1111

1212
struct S<'a> {

0 commit comments

Comments
 (0)