Rollup merge of #138985 - oli-obk:push-mvlqmtmyozro, r=compiler-errors

Use the correct binder scope for elided lifetimes in assoc consts

Beyond diagnostics this has no real effect, and it's also just about a future incompat lint. But it causes ICEs in some refactorings that I'm doing, so trying to get it out of the way
This commit is contained in:
Stuart Cook 2025-03-27 15:57:25 +11:00 committed by GitHub
commit 3db0999100
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 51 additions and 59 deletions

View file

@ -3329,34 +3329,44 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
}, },
|this| { |this| {
this.with_lifetime_rib( this.with_lifetime_rib(
LifetimeRibKind::StaticIfNoLifetimeInScope { // Until these are a hard error, we need to create them within the correct binder,
lint_id: item.id, // Otherwise the lifetimes of this assoc const think they are lifetimes of the trait.
// In impls, it's not a hard error yet due to backcompat. LifetimeRibKind::AnonymousCreateParameter {
emit_lint: true, binder: item.id,
report_in_path: true,
}, },
|this| { |this| {
// If this is a trait impl, ensure the const this.with_lifetime_rib(
// exists in trait LifetimeRibKind::StaticIfNoLifetimeInScope {
this.check_trait_item( lint_id: item.id,
item.id, // In impls, it's not a hard error yet due to backcompat.
item.ident, emit_lint: true,
&item.kind, },
ValueNS, |this| {
item.span, // If this is a trait impl, ensure the const
seen_trait_items, // exists in trait
|i, s, c| ConstNotMemberOfTrait(i, s, c), 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_generics(generics);
this.visit_ty(ty); this.visit_ty(ty);
if let Some(expr) = expr { if let Some(expr) = expr {
// We allow arbitrary const expressions inside of associated consts, // We allow arbitrary const expressions inside of associated consts,
// even if they are potentially not const evaluatable. // even if they are potentially not const evaluatable.
// //
// Type parameters can already be used and as associated consts are // Type parameters can already be used and as associated consts are
// not used as part of the type system, this is far less surprising. // not used as part of the type system, this is far less surprising.
this.resolve_const_body(expr, None); this.resolve_const_body(expr, None);
} }
},
)
}, },
); );
}, },

View file

@ -35,8 +35,6 @@ note: cannot automatically infer `'static` because of other lifetimes in scope
| |
LL | impl<'a> Foo<'a> { LL | impl<'a> Foo<'a> {
| ^^ | ^^
LL | const FOO: Foo<'_> = Foo { x: PhantomData::<&()> };
| ^^
help: use the `'static` lifetime help: use the `'static` lifetime
| |
LL | const BAR: &'static () = &(); LL | const BAR: &'static () = &();

View file

@ -16,7 +16,7 @@ impl Bar for Foo<'_> {
const STATIC: &str = ""; const STATIC: &str = "";
//~^ ERROR `&` without an explicit lifetime name cannot be used here //~^ ERROR `&` without an explicit lifetime name cannot be used here
//~| WARN this was previously accepted by the compiler but is being phased out //~| WARN this was previously accepted by the compiler but is being phased out
//~| ERROR const not compatible with trait //~| ERROR lifetime parameters or bounds on const `STATIC` do not match the trait declaration
} }
fn main() {} fn main() {}

View file

@ -39,21 +39,15 @@ help: use the `'static` lifetime
LL | const STATIC: &'static str = ""; LL | const STATIC: &'static str = "";
| +++++++ | +++++++
error[E0308]: const not compatible with trait error[E0195]: lifetime parameters or bounds on const `STATIC` do not match the trait declaration
--> $DIR/elided-lifetime.rs:16:5 --> $DIR/elided-lifetime.rs:16:17
| |
LL | const STATIC: &str;
| - lifetimes in impl do not match this const in trait
...
LL | const STATIC: &str = ""; LL | const STATIC: &str = "";
| ^^^^^^^^^^^^^^^^^^ lifetime mismatch | ^ lifetimes do not match const in trait
|
= note: expected reference `&'static _`
found reference `&_`
note: the anonymous lifetime as defined here...
--> $DIR/elided-lifetime.rs:16:19
|
LL | const STATIC: &str = "";
| ^
= note: ...does not necessarily outlive the static lifetime
error: aborting due to 3 previous errors error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0308`. For more information about this error, try `rustc --explain E0195`.

View file

@ -9,7 +9,7 @@ impl Bar<'_> for A {
const STATIC: &str = ""; const STATIC: &str = "";
//~^ ERROR `&` without an explicit lifetime name cannot be used here //~^ ERROR `&` without an explicit lifetime name cannot be used here
//~| WARN this was previously accepted by the compiler but is being phased out //~| WARN this was previously accepted by the compiler but is being phased out
//~| ERROR const not compatible with trait //~| ERROR lifetime parameters or bounds on const `STATIC` do not match the trait declaration
} }
struct B; struct B;

View file

@ -21,25 +21,15 @@ help: use the `'static` lifetime
LL | const STATIC: &'static str = ""; LL | const STATIC: &'static str = "";
| +++++++ | +++++++
error[E0308]: const not compatible with trait error[E0195]: lifetime parameters or bounds on const `STATIC` do not match the trait declaration
--> $DIR/static-trait-impl.rs:9:5 --> $DIR/static-trait-impl.rs:9:17
| |
LL | const STATIC: &'a str;
| - lifetimes in impl do not match this const in trait
...
LL | const STATIC: &str = ""; LL | const STATIC: &str = "";
| ^^^^^^^^^^^^^^^^^^ lifetime mismatch | ^ lifetimes do not match const in trait
|
= note: expected reference `&_`
found reference `&_`
note: the anonymous lifetime as defined here...
--> $DIR/static-trait-impl.rs:9:19
|
LL | const STATIC: &str = "";
| ^
note: ...does not necessarily outlive the anonymous lifetime as defined here
--> $DIR/static-trait-impl.rs:8:10
|
LL | impl Bar<'_> for A {
| ^^
error: aborting due to 2 previous errors error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0308`. For more information about this error, try `rustc --explain E0195`.