Skip to content

Commit 9229b1e

Browse files
committed
Auto merge of rust-lang#115429 - compiler-errors:assoc-ct-lt-fallthrough, r=cjgillot
Fall through when resolving elided assoc const lifetimes `@QuineDot` makes a good point in rust-lang#115010 (comment) that we probably should not accept *more* code due to rust-lang#115011 even though that code will eventually become a forbid-warning in a few versions (rust-lang#115010 (comment)). Fall through when walking thru the `AnonymousWarnToStatic` (renamed to `AnonymousWarn`) rib so that we can resolve as a fresh lifetime like we did before.
2 parents 585bb5e + b62eeb2 commit 9229b1e

File tree

3 files changed

+88
-34
lines changed

3 files changed

+88
-34
lines changed

compiler/rustc_resolve/src/late.rs

+29-34
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ enum LifetimeRibKind {
313313

314314
/// Resolves elided lifetimes to `'static`, but gives a warning that this behavior
315315
/// is a bug and will be reverted soon.
316-
AnonymousWarnToStatic(NodeId),
316+
AnonymousWarn(NodeId),
317317

318318
/// Signal we cannot find which should be the anonymous lifetime.
319319
ElisionFailure,
@@ -1154,7 +1154,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
11541154
}
11551155
LifetimeRibKind::AnonymousCreateParameter { .. }
11561156
| LifetimeRibKind::AnonymousReportError
1157-
| LifetimeRibKind::AnonymousWarnToStatic(_)
1157+
| LifetimeRibKind::AnonymousWarn(_)
11581158
| LifetimeRibKind::Elided(_)
11591159
| LifetimeRibKind::ElisionFailure
11601160
| LifetimeRibKind::ConcreteAnonConst(_)
@@ -1522,7 +1522,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15221522
// lifetime would be illegal.
15231523
LifetimeRibKind::Item
15241524
| LifetimeRibKind::AnonymousReportError
1525-
| LifetimeRibKind::AnonymousWarnToStatic(_)
1525+
| LifetimeRibKind::AnonymousWarn(_)
15261526
| LifetimeRibKind::ElisionFailure => Some(LifetimeUseSet::Many),
15271527
// An anonymous lifetime is legal here, and bound to the right
15281528
// place, go ahead.
@@ -1585,7 +1585,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15851585
| LifetimeRibKind::Generics { .. }
15861586
| LifetimeRibKind::ElisionFailure
15871587
| LifetimeRibKind::AnonymousReportError
1588-
| LifetimeRibKind::AnonymousWarnToStatic(_) => {}
1588+
| LifetimeRibKind::AnonymousWarn(_) => {}
15891589
}
15901590
}
15911591

@@ -1625,8 +1625,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
16251625
self.record_lifetime_res(lifetime.id, res, elision_candidate);
16261626
return;
16271627
}
1628-
LifetimeRibKind::AnonymousWarnToStatic(node_id) => {
1629-
self.record_lifetime_res(lifetime.id, LifetimeRes::Static, elision_candidate);
1628+
LifetimeRibKind::AnonymousWarn(node_id) => {
16301629
let msg = if elided {
16311630
"`&` without an explicit lifetime name cannot be used here"
16321631
} else {
@@ -1642,7 +1641,6 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
16421641
span: lifetime.ident.span,
16431642
},
16441643
);
1645-
return;
16461644
}
16471645
LifetimeRibKind::AnonymousReportError => {
16481646
let (msg, note) = if elided {
@@ -1840,7 +1838,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
18401838
// impl Foo for std::cell::Ref<u32> // note lack of '_
18411839
// async fn foo(_: std::cell::Ref<u32>) { ... }
18421840
LifetimeRibKind::AnonymousCreateParameter { report_in_path: true, .. }
1843-
| LifetimeRibKind::AnonymousWarnToStatic(_) => {
1841+
| LifetimeRibKind::AnonymousWarn(_) => {
18441842
let sess = self.r.tcx.sess;
18451843
let mut err = rustc_errors::struct_span_err!(
18461844
sess,
@@ -2936,33 +2934,30 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
29362934
kind: LifetimeBinderKind::ConstItem,
29372935
},
29382936
|this| {
2939-
this.with_lifetime_rib(
2940-
LifetimeRibKind::AnonymousWarnToStatic(item.id),
2941-
|this| {
2942-
// If this is a trait impl, ensure the const
2943-
// exists in trait
2944-
this.check_trait_item(
2945-
item.id,
2946-
item.ident,
2947-
&item.kind,
2948-
ValueNS,
2949-
item.span,
2950-
seen_trait_items,
2951-
|i, s, c| ConstNotMemberOfTrait(i, s, c),
2952-
);
2937+
this.with_lifetime_rib(LifetimeRibKind::AnonymousWarn(item.id), |this| {
2938+
// If this is a trait impl, ensure the const
2939+
// exists in trait
2940+
this.check_trait_item(
2941+
item.id,
2942+
item.ident,
2943+
&item.kind,
2944+
ValueNS,
2945+
item.span,
2946+
seen_trait_items,
2947+
|i, s, c| ConstNotMemberOfTrait(i, s, c),
2948+
);
29532949

2954-
this.visit_generics(generics);
2955-
this.visit_ty(ty);
2956-
if let Some(expr) = expr {
2957-
// We allow arbitrary const expressions inside of associated consts,
2958-
// even if they are potentially not const evaluatable.
2959-
//
2960-
// Type parameters can already be used and as associated consts are
2961-
// not used as part of the type system, this is far less surprising.
2962-
this.resolve_const_body(expr, None);
2963-
}
2964-
},
2965-
);
2950+
this.visit_generics(generics);
2951+
this.visit_ty(ty);
2952+
if let Some(expr) = expr {
2953+
// We allow arbitrary const expressions inside of associated consts,
2954+
// even if they are potentially not const evaluatable.
2955+
//
2956+
// Type parameters can already be used and as associated consts are
2957+
// not used as part of the type system, this is far less surprising.
2958+
this.resolve_const_body(expr, None);
2959+
}
2960+
});
29662961
},
29672962
);
29682963
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
struct S;
2+
3+
impl S {
4+
const C: &&str = &"";
5+
//~^ WARN `&` without an explicit lifetime name cannot be used here
6+
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
7+
//~| WARN `&` without an explicit lifetime name cannot be used here
8+
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9+
//~| ERROR in type `&&str`, reference has a longer lifetime than the data it references
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
warning: `&` without an explicit lifetime name cannot be used here
2+
--> $DIR/double-elided.rs:4:14
3+
|
4+
LL | const C: &&str = &"";
5+
| ^
6+
|
7+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8+
= note: for more information, see issue #115010 <https://github.com/rust-lang/rust/issues/115010>
9+
= note: `#[warn(elided_lifetimes_in_associated_constant)]` on by default
10+
help: use the `'static` lifetime
11+
|
12+
LL | const C: &'static &str = &"";
13+
| +++++++
14+
15+
warning: `&` without an explicit lifetime name cannot be used here
16+
--> $DIR/double-elided.rs:4:15
17+
|
18+
LL | const C: &&str = &"";
19+
| ^
20+
|
21+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
22+
= note: for more information, see issue #115010 <https://github.com/rust-lang/rust/issues/115010>
23+
help: use the `'static` lifetime
24+
|
25+
LL | const C: &&'static str = &"";
26+
| +++++++
27+
28+
error[E0491]: in type `&&str`, reference has a longer lifetime than the data it references
29+
--> $DIR/double-elided.rs:4:5
30+
|
31+
LL | const C: &&str = &"";
32+
| ^^^^^^^^^^^^^^^^^^^^^
33+
|
34+
note: the pointer is valid for the anonymous lifetime as defined here
35+
--> $DIR/double-elided.rs:4:14
36+
|
37+
LL | const C: &&str = &"";
38+
| ^
39+
note: but the referenced data is only valid for the anonymous lifetime as defined here
40+
--> $DIR/double-elided.rs:4:14
41+
|
42+
LL | const C: &&str = &"";
43+
| ^
44+
45+
error: aborting due to previous error; 2 warnings emitted
46+
47+
For more information about this error, try `rustc --explain E0491`.

0 commit comments

Comments
 (0)