Skip to content
/ rust Public
forked from rust-lang/rust

Commit 61c40bb

Browse files
authored
Rollup merge of rust-lang#156508 - oli-obk:boom, r=petrochenkov
Infer all anonymous lifetimes in assoc consts as `'static` fixes rust-lang#115010 This FCW has been on for almost a year, let's actually do the change that T-lang asked for now that there's no breakage to be expected anymore. I first did a crater run checking if turning it into a hard error breaks anything. There are two cases that would be a hard error (but totally fine by defaulting to `'static`), both based on an old version of `minio`. One is a personal crate on github with no changes in the last year (https://github.com/PapePathe/daaray_kaamil) and the other is https://github.com/gear-tech/gear, which is developed on github actively and does not have this issue anymore (also old `minio` dependency that was since updated). The crates.io version is 2 years outdated. Thus we can pretty much conclude we're not going to cause anyone any confusion or subtle bugs by changing the rules. The rules for associated constants' types are now the same as the rules for free constants' types: We now treat *all* anonymous lifetimes as `'static`, irrespective of whether other lifetimes are in scope or not. This means the following piece of code starts compiling without warnings or errors again. ```rust struct Foo<'a>(&'a ()); impl<'a> Foo<'a> { const STR: &str = "hello, world"; } ``` We keep the hard error on ```rust trait Foo { const B: S = S { s: &() }; } struct S<'a> { s: &'a (), } ``` in contrast to free constant, which are orthogonal to this change and cause more breakage
2 parents 019294a + c3257a5 commit 61c40bb

12 files changed

Lines changed: 89 additions & 392 deletions

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ pub mod hardwired {
4444
DEPRECATED_WHERE_CLAUSE_LOCATION,
4545
DUPLICATE_FEATURES,
4646
DUPLICATE_MACRO_ATTRIBUTES,
47-
ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
4847
ELIDED_LIFETIMES_IN_PATHS,
4948
EXPLICIT_BUILTIN_CFGS_IN_FLAGS,
5049
EXPORTED_PRIVATE_DEPENDENCIES,
@@ -4762,48 +4761,6 @@ declare_lint! {
47624761
"impl trait in impl method signature does not match trait method signature",
47634762
}
47644763

4765-
declare_lint! {
4766-
/// The `elided_lifetimes_in_associated_constant` lint detects elided lifetimes
4767-
/// in associated constants when there are other lifetimes in scope. This was
4768-
/// accidentally supported, and this lint was later relaxed to allow eliding
4769-
/// lifetimes to `'static` when there are no lifetimes in scope.
4770-
///
4771-
/// ### Example
4772-
///
4773-
/// ```rust,compile_fail
4774-
/// #![deny(elided_lifetimes_in_associated_constant)]
4775-
///
4776-
/// struct Foo<'a>(&'a ());
4777-
///
4778-
/// impl<'a> Foo<'a> {
4779-
/// const STR: &str = "hello, world";
4780-
/// }
4781-
/// ```
4782-
///
4783-
/// {{produces}}
4784-
///
4785-
/// ### Explanation
4786-
///
4787-
/// Previous version of Rust
4788-
///
4789-
/// Implicit static-in-const behavior was decided [against] for associated
4790-
/// constants because of ambiguity. This, however, regressed and the compiler
4791-
/// erroneously treats elided lifetimes in associated constants as lifetime
4792-
/// parameters on the impl.
4793-
///
4794-
/// This is a [future-incompatible] lint to transition this to a
4795-
/// hard error in the future.
4796-
///
4797-
/// [against]: https://github.com/rust-lang/rust/issues/38831
4798-
/// [future-incompatible]: ../index.md#future-incompatible-lints
4799-
pub ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
4800-
Deny,
4801-
"elided lifetimes cannot be used in associated constants in impls",
4802-
@future_incompatible = FutureIncompatibleInfo {
4803-
reason: fcw!(FutureReleaseError #115010),
4804-
};
4805-
}
4806-
48074764
declare_lint! {
48084765
/// The `private_macro_use` lint detects private macros that are imported
48094766
/// with `#[macro_use]`.

compiler/rustc_resolve/src/diagnostics.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: 79 additions & 137 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(elided_lifetimes_in_associated_constant)]
1+
//@ check-pass
22

33
use std::marker::PhantomData;
44

@@ -8,12 +8,8 @@ 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!
1311

1412
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!
1713
}
1814

1915
fn main() {}

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

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
#![deny(elided_lifetimes_in_associated_constant)]
1+
//@check-pass
22

33
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
97
}
108

119
trait Bar {
@@ -14,9 +12,6 @@ trait Bar {
1412

1513
impl Bar for Foo<'_> {
1614
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
2015
}
2116

2217
fn main() {}

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

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
#![deny(elided_lifetimes_in_associated_constant)]
1+
//@ check-pass
2+
23
#![feature(generic_const_items)]
34

45
struct A;
56
impl A {
67
const GAC_TYPE<T>: &str = "";
78
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
109
}
1110

1211
trait Trait {
1312
const GAC_TYPE<T>: &str = "";
1413
const GAC_LIFETIME<'a>: &str = "";
15-
//~^ ERROR missing lifetime specifier
1614
}
1715

1816
fn main() {}

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/ui/consts/static-default-lifetime/static-trait-impl.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(elided_lifetimes_in_associated_constant)]
1+
//@ check-pass
22

33
trait Bar<'a> {
44
const STATIC: &'a str;
@@ -7,9 +7,6 @@ trait Bar<'a> {
77
struct A;
88
impl Bar<'_> for A {
99
const STATIC: &str = "";
10-
//~^ ERROR `&` without an explicit lifetime name cannot be used here
11-
//~| WARN this was previously accepted by the compiler but is being phased out
12-
//~| ERROR lifetime parameters or bounds on associated constant `STATIC` do not match the trait declaration
1310
}
1411

1512
struct B;
@@ -19,12 +16,10 @@ impl Bar<'static> for B {
1916

2017
struct C;
2118
impl Bar<'_> for C {
22-
// make ^^ not cause
2319
const STATIC: &'static str = {
2420
struct B;
2521
impl Bar<'static> for B {
2622
const STATIC: &str = "";
27-
// ^ to emit a future incompat warning
2823
}
2924
""
3025
};

0 commit comments

Comments
 (0)