gccrs: Check for mutable references in const functions

Use StackedContext instead. Fix error string

Signed-off-by: Dave Evans <dave@dmetwo.org>

(Squashed commits) Check for mutable references in const functions using StackedContext

gcc/rust/ChangeLog:

	* checks/errors/rust-const-checker.cc (ConstChecker::visit): Use StackedContext
	class.

gcc/testsuite/ChangeLog:

	* rust/compile/const10.rs: New test.

Signed-off-by: Dave Evans <dave@dmetwo.org>
This commit is contained in:
Dave
2022-12-19 10:59:00 -06:00
committed by Arthur Cohen
parent 776ff05380
commit d9e05700ac
2 changed files with 9 additions and 2 deletions

View File

@@ -898,8 +898,12 @@ ConstChecker::visit (RawPointerType &)
{}
void
ConstChecker::visit (ReferenceType &)
{}
ConstChecker::visit (ReferenceType &type)
{
if (const_context.is_in_context () && type.is_mut ())
rust_error_at (type.get_locus (),
"mutable references are not allowed in constant functions");
}
void
ConstChecker::visit (ArrayType &type)

View File

@@ -0,0 +1,3 @@
const fn foo (a: &mut i32) { // { dg-error "mutable references are not allowed in constant functions" }
*a = 1;
}