From patchwork Tue Jan 30 12:07:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 194131 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1189917dyb; Tue, 30 Jan 2024 04:40:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IHUQwi9ROliF8fFI0N+ePoM44nCDAzro4Y1fVCxxwvt+g6d7r0kxqQfeLPXmSsmoFwghaYt X-Received: by 2002:a05:622a:1906:b0:42a:7092:c19b with SMTP id w6-20020a05622a190600b0042a7092c19bmr10004787qtc.83.1706618454629; Tue, 30 Jan 2024 04:40:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706618454; cv=pass; d=google.com; s=arc-20160816; b=HbeTQEWre0ha9TmP2ud7NlZUsMKVq6Wv64k+pGZ37hxrzECnrVIEf1Q36sNF9BjCVG iodQKvAA8tnIwZzQhI/8H9PUtuwNQeWTbten0ZGUYt4VcMUKexXAQt5Qz5RQgGqJ6aSq vNOVqrB5IWDzGkxWYjK9z1P+F/Rz9i2BaYebzNmfVAPpE4dvQvjVhQWXJDKjCT4jLQYK vQ4r8LLUCBUMgqhCWWHBloe3vl6rk40745s0Mi6NJAP6VcnV6YCmSpd8YlCnfxbYJokC ndAlz/53ATUsolzdCOmoDJsSCnSgjqkeRMViR41R5Y140JcYRNNlnFBZdJaaNIkZBtl+ 9jEQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=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:arc-filter:dmarc-filter:delivered-to; bh=ZH4vvIuF+7pTIw66MvBZAdzSsMEAsHlBYHth5nnlIsI=; fh=s1GzlUPo1djGkCl3rkRJI30hDOSCVwGp0RAbVwQ8dxQ=; b=rrp0Mkv42Qr0KXeI+CcMUtN3rcCrk0eKGfj1NJhcYsyNRE9TQu4eCQcvsFZAA9TfJk xe8J6BOwlZyXOKlIy+2gcpqaGryIPB9dmbCFKkB6+woNsb/BiZQP4VfNuTh6Is7hL8nC k+MtcFvVUyor+8SerLE43gR4dk18djiplTSh57jKJFIjx9RH8O2v7kSpd4V1uhEhXK21 LDt13HsrWrSiBwQKBjMBfpShZKS/PP5ehDC9HbN2y1for6NwDwixgGffbOMRIf7RpwLD qBpB8UxxMhvSzBofleZ5MGBrHXrVssFGpIjHdMbhpXamwp8nRxPbxe2NBYpXCDf+CM7x xKyQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=gMkmYAi2; arc=pass (i=1); 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 (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id j14-20020ac85c4e000000b0042ab228f40dsi2134726qtj.451.2024.01.30.04.40.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 04:40:54 -0800 (PST) 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=gMkmYAi2; arc=pass (i=1); 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 8C36F386F018 for ; Tue, 30 Jan 2024 12:40:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 04F353858030 for ; Tue, 30 Jan 2024 12:11:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 04F353858030 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 04F353858030 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::42e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706616717; cv=none; b=LHddiDQU91tE5j5FX9OdHcrrOWbxJ53+Cgw/GErY5u6pWflm9FM+3NR9Ii9NFWt4nkD/Apnshx9lcGsL5CrkhlOihmWbSoQm/MUnRX6ETIxOsPa5TUg0U1kH2ps0+RmbE74bnhzcczOscMYPK4MVVvmZqtlZWIQMQm56Bkjbm4Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706616717; c=relaxed/simple; bh=00nRjnHHJq9fBu0ZaHV3VwDZTOTf5fE0DHaZ1dHL9ho=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Iw6ea5lZDEg+ZgPa0MkMJrSGXHYb2T/iuYjjhoRHOIXAX2KVSjF9PqFdcpOcfqHE3pEDmLpQphc0wouqsBbjQ/Co6GHX3JY2nJekQVNgZd7P55Efa8jMVXiMiJYndbLcHJllGEz7KctpG2OcYLNogJWnC7sR6APoaB8u0nQKkSc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-33af4b34e2cso886292f8f.1 for ; Tue, 30 Jan 2024 04:11:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1706616685; x=1707221485; darn=gcc.gnu.org; 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=ZH4vvIuF+7pTIw66MvBZAdzSsMEAsHlBYHth5nnlIsI=; b=gMkmYAi2hv6tqg7bD0WqV0WNFMR/IJ+stDiPbQK9n9vLnv5l4yBnGiHBTaMfajEvjy 16Ec64BkjIXaeq8lxXuv0o29P9/hyeEM6XHBXdbkMUk/48vdK5qXXR84r8ooYPRIIico Svuxf7VCVn1Z9UsFvADEuavWbYelgqE/mQ4WYrhfQ/nOpGOS1StZBZ5qRtTS25670Gl3 tS6DzvHdamCUUvoI29FGvBiXyDkLK1DqJsu7OEy084pNu75HXGkthN9Vd24v3hwGTmUJ 5q1PHYbfofJe3zM4FPIop53FxYV2bAGXGyoQ6c/oj+ZC2zjVtGjDRYMFMmU7hujkhIS2 5Q2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706616685; x=1707221485; 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=ZH4vvIuF+7pTIw66MvBZAdzSsMEAsHlBYHth5nnlIsI=; b=OFLf915SQyJeDDhbh9pe1pT6eu5aSnRTSn1zz6ky/frMK48hrMiVB4lUaWZniaBVY7 hgTOnhi2k2iSkqcffyMxXLWMV3kcJvsNl8G0rqAKphYUOGzjudw9ekjU2mFbhsrBHqco QOY8EiryB1Muj9tBztADaH0m82GD8mJWZzN4G/R/6ozr6d3dfwEE7c1VL0dJxcAQEfDo YPvTKF3GWEnfxxmwmhUZ6M+6n/1nBAalSWQlw9Yw9NI35AxmTF3w6It4vvvImZ20CEx9 Tqg87cSOPhFxha4h9vVR7cSzwtk2/FGAscTpiDpSpVr4iLhLFOnESDYGF/NMLop2O20q 7jCw== X-Gm-Message-State: AOJu0YwFkU/xJjmODBda5SkLaRbz/F1psN6ndbsqbUiRHSLkC9kdDFSo NVyyo0rGoGuVl3zxVZFoihxqbOMp7EAkzjYl8bKfWp/ATiZ1QOkJI2LlxWvCAk/qdM5g0eU3LBc 5SQ== X-Received: by 2002:a5d:6d82:0:b0:33a:fe2f:6c96 with SMTP id l2-20020a5d6d82000000b0033afe2f6c96mr482588wrs.8.1706616685556; Tue, 30 Jan 2024 04:11:25 -0800 (PST) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id f9-20020a056000036900b00339307d9d31sm10569894wrf.112.2024.01.30.04.11.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 04:11:24 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Pierre-Emmanuel Patry Subject: [COMMITTED 047/101] gccrs: Emit an error on unsafe modules Date: Tue, 30 Jan 2024 13:07:03 +0100 Message-ID: <20240130121026.807464-50-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20240130121026.807464-2-arthur.cohen@embecosm.com> References: <20240130121026.807464-2-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham 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.30 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789519153069568487 X-GMAIL-MSGID: 1789519153069568487 From: Pierre-Emmanuel Patry An error should be emitted on unsafe modules during the AST validation pass as the syntax allows those even though they're not alowed later down the line. gcc/rust/ChangeLog: * ast/rust-item.h: Add safety getter to modules. * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Check a module's safety and emit an error when meeting an unsafe module. * checks/errors/rust-ast-validation.h: Add function prototype. * parse/rust-parse-impl.h (Parser::parse_module): Move the module locus to the first token instead of the mod keyword. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/ast/rust-item.h | 2 ++ gcc/rust/checks/errors/rust-ast-validation.cc | 10 ++++++++++ gcc/rust/checks/errors/rust-ast-validation.h | 1 + gcc/rust/parse/rust-parse-impl.h | 3 ++- 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 6c3715e1eeb..3bf023b3c5a 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -840,6 +840,8 @@ public: // Returns the kind of the module enum ModuleKind get_kind () const { return kind; } + Unsafety get_unsafety () const { return safety; } + // TODO: think of better way to do this - mutable getter seems dodgy const std::vector &get_inner_attrs () const { return inner_attrs; } std::vector &get_inner_attrs () { return inner_attrs; } diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc index dad7f5edded..4b209908f9e 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.cc +++ b/gcc/rust/checks/errors/rust-ast-validation.cc @@ -17,6 +17,7 @@ // . #include "rust-ast-validation.h" +#include "rust-common.h" #include "rust-diagnostics.h" #include "rust-item.h" #include "rust-keyword-values.h" @@ -136,4 +137,13 @@ ASTValidation::visit (AST::Trait &trait) AST::ContextualASTVisitor::visit (trait); } +void +ASTValidation::visit (AST::Module &module) +{ + if (module.get_unsafety () == Unsafety::Unsafe) + rust_error_at (module.get_locus (), "module cannot be declared unsafe"); + + AST::ContextualASTVisitor::visit (module); +} + } // namespace Rust diff --git a/gcc/rust/checks/errors/rust-ast-validation.h b/gcc/rust/checks/errors/rust-ast-validation.h index 1052168ea72..01d923ceff3 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.h +++ b/gcc/rust/checks/errors/rust-ast-validation.h @@ -34,6 +34,7 @@ public: void check (AST::Crate &crate) { AST::ContextualASTVisitor::visit (crate); } + virtual void visit (AST::Module &module); virtual void visit (AST::ConstantItem &const_item); virtual void visit (AST::Lifetime &lifetime); virtual void visit (AST::LoopLabel &label); diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 8087e0c2b94..f83cc122c89 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -2429,6 +2429,8 @@ std::unique_ptr Parser::parse_module (AST::Visibility vis, AST::AttrVec outer_attrs) { + location_t locus = lexer.peek_token ()->get_locus (); + Unsafety safety = Unsafety::Normal; if (lexer.peek_token ()->get_id () == UNSAFE) { @@ -2436,7 +2438,6 @@ Parser::parse_module (AST::Visibility vis, skip_token (UNSAFE); } - location_t locus = lexer.peek_token ()->get_locus (); skip_token (MOD); const_TokenPtr module_name = expect_token (IDENTIFIER);