Skip to content

Commit eaec6ce

Browse files
committed
Turn elided_lifetimes_in_associated_constant FCW into a hard error
1 parent 2aabf3c commit eaec6ce

10 files changed

Lines changed: 84 additions & 230 deletions

compiler/rustc_resolve/src/errors.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,28 +1661,6 @@ pub(crate) struct UnusedQualifications {
16611661
pub removal_span: Span,
16621662
}
16631663

1664-
#[derive(Diagnostic)]
1665-
#[diag(
1666-
"{$elided ->
1667-
[true] `&` without an explicit lifetime name cannot be used here
1668-
*[false] `'_` cannot be used here
1669-
}"
1670-
)]
1671-
pub(crate) struct AssociatedConstElidedLifetime {
1672-
#[suggestion(
1673-
"use the `'static` lifetime",
1674-
style = "verbose",
1675-
code = "{code}",
1676-
applicability = "machine-applicable"
1677-
)]
1678-
pub span: Span,
1679-
1680-
pub code: &'static str,
1681-
pub elided: bool,
1682-
#[note("cannot automatically infer `'static` because of other lifetimes in scope")]
1683-
pub lifetimes_in_scope: MultiSpan,
1684-
}
1685-
16861664
#[derive(Diagnostic)]
16871665
#[diag("lifetime parameter `{$ident}` only used once")]
16881666
pub(crate) struct SingleUseLifetime {

compiler/rustc_resolve/src/late.rs

Lines changed: 40 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ enum LifetimeRibKind {
345345
/// Resolves elided lifetimes to `'static` if there are no other lifetimes in scope,
346346
/// otherwise give a warning that the previous behavior of introducing a new early-bound
347347
/// lifetime is a bug and will be removed (if `emit_lint` is enabled).
348-
StaticIfNoLifetimeInScope { lint_id: NodeId, emit_lint: bool },
348+
StaticIfNoLifetimeInScope { emit_lint: bool },
349349

350350
/// Signal we cannot find which should be the anonymous lifetime.
351351
ElisionFailure,
@@ -1876,7 +1876,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
18761876
self.record_lifetime_res(lifetime.id, res, elision_candidate);
18771877
return;
18781878
}
1879-
LifetimeRibKind::StaticIfNoLifetimeInScope { lint_id: node_id, emit_lint } => {
1879+
LifetimeRibKind::StaticIfNoLifetimeInScope { emit_lint } => {
18801880
let mut lifetimes_in_scope = vec![];
18811881
for rib in self.lifetime_ribs[..i].iter().rev() {
18821882
lifetimes_in_scope.extend(rib.bindings.iter().map(|(ident, _)| ident.span));
@@ -1898,24 +1898,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
18981898
);
18991899
return;
19001900
} else if emit_lint {
1901-
let lt_span = if elided {
1902-
lifetime.ident.span.shrink_to_hi()
1903-
} else {
1904-
lifetime.ident.span
1905-
};
1906-
let code = if elided { "'static " } else { "'static" };
1907-
1908-
self.r.lint_buffer.buffer_lint(
1909-
lint::builtin::ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
1910-
node_id,
1911-
lifetime.ident.span,
1912-
crate::errors::AssociatedConstElidedLifetime {
1913-
elided,
1914-
code,
1915-
span: lt_span,
1916-
lifetimes_in_scope: lifetimes_in_scope.into(),
1917-
},
1918-
);
1901+
break;
19191902
}
19201903
}
19211904
LifetimeRibKind::AnonymousReportError => {
@@ -3356,10 +3339,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
33563339
generics.span,
33573340
|this| {
33583341
this.with_lifetime_rib(
3359-
LifetimeRibKind::StaticIfNoLifetimeInScope {
3360-
lint_id: item.id,
3361-
emit_lint: false,
3362-
},
3342+
LifetimeRibKind::StaticIfNoLifetimeInScope { emit_lint: false },
33633343
|this| {
33643344
this.visit_generics(generics);
33653345
if rhs_kind.is_type_const()
@@ -3576,66 +3556,46 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
35763556
generics.span,
35773557
|this| {
35783558
this.with_lifetime_rib(
3579-
// Until these are a hard error, we need to create them within the
3580-
// correct binder, Otherwise the lifetimes of this assoc const think
3581-
// they are lifetimes of the trait.
3582-
LifetimeRibKind::AnonymousCreateParameter {
3583-
binder: item.id,
3584-
report_in_path: true,
3559+
LifetimeRibKind::StaticIfNoLifetimeInScope {
3560+
// In impls, it's not a hard error yet due to backcompat.
3561+
emit_lint: true,
35853562
},
35863563
|this| {
3587-
this.with_lifetime_rib(
3588-
LifetimeRibKind::StaticIfNoLifetimeInScope {
3589-
lint_id: item.id,
3590-
// In impls, it's not a hard error yet due to backcompat.
3591-
emit_lint: true,
3592-
},
3593-
|this| {
3594-
// If this is a trait impl, ensure the const
3595-
// exists in trait
3596-
this.check_trait_item(
3597-
item.id,
3598-
*ident,
3599-
&item.kind,
3600-
ValueNS,
3601-
item.span,
3602-
seen_trait_items,
3603-
|i, s, c| ConstNotMemberOfTrait(i, s, c),
3604-
);
3564+
// If this is a trait impl, ensure the const
3565+
// exists in trait
3566+
this.check_trait_item(
3567+
item.id,
3568+
*ident,
3569+
&item.kind,
3570+
ValueNS,
3571+
item.span,
3572+
seen_trait_items,
3573+
|i, s, c| ConstNotMemberOfTrait(i, s, c),
3574+
);
36053575

3606-
this.visit_generics(generics);
3607-
if rhs_kind.is_type_const()
3608-
&& !this
3609-
.r
3610-
.tcx
3611-
.features()
3612-
.generic_const_parameter_types()
3613-
{
3614-
this.with_rib(TypeNS, RibKind::ConstParamTy, |this| {
3615-
this.with_rib(
3616-
ValueNS,
3617-
RibKind::ConstParamTy,
3618-
|this| {
3619-
this.with_lifetime_rib(
3620-
LifetimeRibKind::ConstParamTy,
3621-
|this| this.visit_ty(ty),
3622-
)
3623-
},
3624-
)
3625-
});
3626-
} else {
3627-
this.visit_ty(ty);
3628-
}
3629-
// We allow arbitrary const expressions inside of associated consts,
3630-
// even if they are potentially not const evaluatable.
3631-
//
3632-
// Type parameters can already be used and as associated consts are
3633-
// not used as part of the type system, this is far less surprising.
3634-
this.resolve_const_item_rhs(rhs_kind, None);
3635-
},
3636-
)
3576+
this.visit_generics(generics);
3577+
if rhs_kind.is_type_const()
3578+
&& !this.r.tcx.features().generic_const_parameter_types()
3579+
{
3580+
this.with_rib(TypeNS, RibKind::ConstParamTy, |this| {
3581+
this.with_rib(ValueNS, RibKind::ConstParamTy, |this| {
3582+
this.with_lifetime_rib(
3583+
LifetimeRibKind::ConstParamTy,
3584+
|this| this.visit_ty(ty),
3585+
)
3586+
})
3587+
});
3588+
} else {
3589+
this.visit_ty(ty);
3590+
}
3591+
// We allow arbitrary const expressions inside of associated consts,
3592+
// even if they are potentially not const evaluatable.
3593+
//
3594+
// Type parameters can already be used and as associated consts are
3595+
// not used as part of the type system, this is far less surprising.
3596+
this.resolve_const_item_rhs(rhs_kind, None);
36373597
},
3638-
);
3598+
)
36393599
},
36403600
);
36413601
self.resolve_define_opaques(define_opaque);

tests/ui/consts/assoc-const-elided-lifetime.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ struct Foo<'a> {
88

99
impl<'a> Foo<'a> {
1010
const FOO: Foo<'_> = Foo { x: PhantomData::<&()> };
11-
//~^ ERROR `'_` cannot be used here
12-
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
11+
//~^ ERROR missing lifetime specifier
1312

1413
const BAR: &() = &();
15-
//~^ ERROR `&` without an explicit lifetime name cannot be used here
16-
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
14+
//~^ ERROR missing lifetime specifier
1715
}
1816

1917
fn main() {}
Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,26 @@
1-
error: `'_` cannot be used here
1+
error[E0106]: missing lifetime specifier
22
--> $DIR/assoc-const-elided-lifetime.rs:10:20
33
|
44
LL | const FOO: Foo<'_> = Foo { x: PhantomData::<&()> };
5-
| ^^
5+
| ^^ expected named lifetime parameter
66
|
7-
note: cannot automatically infer `'static` because of other lifetimes in scope
8-
--> $DIR/assoc-const-elided-lifetime.rs:9:6
9-
|
10-
LL | impl<'a> Foo<'a> {
11-
| ^^
12-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
13-
= note: for more information, see issue #115010 <https://github.com/rust-lang/rust/issues/115010>
14-
note: the lint level is defined here
15-
--> $DIR/assoc-const-elided-lifetime.rs:1:9
16-
|
17-
LL | #![deny(elided_lifetimes_in_associated_constant)]
18-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19-
help: use the `'static` lifetime
7+
help: consider using the `'a` lifetime
208
|
219
LL - const FOO: Foo<'_> = Foo { x: PhantomData::<&()> };
22-
LL + const FOO: Foo<'static> = Foo { x: PhantomData::<&()> };
10+
LL + const FOO: Foo<'a> = Foo { x: PhantomData::<&()> };
2311
|
2412

25-
error: `&` without an explicit lifetime name cannot be used here
26-
--> $DIR/assoc-const-elided-lifetime.rs:14:16
13+
error[E0106]: missing lifetime specifier
14+
--> $DIR/assoc-const-elided-lifetime.rs:13:16
2715
|
2816
LL | const BAR: &() = &();
29-
| ^
30-
|
31-
note: cannot automatically infer `'static` because of other lifetimes in scope
32-
--> $DIR/assoc-const-elided-lifetime.rs:9:6
17+
| ^ expected named lifetime parameter
3318
|
34-
LL | impl<'a> Foo<'a> {
35-
| ^^
36-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
37-
= note: for more information, see issue #115010 <https://github.com/rust-lang/rust/issues/115010>
38-
help: use the `'static` lifetime
19+
help: consider using the `'a` lifetime
3920
|
40-
LL | const BAR: &'static () = &();
41-
| +++++++
21+
LL | const BAR: &'a () = &();
22+
| ++
4223

4324
error: aborting due to 2 previous errors
4425

26+
For more information about this error, try `rustc --explain E0106`.

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ struct Foo<'a>(&'a ());
44

55
impl Foo<'_> {
66
const STATIC: &str = "";
7-
//~^ ERROR `&` without an explicit lifetime name cannot be used here
8-
//~| WARN this was previously accepted by the compiler but is being phased out
7+
//~^ ERROR missing lifetime specifier
98
}
109

1110
trait Bar {
@@ -14,9 +13,7 @@ trait Bar {
1413

1514
impl Bar for Foo<'_> {
1615
const STATIC: &str = "";
17-
//~^ ERROR `&` without an explicit lifetime name cannot be used here
18-
//~| WARN this was previously accepted by the compiler but is being phased out
19-
//~| ERROR lifetime parameters or bounds on associated constant `STATIC` do not match the trait declaration
16+
//~^ ERROR missing lifetime specifier
2017
}
2118

2219
fn main() {}
Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,27 @@
1-
error: `&` without an explicit lifetime name cannot be used here
1+
error[E0106]: missing lifetime specifier
22
--> $DIR/elided-lifetime.rs:6:19
33
|
44
LL | const STATIC: &str = "";
5-
| ^
5+
| ^ expected named lifetime parameter
66
|
7-
note: cannot automatically infer `'static` because of other lifetimes in scope
8-
--> $DIR/elided-lifetime.rs:5:10
7+
help: consider introducing a named lifetime parameter
98
|
10-
LL | impl Foo<'_> {
11-
| ^^
12-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
13-
= note: for more information, see issue #115010 <https://github.com/rust-lang/rust/issues/115010>
14-
note: the lint level is defined here
15-
--> $DIR/elided-lifetime.rs:1:9
9+
LL ~ impl<'a> Foo<'_> {
10+
LL ~ const STATIC: &'a str = "";
1611
|
17-
LL | #![deny(elided_lifetimes_in_associated_constant)]
18-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19-
help: use the `'static` lifetime
20-
|
21-
LL | const STATIC: &'static str = "";
22-
| +++++++
2312

24-
error: `&` without an explicit lifetime name cannot be used here
25-
--> $DIR/elided-lifetime.rs:16:19
13+
error[E0106]: missing lifetime specifier
14+
--> $DIR/elided-lifetime.rs:15:19
2615
|
2716
LL | const STATIC: &str = "";
28-
| ^
29-
|
30-
note: cannot automatically infer `'static` because of other lifetimes in scope
31-
--> $DIR/elided-lifetime.rs:15:18
17+
| ^ expected named lifetime parameter
3218
|
33-
LL | impl Bar for Foo<'_> {
34-
| ^^
35-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
36-
= note: for more information, see issue #115010 <https://github.com/rust-lang/rust/issues/115010>
37-
help: use the `'static` lifetime
19+
help: consider introducing a named lifetime parameter
3820
|
39-
LL | const STATIC: &'static str = "";
40-
| +++++++
41-
42-
error[E0195]: lifetime parameters or bounds on associated constant `STATIC` do not match the trait declaration
43-
--> $DIR/elided-lifetime.rs:16:17
21+
LL ~ impl<'a> Bar for Foo<'_> {
22+
LL ~ const STATIC: &'a str = "";
4423
|
45-
LL | const STATIC: &str;
46-
| - lifetimes in impl do not match this associated constant in trait
47-
...
48-
LL | const STATIC: &str = "";
49-
| ^ lifetimes do not match associated constant in trait
5024

51-
error: aborting due to 3 previous errors
25+
error: aborting due to 2 previous errors
5226

53-
For more information about this error, try `rustc --explain E0195`.
27+
For more information about this error, try `rustc --explain E0106`.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ struct A;
55
impl A {
66
const GAC_TYPE<T>: &str = "";
77
const GAC_LIFETIME<'a>: &str = "";
8-
//~^ ERROR `&` without an explicit lifetime name cannot be used here
9-
//~| WARN this was previously accepted by the compiler but is being phased out
8+
//~^ ERROR missing lifetime specifier
109
}
1110

1211
trait Trait {

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

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0106]: missing lifetime specifier
2-
--> $DIR/generic-associated-const.rs:14:29
2+
--> $DIR/generic-associated-const.rs:7:29
33
|
44
LL | const GAC_LIFETIME<'a>: &str = "";
55
| ^ expected named lifetime parameter
@@ -9,28 +9,16 @@ help: consider using the `'a` lifetime
99
LL | const GAC_LIFETIME<'a>: &'a str = "";
1010
| ++
1111

12-
error: `&` without an explicit lifetime name cannot be used here
13-
--> $DIR/generic-associated-const.rs:7:29
14-
|
15-
LL | const GAC_LIFETIME<'a>: &str = "";
16-
| ^
17-
|
18-
note: cannot automatically infer `'static` because of other lifetimes in scope
19-
--> $DIR/generic-associated-const.rs:7:24
12+
error[E0106]: missing lifetime specifier
13+
--> $DIR/generic-associated-const.rs:13:29
2014
|
2115
LL | const GAC_LIFETIME<'a>: &str = "";
22-
| ^^
23-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
24-
= note: for more information, see issue #115010 <https://github.com/rust-lang/rust/issues/115010>
25-
note: the lint level is defined here
26-
--> $DIR/generic-associated-const.rs:1:9
16+
| ^ expected named lifetime parameter
2717
|
28-
LL | #![deny(elided_lifetimes_in_associated_constant)]
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30-
help: use the `'static` lifetime
18+
help: consider using the `'a` lifetime
3119
|
32-
LL | const GAC_LIFETIME<'a>: &'static str = "";
33-
| +++++++
20+
LL | const GAC_LIFETIME<'a>: &'a str = "";
21+
| ++
3422

3523
error: aborting due to 2 previous errors
3624

0 commit comments

Comments
 (0)