Skip to content

Commit 5a15694

Browse files
Merge #1063
1063: Handle :meta fragments properly r=CohenArthur a=CohenArthur This expands :meta fragments properly and allows us to strip assignment expressions Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2 parents a7e7234 + 7fa6e72 commit 5a15694

File tree

8 files changed

+63
-20
lines changed

8 files changed

+63
-20
lines changed

gcc/rust/ast/rust-expr.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,9 @@ class AssignmentExpr : public OperatorExpr
704704

705705
// Call OperatorExpr constructor to initialise left_expr
706706
AssignmentExpr (std::unique_ptr<Expr> value_to_assign_to,
707-
std::unique_ptr<Expr> value_to_assign, Location locus)
708-
: OperatorExpr (std::move (value_to_assign_to), std::vector<Attribute> (),
707+
std::unique_ptr<Expr> value_to_assign,
708+
std::vector<Attribute> outer_attribs, Location locus)
709+
: OperatorExpr (std::move (value_to_assign_to), std::move (outer_attribs),
709710
locus),
710711
right_expr (std::move (value_to_assign))
711712
{}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,12 @@ AttrVisitor::visit (AST::TypeCastExpr &expr)
628628
void
629629
AttrVisitor::visit (AST::AssignmentExpr &expr)
630630
{
631-
/* outer attributes never allowed before these. while cannot strip
632-
* two direct descendant expressions, can strip ones below that */
631+
expander.expand_cfg_attrs (expr.get_outer_attrs ());
632+
if (expander.fails_cfg_with_expand (expr.get_outer_attrs ()))
633+
{
634+
expr.mark_for_strip ();
635+
return;
636+
}
633637

634638
/* should have no possibility for outer attrs as would be parsed
635639
* with outer expr */

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ class AttrVisitor : public AST::ASTVisitor
7171
it = values.erase (it);
7272
for (auto &node : fragment.get_nodes ())
7373
{
74-
it = values.insert (it, extractor (node));
75-
it++;
74+
auto new_node = extractor (node);
75+
if (new_node != nullptr && !new_node->is_marked_for_strip ())
76+
{
77+
it = values.insert (it, std::move (new_node));
78+
it++;
79+
}
7680
}
7781
}
7882
else if (value->is_marked_for_strip ())

gcc/rust/expand/rust-macro-expand.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,7 @@ MacroExpander::match_fragment (Parser<MacroInvocLexer> &parser,
490490

491491
// is meta attributes?
492492
case AST::MacroFragSpec::META:
493-
// parser.parse_inner_attribute ?
494-
// parser.parse_outer_attribute ?
495-
// parser.parse_attribute_body ?
496-
// parser.parse_doc_comment ?
497-
gcc_unreachable ();
493+
parser.parse_attribute_body ();
498494
break;
499495

500496
case AST::MacroFragSpec::TT:

gcc/rust/parse/rust-parse-impl.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11719,7 +11719,7 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr_without_block ()
1171911719
{
1172011720
// should be expr without block
1172111721
std::unique_ptr<AST::ExprWithoutBlock> expr
11722-
= parse_expr_without_block ();
11722+
= parse_expr_without_block (std::move (outer_attrs));
1172311723

1172411724
if (lexer.peek_token ()->get_id () == SEMICOLON)
1172511725
{
@@ -11764,7 +11764,7 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr_without_block ()
1176411764
// FIXME: old code was good until composability was required
1176511765
// return parse_path_based_stmt_or_expr(std::move(outer_attrs));
1176611766
std::unique_ptr<AST::ExprWithoutBlock> expr
11767-
= parse_expr_without_block ();
11767+
= parse_expr_without_block (std::move (outer_attrs));
1176811768

1176911769
if (lexer.peek_token ()->get_id () == SEMICOLON)
1177011770
{
@@ -11787,7 +11787,7 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr_without_block ()
1178711787
* expression then make it statement if semi afterwards */
1178811788

1178911789
std::unique_ptr<AST::ExprWithoutBlock> expr
11790-
= parse_expr_without_block ();
11790+
= parse_expr_without_block (std::move (outer_attrs));
1179111791

1179211792
if (lexer.peek_token ()->get_id () == SEMICOLON)
1179311793
{
@@ -12462,7 +12462,7 @@ Parser<ManagedTokenSource>::parse_expr (int right_binding_power,
1246212462

1246312463
// parse null denotation (unary part of expression)
1246412464
std::unique_ptr<AST::Expr> expr
12465-
= null_denotation (current_token, std::move (outer_attrs), restrictions);
12465+
= null_denotation (current_token, {}, restrictions);
1246612466

1246712467
if (expr == nullptr)
1246812468
{
@@ -12477,8 +12477,8 @@ Parser<ManagedTokenSource>::parse_expr (int right_binding_power,
1247712477
current_token = lexer.peek_token ();
1247812478
lexer.skip_token ();
1247912479

12480-
expr = left_denotation (current_token, std::move (expr), AST::AttrVec (),
12481-
restrictions);
12480+
expr = left_denotation (current_token, std::move (expr),
12481+
std::move (outer_attrs), restrictions);
1248212482

1248312483
if (expr == nullptr)
1248412484
{
@@ -13811,7 +13811,7 @@ template <typename ManagedTokenSource>
1381113811
std::unique_ptr<AST::AssignmentExpr>
1381213812
Parser<ManagedTokenSource>::parse_assig_expr (
1381313813
const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13814-
AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13814+
AST::AttrVec outer_attrs, ParseRestrictions restrictions)
1381513815
{
1381613816
// parse RHS (as tok has already been consumed in parse_expression)
1381713817
std::unique_ptr<AST::Expr> right
@@ -13824,7 +13824,8 @@ Parser<ManagedTokenSource>::parse_assig_expr (
1382413824
Location locus = left->get_locus ();
1382513825

1382613826
return std::unique_ptr<AST::AssignmentExpr> (
13827-
new AST::AssignmentExpr (std::move (left), std::move (right), locus));
13827+
new AST::AssignmentExpr (std::move (left), std::move (right),
13828+
std::move (outer_attrs), locus));
1382813829
}
1382913830

1383013831
/* Returns the left binding power for the given CompoundAssignmentExpr type.

gcc/rust/parse/rust-parse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ template <typename ManagedTokenSource> class Parser
143143
AST::Visibility parse_visibility ();
144144
std::unique_ptr<AST::IdentifierPattern> parse_identifier_pattern ();
145145
std::unique_ptr<AST::TokenTree> parse_token_tree ();
146+
AST::Attribute parse_attribute_body ();
146147

147148
private:
148149
void skip_after_semicolon ();
@@ -162,7 +163,6 @@ template <typename ManagedTokenSource> class Parser
162163
AST::Attribute parse_inner_attribute ();
163164
AST::AttrVec parse_outer_attributes ();
164165
AST::Attribute parse_outer_attribute ();
165-
AST::Attribute parse_attribute_body ();
166166
std::unique_ptr<AST::AttrInput> parse_attr_input ();
167167
AST::Attribute parse_doc_comment ();
168168

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// { dg-additional-options "-w -frust-cfg=A" }
2+
3+
fn main() -> i32 {
4+
let mut a = 0;
5+
6+
#[cfg(A)]
7+
a = 3;
8+
9+
#[cfg(B)]
10+
a = 40;
11+
12+
a - 3
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// { dg-additional-options "-frust-cfg=A" }
2+
3+
macro_rules! attr {
4+
(#[$attr:meta] $s:stmt) => {
5+
#[$attr]
6+
$s;
7+
};
8+
}
9+
10+
fn main() -> i32 {
11+
let mut a = 0;
12+
13+
attr! {
14+
#[cfg(A)]
15+
a = 3
16+
};
17+
18+
attr! {
19+
#[cfg(B)]
20+
a = 40
21+
};
22+
23+
a - 3
24+
}

0 commit comments

Comments
 (0)