Skip to content

Commit a830c59

Browse files
committed
Use the correct binder scope for elided lifetimes in assoc consts
1 parent 6e8abb5 commit a830c59

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
@@ -3334,34 +3334,44 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
33343334
},
33353335
|this| {
33363336
this.with_lifetime_rib(
3337-
LifetimeRibKind::StaticIfNoLifetimeInScope {
3338-
lint_id: item.id,
3339-
// In impls, it's not a hard error yet due to backcompat.
3340-
emit_lint: true,
3337+
// Until these are a hard error, we need to create them within the correct binder,
3338+
// Otherwise the lifetimes of this assoc const think they are lifetimes of the trait.
3339+
LifetimeRibKind::AnonymousCreateParameter {
3340+
binder: item.id,
3341+
report_in_path: true,
33413342
},
33423343
|this| {
3343-
// If this is a trait impl, ensure the const
3344-
// exists in trait
3345-
this.check_trait_item(
3346-
item.id,
3347-
item.ident,
3348-
&item.kind,
3349-
ValueNS,
3350-
item.span,
3351-
seen_trait_items,
3352-
|i, s, c| ConstNotMemberOfTrait(i, s, c),
3353-
);
3344+
this.with_lifetime_rib(
3345+
LifetimeRibKind::StaticIfNoLifetimeInScope {
3346+
lint_id: item.id,
3347+
// In impls, it's not a hard error yet due to backcompat.
3348+
emit_lint: true,
3349+
},
3350+
|this| {
3351+
// If this is a trait impl, ensure the const
3352+
// exists in trait
3353+
this.check_trait_item(
3354+
item.id,
3355+
item.ident,
3356+
&item.kind,
3357+
ValueNS,
3358+
item.span,
3359+
seen_trait_items,
3360+
|i, s, c| ConstNotMemberOfTrait(i, s, c),
3361+
);
33543362

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

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)