aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimplyTheOther <simplytheother@gmail.com>2021-02-03 18:23:00 +0800
committerSimplyTheOther <simplytheother@gmail.com>2021-02-03 18:23:00 +0800
commit877e7ac6c72608950fbe2ffde04142bbfb01b29d (patch)
treef89e30aecdd398745a61c69ed0a7f8051c63cf93
parent14aacf472dd089804302c5a2f41c1fc37ba5e824 (diff)
downloadgcc-877e7ac6c72608950fbe2ffde04142bbfb01b29d.zip
gcc-877e7ac6c72608950fbe2ffde04142bbfb01b29d.tar.gz
gcc-877e7ac6c72608950fbe2ffde04142bbfb01b29d.tar.bz2
Readded outer attributes to if and if let exprs
-rw-r--r--gcc/rust/ast/rust-expr.h52
-rw-r--r--gcc/rust/expand/rust-macro-expand.cc71
-rw-r--r--gcc/rust/parse/rust-parse-impl.h28
3 files changed, 101 insertions, 50 deletions
diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index b9a6c88..74a6473 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -4075,6 +4075,7 @@ class IfLetExpr;
// Base if expression with no "else" or "if let" AST node
class IfExpr : public ExprWithBlock
{
+ std::vector<Attribute> outer_attrs;
std::unique_ptr<Expr> condition;
std::unique_ptr<BlockExpr> if_block;
Location locus;
@@ -4083,14 +4084,14 @@ public:
std::string as_string () const override;
IfExpr (std::unique_ptr<Expr> condition, std::unique_ptr<BlockExpr> if_block,
- Location locus)
- : condition (std::move (condition)), if_block (std::move (if_block)),
+ std::vector<Attribute> outer_attrs, Location locus)
+ : outer_attrs (std::move (outer_attrs)), condition (std::move (condition)), if_block (std::move (if_block)),
locus (locus)
{}
// outer attributes are never allowed on IfExprs
// Copy constructor with clone
- IfExpr (IfExpr const &other) : ExprWithBlock (other), locus (other.locus)
+ IfExpr (IfExpr const &other) : ExprWithBlock (other), outer_attrs (other.outer_attrs), locus (other.locus)
{
// guard to prevent null dereference (only required if error state)
if (other.condition != nullptr)
@@ -4103,6 +4104,7 @@ public:
IfExpr &operator= (IfExpr const &other)
{
ExprWithBlock::operator= (other);
+ outer_attrs = other.outer_attrs;
locus = other.locus;
// guard to prevent null dereference (only required if error state)
@@ -4165,8 +4167,11 @@ public:
return if_block == nullptr && condition == nullptr;
}
- // this should never be called
- void set_outer_attrs (std::vector<Attribute> new_attrs) override { rust_assert (false); }
+ void set_outer_attrs (std::vector<Attribute> new_attrs) override { outer_attrs = std::move (new_attrs); }
+
+ // TODO: this mutable getter seems really dodgy. Think up better way.
+ const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
+ std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
protected:
// Base clone function but still concrete as concrete base class
@@ -4190,8 +4195,8 @@ public:
IfExprConseqElse (std::unique_ptr<Expr> condition,
std::unique_ptr<BlockExpr> if_block,
- std::unique_ptr<BlockExpr> else_block, Location locus)
- : IfExpr (std::move (condition), std::move (if_block), locus),
+ std::unique_ptr<BlockExpr> else_block, std::vector<Attribute> outer_attrs, Location locus)
+ : IfExpr (std::move (condition), std::move (if_block), std::move (outer_attrs), locus),
else_block (std::move (else_block))
{}
// again, outer attributes not allowed
@@ -4246,8 +4251,8 @@ public:
IfExprConseqIf (std::unique_ptr<Expr> condition,
std::unique_ptr<BlockExpr> if_block,
- std::unique_ptr<IfExpr> conseq_if_expr, Location locus)
- : IfExpr (std::move (condition), std::move (if_block), locus),
+ std::unique_ptr<IfExpr> conseq_if_expr, std::vector<Attribute> outer_attrs, Location locus)
+ : IfExpr (std::move (condition), std::move (if_block), std::move (outer_attrs), locus),
conseq_if_expr (std::move (conseq_if_expr))
{}
// outer attributes not allowed
@@ -4298,6 +4303,7 @@ protected:
// Basic "if let" expression AST node with no else
class IfLetExpr : public ExprWithBlock
{
+ std::vector<Attribute> outer_attrs;
std::vector<std::unique_ptr<Pattern> > match_arm_patterns; // inlined
std::unique_ptr<Expr> value;
std::unique_ptr<BlockExpr> if_block;
@@ -4307,7 +4313,7 @@ public:
std::string as_string () const override;
IfLetExpr (std::vector<std::unique_ptr<Pattern> > match_arm_patterns,
- std::unique_ptr<Expr> value, std::unique_ptr<BlockExpr> if_block,
+ std::unique_ptr<Expr> value, std::unique_ptr<BlockExpr> if_block, std::vector<Attribute> outer_attrs,
Location locus)
: match_arm_patterns (std::move (match_arm_patterns)),
value (std::move (value)), if_block (std::move (if_block)), locus (locus)
@@ -4316,7 +4322,7 @@ public:
// copy constructor with clone
IfLetExpr (IfLetExpr const &other)
- : ExprWithBlock (other), locus (other.locus)
+ : ExprWithBlock (other), outer_attrs (other.outer_attrs), locus (other.locus)
{
// guard to prevent null dereference (only required if error state)
if (other.value != nullptr)
@@ -4333,6 +4339,7 @@ public:
IfLetExpr &operator= (IfLetExpr const &other)
{
ExprWithBlock::operator= (other);
+ outer_attrs = other.outer_attrs;
locus = other.locus;
// guard to prevent null dereference (only required if error state)
@@ -4402,8 +4409,11 @@ public:
return match_arm_patterns;
}
- // this should never be called
- void set_outer_attrs (std::vector<Attribute> new_attrs) override { rust_assert (false); }
+ void set_outer_attrs (std::vector<Attribute> new_attrs) override { outer_attrs = std::move (new_attrs); }
+
+ // TODO: this mutable getter seems really dodgy. Think up better way.
+ const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
+ std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
protected:
/* Use covariance to implement clone function as returning this object rather
@@ -4430,9 +4440,9 @@ public:
IfExprConseqIfLet (std::unique_ptr<Expr> condition,
std::unique_ptr<BlockExpr> if_block,
- std::unique_ptr<IfLetExpr> conseq_if_let_expr,
+ std::unique_ptr<IfLetExpr> conseq_if_let_expr, std::vector<Attribute> outer_attrs,
Location locus)
- : IfExpr (std::move (condition), std::move (if_block), locus),
+ : IfExpr (std::move (condition), std::move (if_block), std::move (outer_attrs), locus),
if_let_expr (std::move (conseq_if_let_expr))
{}
// outer attributes not allowed
@@ -4487,9 +4497,9 @@ public:
IfLetExprConseqElse (
std::vector<std::unique_ptr<Pattern> > match_arm_patterns,
std::unique_ptr<Expr> value, std::unique_ptr<BlockExpr> if_block,
- std::unique_ptr<BlockExpr> else_block, Location locus)
+ std::unique_ptr<BlockExpr> else_block, std::vector<Attribute> outer_attrs, Location locus)
: IfLetExpr (std::move (match_arm_patterns), std::move (value),
- std::move (if_block), locus),
+ std::move (if_block), std::move (outer_attrs), locus),
else_block (std::move (else_block))
{}
// outer attributes not allowed
@@ -4546,9 +4556,9 @@ public:
IfLetExprConseqIf (std::vector<std::unique_ptr<Pattern> > match_arm_patterns,
std::unique_ptr<Expr> value,
std::unique_ptr<BlockExpr> if_block,
- std::unique_ptr<IfExpr> if_expr, Location locus)
+ std::unique_ptr<IfExpr> if_expr, std::vector<Attribute> outer_attrs, Location locus)
: IfLetExpr (std::move (match_arm_patterns), std::move (value),
- std::move (if_block), locus),
+ std::move (if_block), std::move (outer_attrs), locus),
if_expr (std::move (if_expr))
{}
// again, outer attributes not allowed
@@ -4604,9 +4614,9 @@ public:
IfLetExprConseqIfLet (
std::vector<std::unique_ptr<Pattern> > match_arm_patterns,
std::unique_ptr<Expr> value, std::unique_ptr<BlockExpr> if_block,
- std::unique_ptr<IfLetExpr> if_let_expr, Location locus)
+ std::unique_ptr<IfLetExpr> if_let_expr, std::vector<Attribute> outer_attrs, Location locus)
: IfLetExpr (std::move (match_arm_patterns), std::move (value),
- std::move (if_block), locus),
+ std::move (if_block), std::move (outer_attrs), locus),
if_let_expr (std::move (if_let_expr))
{}
// outer attributes not allowed
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index 2f4c010..cfd587b 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -1529,12 +1529,15 @@ public:
}
void visit (AST::IfExpr &expr) override
{
- // TODO: for if expressions, are attributes allowed if it is part of an expression statement?
- // if so, probably have to add a "does_expr_allow_attrs()" method to Expr and then don't move attrs.
- // otherwise, that may be useful anyway for better error messages.
+ // rust playground test shows that IfExpr does support outer attrs, at least when used as statement
- // NOTE: IfExpr literally doesn't support outer attrs, so no strip code
- // TODO: is this still true? can't find info on page
+ // initial strip test based on outer attrs
+ expander.expand_cfg_attrs (expr.get_outer_attrs ());
+ if (expander.fails_cfg_with_expand (expr.get_outer_attrs ()))
+ {
+ expr.mark_for_strip ();
+ return;
+ }
// can't strip condition expr itself, but can strip sub-expressions
auto &condition_expr = expr.get_condition_expr ();
@@ -1554,7 +1557,13 @@ public:
}
void visit (AST::IfExprConseqElse &expr) override
{
- // NOTE: IfExpr literally doesn't support outer attrs, so no strip code
+ // initial strip test based on outer attrs
+ expander.expand_cfg_attrs (expr.get_outer_attrs ());
+ if (expander.fails_cfg_with_expand (expr.get_outer_attrs ()))
+ {
+ expr.mark_for_strip ();
+ return;
+ }
// can't strip condition expr itself, but can strip sub-expressions
auto &condition_expr = expr.get_condition_expr ();
@@ -1582,7 +1591,13 @@ public:
}
void visit (AST::IfExprConseqIf &expr) override
{
- // NOTE: IfExpr literally doesn't support outer attrs, so no strip code
+ // initial strip test based on outer attrs
+ expander.expand_cfg_attrs (expr.get_outer_attrs ());
+ if (expander.fails_cfg_with_expand (expr.get_outer_attrs ()))
+ {
+ expr.mark_for_strip ();
+ return;
+ }
// can't strip condition expr itself, but can strip sub-expressions
auto &condition_expr = expr.get_condition_expr ();
@@ -1610,7 +1625,13 @@ public:
}
void visit (AST::IfExprConseqIfLet &expr) override
{
- // NOTE: IfExpr literally doesn't support outer attrs, so no strip code
+ // initial strip test based on outer attrs
+ expander.expand_cfg_attrs (expr.get_outer_attrs ());
+ if (expander.fails_cfg_with_expand (expr.get_outer_attrs ()))
+ {
+ expr.mark_for_strip ();
+ return;
+ }
// can't strip condition expr itself, but can strip sub-expressions
auto &condition_expr = expr.get_condition_expr ();
@@ -1639,8 +1660,14 @@ public:
}
void visit (AST::IfLetExpr &expr) override
{
- // NOTE: IfLetExpr literally doesn't support outer attrs, so no strip code
-
+ // initial strip test based on outer attrs
+ expander.expand_cfg_attrs (expr.get_outer_attrs ());
+ if (expander.fails_cfg_with_expand (expr.get_outer_attrs ()))
+ {
+ expr.mark_for_strip ();
+ return;
+ }
+
for (auto &pattern : expr.get_patterns ())
{
pattern->accept_vis (*this);
@@ -1667,7 +1694,13 @@ public:
}
void visit (AST::IfLetExprConseqElse &expr) override
{
- // NOTE: IfLetExpr literally doesn't support outer attrs, so no strip code
+ // initial strip test based on outer attrs
+ expander.expand_cfg_attrs (expr.get_outer_attrs ());
+ if (expander.fails_cfg_with_expand (expr.get_outer_attrs ()))
+ {
+ expr.mark_for_strip ();
+ return;
+ }
for (auto &pattern : expr.get_patterns ())
{
@@ -1703,7 +1736,13 @@ public:
}
void visit (AST::IfLetExprConseqIf &expr) override
{
- // NOTE: IfLetExpr literally doesn't support outer attrs, so no strip code
+ // initial strip test based on outer attrs
+ expander.expand_cfg_attrs (expr.get_outer_attrs ());
+ if (expander.fails_cfg_with_expand (expr.get_outer_attrs ()))
+ {
+ expr.mark_for_strip ();
+ return;
+ }
for (auto &pattern : expr.get_patterns ())
{
@@ -1739,7 +1778,13 @@ public:
}
void visit (AST::IfLetExprConseqIfLet &expr) override
{
- // NOTE: IfLetExpr literally doesn't support outer attrs, so no strip code
+ // initial strip test based on outer attrs
+ expander.expand_cfg_attrs (expr.get_outer_attrs ());
+ if (expander.fails_cfg_with_expand (expr.get_outer_attrs ()))
+ {
+ expr.mark_for_strip ();
+ return;
+ }
for (auto &pattern : expr.get_patterns ())
{
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index ffe4e91..3bea914 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -7503,10 +7503,8 @@ Parser<ManagedTokenSource>::parse_loop_label ()
template <typename ManagedTokenSource>
std::unique_ptr<AST::IfExpr>
Parser<ManagedTokenSource>::parse_if_expr (
- std::vector<AST::Attribute> outer_attrs ATTRIBUTE_UNUSED)
+ std::vector<AST::Attribute> outer_attrs)
{
- // TODO: make having outer attributes an error?
-
Location locus = lexer.peek_token ()->get_locus ();
skip_token (IF);
@@ -7525,7 +7523,7 @@ Parser<ManagedTokenSource>::parse_if_expr (
ParseRestrictions no_struct_expr;
no_struct_expr.can_be_struct_expr = false;
std::unique_ptr<AST::Expr> condition
- = parse_expr (std::vector<AST::Attribute> (), no_struct_expr);
+ = parse_expr ({}, no_struct_expr);
if (condition == nullptr)
{
rust_error_at (lexer.peek_token ()->get_locus (),
@@ -7550,7 +7548,7 @@ Parser<ManagedTokenSource>::parse_if_expr (
{
// single selection - end of if expression
return std::unique_ptr<AST::IfExpr> (
- new AST::IfExpr (std::move (condition), std::move (if_body), locus));
+ new AST::IfExpr (std::move (condition), std::move (if_body), std::move (outer_attrs), locus));
}
else
{
@@ -7579,7 +7577,7 @@ Parser<ManagedTokenSource>::parse_if_expr (
return std::unique_ptr<AST::IfExprConseqElse> (
new AST::IfExprConseqElse (std::move (condition),
std::move (if_body),
- std::move (else_body), locus));
+ std::move (else_body), std::move (outer_attrs), locus));
}
case IF: {
// multiple selection - else if or else if let
@@ -7601,7 +7599,7 @@ Parser<ManagedTokenSource>::parse_if_expr (
return std::unique_ptr<AST::IfExprConseqIfLet> (
new AST::IfExprConseqIfLet (std::move (condition),
std::move (if_body),
- std::move (if_let_expr), locus));
+ std::move (if_let_expr), std::move (outer_attrs), locus));
}
else
{
@@ -7619,7 +7617,7 @@ Parser<ManagedTokenSource>::parse_if_expr (
return std::unique_ptr<AST::IfExprConseqIf> (
new AST::IfExprConseqIf (std::move (condition),
std::move (if_body),
- std::move (if_expr), locus));
+ std::move (if_expr), std::move (outer_attrs), locus));
}
}
default:
@@ -7639,10 +7637,8 @@ Parser<ManagedTokenSource>::parse_if_expr (
template <typename ManagedTokenSource>
std::unique_ptr<AST::IfLetExpr>
Parser<ManagedTokenSource>::parse_if_let_expr (
- std::vector<AST::Attribute> outer_attrs ATTRIBUTE_UNUSED)
+ std::vector<AST::Attribute> outer_attrs)
{
- // TODO: make having outer attributes an error?
-
Location locus = lexer.peek_token ()->get_locus ();
skip_token (IF);
@@ -7679,7 +7675,7 @@ Parser<ManagedTokenSource>::parse_if_let_expr (
ParseRestrictions no_struct_expr;
no_struct_expr.can_be_struct_expr = false;
std::unique_ptr<AST::Expr> scrutinee_expr
- = parse_expr (std::vector<AST::Attribute> (), no_struct_expr);
+ = parse_expr ({}, no_struct_expr);
if (scrutinee_expr == nullptr)
{
rust_error_at (
@@ -7709,7 +7705,7 @@ Parser<ManagedTokenSource>::parse_if_let_expr (
return std::unique_ptr<AST::IfLetExpr> (
new AST::IfLetExpr (std::move (match_arm_patterns),
std::move (scrutinee_expr), std::move (if_let_body),
- locus));
+ std::move (outer_attrs), locus));
}
else
{
@@ -7739,7 +7735,7 @@ Parser<ManagedTokenSource>::parse_if_let_expr (
new AST::IfLetExprConseqElse (std::move (match_arm_patterns),
std::move (scrutinee_expr),
std::move (if_let_body),
- std::move (else_body), locus));
+ std::move (else_body), std::move (outer_attrs), locus));
}
case IF: {
// multiple selection - else if or else if let
@@ -7761,7 +7757,7 @@ Parser<ManagedTokenSource>::parse_if_let_expr (
return std::unique_ptr<AST::IfLetExprConseqIfLet> (
new AST::IfLetExprConseqIfLet (
std::move (match_arm_patterns), std::move (scrutinee_expr),
- std::move (if_let_body), std::move (if_let_expr), locus));
+ std::move (if_let_body), std::move (if_let_expr), std::move (outer_attrs), locus));
}
else
{
@@ -7780,7 +7776,7 @@ Parser<ManagedTokenSource>::parse_if_let_expr (
new AST::IfLetExprConseqIf (std::move (match_arm_patterns),
std::move (scrutinee_expr),
std::move (if_let_body),
- std::move (if_expr), locus));
+ std::move (if_expr), std::move (outer_attrs), locus));
}
}
default: