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 @@ -4809,48 +4808,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
153 changes: 38 additions & 115 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,6 @@ enum LifetimeRibKind {
/// error on default object bounds (e.g., `Box<dyn Foo>`).
AnonymousReportError,

/// 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 },

/// Signal we cannot find which should be the anonymous lifetime.
ElisionFailure,

Expand Down Expand Up @@ -1373,7 +1368,6 @@ impl<'ast, 'ra, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'ra, 'tc
}
LifetimeRibKind::AnonymousCreateParameter { .. }
| LifetimeRibKind::AnonymousReportError
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. }
| LifetimeRibKind::ImplTrait
| LifetimeRibKind::Elided(_)
| LifetimeRibKind::ElisionFailure
Expand Down Expand Up @@ -1779,7 +1773,6 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
// lifetime would be illegal.
LifetimeRibKind::Item
| LifetimeRibKind::AnonymousReportError
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. }
| LifetimeRibKind::ElisionFailure => Some(LifetimeUseSet::Many),
// An anonymous lifetime is legal here, and bound to the right
// place, go ahead.
Expand Down Expand Up @@ -1843,8 +1836,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
| LifetimeRibKind::Generics { .. }
| LifetimeRibKind::ElisionFailure
| LifetimeRibKind::AnonymousReportError
| LifetimeRibKind::ImplTrait
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. } => {}
| LifetimeRibKind::ImplTrait => {}
}
}

Expand Down Expand Up @@ -1883,48 +1875,6 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
self.record_lifetime_use(lifetime.id, res, elision_candidate);
return;
}
LifetimeRibKind::StaticIfNoLifetimeInScope { lint_id: node_id, 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));
// Consider any anonymous lifetimes, too
if let LifetimeRibKind::AnonymousCreateParameter { binder, .. } = rib.kind
&& let Some(extra) = self.r.extra_lifetime_params_map.get(&binder)
{
lifetimes_in_scope.extend(extra.iter().map(|(ident, _, _)| ident.span));
}
if let LifetimeRibKind::Item = rib.kind {
break;
}
}
if lifetimes_in_scope.is_empty() {
self.record_lifetime_use(
lifetime.id,
LifetimeRes::Static,
elision_candidate,
);
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(),
},
);
}
}
LifetimeRibKind::AnonymousReportError => {
let guar = if elided {
let suggestion = self.lifetime_ribs[i..].iter().rev().find_map(|rib| {
Expand Down Expand Up @@ -2274,8 +2224,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
//
// impl Foo for std::cell::Ref<u32> // note lack of '_
// async fn foo(_: std::cell::Ref<u32>) { ... }
LifetimeRibKind::AnonymousCreateParameter { report_in_path: true, .. }
| LifetimeRibKind::StaticIfNoLifetimeInScope { .. } => {
LifetimeRibKind::AnonymousCreateParameter { report_in_path: true, .. } => {
let sess = self.r.tcx.sess;
let subdiag = elided_lifetime_in_path_suggestion(
sess.source_map(),
Expand Down Expand Up @@ -3348,10 +3297,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::Elided(LifetimeRes::Static),
|this| {
this.visit_generics(generics);
if rhs_kind.is_type_const()
Expand Down Expand Up @@ -3567,67 +3513,44 @@ 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::Elided(LifetimeRes::Static),
|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,
*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,
*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
6 changes: 1 addition & 5 deletions tests/ui/consts/assoc-const-elided-lifetime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(elided_lifetimes_in_associated_constant)]
//@ check-pass

use std::marker::PhantomData;

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

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!
}

fn main() {}
44 changes: 0 additions & 44 deletions tests/ui/consts/assoc-const-elided-lifetime.stderr

This file was deleted.

Loading
Loading