Skip to content

Commit

Permalink
Generate error for const trait functions
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
nobel-sh authored and P-E-P committed Dec 6, 2023
1 parent a48b4f8 commit 5e3a875
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
12 changes: 12 additions & 0 deletions gcc/testsuite/rust/compile/issue-2040.rs
Original file line number Diff line number Diff line change
@@ -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() {}

0 comments on commit 5e3a875

Please sign in to comment.