]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Check for mutable references in const functions
authorDave <dme2223@gmail.com>
Mon, 19 Dec 2022 16:59:00 +0000 (10:59 -0600)
committerDave Evans <dave@dmetwo.org>
Mon, 2 Jan 2023 23:56:59 +0000 (17:56 -0600)
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

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 3de27ec134b7c18bb9dfc680c66aac3dd6118b82..5f20437b7fcc334df5d1baed9f913f970be448a5 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;
+}