diff options
author | Nobel Singh <nobel2073@gmail.com> | 2023-12-05 18:12:29 +0545 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-01-30 12:36:49 +0100 |
commit | f48f9a9ac6f20ffe2775437e3b21ed0208a9cee2 (patch) | |
tree | 8304a044d4e1f519beec5371df1335805ffe65c9 | |
parent | a7991de39ff3ef42cd48bb460a02134af73d7b9d (diff) | |
download | gcc-f48f9a9ac6f20ffe2775437e3b21ed0208a9cee2.zip gcc-f48f9a9ac6f20ffe2775437e3b21ed0208a9cee2.tar.gz gcc-f48f9a9ac6f20ffe2775437e3b21ed0208a9cee2.tar.bz2 |
gccrs: Generate error for const trait functions
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>
-rw-r--r-- | gcc/rust/checks/errors/rust-ast-validation.cc | 4 | ||||
-rw-r--r-- | gcc/testsuite/rust/compile/issue-2040.rs | 12 |
2 files changed, 16 insertions, 0 deletions
diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc index 6fb142c..cd197fc 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 %<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 ( diff --git a/gcc/testsuite/rust/compile/issue-2040.rs b/gcc/testsuite/rust/compile/issue-2040.rs new file mode 100644 index 0000000..fbac168 --- /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() {} |