From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id BC48C3858287 for ; Tue, 30 Jan 2024 12:11:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BC48C3858287 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 BC48C3858287 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::42d 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-x42d.google.com with SMTP id ffacd0b85a97d-3394bec856fso2792175f8f.0 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=FpJi39+rGtGWYmm9T8hri01uGsud1U7k0dVXTc9MR2A1vUdxeUovWniDyd28XVL/0V OUUDaBqh0QO9p5v+JllfJrP269kbvhxP6aQjtcbTQ2uHba3j+M1/PnsJTt6osyW0koKA J9xW1HbX7xpDxskXf7pwlkDpIFI0p+UMRkYwNbh0enp6gxfKsunQreW17bijQf32XRqG J1b6d7deC8jRxya/VBuPo3nQf3ugIK5xnY01z7pH4kqahFakTLvmlX0t/ZGOxxIncMM6 NY2AB/vId9WMedR+dxLSD7v38tE9HKC0azNwVKUw3EcWPvEZuduh+bF+YLDq1WbEGIvL 1NHQ== X-Gm-Message-State: AOJu0YzMHBxO5DsrK9BGQ5RCTQ4q5l2kniisWbu1iEEA9m/FzVgaU+iM 0sU5jP+0VljeS/oqTO/doZFoxSt7czaouziEb1YKuVtGa4BbBO6CY94JTzB/gw== 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> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.2 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 List-Id: 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); -- 2.42.1