Skip to content

Commit e705000

Browse files
committed
lower: Handle let-else properly
gcc/rust/ChangeLog: * hir/tree/rust-hir-stmt.h (class LetStmt): Add optional diverging else expression. * hir/tree/rust-hir-stmt.cc: Likewise. * hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): Add handling for lowering diverging else.
1 parent 926331f commit e705000

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

gcc/rust/hir/rust-ast-lower-stmt.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,26 @@ ASTLoweringStmt::visit (AST::LetStmt &stmt)
7676
type
7777
= std::unique_ptr<Type> (ASTLoweringType::translate (stmt.get_type ()));
7878

79-
tl::optional<std::unique_ptr<HIR::Expr>> init_expression = tl::nullopt;
79+
tl::optional<std::unique_ptr<HIR::Expr>> init_expr = tl::nullopt;
80+
tl::optional<std::unique_ptr<HIR::Expr>> else_expr = tl::nullopt;
8081

8182
if (stmt.has_init_expr ())
82-
init_expression = std::unique_ptr<HIR::Expr> (
83+
init_expr = std::unique_ptr<HIR::Expr> (
8384
ASTLoweringExpr::translate (stmt.get_init_expr ()));
8485

86+
if (stmt.has_else_expr ())
87+
else_expr = std::unique_ptr<HIR::Expr> (
88+
ASTLoweringExpr::translate (stmt.get_else_expr ()));
89+
8590
auto crate_num = mappings.get_current_crate ();
8691
Analysis::NodeMapping mapping (crate_num, stmt.get_node_id (),
8792
mappings.get_next_hir_id (crate_num),
8893
UNKNOWN_LOCAL_DEFID);
8994
translated
9095
= new HIR::LetStmt (mapping, std::unique_ptr<HIR::Pattern> (variables),
91-
std::move (init_expression), std::move (type),
92-
stmt.get_outer_attrs (), stmt.get_locus ());
96+
std::move (init_expr), std::move (else_expr),
97+
std::move (type), stmt.get_outer_attrs (),
98+
stmt.get_locus ());
9399
}
94100

95101
void

gcc/rust/hir/tree/rust-hir-stmt.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ namespace HIR {
2626
LetStmt::LetStmt (Analysis::NodeMapping mappings,
2727
std::unique_ptr<Pattern> variables_pattern,
2828
tl::optional<std::unique_ptr<Expr>> init_expr,
29+
tl::optional<std::unique_ptr<Expr>> else_expr,
2930
tl::optional<std::unique_ptr<Type>> type,
3031
AST::AttrVec outer_attrs, location_t locus)
3132
: Stmt (std::move (mappings)), outer_attrs (std::move (outer_attrs)),
3233
variables_pattern (std::move (variables_pattern)), type (std::move (type)),
33-
init_expr (std::move (init_expr)), locus (locus)
34+
init_expr (std::move (init_expr)), else_expr (std::move (else_expr)),
35+
locus (locus)
3436
{}
3537

3638
LetStmt::LetStmt (LetStmt const &other)
@@ -43,6 +45,8 @@ LetStmt::LetStmt (LetStmt const &other)
4345
// guard to prevent null dereference (always required)
4446
if (other.has_init_expr ())
4547
init_expr = other.get_init_expr ().clone_expr ();
48+
if (other.has_else_expr ())
49+
else_expr = other.get_else_expr ().clone_expr ();
4650

4751
if (other.has_type ())
4852
type = other.get_type ().clone_type ();
@@ -67,6 +71,12 @@ LetStmt::operator= (LetStmt const &other)
6771
init_expr = other.get_init_expr ().clone_expr ();
6872
else
6973
init_expr = nullptr;
74+
75+
if (other.has_else_expr ())
76+
else_expr = other.get_else_expr ().clone_expr ();
77+
else
78+
else_expr = tl::nullopt;
79+
7080
if (other.has_type ())
7181
type = other.get_type ().clone_type ();
7282
else

gcc/rust/hir/tree/rust-hir-stmt.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class LetStmt : public Stmt
101101
tl::optional<std::unique_ptr<Type>> type;
102102

103103
tl::optional<std::unique_ptr<Expr>> init_expr;
104+
tl::optional<std::unique_ptr<Expr>> else_expr;
104105

105106
location_t locus;
106107

@@ -113,12 +114,15 @@ class LetStmt : public Stmt
113114

114115
// Returns whether let statement has an initialisation expression.
115116
bool has_init_expr () const { return init_expr.has_value (); }
117+
// Returns whether let statement has a diverging else expression.
118+
bool has_else_expr () const { return else_expr.has_value (); }
116119

117120
std::string as_string () const override;
118121

119122
LetStmt (Analysis::NodeMapping mappings,
120123
std::unique_ptr<Pattern> variables_pattern,
121124
tl::optional<std::unique_ptr<Expr>> init_expr,
125+
tl::optional<std::unique_ptr<Expr>> else_expr,
122126
tl::optional<std::unique_ptr<Type>> type, AST::AttrVec outer_attrs,
123127
location_t locus);
124128

@@ -167,6 +171,18 @@ class LetStmt : public Stmt
167171
return *init_expr.value ();
168172
}
169173

174+
HIR::Expr &get_else_expr ()
175+
{
176+
rust_assert (*else_expr);
177+
return *else_expr.value ();
178+
}
179+
180+
const HIR::Expr &get_else_expr () const
181+
{
182+
rust_assert (*else_expr);
183+
return *else_expr.value ();
184+
}
185+
170186
HIR::Pattern &get_pattern () { return *variables_pattern; }
171187

172188
bool is_item () const override final { return false; }

0 commit comments

Comments
 (0)