Skip to content

Commit e19e30e

Browse files
committed
Improve macro expansion for libcore
This should prevent issues like #3444 from affecting compilation of libcore 1.49. gcc/rust/ChangeLog: * ast/rust-expr.h (BorrowExpr::get_borrowed_expr_ptr): New member function. (ErrorPropagationExpr::get_propagating_expr_ptr): Likewise. (MethodCallExpr::get_receiver_expr_ptr): Likewise. (MatchExpr::get_scrutinee_expr_ptr): Likewise. * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Improve handling of expression child nodes. * expand/rust-expand-visitor.h (ExpandVisitor::visit): Add overloads. Signed-off-by: Owen Avery <[email protected]>
1 parent 85ec714 commit e19e30e

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

gcc/rust/ast/rust-expr.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,12 @@ class BorrowExpr : public OperatorExpr
414414
return *main_or_left_expr;
415415
}
416416

417+
std::unique_ptr<Expr> &get_borrowed_expr_ptr ()
418+
{
419+
rust_assert (main_or_left_expr != nullptr);
420+
return main_or_left_expr;
421+
}
422+
417423
bool has_borrow_expr () const { return main_or_left_expr != nullptr; }
418424

419425
bool get_is_mut () const { return mutability == Mutability::Mut; }
@@ -488,6 +494,12 @@ class ErrorPropagationExpr : public OperatorExpr
488494
return *main_or_left_expr;
489495
}
490496

497+
std::unique_ptr<Expr> &get_propagating_expr_ptr ()
498+
{
499+
rust_assert (main_or_left_expr != nullptr);
500+
return main_or_left_expr;
501+
}
502+
491503
Expr::Kind get_expr_kind () const override
492504
{
493505
return Expr::Kind::ErrorPropagation;
@@ -2293,6 +2305,12 @@ class MethodCallExpr : public ExprWithoutBlock
22932305
return *receiver;
22942306
}
22952307

2308+
std::unique_ptr<Expr> &get_receiver_expr_ptr ()
2309+
{
2310+
rust_assert (receiver != nullptr);
2311+
return receiver;
2312+
}
2313+
22962314
const PathExprSegment &get_method_name () const { return method_name; }
22972315
PathExprSegment &get_method_name () { return method_name; }
22982316

@@ -4892,6 +4910,12 @@ class MatchExpr : public ExprWithBlock
48924910
return *branch_value;
48934911
}
48944912

4913+
std::unique_ptr<Expr> &get_scrutinee_expr_ptr ()
4914+
{
4915+
rust_assert (branch_value != nullptr);
4916+
return branch_value;
4917+
}
4918+
48954919
const std::vector<MatchCase> &get_match_cases () const { return match_arms; }
48964920
std::vector<MatchCase> &get_match_cases () { return match_arms; }
48974921

gcc/rust/expand/rust-expand-visitor.cc

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,10 +541,16 @@ void
541541
ExpandVisitor::visit (AST::MetaItemPathExpr &)
542542
{}
543543

544+
void
545+
ExpandVisitor::visit (AST::BorrowExpr &expr)
546+
{
547+
maybe_expand_expr (expr.get_borrowed_expr_ptr ());
548+
}
549+
544550
void
545551
ExpandVisitor::visit (AST::ErrorPropagationExpr &expr)
546552
{
547-
visit (expr.get_propagating_expr ());
553+
maybe_expand_expr (expr.get_propagating_expr_ptr ());
548554
}
549555

550556
void
@@ -588,6 +594,14 @@ ExpandVisitor::visit (AST::GroupedExpr &expr)
588594
maybe_expand_expr (expr.get_expr_in_parens_ptr ());
589595
}
590596

597+
void
598+
ExpandVisitor::visit (AST::TupleExpr &expr)
599+
{
600+
// TODO: expand as multiple expressions?
601+
for (auto &elem : expr.get_tuple_elems ())
602+
maybe_expand_expr (elem);
603+
}
604+
591605
void
592606
ExpandVisitor::visit (AST::StructExprStruct &expr)
593607
{}
@@ -604,7 +618,7 @@ ExpandVisitor::visit (AST::CallExpr &expr)
604618
void
605619
ExpandVisitor::visit (AST::MethodCallExpr &expr)
606620
{
607-
visit (expr.get_receiver_expr ());
621+
maybe_expand_expr (expr.get_receiver_expr_ptr ());
608622

609623
for (auto &param : expr.get_params ())
610624
maybe_expand_expr (param);
@@ -679,7 +693,7 @@ ExpandVisitor::visit (AST::IfLetExprConseqElse &expr)
679693
void
680694
ExpandVisitor::visit (AST::MatchExpr &expr)
681695
{
682-
visit (expr.get_scrutinee_expr ());
696+
maybe_expand_expr (expr.get_scrutinee_expr_ptr ());
683697

684698
for (auto &match_case : expr.get_match_cases ())
685699
{

gcc/rust/expand/rust-expand-visitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,15 @@ class ExpandVisitor : public AST::DefaultASTVisitor
210210
void visit (AST::AttrInputMacro &) override;
211211
void visit (AST::MetaItemLitExpr &) override;
212212
void visit (AST::MetaItemPathExpr &) override;
213+
void visit (AST::BorrowExpr &) override;
213214
void visit (AST::ErrorPropagationExpr &expr) override;
214215
void visit (AST::ArithmeticOrLogicalExpr &expr) override;
215216
void visit (AST::ComparisonExpr &expr) override;
216217
void visit (AST::LazyBooleanExpr &expr) override;
217218
void visit (AST::AssignmentExpr &expr) override;
218219
void visit (AST::CompoundAssignmentExpr &expr) override;
219220
void visit (AST::GroupedExpr &expr) override;
221+
void visit (AST::TupleExpr &expr) override;
220222
void visit (AST::StructExprStruct &expr) override;
221223

222224
void visit (AST::CallExpr &expr) override;

0 commit comments

Comments
 (0)