From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id EAE8A3856DE2 for ; Wed, 5 Apr 2023 14:05:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EAE8A3856DE2 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wr1-x42e.google.com with SMTP id t4so31096709wra.7 for ; Wed, 05 Apr 2023 07:05:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1680703528; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=FSxq5+A8+oY5sQbXA81GHyEj8sJ39KrhMhafqu7g3kM=; b=O6qjsX0TFaKOcleF7oU7TgNnkjWQ/GZ8cF18osltZg0g1t1E7VijdGqRDMPZlaCzlL SlLRc408cxxnZCxOTp3QUpJLHN61/0dNuocBzIsI3DPnxQhamd7Tp+TLndnF7WuKXuTU jcpFN8sslWREPxyV84XY43eFrU0Kjak30UItr2wgCYvXvZDnC3HeVvR50cpNiDHpsF3p hoAc56OAEJehllEYtwUegCauunvlzXYfh+CpdAbhoevky9+LOzRq/DCEN77QBQJy1yVu sMFqDZm7lq8SY7DbbqEbisoiOk9N36FiqkJpkthLvD5ekLg3szQnum0qh+5hkDx15aAy HoUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680703528; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=FSxq5+A8+oY5sQbXA81GHyEj8sJ39KrhMhafqu7g3kM=; b=zKx5vDScYAxKuKarjgL9JE98ktBn5DmWnjMUgfgigYeWY9WvRXeEW7kVEnDYbs7NP9 nHjjENA7ouYNwqPngP/Y+TRobrP6VG5wo1GWDNPiVX2t7khsP19RASeRsxH+RCnwx4Sn xcQtAA11PN1VKsDAP4KZQdxPTtk2xy9pmG20Lpx4xC3oS140r8/H8C+rLf28+DlkirDv +ujY09iIyUUE/RjTo//6kwp/eBZvPTMeNWktsCBSELQ5c/zqP4J7mvqKepCx68oDXfF9 twZ31bPqCAwO92ffyeFjqnWa7G9sW2tPwk+jQwFDfphNTOyWFrZ32QdMJsv4RUA7SpG+ 8KSg== X-Gm-Message-State: AAQBX9fHQ/AfqT5uSnq8tjfrTIRMN3WiYGWnHEX1P+JxJEwgElO+n7hd U4Z+c3iQMuQJCaDADV7rF6Pc X-Google-Smtp-Source: AKy350aZTqJ1+VmG1V4gRjVHdrIXZ9eHtxiHYU/eVvFkg1uvnA82bStJVLbb7BURCoVLK50Fc9IwKQ== X-Received: by 2002:a5d:4b0a:0:b0:2e5:6de5:fe9 with SMTP id v10-20020a5d4b0a000000b002e56de50fe9mr4284820wrq.66.1680703528761; Wed, 05 Apr 2023 07:05:28 -0700 (PDT) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id ay8-20020a05600c1e0800b003edddae1068sm2330150wmb.9.2023.04.05.07.05.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:05:28 -0700 (PDT) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Dave , Dave Evans Subject: [committed 03/88] gccrs: Check for mutable references in const functions Date: Wed, 5 Apr 2023 16:02:47 +0200 Message-Id: <20230405140411.3016563-4-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230405140411.3016563-1-arthur.cohen@embecosm.com> References: <20230405140411.3016563-1-arthur.cohen@embecosm.com> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Dave 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 576c1b170d6..7e31c9f9c28 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; +} -- 2.40.0