Skip to content

Commit ee8382f

Browse files
committed
Auto merge of #84544 - RalfJung:const_fn_in_trait, r=oli-obk
Always reject `const fn` in `trait` during parsing. 'const fn' in trait are rejected in the AST: https://github.com/rust-lang/rust/blob/b78c0d8a4d5af91a4a55d029293e3ecb879ec142/compiler/rustc_ast_passes/src/ast_validation.rs#L1411 So this feature gate check is a NOP and we can just remove it. The src/test/ui/feature-gates/feature-gate-min_const_fn.rs and src/test/ui/feature-gates/feature-gate-const_fn.rs tests ensure that we still reject `const fn` in `trait` Cc #84510 r? `@oli-obk`
2 parents f8f5968 + 8a961a5 commit ee8382f

File tree

8 files changed

+14
-65
lines changed

8 files changed

+14
-65
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -586,12 +586,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
586586

587587
fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
588588
let is_fn = match i.kind {
589-
ast::AssocItemKind::Fn(box ast::FnKind(_, ref sig, _, _)) => {
590-
if let (ast::Const::Yes(_), AssocCtxt::Trait) = (sig.header.constness, ctxt) {
591-
gate_feature_post!(&self, const_fn, i.span, "const fn is unstable");
592-
}
593-
true
594-
}
589+
ast::AssocItemKind::Fn(_) => true,
595590
ast::AssocItemKind::TyAlias(box ast::TyAliasKind(_, ref generics, _, ref ty)) => {
596591
if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) {
597592
gate_feature_post!(

compiler/rustc_error_codes/src/error_codes/E0379.md

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ A trait method was declared const.
33
Erroneous code example:
44

55
```compile_fail,E0379
6-
#![feature(const_fn)]
7-
86
trait Foo {
97
const fn bar() -> u32; // error!
108
}

compiler/rustc_error_codes/src/error_codes/E0764.md

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ A mutable reference was used in a constant.
33
Erroneous code example:
44

55
```compile_fail,E0764
6-
#![feature(const_fn)]
76
#![feature(const_mut_refs)]
87
98
fn main() {
@@ -27,7 +26,6 @@ Remember: you cannot use a function call inside a constant or static. However,
2726
you can totally use it in constant functions:
2827

2928
```
30-
#![feature(const_fn)]
3129
#![feature(const_mut_refs)]
3230
3331
const fn foo(x: usize) -> usize {

src/test/ui/consts/const-fn-not-in-trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Test that const fn is illegal in a trait declaration, whether or
2-
// not a default is provided.
2+
// not a default is provided, and even with the feature gate.
33

44
#![feature(const_fn)]
55

src/test/ui/feature-gates/feature-gate-const_fn.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
const fn foo() -> usize { 0 } // ok
44

55
trait Foo {
6-
const fn foo() -> u32; //~ ERROR const fn is unstable
7-
//~| ERROR functions in traits cannot be declared const
8-
const fn bar() -> u32 { 0 } //~ ERROR const fn is unstable
9-
//~| ERROR functions in traits cannot be declared const
6+
const fn foo() -> u32; //~ ERROR functions in traits cannot be declared const
7+
const fn bar() -> u32 { 0 } //~ ERROR functions in traits cannot be declared const
108
}
119

1210
impl Foo for u32 {

src/test/ui/feature-gates/feature-gate-const_fn.stderr

+4-23
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,17 @@ LL | const fn foo() -> u32;
55
| ^^^^^ functions in traits cannot be const
66

77
error[E0379]: functions in traits cannot be declared const
8-
--> $DIR/feature-gate-const_fn.rs:8:5
8+
--> $DIR/feature-gate-const_fn.rs:7:5
99
|
1010
LL | const fn bar() -> u32 { 0 }
1111
| ^^^^^ functions in traits cannot be const
1212

1313
error[E0379]: functions in traits cannot be declared const
14-
--> $DIR/feature-gate-const_fn.rs:13:5
14+
--> $DIR/feature-gate-const_fn.rs:11:5
1515
|
1616
LL | const fn foo() -> u32 { 0 }
1717
| ^^^^^ functions in traits cannot be const
1818

19-
error[E0658]: const fn is unstable
20-
--> $DIR/feature-gate-const_fn.rs:6:5
21-
|
22-
LL | const fn foo() -> u32;
23-
| ^^^^^^^^^^^^^^^^^^^^^^
24-
|
25-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
26-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
27-
28-
error[E0658]: const fn is unstable
29-
--> $DIR/feature-gate-const_fn.rs:8:5
30-
|
31-
LL | const fn bar() -> u32 { 0 }
32-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
33-
|
34-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
35-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
36-
37-
error: aborting due to 5 previous errors
19+
error: aborting due to 3 previous errors
3820

39-
Some errors have detailed explanations: E0379, E0658.
40-
For more information about an error, try `rustc --explain E0379`.
21+
For more information about this error, try `rustc --explain E0379`.

src/test/ui/feature-gates/feature-gate-min_const_fn.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
const fn foo() -> usize { 0 } // stabilized
44

55
trait Foo {
6-
const fn foo() -> u32; //~ ERROR const fn is unstable
7-
//~| ERROR functions in traits cannot be declared const
8-
const fn bar() -> u32 { 0 } //~ ERROR const fn is unstable
9-
//~| ERROR functions in traits cannot be declared const
6+
const fn foo() -> u32; //~ ERROR functions in traits cannot be declared const
7+
const fn bar() -> u32 { 0 } //~ ERROR functions in traits cannot be declared const
108
}
119

1210
impl Foo for u32 {

src/test/ui/feature-gates/feature-gate-min_const_fn.stderr

+4-23
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,17 @@ LL | const fn foo() -> u32;
55
| ^^^^^ functions in traits cannot be const
66

77
error[E0379]: functions in traits cannot be declared const
8-
--> $DIR/feature-gate-min_const_fn.rs:8:5
8+
--> $DIR/feature-gate-min_const_fn.rs:7:5
99
|
1010
LL | const fn bar() -> u32 { 0 }
1111
| ^^^^^ functions in traits cannot be const
1212

1313
error[E0379]: functions in traits cannot be declared const
14-
--> $DIR/feature-gate-min_const_fn.rs:13:5
14+
--> $DIR/feature-gate-min_const_fn.rs:11:5
1515
|
1616
LL | const fn foo() -> u32 { 0 }
1717
| ^^^^^ functions in traits cannot be const
1818

19-
error[E0658]: const fn is unstable
20-
--> $DIR/feature-gate-min_const_fn.rs:6:5
21-
|
22-
LL | const fn foo() -> u32;
23-
| ^^^^^^^^^^^^^^^^^^^^^^
24-
|
25-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
26-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
27-
28-
error[E0658]: const fn is unstable
29-
--> $DIR/feature-gate-min_const_fn.rs:8:5
30-
|
31-
LL | const fn bar() -> u32 { 0 }
32-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
33-
|
34-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
35-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
36-
37-
error: aborting due to 5 previous errors
19+
error: aborting due to 3 previous errors
3820

39-
Some errors have detailed explanations: E0379, E0658.
40-
For more information about an error, try `rustc --explain E0379`.
21+
For more information about this error, try `rustc --explain E0379`.

0 commit comments

Comments
 (0)