-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn on elided lifetimes in associated constants (ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT
)
#115011
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -311,6 +311,10 @@ enum LifetimeRibKind { | |
/// error on default object bounds (e.g., `Box<dyn Foo>`). | ||
AnonymousReportError, | ||
|
||
/// Resolves elided lifetimes to `'static`, but gives a warning that this behavior | ||
/// is a bug and will be reverted soon. | ||
AnonymousWarnToStatic(NodeId), | ||
|
||
/// Signal we cannot find which should be the anonymous lifetime. | ||
ElisionFailure, | ||
|
||
|
@@ -1148,6 +1152,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast, | |
} | ||
LifetimeRibKind::AnonymousCreateParameter { .. } | ||
| LifetimeRibKind::AnonymousReportError | ||
| LifetimeRibKind::AnonymousWarnToStatic(_) | ||
| LifetimeRibKind::Elided(_) | ||
| LifetimeRibKind::ElisionFailure | ||
| LifetimeRibKind::ConcreteAnonConst(_) | ||
|
@@ -1515,6 +1520,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { | |
// lifetime would be illegal. | ||
LifetimeRibKind::Item | ||
| LifetimeRibKind::AnonymousReportError | ||
| LifetimeRibKind::AnonymousWarnToStatic(_) | ||
| LifetimeRibKind::ElisionFailure => Some(LifetimeUseSet::Many), | ||
// An anonymous lifetime is legal here, and bound to the right | ||
// place, go ahead. | ||
|
@@ -1576,7 +1582,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { | |
| LifetimeRibKind::Elided(_) | ||
| LifetimeRibKind::Generics { .. } | ||
| LifetimeRibKind::ElisionFailure | ||
| LifetimeRibKind::AnonymousReportError => {} | ||
| LifetimeRibKind::AnonymousReportError | ||
| LifetimeRibKind::AnonymousWarnToStatic(_) => {} | ||
} | ||
} | ||
|
||
|
@@ -1616,6 +1623,25 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { | |
self.record_lifetime_res(lifetime.id, res, elision_candidate); | ||
return; | ||
} | ||
LifetimeRibKind::AnonymousWarnToStatic(node_id) => { | ||
self.record_lifetime_res(lifetime.id, LifetimeRes::Static, elision_candidate); | ||
let msg = if elided { | ||
"`&` without an explicit lifetime name cannot be used here" | ||
} else { | ||
"`'_` cannot be used here" | ||
}; | ||
self.r.lint_buffer.buffer_lint_with_diagnostic( | ||
lint::builtin::ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT, | ||
node_id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Elided lifetimes are not present in the AST, so buffered lints on that node ID ( |
||
lifetime.ident.span, | ||
msg, | ||
lint::BuiltinLintDiagnostics::AssociatedConstElidedLifetime { | ||
elided, | ||
span: lifetime.ident.span, | ||
}, | ||
); | ||
return; | ||
} | ||
LifetimeRibKind::AnonymousReportError => { | ||
let (msg, note) = if elided { | ||
( | ||
|
@@ -1811,7 +1837,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, '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::AnonymousCreateParameter { report_in_path: true, .. } | ||
| LifetimeRibKind::AnonymousWarnToStatic(_) => { | ||
let sess = self.r.tcx.sess; | ||
let mut err = rustc_errors::struct_span_err!( | ||
sess, | ||
|
@@ -2898,7 +2925,6 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { | |
match &item.kind { | ||
AssocItemKind::Const(box ast::ConstItem { generics, ty, expr, .. }) => { | ||
debug!("resolve_implementation AssocItemKind::Const"); | ||
|
||
self.with_generic_param_rib( | ||
&generics.params, | ||
RibKind::AssocItem, | ||
|
@@ -2908,28 +2934,33 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { | |
kind: LifetimeBinderKind::ConstItem, | ||
}, | ||
|this| { | ||
// If this is a trait impl, ensure the const | ||
// exists in trait | ||
this.check_trait_item( | ||
item.id, | ||
item.ident, | ||
&item.kind, | ||
ValueNS, | ||
item.span, | ||
seen_trait_items, | ||
|i, s, c| ConstNotMemberOfTrait(i, s, c), | ||
); | ||
this.with_lifetime_rib( | ||
LifetimeRibKind::AnonymousWarnToStatic(item.id), | ||
|this| { | ||
// If this is a trait impl, ensure the const | ||
// exists in trait | ||
this.check_trait_item( | ||
item.id, | ||
item.ident, | ||
&item.kind, | ||
ValueNS, | ||
item.span, | ||
seen_trait_items, | ||
|i, s, c| ConstNotMemberOfTrait(i, s, c), | ||
); | ||
|
||
this.visit_generics(generics); | ||
this.visit_ty(ty); | ||
if let Some(expr) = expr { | ||
// 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_body(expr, None); | ||
} | ||
this.visit_generics(generics); | ||
this.visit_ty(ty); | ||
if let Some(expr) = expr { | ||
// 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_body(expr, None); | ||
} | ||
}, | ||
); | ||
}, | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
warning: `&` without an explicit lifetime name cannot be used here | ||
--> $DIR/infer-placeholder-in-non-suggestable-pos.rs:6:18 | ||
| | ||
LL | const ASSOC: &dyn Fn(_) = 1i32; | ||
| ^ | ||
| | ||
= 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: `#[warn(elided_lifetimes_in_associated_constant)]` on by default | ||
help: use the `'static` lifetime | ||
| | ||
LL | const ASSOC: &'static dyn Fn(_) = 1i32; | ||
| +++++++ | ||
|
||
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants | ||
--> $DIR/infer-placeholder-in-non-suggestable-pos.rs:6:26 | ||
| | ||
LL | const ASSOC: &dyn Fn(_) = 1i32; | ||
| ^ not allowed in type signatures | ||
|
||
error: aborting due to previous error | ||
error: aborting due to previous error; 1 warning emitted | ||
|
||
For more information about this error, try `rustc --explain E0121`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#![deny(elided_lifetimes_in_associated_constant)] | ||
|
||
use std::marker::PhantomData; | ||
|
||
struct Foo<'a> { | ||
x: PhantomData<&'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() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
error: `'_` cannot be used here | ||
--> $DIR/assoc-const-elided-lifetime.rs:10:20 | ||
| | ||
LL | const FOO: Foo<'_> = Foo { x: PhantomData::<&()> }; | ||
| ^^ | ||
| | ||
= 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 | ||
| | ||
LL | const FOO: Foo<'static> = Foo { x: PhantomData::<&()> }; | ||
| ~~~~~~~ | ||
|
||
error: `&` without an explicit lifetime name cannot be used here | ||
--> $DIR/assoc-const-elided-lifetime.rs:14:16 | ||
| | ||
LL | const BAR: &() = &(); | ||
| ^ | ||
| | ||
= 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 | ||
| | ||
LL | const BAR: &'static () = &(); | ||
| +++++++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW this FutureIncompatibilityReason will make the warning only show up in locally built crates, but not in cargo's future breakage report that also shows warnings from dependencies. Just making sure that this is a deliberate choice and not an accident. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not deliberate, but isn't it conventional to bump it later? Possibly at the same time that this lint gets bumped to Deny? Not sure if there's a definite process for how to gradually raise FCW severity 🫠
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we have a standard process. :/