From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by sourceware.org (Postfix) with ESMTPS id DA7C93894C21 for ; Tue, 31 Jan 2023 13:21:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DA7C93894C21 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-x433.google.com with SMTP id q10so14190775wrm.4 for ; Tue, 31 Jan 2023 05:21:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=4riLGcGE1q08h8oGe/Gt/bWI6HYP9qRLjFrK8+fFPaU=; b=Ub37ArT4Vl0eJR31HbnJP9xAcFo9CKEefkBhZy7wJtrUmAnJ7prbaWqzJPZ1ugbpcT kd32cF0bzje0flbZcyEQB2duIMTqCDmt99LU4C8eXX/qD86HL/kHQtoGZhd+cgGU2LtP /u/oLr4X8Y8itAaVmxpW26WY0DkrY/o2ixYTjpVqw0mEr0Kv3hx9DsXYXrS5LgR5EzoO mg7vLDyT7Mx83wt4Tks3q1uWJZ2ogY60hYaLObImjb3lJDsovIKxi5VCMX8WLdEx2KKw N1oNFXV5L5nZndx3v9BzP3zP5hga724515e5W+eUq57KmnZkFCWT+O159xGKBH4A1eb/ yhLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4riLGcGE1q08h8oGe/Gt/bWI6HYP9qRLjFrK8+fFPaU=; b=jD6bvfG7ueLacIZfxCUHMXha/hpgGieDMQTP3FFOWKrP+Sjcb6H/lgvsscysJr9RRN FNiebb9On814sO7sKjgru1pkeZTxj8CpDKQycVr5ZxNFRx4LZVqbiwtxBzqir0cD1cVT KKR+FWFGVWdLZXGWm2ZYGIcKvQgbw5wcHiO1tacSd8TSU4tKPpdMBKb+JDGLW72dLp3K 60TYD76M2xN1luWs75mB7iN/Vf8ef/uxpehJ2moYyWu69AvH0pM5oTwlOGDm0euZ0zTM YXW+ViK7DKMFrz5wzlklJyolPP3Nsa6RNt8yVwgqDQgEJpWIkKphA6pnWBcBHQ0kDDh1 Uw+A== X-Gm-Message-State: AO0yUKW1YFrRQh3tUsBMl68NZxjK402DoZU+QHl5A6JQraRnUy/VOlvt dXHjeBMDGvl1wsltN3v/R0Kq X-Google-Smtp-Source: AK7set8QdGwB8XNNCDDkz3jkVPRJVxkETo5LgdjAsX5KlQRt/D0/5AYZLNdwkYnO1lagGDZOC87ImA== X-Received: by 2002:a5d:60cc:0:b0:2c1:6845:ac37 with SMTP id x12-20020a5d60cc000000b002c16845ac37mr2193143wrt.49.1675171279666; Tue, 31 Jan 2023 05:21:19 -0800 (PST) Received: from platypus.lan ([2001:861:5e4c:3bb0:6424:328a:1734:3249]) by smtp.gmail.com with ESMTPSA id m3-20020a056000180300b002755e301eeasm14793912wrh.100.2023.01.31.05.21.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Jan 2023 05:21:19 -0800 (PST) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [COMMITTED] gccrs: ast: Only expand expressions and types if the kind is right Date: Tue, 31 Jan 2023 14:25:04 +0100 Message-Id: <20230131132504.661840-1-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-15.7 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: gcc/rust/ChangeLog: * ast/rust-ast.h: Add assertions and accessors for fragment nodes. * expand/rust-attribute-visitor.cc (AttrVisitor::visit): Fix expansion context typo when visiting `InherentImpl` items. (AttrVisitor::maybe_expand_expr): Use new Fragment accessor to fetch properly typed node. (AttrVisitor::maybe_expand_type): Likewise. * expand/rust-macro-expand.cc (transcribe_type): Emit parse errors when trying to parse a type. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/rust/ast/rust-ast.h | 15 +++++++++++++++ gcc/rust/expand/rust-attribute-visitor.cc | 11 +++++++---- gcc/rust/expand/rust-macro-expand.cc | 4 +++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 213208efb56..58fe2674479 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -24,6 +24,7 @@ #include "rust-hir-map.h" #include "rust-token.h" #include "rust-location.h" +#include "rust-diagnostics.h" namespace Rust { // TODO: remove typedefs and make actual types for these @@ -1868,6 +1869,10 @@ private: */ bool is_single_fragment () const { return nodes.size () == 1; } + bool is_single_fragment (SingleASTNode::NodeType expected) const + { + return is_single_fragment () && nodes[0].get_kind () == expected; + } bool is_single_fragment_kind (SingleASTNode::NodeType kind) const { @@ -1913,6 +1918,16 @@ public: bool should_expand () const { return !is_error (); } + bool is_expression_fragment () const + { + return is_single_fragment (SingleASTNode::NodeType::EXPRESSION); + } + + bool is_type_fragment () const + { + return is_single_fragment (SingleASTNode::NodeType::TYPE); + } + std::unique_ptr take_expression_fragment () { rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::EXPRESSION)); diff --git a/gcc/rust/expand/rust-attribute-visitor.cc b/gcc/rust/expand/rust-attribute-visitor.cc index 15aedbfa668..673f0432a30 100644 --- a/gcc/rust/expand/rust-attribute-visitor.cc +++ b/gcc/rust/expand/rust-attribute-visitor.cc @@ -2662,7 +2662,7 @@ AttrVisitor::visit (AST::InherentImpl &impl) for (auto ¶m : impl.get_generic_params ()) param->accept_vis (*this); - expander.push_context (MacroExpander::ContextType::TYPE); + expander.push_context (MacroExpander::ContextType::ITEM); auto &type = impl.get_type (); type->accept_vis (*this); @@ -2706,7 +2706,7 @@ AttrVisitor::visit (AST::TraitImpl &impl) for (auto ¶m : impl.get_generic_params ()) param->accept_vis (*this); - expander.push_context (MacroExpander::ContextType::TYPE); + expander.push_context (MacroExpander::ContextType::ITEM); auto &type = impl.get_type (); type->accept_vis (*this); @@ -3427,11 +3427,13 @@ AttrVisitor::visit (AST::BareFunctionType &type) // no where clause, apparently } + void AttrVisitor::maybe_expand_expr (std::unique_ptr &expr) { auto final_fragment = expand_macro_fragment_recursive (); - if (final_fragment.should_expand ()) + if (final_fragment.should_expand () + && final_fragment.is_expression_fragment ()) expr = final_fragment.take_expression_fragment (); } @@ -3439,7 +3441,8 @@ void AttrVisitor::maybe_expand_type (std::unique_ptr &type) { auto final_fragment = expand_macro_fragment_recursive (); - if (final_fragment.should_expand ()) + if (final_fragment.should_expand () && final_fragment.is_type_fragment ()) type = final_fragment.take_type_fragment (); } + } // namespace Rust diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index ed1b838c987..c68faba86ad 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -864,7 +864,9 @@ transcribe_expression (Parser &parser) static AST::ASTFragment transcribe_type (Parser &parser) { - auto type = parser.parse_type (); + auto type = parser.parse_type (true); + for (auto err : parser.get_errors ()) + err.emit_error (); return AST::ASTFragment ({std::move (type)}); } -- 2.39.1