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
Error is produced if a trait function is declared const.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItemWithTrait::visit): Produce error if trait funtion is found to be const.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-2040.rs: New test.

Signed-off-by: Nobel Singh <[email protected]>
  • Loading branch information
nobel-sh committed Nov 22, 2023
1 parent 50fe556 commit 5450f98
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions gcc/rust/typecheck/rust-hir-type-check-implitem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,12 @@ TypeCheckImplItemWithTrait::visit (HIR::Function &function)
trait_reference.get_name ().c_str ());
return;
}
// const function as trait item
if (function.get_qualifiers ().is_const ())
{
rust_error_at (function.get_locus (), ErrorCode::E0379,
"functions in traits cannot be declared const");
}

// get the item from the predicate
resolved_trait_item = trait_reference.lookup_associated_item (raw_trait_item);
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 5450f98

Please sign in to comment.