Skip to content

Commit b4fddf0

Browse files
committed
Forbid elided lifetimes within const generic parameter types.
1 parent 1389494 commit b4fddf0

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

src/librustc_ast_lowering/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,12 +2121,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21212121

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

21322134
hir::GenericParam {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(const_generics)]
2+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
3+
4+
struct A<const N: &u8>;
5+
//~^ ERROR `&` without an explicit lifetime name cannot be used here
6+
trait B {}
7+
8+
impl<const N: &u8> A<N> { //~ ERROR `&` without an explicit lifetime name cannot be used here
9+
fn foo<const M: &u8>(&self) {}
10+
//~^ ERROR `&` without an explicit lifetime name cannot be used here
11+
}
12+
13+
impl<const N: &u8> B for A<N> {}
14+
//~^ ERROR `&` without an explicit lifetime name cannot be used here
15+
16+
fn bar<const N: &u8>() {}
17+
//~^ ERROR `&` without an explicit lifetime name cannot be used here
18+
19+
fn main() {}
Lines changed: 40 additions & 0 deletions
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:4: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:8: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:9: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:13: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:16: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:1: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)