Skip to content

Commit e8a6e60

Browse files
committed
resolve: Add some asserts for unexpected lifetime rib combinations
1 parent 3cf5fc5 commit e8a6e60

File tree

3 files changed

+115
-12
lines changed

3 files changed

+115
-12
lines changed

Diff for: compiler/rustc_resolve/src/late.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -1423,9 +1423,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
14231423
} else {
14241424
LifetimeUseSet::Many
14251425
}),
1426-
LifetimeRibKind::Generics { .. }
1427-
| LifetimeRibKind::ConstGeneric
1428-
| LifetimeRibKind::AnonConst => None,
1426+
LifetimeRibKind::Generics { .. } => None,
1427+
LifetimeRibKind::ConstGeneric | LifetimeRibKind::AnonConst => {
1428+
span_bug!(ident.span, "unexpected rib kind: {:?}", rib.kind)
1429+
}
14291430
})
14301431
.unwrap_or(LifetimeUseSet::Many);
14311432
debug!(?use_ctxt, ?use_set);
@@ -1460,7 +1461,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
14601461
);
14611462
return;
14621463
}
1463-
_ => {}
1464+
LifetimeRibKind::AnonymousCreateParameter { .. }
1465+
| LifetimeRibKind::Elided(_)
1466+
| LifetimeRibKind::Generics { .. }
1467+
| LifetimeRibKind::ElisionFailure
1468+
| LifetimeRibKind::AnonymousReportError => {}
14641469
}
14651470
}
14661471

@@ -1534,9 +1539,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15341539
return;
15351540
}
15361541
LifetimeRibKind::Item => break,
1537-
LifetimeRibKind::Generics { .. }
1538-
| LifetimeRibKind::ConstGeneric
1539-
| LifetimeRibKind::AnonConst => {}
1542+
LifetimeRibKind::Generics { .. } | LifetimeRibKind::ConstGeneric => {}
1543+
LifetimeRibKind::AnonConst => {
1544+
// There is always an `Elided(LifetimeRes::Static)` inside an `AnonConst`.
1545+
span_bug!(lifetime.ident.span, "unexpected rib kind: {:?}", rib.kind)
1546+
}
15401547
}
15411548
}
15421549
self.record_lifetime_res(lifetime.id, LifetimeRes::Error, elision_candidate);
@@ -1751,9 +1758,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
17511758
self.report_missing_lifetime_specifiers(vec![missing_lifetime], None);
17521759
break;
17531760
}
1754-
LifetimeRibKind::Generics { .. }
1755-
| LifetimeRibKind::ConstGeneric
1756-
| LifetimeRibKind::AnonConst => {}
1761+
LifetimeRibKind::Generics { .. } | LifetimeRibKind::ConstGeneric => {}
1762+
LifetimeRibKind::AnonConst => {
1763+
// There is always an `Elided(LifetimeRes::Static)` inside an `AnonConst`.
1764+
span_bug!(elided_lifetime_span, "unexpected rib kind: {:?}", rib.kind)
1765+
}
17571766
}
17581767
}
17591768

@@ -3938,7 +3947,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
39383947
fn_id: NodeId,
39393948
async_node_id: Option<(NodeId, Span)>,
39403949
) {
3941-
if let Some((async_node_id, _)) = async_node_id {
3950+
if let Some((async_node_id, span)) = async_node_id {
39423951
let mut extra_lifetime_params =
39433952
self.r.extra_lifetime_params_map.get(&fn_id).cloned().unwrap_or_default();
39443953
for rib in self.lifetime_ribs.iter().rev() {
@@ -3952,7 +3961,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
39523961
extra_lifetime_params.extend(earlier_fresh);
39533962
}
39543963
}
3955-
_ => {}
3964+
LifetimeRibKind::Generics { .. } => {}
3965+
_ => {
3966+
// We are in a function definition. We should only find `Generics`
3967+
// and `AnonymousCreateParameter` inside the innermost `Item`.
3968+
span_bug!(span, "unexpected rib kind: {:?}", rib.kind)
3969+
}
39563970
}
39573971
}
39583972
self.r.extra_lifetime_params_map.insert(async_node_id, extra_lifetime_params);

Diff for: src/test/ui/lifetimes/unusual-rib-combinations.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(inline_const)]
2+
3+
struct S<'a>(&'a u8);
4+
fn foo() {}
5+
6+
// Paren generic args in AnonConst
7+
fn a() -> [u8; foo::()] {
8+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
9+
//~| ERROR mismatched types
10+
panic!()
11+
}
12+
13+
// Paren generic args in ConstGeneric
14+
fn b<const C: u8()>() {}
15+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
16+
17+
// Paren generic args in AnonymousReportError
18+
fn c<T = u8()>() {}
19+
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
20+
//~| ERROR defaults for type parameters are only allowed in
21+
//~| WARN this was previously accepted
22+
23+
// Elided lifetime in path in ConstGeneric
24+
fn d<const C: S>() {}
25+
//~^ ERROR missing lifetime specifier
26+
//~| ERROR `S<'static>` is forbidden as the type of a const generic parameter
27+
28+
fn main() {}
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
error[E0106]: missing lifetime specifier
2+
--> $DIR/unusual-rib-combinations.rs:24:15
3+
|
4+
LL | fn d<const C: S>() {}
5+
| ^ expected named lifetime parameter
6+
|
7+
help: consider introducing a named lifetime parameter
8+
|
9+
LL | fn d<'a, const C: S<'a>>() {}
10+
| +++ ++++
11+
12+
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
13+
--> $DIR/unusual-rib-combinations.rs:7:16
14+
|
15+
LL | fn a() -> [u8; foo::()] {
16+
| ^^^^^^^ only `Fn` traits may use parentheses
17+
18+
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
19+
--> $DIR/unusual-rib-combinations.rs:14:15
20+
|
21+
LL | fn b<const C: u8()>() {}
22+
| ^^^^ only `Fn` traits may use parentheses
23+
24+
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
25+
--> $DIR/unusual-rib-combinations.rs:18:10
26+
|
27+
LL | fn c<T = u8()>() {}
28+
| ^^^^ only `Fn` traits may use parentheses
29+
30+
error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
31+
--> $DIR/unusual-rib-combinations.rs:18:6
32+
|
33+
LL | fn c<T = u8()>() {}
34+
| ^^^^^^^^
35+
|
36+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
37+
= note: for more information, see issue #36887 <https://github.com/rust-lang/rust/issues/36887>
38+
= note: `#[deny(invalid_type_param_default)]` on by default
39+
40+
error[E0308]: mismatched types
41+
--> $DIR/unusual-rib-combinations.rs:7:16
42+
|
43+
LL | fn a() -> [u8; foo::()] {
44+
| ^^^^^^^ expected `usize`, found fn item
45+
|
46+
= note: expected type `usize`
47+
found fn item `fn() {foo}`
48+
49+
error: `S<'static>` is forbidden as the type of a const generic parameter
50+
--> $DIR/unusual-rib-combinations.rs:24:15
51+
|
52+
LL | fn d<const C: S>() {}
53+
| ^
54+
|
55+
= note: the only supported types are integers, `bool` and `char`
56+
= help: more complex types are supported with `#![feature(adt_const_params)]`
57+
58+
error: aborting due to 7 previous errors
59+
60+
Some errors have detailed explanations: E0106, E0214, E0308.
61+
For more information about an error, try `rustc --explain E0106`.

0 commit comments

Comments
 (0)