Skip to content

Commit e71b673

Browse files
authored
Rollup merge of #68143 - skinny121:const-param-type-elided-lifetime, r=petrochenkov
Forbid elided lifetimes within const generic parameter types Disallows `fn foo<const T: &u32>()`, the lifetime must be explicitly given, i.e. `fn foo<const T: &'static u32>()`. Fixes #67883
2 parents 1153403 + 82b90bd commit e71b673

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

src/librustc_ast_lowering/lib.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -2120,12 +2120,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21202120

21212121
(hir::ParamName::Plain(param.ident), kind)
21222122
}
2123-
GenericParamKind::Const { ref ty } => (
2124-
hir::ParamName::Plain(param.ident),
2125-
hir::GenericParamKind::Const {
2126-
ty: self.lower_ty(&ty, ImplTraitContext::disallowed()),
2127-
},
2128-
),
2123+
GenericParamKind::Const { ref ty } => {
2124+
let ty = self
2125+
.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
2126+
this.lower_ty(&ty, ImplTraitContext::disallowed())
2127+
});
2128+
2129+
(hir::ParamName::Plain(param.ident), hir::GenericParamKind::Const { ty })
2130+
}
21292131
};
21302132

21312133
hir::GenericParam {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Elided lifetimes within the type of a const generic parameters is disallowed. This matches the
2+
// behaviour of trait bounds where `fn foo<T: Ord<&u8>>() {}` is illegal. Though we could change
3+
// elided lifetimes within the type of a const generic parameters to be 'static, like elided
4+
// lifetimes within const/static items.
5+
6+
#![feature(const_generics)]
7+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
8+
9+
struct A<const N: &u8>;
10+
//~^ ERROR `&` without an explicit lifetime name cannot be used here
11+
trait B {}
12+
13+
impl<const N: &u8> A<N> { //~ ERROR `&` without an explicit lifetime name cannot be used here
14+
fn foo<const M: &u8>(&self) {}
15+
//~^ ERROR `&` without an explicit lifetime name cannot be used here
16+
}
17+
18+
impl<const N: &u8> B for A<N> {}
19+
//~^ ERROR `&` without an explicit lifetime name cannot be used here
20+
21+
fn bar<const N: &u8>() {}
22+
//~^ ERROR `&` without an explicit lifetime name cannot be used here
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error[E0637]: `&` without an explicit lifetime name cannot be used here
2+
--> $DIR/const-param-elided-lifetime.rs:9:19
3+
|
4+
LL | struct A<const N: &u8>;
5+
| ^ explicit lifetime name needed here
6+
7+
error[E0637]: `&` without an explicit lifetime name cannot be used here
8+
--> $DIR/const-param-elided-lifetime.rs:13:15
9+
|
10+
LL | impl<const N: &u8> A<N> {
11+
| ^ explicit lifetime name needed here
12+
13+
error[E0637]: `&` without an explicit lifetime name cannot be used here
14+
--> $DIR/const-param-elided-lifetime.rs:14:21
15+
|
16+
LL | fn foo<const M: &u8>(&self) {}
17+
| ^ explicit lifetime name needed here
18+
19+
error[E0637]: `&` without an explicit lifetime name cannot be used here
20+
--> $DIR/const-param-elided-lifetime.rs:18:15
21+
|
22+
LL | impl<const N: &u8> B for A<N> {}
23+
| ^ explicit lifetime name needed here
24+
25+
error[E0637]: `&` without an explicit lifetime name cannot be used here
26+
--> $DIR/const-param-elided-lifetime.rs:21:17
27+
|
28+
LL | fn bar<const N: &u8>() {}
29+
| ^ explicit lifetime name needed here
30+
31+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
32+
--> $DIR/const-param-elided-lifetime.rs:6:12
33+
|
34+
LL | #![feature(const_generics)]
35+
| ^^^^^^^^^^^^^^
36+
|
37+
= note: `#[warn(incomplete_features)]` on by default
38+
39+
error: aborting due to 5 previous errors
40+

0 commit comments

Comments
 (0)