Skip to content

Commit 6c99a5a

Browse files
committed
parser: Move outer attrs properly intoto AssignmentExpr
AssignmentExpressions could not access their outer attributes properly, since they were being eagerly moved into the `IdentifierExpr` type they are based on. The base `OperatorExpr` class would thus end up with an empty vector of outer attributes
1 parent 2249a4d commit 6c99a5a

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
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/parse/rust-parse-impl.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11694,7 +11694,7 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr_without_block ()
1169411694
{
1169511695
// should be expr without block
1169611696
std::unique_ptr<AST::ExprWithoutBlock> expr
11697-
= parse_expr_without_block ();
11697+
= parse_expr_without_block (std::move (outer_attrs));
1169811698

1169911699
if (lexer.peek_token ()->get_id () == SEMICOLON)
1170011700
{
@@ -11739,7 +11739,7 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr_without_block ()
1173911739
// FIXME: old code was good until composability was required
1174011740
// return parse_path_based_stmt_or_expr(std::move(outer_attrs));
1174111741
std::unique_ptr<AST::ExprWithoutBlock> expr
11742-
= parse_expr_without_block ();
11742+
= parse_expr_without_block (std::move (outer_attrs));
1174311743

1174411744
if (lexer.peek_token ()->get_id () == SEMICOLON)
1174511745
{
@@ -11762,7 +11762,7 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr_without_block ()
1176211762
* expression then make it statement if semi afterwards */
1176311763

1176411764
std::unique_ptr<AST::ExprWithoutBlock> expr
11765-
= parse_expr_without_block ();
11765+
= parse_expr_without_block (std::move (outer_attrs));
1176611766

1176711767
if (lexer.peek_token ()->get_id () == SEMICOLON)
1176811768
{
@@ -12437,7 +12437,7 @@ Parser<ManagedTokenSource>::parse_expr (int right_binding_power,
1243712437

1243812438
// parse null denotation (unary part of expression)
1243912439
std::unique_ptr<AST::Expr> expr
12440-
= null_denotation (current_token, std::move (outer_attrs), restrictions);
12440+
= null_denotation (current_token, {}, restrictions);
1244112441

1244212442
if (expr == nullptr)
1244312443
{
@@ -12452,8 +12452,8 @@ Parser<ManagedTokenSource>::parse_expr (int right_binding_power,
1245212452
current_token = lexer.peek_token ();
1245312453
lexer.skip_token ();
1245412454

12455-
expr = left_denotation (current_token, std::move (expr), AST::AttrVec (),
12456-
restrictions);
12455+
expr = left_denotation (current_token, std::move (expr),
12456+
std::move (outer_attrs), restrictions);
1245712457

1245812458
if (expr == nullptr)
1245912459
{
@@ -13786,7 +13786,7 @@ template <typename ManagedTokenSource>
1378613786
std::unique_ptr<AST::AssignmentExpr>
1378713787
Parser<ManagedTokenSource>::parse_assig_expr (
1378813788
const_TokenPtr tok ATTRIBUTE_UNUSED, std::unique_ptr<AST::Expr> left,
13789-
AST::AttrVec outer_attrs ATTRIBUTE_UNUSED, ParseRestrictions restrictions)
13789+
AST::AttrVec outer_attrs, ParseRestrictions restrictions)
1379013790
{
1379113791
// parse RHS (as tok has already been consumed in parse_expression)
1379213792
std::unique_ptr<AST::Expr> right
@@ -13799,7 +13799,8 @@ Parser<ManagedTokenSource>::parse_assig_expr (
1379913799
Location locus = left->get_locus ();
1380013800

1380113801
return std::unique_ptr<AST::AssignmentExpr> (
13802-
new AST::AssignmentExpr (std::move (left), std::move (right), locus));
13802+
new AST::AssignmentExpr (std::move (left), std::move (right),
13803+
std::move (outer_attrs), locus));
1380313804
}
1380413805

1380513806
/* Returns the left binding power for the given CompoundAssignmentExpr type.
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+
}

0 commit comments

Comments
 (0)