Skip to content

Commit 50eb397

Browse files
committed
allow const generics in const fn
1 parent c58c532 commit 50eb397

File tree

3 files changed

+5
-41
lines changed

3 files changed

+5
-41
lines changed

src/librustc_ast_passes/ast_validation.rs

+1-24
Original file line numberDiff line numberDiff line change
@@ -561,28 +561,6 @@ impl<'a> AstValidator<'a> {
561561
}
562562
}
563563

564-
/// We currently do not permit const generics in `const fn`,
565-
/// as this is tantamount to allowing compile-time dependent typing.
566-
///
567-
/// FIXME(const_generics): Is this really true / necessary? Discuss with @varkor.
568-
/// At any rate, the restriction feels too syntactic. Consider moving it to e.g. typeck.
569-
fn check_const_fn_const_generic(&self, span: Span, sig: &FnSig, generics: &Generics) {
570-
if let Const::Yes(const_span) = sig.header.constness {
571-
// Look for const generics and error if we find any.
572-
for param in &generics.params {
573-
if let GenericParamKind::Const { .. } = param.kind {
574-
self.err_handler()
575-
.struct_span_err(
576-
span,
577-
"const parameters are not permitted in const functions",
578-
)
579-
.span_label(const_span, "`const` because of this")
580-
.emit();
581-
}
582-
}
583-
}
584-
}
585-
586564
fn check_item_named(&self, ident: Ident, kind: &str) {
587565
if ident.name != kw::Underscore {
588566
return;
@@ -966,9 +944,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
966944
.emit();
967945
}
968946
}
969-
ItemKind::Fn(def, ref sig, ref generics, ref body) => {
947+
ItemKind::Fn(def, _, _, ref body) => {
970948
self.check_defaultness(item.span, def);
971-
self.check_const_fn_const_generic(item.span, sig, generics);
972949

973950
if body.is_none() {
974951
let msg = "free function without a body";
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
// run-pass
12
#![feature(const_generics)]
23
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
34

45
const fn const_u32_identity<const X: u32>() -> u32 {
5-
//~^ ERROR const parameters are not permitted in const functions
66
X
77
}
88

99
fn main() {
10-
println!("{:?}", const_u32_identity::<18>());
10+
assert_eq!(const_u32_identity::<18>(), 18);
1111
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
1-
error: const parameters are not permitted in const functions
2-
--> $DIR/const-fn-with-const-param.rs:4:1
3-
|
4-
LL | const fn const_u32_identity<const X: u32>() -> u32 {
5-
| ^----
6-
| |
7-
| _`const` because of this
8-
| |
9-
LL | |
10-
LL | | X
11-
LL | | }
12-
| |_^
13-
141
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
15-
--> $DIR/const-fn-with-const-param.rs:1:12
2+
--> $DIR/const-fn-with-const-param.rs:2:12
163
|
174
LL | #![feature(const_generics)]
185
| ^^^^^^^^^^^^^^
196
|
207
= note: `#[warn(incomplete_features)]` on by default
218

22-
error: aborting due to previous error; 1 warning emitted
9+
warning: 1 warning emitted
2310

0 commit comments

Comments
 (0)