Skip to content

Commit

Permalink
Auto merge of #84544 - RalfJung:const_fn_in_trait, r=oli-obk
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bors committed Apr 26, 2021
2 parents f8f5968 + 8a961a5 commit ee8382f
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 65 deletions.
7 changes: 1 addition & 6 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,12 +586,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {

fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
let is_fn = match i.kind {
ast::AssocItemKind::Fn(box ast::FnKind(_, ref sig, _, _)) => {
if let (ast::Const::Yes(_), AssocCtxt::Trait) = (sig.header.constness, ctxt) {
gate_feature_post!(&self, const_fn, i.span, "const fn is unstable");
}
true
}
ast::AssocItemKind::Fn(_) => true,
ast::AssocItemKind::TyAlias(box ast::TyAliasKind(_, ref generics, _, ref ty)) => {
if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) {
gate_feature_post!(
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_error_codes/src/error_codes/E0379.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ A trait method was declared const.
Erroneous code example:

```compile_fail,E0379
#![feature(const_fn)]
trait Foo {
const fn bar() -> u32; // error!
}
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_error_codes/src/error_codes/E0764.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ A mutable reference was used in a constant.
Erroneous code example:

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

```
#![feature(const_fn)]
#![feature(const_mut_refs)]
const fn foo(x: usize) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-fn-not-in-trait.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Test that const fn is illegal in a trait declaration, whether or
// not a default is provided.
// not a default is provided, and even with the feature gate.

#![feature(const_fn)]

Expand Down
6 changes: 2 additions & 4 deletions src/test/ui/feature-gates/feature-gate-const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
const fn foo() -> usize { 0 } // ok

trait Foo {
const fn foo() -> u32; //~ ERROR const fn is unstable
//~| ERROR functions in traits cannot be declared const
const fn bar() -> u32 { 0 } //~ ERROR const fn is unstable
//~| ERROR functions in traits cannot be declared const
const fn foo() -> u32; //~ ERROR functions in traits cannot be declared const
const fn bar() -> u32 { 0 } //~ ERROR functions in traits cannot be declared const
}

impl Foo for u32 {
Expand Down
27 changes: 4 additions & 23 deletions src/test/ui/feature-gates/feature-gate-const_fn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,17 @@ LL | const fn foo() -> u32;
| ^^^^^ functions in traits cannot be const

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

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

error[E0658]: const fn is unstable
--> $DIR/feature-gate-const_fn.rs:6:5
|
LL | const fn foo() -> u32;
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
= help: add `#![feature(const_fn)]` to the crate attributes to enable

error[E0658]: const fn is unstable
--> $DIR/feature-gate-const_fn.rs:8:5
|
LL | const fn bar() -> u32 { 0 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
= help: add `#![feature(const_fn)]` to the crate attributes to enable

error: aborting due to 5 previous errors
error: aborting due to 3 previous errors

Some errors have detailed explanations: E0379, E0658.
For more information about an error, try `rustc --explain E0379`.
For more information about this error, try `rustc --explain E0379`.
6 changes: 2 additions & 4 deletions src/test/ui/feature-gates/feature-gate-min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
const fn foo() -> usize { 0 } // stabilized

trait Foo {
const fn foo() -> u32; //~ ERROR const fn is unstable
//~| ERROR functions in traits cannot be declared const
const fn bar() -> u32 { 0 } //~ ERROR const fn is unstable
//~| ERROR functions in traits cannot be declared const
const fn foo() -> u32; //~ ERROR functions in traits cannot be declared const
const fn bar() -> u32 { 0 } //~ ERROR functions in traits cannot be declared const
}

impl Foo for u32 {
Expand Down
27 changes: 4 additions & 23 deletions src/test/ui/feature-gates/feature-gate-min_const_fn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,17 @@ LL | const fn foo() -> u32;
| ^^^^^ functions in traits cannot be const

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

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

error[E0658]: const fn is unstable
--> $DIR/feature-gate-min_const_fn.rs:6:5
|
LL | const fn foo() -> u32;
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
= help: add `#![feature(const_fn)]` to the crate attributes to enable

error[E0658]: const fn is unstable
--> $DIR/feature-gate-min_const_fn.rs:8:5
|
LL | const fn bar() -> u32 { 0 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
= help: add `#![feature(const_fn)]` to the crate attributes to enable

error: aborting due to 5 previous errors
error: aborting due to 3 previous errors

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

0 comments on commit ee8382f

Please sign in to comment.