Skip to content

Commit

Permalink
Use a loop label in break expression
Browse files Browse the repository at this point in the history
Break expression were using a raw lifetime value instead of a loop label
this behavior would have lead to some errors in ast validation.

gcc/rust/ChangeLog:

	* ast/rust-expr.h (class BreakExpr): Change Lifetime to LoopLabel.
	* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Lower lifetime
	inside the label instead.
	* resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit): Resolve the
	inner lifetime.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
  • Loading branch information
P-E-P committed Nov 8, 2023
1 parent c904e25 commit bf2244e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
6 changes: 3 additions & 3 deletions gcc/rust/ast/rust-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2727,7 +2727,7 @@ class ContinueExpr : public ExprWithoutBlock
class BreakExpr : public ExprWithoutBlock
{
std::vector<Attribute> outer_attrs;
Lifetime label;
LoopLabel label;
std::unique_ptr<Expr> break_expr;
location_t locus;

Expand All @@ -2745,7 +2745,7 @@ class BreakExpr : public ExprWithoutBlock
bool has_break_expr () const { return break_expr != nullptr; }

// Constructor for a break expression
BreakExpr (Lifetime break_label, std::unique_ptr<Expr> expr_in_break,
BreakExpr (LoopLabel break_label, std::unique_ptr<Expr> expr_in_break,
std::vector<Attribute> outer_attribs, location_t locus)
: outer_attrs (std::move (outer_attribs)), label (std::move (break_label)),
break_expr (std::move (expr_in_break)), locus (locus)
Expand Down Expand Up @@ -2807,7 +2807,7 @@ class BreakExpr : public ExprWithoutBlock
outer_attrs = std::move (new_attrs);
}

Lifetime &get_label () { return label; }
LoopLabel &get_label () { return label; }

protected:
/* Use covariance to implement clone function as returning this object rather
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/hir/rust-ast-lower-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,8 @@ ASTLoweringExpr::visit (AST::ForLoopExpr &expr)
void
ASTLoweringExpr::visit (AST::BreakExpr &expr)
{
HIR::Lifetime break_label = lower_lifetime (expr.get_label ());
HIR::Lifetime break_label
= lower_lifetime (expr.get_label ().get_lifetime ());
HIR::Expr *break_expr
= expr.has_break_expr ()
? ASTLoweringExpr::translate (expr.get_break_expr ().get ())
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/resolve/rust-ast-resolve-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ ResolveExpr::visit (AST::BreakExpr &expr)
{
if (expr.has_label ())
{
auto label = expr.get_label ();
auto label = expr.get_label ().get_lifetime ();
if (label.get_lifetime_type () != AST::Lifetime::LifetimeType::NAMED)
{
rust_error_at (label.get_locus (),
Expand All @@ -466,7 +466,7 @@ ResolveExpr::visit (AST::BreakExpr &expr)
label.get_lifetime_name ()),
&resolved_node))
{
rust_error_at (expr.get_label ().get_locus (), ErrorCode::E0426,
rust_error_at (label.get_locus (), ErrorCode::E0426,
"use of undeclared label %qs in %<break%>",
label.get_lifetime_name ().c_str ());
return;
Expand Down

0 comments on commit bf2244e

Please sign in to comment.