diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc index 6fb142c78455..cd197fc1ea7d 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.cc +++ b/gcc/rust/checks/errors/rust-ast-validation.cc @@ -103,6 +103,10 @@ ASTValidation::visit (AST::Function &function) rust_error_at (function.get_locus (), "functions cannot be both % and %"); + if (qualifiers.is_const () && context.back () == Context::TRAIT_IMPL) + rust_error_at (function.get_locus (), ErrorCode::E0379, + "functions in traits cannot be declared const"); + if (valid_context.find (context.back ()) == valid_context.end () && function.has_self_param ()) rust_error_at ( diff --git a/gcc/testsuite/rust/compile/issue-2040.rs b/gcc/testsuite/rust/compile/issue-2040.rs new file mode 100644 index 000000000000..fbac168b9f38 --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-2040.rs @@ -0,0 +1,12 @@ +trait Foo { + fn f() -> u32; +} + +impl Foo for u32 { + const fn f() -> u32 { + // { dg-error "functions in traits cannot be declared const" "" { target *-*-* } .-1 } + 22 + } +} + +fn main() {}