Skip to content

Commit

Permalink
Resolve elided lifetimes in assoc const to static if no other lifetim…
Browse files Browse the repository at this point in the history
…es are in scope
  • Loading branch information
compiler-errors committed May 18, 2024
1 parent c00957a commit 130a9d0
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 115 deletions.
6 changes: 5 additions & 1 deletion compiler/rustc_lint/src/context/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,17 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di
Applicability::MachineApplicable,
);
}
BuiltinLintDiag::AssociatedConstElidedLifetime { elided, span } => {
BuiltinLintDiag::AssociatedConstElidedLifetime { elided, span, lifetimes_in_scope } => {
diag.span_suggestion_verbose(
if elided { span.shrink_to_hi() } else { span },
"use the `'static` lifetime",
if elided { "'static " } else { "'static" },
Applicability::MachineApplicable,
);
diag.span_note(
lifetimes_in_scope,
"cannot automatically infer `'static` because of other lifetimes in scope",
);
}
BuiltinLintDiag::RedundantImportVisibility { max_vis, span } => {
diag.span_note(span, format!("the most public imported item is `{max_vis}`"));
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ pub enum BuiltinLintDiag {
AssociatedConstElidedLifetime {
elided: bool,
span: Span,
lifetimes_in_scope: MultiSpan,
},
RedundantImportVisibility {
span: Span,
Expand Down
114 changes: 69 additions & 45 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,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.
AnonymousWarn(NodeId),
/// 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.
StaticIfNoLifetimeInScope(NodeId),

/// Signal we cannot find which should be the anonymous lifetime.
ElisionFailure,
Expand Down Expand Up @@ -1209,7 +1210,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
}
LifetimeRibKind::AnonymousCreateParameter { .. }
| LifetimeRibKind::AnonymousReportError
| LifetimeRibKind::AnonymousWarn(_)
| LifetimeRibKind::StaticIfNoLifetimeInScope(_)
| LifetimeRibKind::Elided(_)
| LifetimeRibKind::ElisionFailure
| LifetimeRibKind::ConcreteAnonConst(_)
Expand Down Expand Up @@ -1577,7 +1578,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
// lifetime would be illegal.
LifetimeRibKind::Item
| LifetimeRibKind::AnonymousReportError
| LifetimeRibKind::AnonymousWarn(_)
| 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 @@ -1640,7 +1641,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
| LifetimeRibKind::Generics { .. }
| LifetimeRibKind::ElisionFailure
| LifetimeRibKind::AnonymousReportError
| LifetimeRibKind::AnonymousWarn(_) => {}
| LifetimeRibKind::StaticIfNoLifetimeInScope(_) => {}
}
}

Expand Down Expand Up @@ -1674,22 +1675,42 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
self.record_lifetime_res(lifetime.id, res, elision_candidate);
return;
}
LifetimeRibKind::AnonymousWarn(node_id) => {
let msg = if elided {
"`&` without an explicit lifetime name cannot be used here"
LifetimeRibKind::StaticIfNoLifetimeInScope(node_id) => {
let mut lifetimes_in_scope = vec![];
for rib in &self.lifetime_ribs[..i] {
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 lifetimes_in_scope.is_empty() {
self.record_lifetime_res(
lifetime.id,
LifetimeRes::Static,
elision_candidate,
);
return;
} else {
"`'_` cannot be used here"
};
self.r.lint_buffer.buffer_lint_with_diagnostic(
lint::builtin::ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
node_id,
lifetime.ident.span,
msg,
lint::BuiltinLintDiag::AssociatedConstElidedLifetime {
elided,
span: lifetime.ident.span,
},
);
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,
lifetime.ident.span,
msg,
lint::BuiltinLintDiag::AssociatedConstElidedLifetime {
elided,
span: lifetime.ident.span,
lifetimes_in_scope: lifetimes_in_scope.into(),
},
);
}
}
LifetimeRibKind::AnonymousReportError => {
if elided {
Expand Down Expand Up @@ -1882,7 +1903,7 @@ 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::AnonymousWarn(_) => {
| LifetimeRibKind::StaticIfNoLifetimeInScope(_) => {
let sess = self.r.tcx.sess;
let subdiag = rustc_errors::elided_lifetime_in_path_suggestion(
sess.source_map(),
Expand Down Expand Up @@ -3009,30 +3030,33 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
kind: LifetimeBinderKind::ConstItem,
},
|this| {
this.with_lifetime_rib(LifetimeRibKind::AnonymousWarn(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.with_lifetime_rib(
LifetimeRibKind::StaticIfNoLifetimeInScope(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);
}
},
);
},
);
}
Expand Down
8 changes: 3 additions & 5 deletions tests/ui/associated-consts/double-elided.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//@ check-pass

struct S;

impl S {
const C: &&str = &"";
//~^ WARN `&` 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!
//~| WARN `&` 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 in type `&&str`, reference has a longer lifetime than the data it references
// Now resolves to `&'static &'static str`.
}

fn main() {}
47 changes: 0 additions & 47 deletions tests/ui/associated-consts/double-elided.stderr

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ trait Trait {
impl Trait for () {
const ASSOC: &dyn Fn(_) = 1i32;
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
//~| WARN `&` 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
@@ -1,23 +1,9 @@
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 1 previous error; 1 warning emitted
error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0121`.
12 changes: 12 additions & 0 deletions tests/ui/consts/assoc-const-elided-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ 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: cannot automatically infer `'static` because of other lifetimes in scope
--> $DIR/assoc-const-elided-lifetime.rs:9:6
|
LL | impl<'a> Foo<'a> {
| ^^
note: the lint level is defined here
--> $DIR/assoc-const-elided-lifetime.rs:1:9
|
Expand All @@ -24,6 +29,13 @@ 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>
note: cannot automatically infer `'static` because of other lifetimes in scope
--> $DIR/assoc-const-elided-lifetime.rs:9:6
|
LL | impl<'a> Foo<'a> {
| ^^
LL | const FOO: Foo<'_> = Foo { x: PhantomData::<&()> };
| ^^
help: use the `'static` lifetime
|
LL | const BAR: &'static () = &();
Expand Down

0 comments on commit 130a9d0

Please sign in to comment.