gccrs: Check for mutable references in const functions
authorDave <dme2223@gmail.com>
Mon, 19 Dec 2022 16:59:00 +0000 (10:59 -0600)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 6 Apr 2023 08:47:16 +0000 (10:47 +0200)
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>
gcc/rust/checks/errors/rust-const-checker.cc
gcc/testsuite/rust/compile/const10.rs [new file with mode: 0644]

index 576c1b1..7e31c9f 100644 (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)
diff --git a/gcc/testsuite/rust/compile/const10.rs b/gcc/testsuite/rust/compile/const10.rs
new file mode 100644 (file)
index 0000000..9ab8274
--- /dev/null
@@ -0,0 +1,3 @@
+const fn foo (a: &mut i32) { // { dg-error "mutable references are not allowed in constant functions" }
+       *a = 1;
+}