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: 1892891 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=embecosm.com header.i=@embecosm.com header.a=rsa-sha256 header.s=google header.b=gMkmYAi2; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TPPrW0SqZz23dQ for ; Tue, 30 Jan 2024 23:39:55 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E53CA386C580 for ; Tue, 30 Jan 2024 12:39:52 +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-Google-Smtp-Source: AGHT+IFgRh7a4Ybe+dAqDvUc/LRR4b+eCqbdS2WTexe4f/R2/HWCd18Kw7dsnoHWnRggjILM3NmYvg== 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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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);