aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-08-31 09:15:27 +0000
committerGitHub <noreply@github.com>2022-08-31 09:15:27 +0000
commit45f80a2d86c3cb6210f00547eee3e9af59a62fdf (patch)
tree1d032370d7b397f787fc001b239550a5d7ac98d2 /gcc
parent017be6aeb8f3803a26cf11ef633ce24fca7749b8 (diff)
parentfcf6fea382f65930cd4f32f7661bc5d64d1559b4 (diff)
downloadgcc-45f80a2d86c3cb6210f00547eee3e9af59a62fdf.zip
gcc-45f80a2d86c3cb6210f00547eee3e9af59a62fdf.tar.gz
gcc-45f80a2d86c3cb6210f00547eee3e9af59a62fdf.tar.bz2
Merge #1514
1514: Fix SEGV in transcribe_type when compiling libcore 1.29 r=CohenArthur a=CohenArthur Needs #1513, Skip reviewing the first commit Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/ast/rust-ast-dump.h16
-rw-r--r--gcc/rust/ast/rust-ast.h56
-rw-r--r--gcc/rust/expand/rust-attribute-visitor.cc11
-rw-r--r--gcc/rust/expand/rust-macro-expand.cc4
4 files changed, 77 insertions, 10 deletions
diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h
index 7dd38fe..a72bd4d 100644
--- a/gcc/rust/ast/rust-ast-dump.h
+++ b/gcc/rust/ast/rust-ast-dump.h
@@ -52,6 +52,22 @@ public:
void go (AST::Crate &crate);
void go (AST::Item &item);
+ /**
+ * Use the AST Dump as a debugging tool
+ */
+ template <typename T> static void debug (T &instance)
+ {
+ auto dump = Dump (std::cerr);
+
+ std::cerr << '\n';
+ instance.accept_vis (dump);
+ std::cerr << '\n';
+ }
+ template <typename T> static void debug (std::unique_ptr<T> &instance)
+ {
+ debug (*instance);
+ }
+
private:
std::ostream &stream;
Indent indentation;
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 461a246..e9e16e7 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
@@ -1818,7 +1819,7 @@ public:
return true;
}
- std::string as_string ()
+ std::string as_string () const
{
switch (kind)
{
@@ -1869,9 +1870,44 @@ private:
bool is_single_fragment () const { return nodes.size () == 1; }
- bool is_single_fragment_kind (SingleASTNode::NodeType kind) const
+ bool is_single_fragment_of_kind (SingleASTNode::NodeType expected) const
{
- return is_single_fragment () && nodes[0].get_kind () == kind;
+ return is_single_fragment () && nodes[0].get_kind () == expected;
+ }
+
+ void assert_single_fragment (SingleASTNode::NodeType expected) const
+ {
+ static const std::map<SingleASTNode::NodeType, const char *> 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:
@@ -1913,15 +1949,25 @@ public:
bool should_expand () const { return !is_error (); }
+ bool is_expression_fragment () const
+ {
+ return is_single_fragment_of_kind (SingleASTNode::NodeType::EXPRESSION);
+ }
+
+ bool is_type_fragment () const
+ {
+ return is_single_fragment_of_kind (SingleASTNode::NodeType::TYPE);
+ }
+
std::unique_ptr<Expr> 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<Type> take_type_fragment ()
{
- rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::TYPE));
+ assert_single_fragment (SingleASTNode::NodeType::TYPE);
return nodes[0].take_type ();
}
diff --git a/gcc/rust/expand/rust-attribute-visitor.cc b/gcc/rust/expand/rust-attribute-visitor.cc
index 8016f94..12b8cec 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 &param : 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 &param : 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<AST::Expr> &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<AST::Type> &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 eef9194..dd8d468 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -864,7 +864,9 @@ transcribe_expression (Parser<MacroInvocLexer> &parser)
static AST::ASTFragment
transcribe_type (Parser<MacroInvocLexer> &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)});
}