Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2688 labelled block #2689

Merged
merged 5 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions gcc/rust/ast/rust-ast-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ std::unique_ptr<Expr>
AstBuilder::block (std::vector<std::unique_ptr<Stmt>> &&stmts,
std::unique_ptr<Expr> &&tail_expr)
{
return std::unique_ptr<Expr> (
new BlockExpr (std::move (stmts), std::move (tail_expr), {}, {}, loc, loc));
return std::unique_ptr<Expr> (new BlockExpr (std::move (stmts),
std::move (tail_expr), {}, {},
LoopLabel::error (), loc, loc));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from an API point of view, I'm not sure if LoopLabel::error here is very nice. something like empty would probably be better? sorry for nitpicking haha

}

std::unique_ptr<Stmt>
Expand Down
76 changes: 41 additions & 35 deletions gcc/rust/ast/rust-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@ namespace AST {
* "has_whatever" pairs with
* optional types (std::optional or boost::optional)? */

// Loop label expression AST node used with break and continue expressions
// TODO: inline?
class LoopLabel /*: public Node*/
{
Lifetime label; // or type LIFETIME_OR_LABEL
location_t locus;

NodeId node_id;

public:
std::string as_string () const;

LoopLabel (Lifetime loop_label, location_t locus = UNDEF_LOCATION)
: label (std::move (loop_label)), locus (locus),
node_id (Analysis::Mappings::get ()->get_next_node_id ())
{}

// Returns whether the LoopLabel is in an error state.
bool is_error () const { return label.is_error (); }

// Creates an error state LoopLabel.
static LoopLabel error () { return LoopLabel (Lifetime::error ()); }

location_t get_locus () const { return locus; }

Lifetime &get_lifetime () { return label; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Lifetime &get_lifetime () { return label; }
Lifetime &get_label () { return label; }

or

Suggested change
Lifetime &get_lifetime () { return label; }
Lifetime &get_name () { return label; }


NodeId get_node_id () const { return node_id; }
};

// AST node for an expression with an accompanying block - abstract
class ExprWithBlock : public Expr
{
Expand Down Expand Up @@ -2396,6 +2426,7 @@ class BlockExpr : public ExprWithBlock
std::vector<Attribute> inner_attrs;
std::vector<std::unique_ptr<Stmt> > statements;
std::unique_ptr<Expr> expr;
LoopLabel label;
location_t start_locus;
location_t end_locus;
bool marked_for_strip = false;
Expand All @@ -2412,19 +2443,21 @@ class BlockExpr : public ExprWithBlock
BlockExpr (std::vector<std::unique_ptr<Stmt> > block_statements,
std::unique_ptr<Expr> block_expr,
std::vector<Attribute> inner_attribs,
std::vector<Attribute> outer_attribs, location_t start_locus,
location_t end_locus)
std::vector<Attribute> outer_attribs, LoopLabel label,
location_t start_locus, location_t end_locus)
: outer_attrs (std::move (outer_attribs)),
inner_attrs (std::move (inner_attribs)),
statements (std::move (block_statements)), expr (std::move (block_expr)),
start_locus (start_locus), end_locus (end_locus)
label (std::move (label)), start_locus (start_locus),
end_locus (end_locus)
{}

// Copy constructor with clone
BlockExpr (BlockExpr const &other)
: ExprWithBlock (other), outer_attrs (other.outer_attrs),
inner_attrs (other.inner_attrs), start_locus (other.start_locus),
end_locus (other.end_locus), marked_for_strip (other.marked_for_strip)
inner_attrs (other.inner_attrs), label (other.label),
start_locus (other.start_locus), end_locus (other.end_locus),
marked_for_strip (other.marked_for_strip)
{
// guard to protect from null pointer dereference
if (other.expr != nullptr)
Expand Down Expand Up @@ -2524,6 +2557,9 @@ class BlockExpr : public ExprWithBlock
outer_attrs = std::move (new_attrs);
}

bool has_label () { return !label.is_error (); }
LoopLabel &get_label () { return label; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -3352,36 +3388,6 @@ class UnsafeBlockExpr : public ExprWithBlock
}
};

// Loop label expression AST node used with break and continue expressions
// TODO: inline?
class LoopLabel /*: public Node*/
{
Lifetime label; // or type LIFETIME_OR_LABEL
location_t locus;

NodeId node_id;

public:
std::string as_string () const;

LoopLabel (Lifetime loop_label, location_t locus = UNDEF_LOCATION)
: label (std::move (loop_label)), locus (locus),
node_id (Analysis::Mappings::get ()->get_next_node_id ())
{}

// Returns whether the LoopLabel is in an error state.
bool is_error () const { return label.is_error (); }

// Creates an error state LoopLabel.
static LoopLabel error () { return LoopLabel (Lifetime::error ()); }

location_t get_locus () const { return locus; }

Lifetime &get_lifetime () { return label; }

NodeId get_node_id () const { return node_id; }
};

// Base loop expression AST node - aka LoopExpr
class BaseLoopExpr : public ExprWithBlock
{
Expand Down
6 changes: 6 additions & 0 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@ CompileExpr::visit (HIR::IfExprConseqElse &expr)
void
CompileExpr::visit (HIR::BlockExpr &expr)
{
if (expr.has_label ())
{
rust_error_at (expr.get_locus (), "labeled blocks are not supported");
return;
}

TyTy::BaseType *block_tyty = nullptr;
if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
&block_tyty))
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/expand/rust-derive-clone.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ std::unique_ptr<TraitImplItem>
DeriveClone::clone_fn (std::unique_ptr<Expr> &&clone_expr)
{
auto block = std::unique_ptr<BlockExpr> (
new BlockExpr ({}, std::move (clone_expr), {}, {}, loc, loc));
new BlockExpr ({}, std::move (clone_expr), {}, {}, AST::LoopLabel::error (),
loc, loc));
auto big_self_type = builder.single_type_path ("Self");

return std::unique_ptr<TraitImplItem> (
Expand Down
6 changes: 4 additions & 2 deletions gcc/rust/hir/rust-ast-lower.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ ASTLowering::go ()
void
ASTLoweringBlock::visit (AST::BlockExpr &expr)
{
auto label = lower_loop_label (expr.get_label ());

std::vector<std::unique_ptr<HIR::Stmt>> block_stmts;
bool block_did_terminate = false;

Expand Down Expand Up @@ -143,8 +145,8 @@ ASTLoweringBlock::visit (AST::BlockExpr &expr)
= new HIR::BlockExpr (mapping, std::move (block_stmts),
std::unique_ptr<HIR::ExprWithoutBlock> (tail_expr),
tail_reachable, expr.get_inner_attrs (),
expr.get_outer_attrs (), expr.get_start_locus (),
expr.get_end_locus ());
expr.get_outer_attrs (), label,
expr.get_start_locus (), expr.get_end_locus ());

terminated = block_did_terminate;
}
Expand Down
70 changes: 37 additions & 33 deletions gcc/rust/hir/tree/rust-hir-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@
namespace Rust {
namespace HIR {

// Loop label expression HIR node used with break and continue expressions
// TODO: inline?
class LoopLabel /*: public Node*/
{
Lifetime label; // or type LIFETIME_OR_LABEL

location_t locus;

Analysis::NodeMapping mappings;

public:
std::string as_string () const;

LoopLabel (Analysis::NodeMapping mapping, Lifetime loop_label,
location_t locus)
: label (std::move (loop_label)), locus (locus), mappings (mapping)
{}

// Returns whether the LoopLabel is in an error state.
bool is_error () const { return label.is_error (); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, should be is_empty() IMO


location_t get_locus () const { return locus; }

Analysis::NodeMapping &get_mappings () { return mappings; }

Lifetime &get_lifetime () { return label; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

};

// HIR node for an expression with an accompanying block - abstract
class ExprWithBlock : public Expr
{
Expand Down Expand Up @@ -2121,6 +2149,7 @@ class BlockExpr : public ExprWithBlock, public WithInnerAttrs
std::vector<std::unique_ptr<Stmt> > statements;
std::unique_ptr<Expr> expr;
bool tail_reachable;
LoopLabel label;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to use tl::optional<LoopLabel> instead of label.is_error() ? This would help enforce a label exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me, but probably not in this PR - the same logic is used for loops (was taken from there) and it would make more sense to change it everywhere at once, otherwise it is more confusing. Agree @P-E-P ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Let's merge this then 👍

location_t start_locus;
location_t end_locus;

Expand All @@ -2140,19 +2169,19 @@ class BlockExpr : public ExprWithBlock, public WithInnerAttrs
std::vector<std::unique_ptr<Stmt> > block_statements,
std::unique_ptr<Expr> block_expr, bool tail_reachable,
AST::AttrVec inner_attribs, AST::AttrVec outer_attribs,
location_t start_locus, location_t end_locus)
LoopLabel label, location_t start_locus, location_t end_locus)
: ExprWithBlock (std::move (mappings), std::move (outer_attribs)),
WithInnerAttrs (std::move (inner_attribs)),
statements (std::move (block_statements)), expr (std::move (block_expr)),
tail_reachable (tail_reachable), start_locus (start_locus),
end_locus (end_locus)
tail_reachable (tail_reachable), label (std::move (label)),
start_locus (start_locus), end_locus (end_locus)
{}

// Copy constructor with clone
BlockExpr (BlockExpr const &other)
: ExprWithBlock (other), /*statements(other.statements),*/
WithInnerAttrs (other.inner_attrs), start_locus (other.start_locus),
end_locus (other.end_locus)
WithInnerAttrs (other.inner_attrs), label (other.label),
start_locus (other.start_locus), end_locus (other.end_locus)
{
// guard to protect from null pointer dereference
if (other.expr != nullptr)
Expand Down Expand Up @@ -2211,6 +2240,9 @@ class BlockExpr : public ExprWithBlock, public WithInnerAttrs
return ExprType::Block;
}

bool has_label () const { return !label.is_error (); }
LoopLabel &get_label () { return label; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -2835,34 +2867,6 @@ class UnsafeBlockExpr : public ExprWithBlock
}
};

// Loop label expression HIR node used with break and continue expressions
// TODO: inline?
class LoopLabel /*: public Node*/
{
Lifetime label; // or type LIFETIME_OR_LABEL

location_t locus;

Analysis::NodeMapping mappings;

public:
std::string as_string () const;

LoopLabel (Analysis::NodeMapping mapping, Lifetime loop_label,
location_t locus)
: label (std::move (loop_label)), locus (locus), mappings (mapping)
{}

// Returns whether the LoopLabel is in an error state.
bool is_error () const { return label.is_error (); }

location_t get_locus () const { return locus; }

Analysis::NodeMapping &get_mappings () { return mappings; }

Lifetime &get_lifetime () { return label; }
};

// Base loop expression HIR node - aka LoopExpr
class BaseLoopExpr : public ExprWithBlock
{
Expand Down
12 changes: 8 additions & 4 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7359,6 +7359,7 @@ Parser<ManagedTokenSource>::parse_expr_stmt (AST::AttrVec outer_attrs,
template <typename ManagedTokenSource>
std::unique_ptr<AST::BlockExpr>
Parser<ManagedTokenSource>::parse_block_expr (AST::AttrVec outer_attrs,
AST::LoopLabel label,
location_t pratt_parsed_loc)
{
location_t locus = pratt_parsed_loc;
Expand Down Expand Up @@ -7425,8 +7426,8 @@ Parser<ManagedTokenSource>::parse_block_expr (AST::AttrVec outer_attrs,

return std::unique_ptr<AST::BlockExpr> (
new AST::BlockExpr (std::move (stmts), std::move (expr),
std::move (inner_attrs), std::move (outer_attrs), locus,
end_locus));
std::move (inner_attrs), std::move (outer_attrs),
std::move (label), locus, end_locus));
}

/* Parses a "grouped" expression (expression in parentheses), used to control
Expand Down Expand Up @@ -8367,7 +8368,7 @@ Parser<ManagedTokenSource>::parse_for_loop_expr (AST::AttrVec outer_attrs,

// Parses a loop expression with label (any kind of loop - disambiguates).
template <typename ManagedTokenSource>
std::unique_ptr<AST::BaseLoopExpr>
std::unique_ptr<AST::Expr>
Parser<ManagedTokenSource>::parse_labelled_loop_expr (const_TokenPtr tok,
AST::AttrVec outer_attrs)
{
Expand Down Expand Up @@ -8419,6 +8420,8 @@ Parser<ManagedTokenSource>::parse_labelled_loop_expr (const_TokenPtr tok,
return parse_while_loop_expr (std::move (outer_attrs),
std::move (label));
}
case LEFT_CURLY:
return parse_block_expr (std::move (outer_attrs), std::move (label));
default:
// error
add_error (Error (t->get_locus (),
Expand Down Expand Up @@ -12537,7 +12540,8 @@ Parser<ManagedTokenSource>::null_denotation_not_path (
return parse_continue_expr (std::move (outer_attrs), tok->get_locus ());
case LEFT_CURLY:
// ok - this is an expression with block for once.
return parse_block_expr (std::move (outer_attrs), tok->get_locus ());
return parse_block_expr (std::move (outer_attrs),
AST::LoopLabel::error (), tok->get_locus ());
case IF:
// if or if let, so more lookahead to find out
if (lexer.peek_token ()->get_id () == LET)
Expand Down
7 changes: 4 additions & 3 deletions gcc/rust/parse/rust-parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ template <typename ManagedTokenSource> class Parser

std::unique_ptr<AST::BlockExpr>
parse_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
AST::LoopLabel label = AST::LoopLabel::error (),
location_t pratt_parsed_loc = UNKNOWN_LOCATION);

bool is_macro_rules_def (const_TokenPtr t);
Expand Down Expand Up @@ -590,9 +591,9 @@ template <typename ManagedTokenSource> class Parser
AST::MatchArm parse_match_arm ();
std::vector<std::unique_ptr<AST::Pattern> >
parse_match_arm_patterns (TokenId end_token_id);
std::unique_ptr<AST::BaseLoopExpr>
parse_labelled_loop_expr (const_TokenPtr tok,
AST::AttrVec outer_attrs = AST::AttrVec ());
std::unique_ptr<AST::Expr> parse_labelled_loop_expr (const_TokenPtr tok,
AST::AttrVec outer_attrs
= AST::AttrVec ());
AST::LoopLabel parse_loop_label (const_TokenPtr tok);
std::unique_ptr<AST::AsyncBlockExpr>
parse_async_block_expr (AST::AttrVec outer_attrs = AST::AttrVec ());
Expand Down
22 changes: 22 additions & 0 deletions gcc/rust/resolve/rust-ast-resolve-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,28 @@ ResolveExpr::visit (AST::BlockExpr &expr)
resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
resolver->push_new_label_rib (resolver->get_type_scope ().peek ());

if (expr.has_label ())
{
auto label = expr.get_label ();
if (label.get_lifetime ().get_lifetime_type ()
!= AST::Lifetime::LifetimeType::NAMED)
{
rust_error_at (label.get_locus (),
"Labels must be a named lifetime value");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Labels must be a named lifetime value");
"labels must be a named lifetime value");

for consistency :)

return;
}

auto label_name = label.get_lifetime ().get_lifetime_name ();
auto label_lifetime_node_id = label.get_lifetime ().get_node_id ();
resolver->get_label_scope ().insert (
CanonicalPath::new_seg (label.get_node_id (), label_name),
label_lifetime_node_id, label.get_locus (), false, Rib::ItemType::Label,
[&] (const CanonicalPath &, NodeId, location_t locus) -> void {
rust_error_at (label.get_locus (), "label redefined multiple times");
rust_error_at (locus, "was defined here");
});
}

for (auto &s : expr.get_statements ())
{
if (s->is_item ())
Expand Down
Loading
Loading