diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index c6892419443f8..2fba65f473604 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -43,7 +43,6 @@ declare_lint_pass! { DEPRECATED_WHERE_CLAUSE_LOCATION, DUPLICATE_FEATURES, DUPLICATE_MACRO_ATTRIBUTES, - ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT, ELIDED_LIFETIMES_IN_PATHS, EXPLICIT_BUILTIN_CFGS_IN_FLAGS, EXPORTED_PRIVATE_DEPENDENCIES, @@ -4909,48 +4908,6 @@ declare_lint! { "impl trait in impl method signature does not match trait method signature", } -declare_lint! { - /// The `elided_lifetimes_in_associated_constant` lint detects elided lifetimes - /// in associated constants when there are other lifetimes in scope. This was - /// accidentally supported, and this lint was later relaxed to allow eliding - /// lifetimes to `'static` when there are no lifetimes in scope. - /// - /// ### Example - /// - /// ```rust,compile_fail - /// #![deny(elided_lifetimes_in_associated_constant)] - /// - /// struct Foo<'a>(&'a ()); - /// - /// impl<'a> Foo<'a> { - /// const STR: &str = "hello, world"; - /// } - /// ``` - /// - /// {{produces}} - /// - /// ### Explanation - /// - /// Previous version of Rust - /// - /// Implicit static-in-const behavior was decided [against] for associated - /// constants because of ambiguity. This, however, regressed and the compiler - /// erroneously treats elided lifetimes in associated constants as lifetime - /// parameters on the impl. - /// - /// This is a [future-incompatible] lint to transition this to a - /// hard error in the future. - /// - /// [against]: https://github.com/rust-lang/rust/issues/38831 - /// [future-incompatible]: ../index.md#future-incompatible-lints - pub ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT, - Deny, - "elided lifetimes cannot be used in associated constants in impls", - @future_incompatible = FutureIncompatibleInfo { - reason: fcw!(FutureReleaseError #115010), - }; -} - declare_lint! { /// The `private_macro_use` lint detects private macros that are imported /// with `#[macro_use]`. diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index 342484a8c0d51..379ebd5f3c6e9 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -1661,28 +1661,6 @@ pub(crate) struct UnusedQualifications { pub removal_span: Span, } -#[derive(Diagnostic)] -#[diag( - "{$elided -> - [true] `&` without an explicit lifetime name cannot be used here - *[false] `'_` cannot be used here - }" -)] -pub(crate) struct AssociatedConstElidedLifetime { - #[suggestion( - "use the `'static` lifetime", - style = "verbose", - code = "{code}", - applicability = "machine-applicable" - )] - pub span: Span, - - pub code: &'static str, - pub elided: bool, - #[note("cannot automatically infer `'static` because of other lifetimes in scope")] - pub lifetimes_in_scope: MultiSpan, -} - #[derive(Diagnostic)] #[diag("lifetime parameter `{$ident}` only used once")] pub(crate) struct SingleUseLifetime { diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index e8bd3abdc621e..29c3eb1313435 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -345,7 +345,7 @@ enum LifetimeRibKind { /// Resolves elided lifetimes to `'static` if there are no other lifetimes in scope, /// otherwise give a warning that the previous behavior of introducing a new early-bound /// lifetime is a bug and will be removed (if `emit_lint` is enabled). - StaticIfNoLifetimeInScope { lint_id: NodeId, emit_lint: bool }, + StaticIfNoLifetimeInScope { emit_lint: bool }, /// Signal we cannot find which should be the anonymous lifetime. ElisionFailure, @@ -1876,7 +1876,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { self.record_lifetime_res(lifetime.id, res, elision_candidate); return; } - LifetimeRibKind::StaticIfNoLifetimeInScope { lint_id: node_id, emit_lint } => { + LifetimeRibKind::StaticIfNoLifetimeInScope { emit_lint } => { let mut lifetimes_in_scope = vec![]; for rib in self.lifetime_ribs[..i].iter().rev() { 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> { ); return; } else if emit_lint { - let lt_span = if elided { - lifetime.ident.span.shrink_to_hi() - } else { - lifetime.ident.span - }; - let code = if elided { "'static " } else { "'static" }; - - self.r.lint_buffer.buffer_lint( - lint::builtin::ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT, - node_id, - lifetime.ident.span, - crate::errors::AssociatedConstElidedLifetime { - elided, - code, - span: lt_span, - lifetimes_in_scope: lifetimes_in_scope.into(), - }, - ); + break; } } LifetimeRibKind::AnonymousReportError => { @@ -3351,10 +3334,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { generics.span, |this| { this.with_lifetime_rib( - LifetimeRibKind::StaticIfNoLifetimeInScope { - lint_id: item.id, - emit_lint: false, - }, + LifetimeRibKind::StaticIfNoLifetimeInScope { emit_lint: false }, |this| { this.visit_generics(generics); if rhs_kind.is_type_const() @@ -3571,66 +3551,46 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { generics.span, |this| { this.with_lifetime_rib( - // Until these are a hard error, we need to create them within the - // correct binder, Otherwise the lifetimes of this assoc const think - // they are lifetimes of the trait. - LifetimeRibKind::AnonymousCreateParameter { - binder: item.id, - report_in_path: true, + LifetimeRibKind::StaticIfNoLifetimeInScope { + // In impls, it's not a hard error yet due to backcompat. + emit_lint: true, }, |this| { - this.with_lifetime_rib( - LifetimeRibKind::StaticIfNoLifetimeInScope { - lint_id: item.id, - // In impls, it's not a hard error yet due to backcompat. - emit_lint: true, - }, - |this| { - // If this is a trait impl, ensure the const - // exists in trait - this.check_trait_item( - item.id, - *ident, - &item.kind, - ValueNS, - item.span, - seen_trait_items, - |i, s, c| ConstNotMemberOfTrait(i, s, c), - ); + // If this is a trait impl, ensure the const + // exists in trait + this.check_trait_item( + item.id, + *ident, + &item.kind, + ValueNS, + item.span, + seen_trait_items, + |i, s, c| ConstNotMemberOfTrait(i, s, c), + ); - this.visit_generics(generics); - if rhs_kind.is_type_const() - && !this - .r - .tcx - .features() - .generic_const_parameter_types() - { - this.with_rib(TypeNS, RibKind::ConstParamTy, |this| { - this.with_rib( - ValueNS, - RibKind::ConstParamTy, - |this| { - this.with_lifetime_rib( - LifetimeRibKind::ConstParamTy, - |this| this.visit_ty(ty), - ) - }, - ) - }); - } else { - this.visit_ty(ty); - } - // We allow arbitrary const expressions inside of associated consts, - // even if they are potentially not const evaluatable. - // - // Type parameters can already be used and as associated consts are - // not used as part of the type system, this is far less surprising. - this.resolve_const_item_rhs(rhs_kind, None); - }, - ) + this.visit_generics(generics); + if rhs_kind.is_type_const() + && !this.r.tcx.features().generic_const_parameter_types() + { + this.with_rib(TypeNS, RibKind::ConstParamTy, |this| { + this.with_rib(ValueNS, RibKind::ConstParamTy, |this| { + this.with_lifetime_rib( + LifetimeRibKind::ConstParamTy, + |this| this.visit_ty(ty), + ) + }) + }); + } else { + this.visit_ty(ty); + } + // We allow arbitrary const expressions inside of associated consts, + // even if they are potentially not const evaluatable. + // + // Type parameters can already be used and as associated consts are + // not used as part of the type system, this is far less surprising. + this.resolve_const_item_rhs(rhs_kind, None); }, - ); + ) }, ); self.resolve_define_opaques(define_opaque); diff --git a/src/tools/rust-analyzer/crates/ide-db/src/generated/lints.rs b/src/tools/rust-analyzer/crates/ide-db/src/generated/lints.rs index 264bb4fa814dc..a486024332f8f 100644 --- a/src/tools/rust-analyzer/crates/ide-db/src/generated/lints.rs +++ b/src/tools/rust-analyzer/crates/ide-db/src/generated/lints.rs @@ -383,13 +383,6 @@ pub const DEFAULT_LINTS: &[Lint] = &[ warn_since: None, deny_since: None, }, - Lint { - label: "elided_lifetimes_in_associated_constant", - description: r##"elided lifetimes cannot be used in associated constants in impls"##, - default_severity: Severity::Error, - warn_since: None, - deny_since: None, - }, Lint { label: "elided_lifetimes_in_paths", description: r##"hidden lifetime parameters in types are deprecated"##, @@ -1851,7 +1844,6 @@ pub const DEFAULT_LINT_GROUPS: &[LintGroup] = &[ "coherence_leak_check", "conflicting_repr_hints", "const_evaluatable_unchecked", - "elided_lifetimes_in_associated_constant", "float_literal_f32_fallback", "forbidden_lint_groups", "ill_formed_attribute_input", diff --git a/tests/ui/consts/assoc-const-elided-lifetime.rs b/tests/ui/consts/assoc-const-elided-lifetime.rs index 10cd33a8fed59..9cec63f48e008 100644 --- a/tests/ui/consts/assoc-const-elided-lifetime.rs +++ b/tests/ui/consts/assoc-const-elided-lifetime.rs @@ -1,5 +1,3 @@ -#![deny(elided_lifetimes_in_associated_constant)] - use std::marker::PhantomData; struct Foo<'a> { @@ -8,12 +6,10 @@ struct Foo<'a> { impl<'a> Foo<'a> { const FOO: Foo<'_> = Foo { x: PhantomData::<&()> }; - //~^ ERROR `'_` cannot be used here - //~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + //~^ ERROR missing lifetime specifier const BAR: &() = &(); - //~^ ERROR `&` without an explicit lifetime name cannot be used here - //~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + //~^ ERROR missing lifetime specifier } fn main() {} diff --git a/tests/ui/consts/assoc-const-elided-lifetime.stderr b/tests/ui/consts/assoc-const-elided-lifetime.stderr index 6277b079bdac7..a000aec0bdb05 100644 --- a/tests/ui/consts/assoc-const-elided-lifetime.stderr +++ b/tests/ui/consts/assoc-const-elided-lifetime.stderr @@ -1,44 +1,26 @@ -error: `'_` cannot be used here - --> $DIR/assoc-const-elided-lifetime.rs:10:20 +error[E0106]: missing lifetime specifier + --> $DIR/assoc-const-elided-lifetime.rs:8:20 | LL | const FOO: Foo<'_> = Foo { x: PhantomData::<&()> }; - | ^^ + | ^^ expected named lifetime parameter | -note: cannot automatically infer `'static` because of other lifetimes in scope - --> $DIR/assoc-const-elided-lifetime.rs:9:6 - | -LL | impl<'a> Foo<'a> { - | ^^ - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #115010 -note: the lint level is defined here - --> $DIR/assoc-const-elided-lifetime.rs:1:9 - | -LL | #![deny(elided_lifetimes_in_associated_constant)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -help: use the `'static` lifetime +help: consider using the `'a` lifetime | LL - const FOO: Foo<'_> = Foo { x: PhantomData::<&()> }; -LL + const FOO: Foo<'static> = Foo { x: PhantomData::<&()> }; +LL + const FOO: Foo<'a> = Foo { x: PhantomData::<&()> }; | -error: `&` without an explicit lifetime name cannot be used here - --> $DIR/assoc-const-elided-lifetime.rs:14:16 +error[E0106]: missing lifetime specifier + --> $DIR/assoc-const-elided-lifetime.rs:11:16 | LL | const BAR: &() = &(); - | ^ - | -note: cannot automatically infer `'static` because of other lifetimes in scope - --> $DIR/assoc-const-elided-lifetime.rs:9:6 + | ^ expected named lifetime parameter | -LL | impl<'a> Foo<'a> { - | ^^ - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #115010 -help: use the `'static` lifetime +help: consider using the `'a` lifetime | -LL | const BAR: &'static () = &(); - | +++++++ +LL | const BAR: &'a () = &(); + | ++ error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0106`. diff --git a/tests/ui/consts/static-default-lifetime/elided-lifetime.rs b/tests/ui/consts/static-default-lifetime/elided-lifetime.rs index 989de389180a1..3cffe08e05916 100644 --- a/tests/ui/consts/static-default-lifetime/elided-lifetime.rs +++ b/tests/ui/consts/static-default-lifetime/elided-lifetime.rs @@ -1,11 +1,8 @@ -#![deny(elided_lifetimes_in_associated_constant)] - struct Foo<'a>(&'a ()); impl Foo<'_> { const STATIC: &str = ""; - //~^ ERROR `&` without an explicit lifetime name cannot be used here - //~| WARN this was previously accepted by the compiler but is being phased out + //~^ ERROR missing lifetime specifier } trait Bar { @@ -14,9 +11,7 @@ trait Bar { impl Bar for Foo<'_> { const STATIC: &str = ""; - //~^ ERROR `&` without an explicit lifetime name cannot be used here - //~| WARN this was previously accepted by the compiler but is being phased out - //~| ERROR lifetime parameters or bounds on associated constant `STATIC` do not match the trait declaration + //~^ ERROR missing lifetime specifier } fn main() {} diff --git a/tests/ui/consts/static-default-lifetime/elided-lifetime.stderr b/tests/ui/consts/static-default-lifetime/elided-lifetime.stderr index 370e6655d8607..b8bd760b11037 100644 --- a/tests/ui/consts/static-default-lifetime/elided-lifetime.stderr +++ b/tests/ui/consts/static-default-lifetime/elided-lifetime.stderr @@ -1,53 +1,27 @@ -error: `&` without an explicit lifetime name cannot be used here - --> $DIR/elided-lifetime.rs:6:19 +error[E0106]: missing lifetime specifier + --> $DIR/elided-lifetime.rs:4:19 | LL | const STATIC: &str = ""; - | ^ + | ^ expected named lifetime parameter | -note: cannot automatically infer `'static` because of other lifetimes in scope - --> $DIR/elided-lifetime.rs:5:10 +help: consider introducing a named lifetime parameter | -LL | impl Foo<'_> { - | ^^ - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #115010 -note: the lint level is defined here - --> $DIR/elided-lifetime.rs:1:9 +LL ~ impl<'a> Foo<'_> { +LL ~ const STATIC: &'a str = ""; | -LL | #![deny(elided_lifetimes_in_associated_constant)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -help: use the `'static` lifetime - | -LL | const STATIC: &'static str = ""; - | +++++++ -error: `&` without an explicit lifetime name cannot be used here - --> $DIR/elided-lifetime.rs:16:19 +error[E0106]: missing lifetime specifier + --> $DIR/elided-lifetime.rs:13:19 | LL | const STATIC: &str = ""; - | ^ - | -note: cannot automatically infer `'static` because of other lifetimes in scope - --> $DIR/elided-lifetime.rs:15:18 + | ^ expected named lifetime parameter | -LL | impl Bar for Foo<'_> { - | ^^ - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #115010 -help: use the `'static` lifetime +help: consider introducing a named lifetime parameter | -LL | const STATIC: &'static str = ""; - | +++++++ - -error[E0195]: lifetime parameters or bounds on associated constant `STATIC` do not match the trait declaration - --> $DIR/elided-lifetime.rs:16:17 +LL ~ impl<'a> Bar for Foo<'_> { +LL ~ const STATIC: &'a str = ""; | -LL | const STATIC: &str; - | - lifetimes in impl do not match this associated constant in trait -... -LL | const STATIC: &str = ""; - | ^ lifetimes do not match associated constant in trait -error: aborting due to 3 previous errors +error: aborting due to 2 previous errors -For more information about this error, try `rustc --explain E0195`. +For more information about this error, try `rustc --explain E0106`. diff --git a/tests/ui/consts/static-default-lifetime/generic-associated-const.rs b/tests/ui/consts/static-default-lifetime/generic-associated-const.rs index 8fabaa43f5a27..3cf0270b84976 100644 --- a/tests/ui/consts/static-default-lifetime/generic-associated-const.rs +++ b/tests/ui/consts/static-default-lifetime/generic-associated-const.rs @@ -1,12 +1,10 @@ -#![deny(elided_lifetimes_in_associated_constant)] #![feature(generic_const_items)] struct A; impl A { const GAC_TYPE: &str = ""; const GAC_LIFETIME<'a>: &str = ""; - //~^ ERROR `&` without an explicit lifetime name cannot be used here - //~| WARN this was previously accepted by the compiler but is being phased out + //~^ ERROR missing lifetime specifier } trait Trait { diff --git a/tests/ui/consts/static-default-lifetime/generic-associated-const.stderr b/tests/ui/consts/static-default-lifetime/generic-associated-const.stderr index fe858d685f7fa..48fc8eaaabe20 100644 --- a/tests/ui/consts/static-default-lifetime/generic-associated-const.stderr +++ b/tests/ui/consts/static-default-lifetime/generic-associated-const.stderr @@ -1,5 +1,5 @@ error[E0106]: missing lifetime specifier - --> $DIR/generic-associated-const.rs:14:29 + --> $DIR/generic-associated-const.rs:6:29 | LL | const GAC_LIFETIME<'a>: &str = ""; | ^ expected named lifetime parameter @@ -9,28 +9,16 @@ help: consider using the `'a` lifetime LL | const GAC_LIFETIME<'a>: &'a str = ""; | ++ -error: `&` without an explicit lifetime name cannot be used here - --> $DIR/generic-associated-const.rs:7:29 - | -LL | const GAC_LIFETIME<'a>: &str = ""; - | ^ - | -note: cannot automatically infer `'static` because of other lifetimes in scope - --> $DIR/generic-associated-const.rs:7:24 +error[E0106]: missing lifetime specifier + --> $DIR/generic-associated-const.rs:12:29 | LL | const GAC_LIFETIME<'a>: &str = ""; - | ^^ - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #115010 -note: the lint level is defined here - --> $DIR/generic-associated-const.rs:1:9 + | ^ expected named lifetime parameter | -LL | #![deny(elided_lifetimes_in_associated_constant)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -help: use the `'static` lifetime +help: consider using the `'a` lifetime | -LL | const GAC_LIFETIME<'a>: &'static str = ""; - | +++++++ +LL | const GAC_LIFETIME<'a>: &'a str = ""; + | ++ error: aborting due to 2 previous errors diff --git a/tests/ui/consts/static-default-lifetime/static-trait-impl.rs b/tests/ui/consts/static-default-lifetime/static-trait-impl.rs index ecc163aecbf1a..a0088b707656a 100644 --- a/tests/ui/consts/static-default-lifetime/static-trait-impl.rs +++ b/tests/ui/consts/static-default-lifetime/static-trait-impl.rs @@ -1,5 +1,3 @@ -#![deny(elided_lifetimes_in_associated_constant)] - trait Bar<'a> { const STATIC: &'a str; } @@ -7,9 +5,7 @@ trait Bar<'a> { struct A; impl Bar<'_> for A { const STATIC: &str = ""; - //~^ ERROR `&` without an explicit lifetime name cannot be used here - //~| WARN this was previously accepted by the compiler but is being phased out - //~| ERROR lifetime parameters or bounds on associated constant `STATIC` do not match the trait declaration + //~^ ERROR missing lifetime specifier } struct B; diff --git a/tests/ui/consts/static-default-lifetime/static-trait-impl.stderr b/tests/ui/consts/static-default-lifetime/static-trait-impl.stderr index ab82515162014..cfd8eec9aebe5 100644 --- a/tests/ui/consts/static-default-lifetime/static-trait-impl.stderr +++ b/tests/ui/consts/static-default-lifetime/static-trait-impl.stderr @@ -1,35 +1,15 @@ -error: `&` without an explicit lifetime name cannot be used here - --> $DIR/static-trait-impl.rs:9:19 +error[E0106]: missing lifetime specifier + --> $DIR/static-trait-impl.rs:7:19 | LL | const STATIC: &str = ""; - | ^ + | ^ expected named lifetime parameter | -note: cannot automatically infer `'static` because of other lifetimes in scope - --> $DIR/static-trait-impl.rs:8:10 +help: consider introducing a named lifetime parameter | -LL | impl Bar<'_> for A { - | ^^ - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #115010 -note: the lint level is defined here - --> $DIR/static-trait-impl.rs:1:9 +LL ~ impl<'a> Bar<'_> for A { +LL ~ const STATIC: &'a str = ""; | -LL | #![deny(elided_lifetimes_in_associated_constant)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -help: use the `'static` lifetime - | -LL | const STATIC: &'static str = ""; - | +++++++ - -error[E0195]: lifetime parameters or bounds on associated constant `STATIC` do not match the trait declaration - --> $DIR/static-trait-impl.rs:9:17 - | -LL | const STATIC: &'a str; - | - lifetimes in impl do not match this associated constant in trait -... -LL | const STATIC: &str = ""; - | ^ lifetimes do not match associated constant in trait -error: aborting due to 2 previous errors +error: aborting due to 1 previous error -For more information about this error, try `rustc --explain E0195`. +For more information about this error, try `rustc --explain E0106`.