From patchwork Wed Apr 5 14:02:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 79620 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp335167vqo; Wed, 5 Apr 2023 07:08:46 -0700 (PDT) X-Google-Smtp-Source: AKy350a+rM2erESBI21VHGCTh1UqCb6IhFoPz6E7IjO949YiBOxIE4uQdW3WW5dzmrkEZZB9KiVR X-Received: by 2002:a17:906:653:b0:939:90ee:e086 with SMTP id t19-20020a170906065300b0093990eee086mr2866289ejb.28.1680703725922; Wed, 05 Apr 2023 07:08:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680703725; cv=none; d=google.com; s=arc-20160816; b=axXpO2fwEaVJRZES2glaUhAhtmJsUa6y1eZ1TazcKJafUIfWvvoKITmtiL7ZiyVjlR Tz2I6rrS61RT4lZRSbNAP7WrWN/4WYpi0BiG3SzwxMfuLjT0hTBmJy7HoYyoQpDkYeaV A+E8FMX0ebGp5A7bHJnHQgz34o7n2Se0iqF2iO0nwQPLe9AtthC37BJUC9DHDCyIQQk8 qu+suraOjsvDY8wE947HOTy7CwF9ytd/T0lys50H7sXvifeHzJeCZd8SyqpxOl5siCok i+n/UupjaWKgF9xc+jKl4hJMFKQdjYIKzW87O+6nuEcmztKGllPjy0Qi3hnqWvB0m5YR E6jQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dmarc-filter :delivered-to; bh=FSxq5+A8+oY5sQbXA81GHyEj8sJ39KrhMhafqu7g3kM=; b=fWKQUTpqH/pfA3pIFQEulZ6fJ8it4FU8vvRPscPF+T2u4RMuVZEpfIyoazBUJg2Tze k6VHuLAyZaER5ZfcasTU3nX30aAEZ48L3XwV09BxWaWUIDhUv6vpV37st43r0ptoN7Vw AGEDeI3Z9Ec6bIauGdo1NSwqG0DvRPZrcZ7gYEBppFgDA2LvHda2Re3ou9hP8HBZQ/Zc DTblXL7is3soPrp5Oh4v4RpFKmt8vWGe3NjHpZMd+eJ/05jh9dZltqFsBSRVl3gdzEQy xArE7e+NnmD1O9zT3tzn02C7qt0jdlbhHVwHy98mY98GutEYw+O1Y/j2ehPcXQXVc8fg b5Bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=O6qjsX0T; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id qk11-20020a1709077f8b00b00949957e112fsi371382ejc.274.2023.04.05.07.08.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:08:45 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=O6qjsX0T; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4AA6D382A2E2 for ; Wed, 5 Apr 2023 14:06:36 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id 230ED3856DE8 for ; Wed, 5 Apr 2023 14:05:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 230ED3856DE8 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-x435.google.com with SMTP id l12so36314385wrm.10 for ; Wed, 05 Apr 2023 07:05:30 -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=H3iwRlHqRS54fx2ijbqatyt+rMvG2mifhQmSCemVunQOrw7IwQEdlHH8eZqCtrcaqv WT44h46JInDVaaNhksSwW3vhpd7L2gvpqpinQ23eFuZ+HOofjAFUQH9QPz/qBqTpwWtJ CVW0JUrFO1c56MX/UQa/CmSKphpvJpBaVsrCNL5Sgwz+Rdl13g4Ajj7/e11gGHXMTzq2 TytjxCL7sBSbTqw5srp9Jfg66OerlPdRn1Zh+MEVKRc47qYgTSMD++97mzQL67ix7J2i SL+BVgeI87/7hQP2xfcu2doQILTQmIIUGeDTlTauUgin77Qj+D8RF3blslGsUFmZvgEf na7A== X-Gm-Message-State: AAQBX9fduzSN7FK1B/7ezNIANW8tdPCvWCKZ9kcXPVQAMa1zmSSCdeqW thh0mim8EKCm1smiZBzygbf9jnajwTk/m6BRxA== 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> MIME-Version: 1.0 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=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: arthur.cohen@embecosm.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762345589990956944?= X-GMAIL-MSGID: =?utf-8?q?1762345589990956944?= 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; +}