Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always reject const fn in trait during parsing. #84544

Merged
merged 2 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.