resolve: Add some asserts for unexpected lifetime rib combinations

This commit is contained in:
Vadim Petrochenkov 2022-10-09 17:56:40 +04:00
parent 3cf5fc58d5
commit e8a6e60c5d
3 changed files with 115 additions and 12 deletions

View file

@ -1423,9 +1423,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
} else {
LifetimeUseSet::Many
}),
LifetimeRibKind::Generics { .. }
| LifetimeRibKind::ConstGeneric
| LifetimeRibKind::AnonConst => None,
LifetimeRibKind::Generics { .. } => None,
LifetimeRibKind::ConstGeneric | LifetimeRibKind::AnonConst => {
span_bug!(ident.span, "unexpected rib kind: {:?}", rib.kind)
}
})
.unwrap_or(LifetimeUseSet::Many);
debug!(?use_ctxt, ?use_set);
@ -1460,7 +1461,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
);
return;
}
_ => {}
LifetimeRibKind::AnonymousCreateParameter { .. }
| LifetimeRibKind::Elided(_)
| LifetimeRibKind::Generics { .. }
| LifetimeRibKind::ElisionFailure
| LifetimeRibKind::AnonymousReportError => {}
}
}
@ -1534,9 +1539,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
return;
}
LifetimeRibKind::Item => break,
LifetimeRibKind::Generics { .. }
| LifetimeRibKind::ConstGeneric
| LifetimeRibKind::AnonConst => {}
LifetimeRibKind::Generics { .. } | LifetimeRibKind::ConstGeneric => {}
LifetimeRibKind::AnonConst => {
// There is always an `Elided(LifetimeRes::Static)` inside an `AnonConst`.
span_bug!(lifetime.ident.span, "unexpected rib kind: {:?}", rib.kind)
}
}
}
self.record_lifetime_res(lifetime.id, LifetimeRes::Error, elision_candidate);
@ -1751,9 +1758,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.report_missing_lifetime_specifiers(vec![missing_lifetime], None);
break;
}
LifetimeRibKind::Generics { .. }
| LifetimeRibKind::ConstGeneric
| LifetimeRibKind::AnonConst => {}
LifetimeRibKind::Generics { .. } | LifetimeRibKind::ConstGeneric => {}
LifetimeRibKind::AnonConst => {
// There is always an `Elided(LifetimeRes::Static)` inside an `AnonConst`.
span_bug!(elided_lifetime_span, "unexpected rib kind: {:?}", rib.kind)
}
}
}
@ -3938,7 +3947,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
fn_id: NodeId,
async_node_id: Option<(NodeId, Span)>,
) {
if let Some((async_node_id, _)) = async_node_id {
if let Some((async_node_id, span)) = async_node_id {
let mut extra_lifetime_params =
self.r.extra_lifetime_params_map.get(&fn_id).cloned().unwrap_or_default();
for rib in self.lifetime_ribs.iter().rev() {
@ -3952,7 +3961,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
extra_lifetime_params.extend(earlier_fresh);
}
}
_ => {}
LifetimeRibKind::Generics { .. } => {}
_ => {
// We are in a function definition. We should only find `Generics`
// and `AnonymousCreateParameter` inside the innermost `Item`.
span_bug!(span, "unexpected rib kind: {:?}", rib.kind)
}
}
}
self.r.extra_lifetime_params_map.insert(async_node_id, extra_lifetime_params);

View file

@ -0,0 +1,28 @@
#![feature(inline_const)]
struct S<'a>(&'a u8);
fn foo() {}
// Paren generic args in AnonConst
fn a() -> [u8; foo::()] {
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
//~| ERROR mismatched types
panic!()
}
// Paren generic args in ConstGeneric
fn b<const C: u8()>() {}
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
// Paren generic args in AnonymousReportError
fn c<T = u8()>() {}
//~^ ERROR parenthesized type parameters may only be used with a `Fn` trait
//~| ERROR defaults for type parameters are only allowed in
//~| WARN this was previously accepted
// Elided lifetime in path in ConstGeneric
fn d<const C: S>() {}
//~^ ERROR missing lifetime specifier
//~| ERROR `S<'static>` is forbidden as the type of a const generic parameter
fn main() {}

View file

@ -0,0 +1,61 @@
error[E0106]: missing lifetime specifier
--> $DIR/unusual-rib-combinations.rs:24:15
|
LL | fn d<const C: S>() {}
| ^ expected named lifetime parameter
|
help: consider introducing a named lifetime parameter
|
LL | fn d<'a, const C: S<'a>>() {}
| +++ ++++
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
--> $DIR/unusual-rib-combinations.rs:7:16
|
LL | fn a() -> [u8; foo::()] {
| ^^^^^^^ only `Fn` traits may use parentheses
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
--> $DIR/unusual-rib-combinations.rs:14:15
|
LL | fn b<const C: u8()>() {}
| ^^^^ only `Fn` traits may use parentheses
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
--> $DIR/unusual-rib-combinations.rs:18:10
|
LL | fn c<T = u8()>() {}
| ^^^^ only `Fn` traits may use parentheses
error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
--> $DIR/unusual-rib-combinations.rs:18:6
|
LL | fn c<T = u8()>() {}
| ^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #36887 <https://github.com/rust-lang/rust/issues/36887>
= note: `#[deny(invalid_type_param_default)]` on by default
error[E0308]: mismatched types
--> $DIR/unusual-rib-combinations.rs:7:16
|
LL | fn a() -> [u8; foo::()] {
| ^^^^^^^ expected `usize`, found fn item
|
= note: expected type `usize`
found fn item `fn() {foo}`
error: `S<'static>` is forbidden as the type of a const generic parameter
--> $DIR/unusual-rib-combinations.rs:24:15
|
LL | fn d<const C: S>() {}
| ^
|
= note: the only supported types are integers, `bool` and `char`
= help: more complex types are supported with `#![feature(adt_const_params)]`
error: aborting due to 7 previous errors
Some errors have detailed explanations: E0106, E0214, E0308.
For more information about an error, try `rustc --explain E0106`.