From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id 83513385840D; Tue, 31 Jan 2023 13:15:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 83513385840D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675170912; bh=6SSy9n9ZUadOq0o2DRl94SO9D2kIS3Keq2ftmsiHTbE=; h=From:To:Subject:Date:From; b=myRgGKIHiCm749xB3PZK/h7196aRbTEhnZoKSuB00sVqU+vM1bygl1DnRZWXSXAeQ 4mNeFBVEvzo1BqqD7PUe8+T1wk4JeK8q+TDS2ixgZpduASVK3Jil4KvdI95ftlCTh7 8RBN8zSFsltjs2lLA29dOg/uDjPLQQxHJPtQI/Cg= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5545] gccrs: ast: Add better assertion on AST fragments X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/master X-Git-Oldrev: 55fb35c51b2ee6b107204b6a02193cbeef077d01 X-Git-Newrev: 3663d7ef66624b267114db5c59a959cdc7816aaa Message-Id: <20230131131512.83513385840D@sourceware.org> Date: Tue, 31 Jan 2023 13:15:12 +0000 (GMT) List-Id: https://gcc.gnu.org/g:3663d7ef66624b267114db5c59a959cdc7816aaa commit r13-5545-g3663d7ef66624b267114db5c59a959cdc7816aaa Author: Arthur Cohen Date: Mon Aug 29 14:59:18 2022 +0200 gccrs: ast: Add better assertion on AST fragments gcc/rust/ChangeLog: * ast/rust-ast.h: Improve assertions within ASTFragment API. Co-authored-by: philberty Diff: --- gcc/rust/ast/rust-ast.h | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 58fe2674479..9e1b8b11373 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -1819,7 +1819,7 @@ public: return true; } - std::string as_string () + std::string as_string () const { switch (kind) { @@ -1869,14 +1869,45 @@ private: */ bool is_single_fragment () const { return nodes.size () == 1; } - bool is_single_fragment (SingleASTNode::NodeType expected) const + + bool is_single_fragment_of_kind (SingleASTNode::NodeType expected) const { return is_single_fragment () && nodes[0].get_kind () == expected; } - bool is_single_fragment_kind (SingleASTNode::NodeType kind) const + void assert_single_fragment (SingleASTNode::NodeType expected) const { - return is_single_fragment () && nodes[0].get_kind () == kind; + static const std::map str_map = { + {SingleASTNode::NodeType::IMPL, "impl"}, + {SingleASTNode::NodeType::ITEM, "item"}, + {SingleASTNode::NodeType::TYPE, "type"}, + {SingleASTNode::NodeType::EXPRESSION, "expr"}, + {SingleASTNode::NodeType::STMT, "stmt"}, + {SingleASTNode::NodeType::EXTERN, "extern"}, + {SingleASTNode::NodeType::TRAIT, "trait"}, + {SingleASTNode::NodeType::TRAIT_IMPL, "trait impl"}, + }; + + auto actual = nodes[0].get_kind (); + auto fail = false; + + if (!is_single_fragment ()) + { + rust_error_at (Location (), "fragment is not single"); + fail = true; + } + + if (actual != expected) + { + rust_error_at ( + Location (), + "invalid fragment operation: expected %qs node, got %qs node", + str_map.find (expected)->second, + str_map.find (nodes[0].get_kind ())->second); + fail = true; + } + + rust_assert (!fail); } public: @@ -1920,23 +1951,23 @@ public: bool is_expression_fragment () const { - return is_single_fragment (SingleASTNode::NodeType::EXPRESSION); + return is_single_fragment_of_kind (SingleASTNode::NodeType::EXPRESSION); } bool is_type_fragment () const { - return is_single_fragment (SingleASTNode::NodeType::TYPE); + return is_single_fragment_of_kind (SingleASTNode::NodeType::TYPE); } std::unique_ptr take_expression_fragment () { - rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::EXPRESSION)); + assert_single_fragment (SingleASTNode::NodeType::EXPRESSION); return nodes[0].take_expr (); } std::unique_ptr take_type_fragment () { - rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::TYPE)); + assert_single_fragment (SingleASTNode::NodeType::TYPE); return nodes[0].take_type (); }