From d9e05700ac3085f29e95d53b3ec59c22adbc7c71 Mon Sep 17 00:00:00 2001 From: Dave Date: Mon, 19 Dec 2022 10:59:00 -0600 Subject: [PATCH] gccrs: Check for mutable references in const functions Use StackedContext instead. Fix error string Signed-off-by: Dave Evans (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 --- gcc/rust/checks/errors/rust-const-checker.cc | 8 ++++++-- gcc/testsuite/rust/compile/const10.rs | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/rust/compile/const10.rs diff --git a/gcc/rust/checks/errors/rust-const-checker.cc b/gcc/rust/checks/errors/rust-const-checker.cc index 576c1b170d66..7e31c9f9c28c 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 000000000000..9ab82744fbd8 --- /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; +} -- 2.47.2