public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Check for mutable references in const functions
@ 2023-01-12  7:44 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-01-12  7:44 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:5918059fd3a28915546c08e0d3adbc62b9ca3977

commit 5918059fd3a28915546c08e0d3adbc62b9ca3977
Author: Dave <dme2223@gmail.com>
Date:   Mon Dec 19 10:59:00 2022 -0600

    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
    
    Signed-off-by: Dave Evans <dave@dmetwo.org>

Diff:
---
 gcc/rust/checks/errors/rust-const-checker.cc | 8 ++++++--
 gcc/testsuite/rust/compile/const10.rs        | 3 +++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/checks/errors/rust-const-checker.cc b/gcc/rust/checks/errors/rust-const-checker.cc
index 3de27ec134b..5f20437b7fc 100644
--- a/gcc/rust/checks/errors/rust-const-checker.cc
+++ b/gcc/rust/checks/errors/rust-const-checker.cc
@@ -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
index 00000000000..9ab82744fbd
--- /dev/null
+++ b/gcc/testsuite/rust/compile/const10.rs
@@ -0,0 +1,3 @@
+const fn foo (a: &mut i32) { // { dg-error "mutable references are not allowed in constant functions" }
+	*a = 1;
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-01-12  7:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-12  7:44 [gcc/devel/rust/master] Check for mutable references in const functions Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).