From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 24054385B52C; Thu, 12 Jan 2023 07:44:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 24054385B52C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673509444; bh=if58+czgijPA4bpPqcC9U5+HjKY6s46cPrviP+Ylm5Y=; h=From:To:Subject:Date:From; b=BfMk0z3JRkZRu4uoVQdjpvyCzWtvbSGYx+LytR8LDeLsYYc0Un/FldsK1csVrQeMr /YzWwQLuwBwLo5hCVrojzVbF5mePSjYfFfPr1SZ7vgoDQ2Udnko3ovO6dC8U+2m77o xr31Ex/Wq/ziOpJ4YZd4pLho8GUjgxzKSz1hFigI= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Check for mutable references in const functions X-Act-Checkin: gcc X-Git-Author: Dave X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 0152926ab36ba52153f3f457f6f3bb02bb274073 X-Git-Newrev: 5918059fd3a28915546c08e0d3adbc62b9ca3977 Message-Id: <20230112074404.24054385B52C@sourceware.org> Date: Thu, 12 Jan 2023 07:44:04 +0000 (GMT) List-Id: https://gcc.gnu.org/g:5918059fd3a28915546c08e0d3adbc62b9ca3977 commit 5918059fd3a28915546c08e0d3adbc62b9ca3977 Author: Dave 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 (Squashed commits) Check for mutable references in const functions using StackedContext Signed-off-by: Dave Evans 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; +}