Skip to content

Commit d780b20

Browse files
committed
Implement macro expansion in IfExpr, IfExprConseqElse, IfExprConseqIf, IfExprConseqIfLet.
Addresses #1141
1 parent b74044f commit d780b20

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,6 +1630,9 @@ AttrVisitor::visit (AST::IfExpr &expr)
16301630
// can't strip condition expr itself, but can strip sub-expressions
16311631
auto &condition_expr = expr.get_condition_expr ();
16321632
condition_expr->accept_vis (*this);
1633+
auto cond_fragment = expander.take_expanded_fragment (*this);
1634+
if (cond_fragment.should_expand ())
1635+
condition_expr = cond_fragment.take_expression_fragment ();
16331636
if (condition_expr->is_marked_for_strip ())
16341637
rust_error_at (condition_expr->get_locus (),
16351638
"cannot strip expression in this position - outer "
@@ -1657,6 +1660,9 @@ AttrVisitor::visit (AST::IfExprConseqElse &expr)
16571660
// can't strip condition expr itself, but can strip sub-expressions
16581661
auto &condition_expr = expr.get_condition_expr ();
16591662
condition_expr->accept_vis (*this);
1663+
auto cond_fragment = expander.take_expanded_fragment (*this);
1664+
if (cond_fragment.should_expand ())
1665+
condition_expr = cond_fragment.take_expression_fragment ();
16601666
if (condition_expr->is_marked_for_strip ())
16611667
rust_error_at (condition_expr->get_locus (),
16621668
"cannot strip expression in this position - outer "
@@ -1692,6 +1698,9 @@ AttrVisitor::visit (AST::IfExprConseqIf &expr)
16921698
// can't strip condition expr itself, but can strip sub-expressions
16931699
auto &condition_expr = expr.get_condition_expr ();
16941700
condition_expr->accept_vis (*this);
1701+
auto cond_fragment = expander.take_expanded_fragment (*this);
1702+
if (cond_fragment.should_expand ())
1703+
condition_expr = cond_fragment.take_expression_fragment ();
16951704
if (condition_expr->is_marked_for_strip ())
16961705
rust_error_at (condition_expr->get_locus (),
16971706
"cannot strip expression in this position - outer "
@@ -1727,6 +1736,9 @@ AttrVisitor::visit (AST::IfExprConseqIfLet &expr)
17271736
// can't strip condition expr itself, but can strip sub-expressions
17281737
auto &condition_expr = expr.get_condition_expr ();
17291738
condition_expr->accept_vis (*this);
1739+
auto cond_fragment = expander.take_expanded_fragment (*this);
1740+
if (cond_fragment.should_expand ())
1741+
condition_expr = cond_fragment.take_expression_fragment ();
17301742
if (condition_expr->is_marked_for_strip ())
17311743
rust_error_at (condition_expr->get_locus (),
17321744
"cannot strip expression in this position - outer "

gcc/testsuite/rust/compile/macro42.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// { dg-additional-options "-w -frust-cfg=A" }
2+
macro_rules! cfg {
3+
() => {{}};
4+
}
5+
6+
fn main() -> i32 {
7+
let mut res = 0;
8+
if cfg!(A) {
9+
res = 1;
10+
}
11+
12+
if cfg!(A) {
13+
res = 2;
14+
} else {
15+
res = 3;
16+
}
17+
18+
if cfg!(A) {
19+
res = 4;
20+
} else if cfg!(A) {
21+
res = 5;
22+
}
23+
24+
let res = if cfg!(A) {
25+
6
26+
} else {
27+
7
28+
};
29+
30+
return res;
31+
}

0 commit comments

Comments
 (0)