Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]`.
Expand Down
22 changes: 0 additions & 22 deletions compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
120 changes: 40 additions & 80 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand All @@ -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 => {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 0 additions & 8 deletions src/tools/rust-analyzer/crates/ide-db/src/generated/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"##,
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 2 additions & 6 deletions tests/ui/consts/assoc-const-elided-lifetime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![deny(elided_lifetimes_in_associated_constant)]

use std::marker::PhantomData;

struct Foo<'a> {
Expand All @@ -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() {}
42 changes: 12 additions & 30 deletions tests/ui/consts/assoc-const-elided-lifetime.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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`.
9 changes: 2 additions & 7 deletions tests/ui/consts/static-default-lifetime/elided-lifetime.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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() {}
Loading
Loading