Fixes issue #2040
Add check to assure that a function cant be declared const inside trait impl
blocks.
gcc/rust/ChangeLog:
* checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add
check for const funtion.
gcc/testsuite/ChangeLog:
* rust/compile/issue-2040.rs: New test.
Signed-off-by: Nobel Singh <nobel2073@gmail.com>
rust_error_at (function.get_locus (),
"functions cannot be both %<const%> and %<async%>");
+ 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 (
--- /dev/null
+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() {}