Skip to content

Commit

Permalink
'const fn' in trait are rejected in the AST, this feature gate check …
Browse files Browse the repository at this point in the history
…is a NOP
  • Loading branch information
RalfJung committed Apr 25, 2021
1 parent 5da10c0 commit 75bab07
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 61 deletions.
7 changes: 1 addition & 6 deletions compiler/rustc_ast_passes/src/feature_gate.rs
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: 1 addition & 1 deletion src/test/ui/consts/const-fn-not-in-trait.rs
@@ -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
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
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
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
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 75bab07

Please sign in to comment.