Skip to content

Commit 3db0999

Browse files
authored
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
2 parents c33df27 + a830c59 commit 3db0999

File tree

6 files changed

+51
-59
lines changed

6 files changed

+51
-59
lines changed

compiler/rustc_resolve/src/late.rs

+35-25
Original file line numberDiff line numberDiff line change
@@ -3329,34 +3329,44 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
33293329
},
33303330
|this| {
33313331
this.with_lifetime_rib(
3332-
LifetimeRibKind::StaticIfNoLifetimeInScope {
3333-
lint_id: item.id,
3334-
// In impls, it's not a hard error yet due to backcompat.
3335-
emit_lint: true,
3332+
// Until these are a hard error, we need to create them within the correct binder,
3333+
// Otherwise the lifetimes of this assoc const think they are lifetimes of the trait.
3334+
LifetimeRibKind::AnonymousCreateParameter {
3335+
binder: item.id,
3336+
report_in_path: true,
33363337
},
33373338
|this| {
3338-
// If this is a trait impl, ensure the const
3339-
// exists in trait
3340-
this.check_trait_item(
3341-
item.id,
3342-
item.ident,
3343-
&item.kind,
3344-
ValueNS,
3345-
item.span,
3346-
seen_trait_items,
3347-
|i, s, c| ConstNotMemberOfTrait(i, s, c),
3348-
);
3339+
this.with_lifetime_rib(
3340+
LifetimeRibKind::StaticIfNoLifetimeInScope {
3341+
lint_id: item.id,
3342+
// In impls, it's not a hard error yet due to backcompat.
3343+
emit_lint: true,
3344+
},
3345+
|this| {
3346+
// If this is a trait impl, ensure the const
3347+
// exists in trait
3348+
this.check_trait_item(
3349+
item.id,
3350+
item.ident,
3351+
&item.kind,
3352+
ValueNS,
3353+
item.span,
3354+
seen_trait_items,
3355+
|i, s, c| ConstNotMemberOfTrait(i, s, c),
3356+
);
33493357

3350-
this.visit_generics(generics);
3351-
this.visit_ty(ty);
3352-
if let Some(expr) = expr {
3353-
// We allow arbitrary const expressions inside of associated consts,
3354-
// even if they are potentially not const evaluatable.
3355-
//
3356-
// Type parameters can already be used and as associated consts are
3357-
// not used as part of the type system, this is far less surprising.
3358-
this.resolve_const_body(expr, None);
3359-
}
3358+
this.visit_generics(generics);
3359+
this.visit_ty(ty);
3360+
if let Some(expr) = expr {
3361+
// We allow arbitrary const expressions inside of associated consts,
3362+
// even if they are potentially not const evaluatable.
3363+
//
3364+
// Type parameters can already be used and as associated consts are
3365+
// not used as part of the type system, this is far less surprising.
3366+
this.resolve_const_body(expr, None);
3367+
}
3368+
},
3369+
)
33603370
},
33613371
);
33623372
},

tests/ui/consts/assoc-const-elided-lifetime.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ note: cannot automatically infer `'static` because of other lifetimes in scope
3535
|
3636
LL | impl<'a> Foo<'a> {
3737
| ^^
38-
LL | const FOO: Foo<'_> = Foo { x: PhantomData::<&()> };
39-
| ^^
4038
help: use the `'static` lifetime
4139
|
4240
LL | const BAR: &'static () = &();

tests/ui/consts/static-default-lifetime/elided-lifetime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl Bar for Foo<'_> {
1616
const STATIC: &str = "";
1717
//~^ ERROR `&` without an explicit lifetime name cannot be used here
1818
//~| WARN this was previously accepted by the compiler but is being phased out
19-
//~| ERROR const not compatible with trait
19+
//~| ERROR lifetime parameters or bounds on const `STATIC` do not match the trait declaration
2020
}
2121

2222
fn main() {}

tests/ui/consts/static-default-lifetime/elided-lifetime.stderr

+7-13
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,15 @@ help: use the `'static` lifetime
3939
LL | const STATIC: &'static str = "";
4040
| +++++++
4141

42-
error[E0308]: const not compatible with trait
43-
--> $DIR/elided-lifetime.rs:16:5
42+
error[E0195]: lifetime parameters or bounds on const `STATIC` do not match the trait declaration
43+
--> $DIR/elided-lifetime.rs:16:17
4444
|
45+
LL | const STATIC: &str;
46+
| - lifetimes in impl do not match this const in trait
47+
...
4548
LL | const STATIC: &str = "";
46-
| ^^^^^^^^^^^^^^^^^^ lifetime mismatch
47-
|
48-
= note: expected reference `&'static _`
49-
found reference `&_`
50-
note: the anonymous lifetime as defined here...
51-
--> $DIR/elided-lifetime.rs:16:19
52-
|
53-
LL | const STATIC: &str = "";
54-
| ^
55-
= note: ...does not necessarily outlive the static lifetime
49+
| ^ lifetimes do not match const in trait
5650

5751
error: aborting due to 3 previous errors
5852

59-
For more information about this error, try `rustc --explain E0308`.
53+
For more information about this error, try `rustc --explain E0195`.

tests/ui/consts/static-default-lifetime/static-trait-impl.rs

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

1515
struct B;

tests/ui/consts/static-default-lifetime/static-trait-impl.stderr

+7-17
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,15 @@ help: use the `'static` lifetime
2121
LL | const STATIC: &'static str = "";
2222
| +++++++
2323

24-
error[E0308]: const not compatible with trait
25-
--> $DIR/static-trait-impl.rs:9:5
24+
error[E0195]: lifetime parameters or bounds on const `STATIC` do not match the trait declaration
25+
--> $DIR/static-trait-impl.rs:9:17
2626
|
27+
LL | const STATIC: &'a str;
28+
| - lifetimes in impl do not match this const in trait
29+
...
2730
LL | const STATIC: &str = "";
28-
| ^^^^^^^^^^^^^^^^^^ lifetime mismatch
29-
|
30-
= note: expected reference `&_`
31-
found reference `&_`
32-
note: the anonymous lifetime as defined here...
33-
--> $DIR/static-trait-impl.rs:9:19
34-
|
35-
LL | const STATIC: &str = "";
36-
| ^
37-
note: ...does not necessarily outlive the anonymous lifetime as defined here
38-
--> $DIR/static-trait-impl.rs:8:10
39-
|
40-
LL | impl Bar<'_> for A {
41-
| ^^
31+
| ^ lifetimes do not match const in trait
4232

4333
error: aborting due to 2 previous errors
4434

45-
For more information about this error, try `rustc --explain E0308`.
35+
For more information about this error, try `rustc --explain E0195`.

0 commit comments

Comments
 (0)