diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2025-03-05 15:36:19 +0000 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2025-03-31 21:07:15 +0200 |
commit | b31e1976cb0fe767db81febc6a30a6cc0b8f243a (patch) | |
tree | 13819ea0de704ab96f7e219a58c1e5f54e258511 /gcc/rust/hir/tree | |
parent | 02afa004778d49b86fb8c7320efaee4cff4e3a9d (diff) | |
download | gcc-b31e1976cb0fe767db81febc6a30a6cc0b8f243a.zip gcc-b31e1976cb0fe767db81febc6a30a6cc0b8f243a.tar.gz gcc-b31e1976cb0fe767db81febc6a30a6cc0b8f243a.tar.bz2 |
gccrs: 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.
Diffstat (limited to 'gcc/rust/hir/tree')
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-stmt.cc | 12 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-stmt.h | 16 |
2 files changed, 27 insertions, 1 deletions
diff --git a/gcc/rust/hir/tree/rust-hir-stmt.cc b/gcc/rust/hir/tree/rust-hir-stmt.cc index 025f67e..fd58e29 100644 --- a/gcc/rust/hir/tree/rust-hir-stmt.cc +++ b/gcc/rust/hir/tree/rust-hir-stmt.cc @@ -26,11 +26,13 @@ namespace HIR { LetStmt::LetStmt (Analysis::NodeMapping mappings, std::unique_ptr<Pattern> variables_pattern, tl::optional<std::unique_ptr<Expr>> init_expr, + tl::optional<std::unique_ptr<Expr>> else_expr, tl::optional<std::unique_ptr<Type>> type, AST::AttrVec outer_attrs, location_t locus) : Stmt (std::move (mappings)), outer_attrs (std::move (outer_attrs)), variables_pattern (std::move (variables_pattern)), type (std::move (type)), - init_expr (std::move (init_expr)), locus (locus) + init_expr (std::move (init_expr)), else_expr (std::move (else_expr)), + locus (locus) {} LetStmt::LetStmt (LetStmt const &other) @@ -43,6 +45,8 @@ LetStmt::LetStmt (LetStmt const &other) // guard to prevent null dereference (always required) if (other.has_init_expr ()) init_expr = other.get_init_expr ().clone_expr (); + if (other.has_else_expr ()) + else_expr = other.get_else_expr ().clone_expr (); if (other.has_type ()) type = other.get_type ().clone_type (); @@ -67,6 +71,12 @@ LetStmt::operator= (LetStmt const &other) init_expr = other.get_init_expr ().clone_expr (); else init_expr = nullptr; + + if (other.has_else_expr ()) + else_expr = other.get_else_expr ().clone_expr (); + else + else_expr = tl::nullopt; + if (other.has_type ()) type = other.get_type ().clone_type (); else diff --git a/gcc/rust/hir/tree/rust-hir-stmt.h b/gcc/rust/hir/tree/rust-hir-stmt.h index 3db1728..9c1a9ec 100644 --- a/gcc/rust/hir/tree/rust-hir-stmt.h +++ b/gcc/rust/hir/tree/rust-hir-stmt.h @@ -101,6 +101,7 @@ class LetStmt : public Stmt tl::optional<std::unique_ptr<Type>> type; tl::optional<std::unique_ptr<Expr>> init_expr; + tl::optional<std::unique_ptr<Expr>> else_expr; location_t locus; @@ -113,12 +114,15 @@ public: // Returns whether let statement has an initialisation expression. bool has_init_expr () const { return init_expr.has_value (); } + // Returns whether let statement has a diverging else expression. + bool has_else_expr () const { return else_expr.has_value (); } std::string as_string () const override; LetStmt (Analysis::NodeMapping mappings, std::unique_ptr<Pattern> variables_pattern, tl::optional<std::unique_ptr<Expr>> init_expr, + tl::optional<std::unique_ptr<Expr>> else_expr, tl::optional<std::unique_ptr<Type>> type, AST::AttrVec outer_attrs, location_t locus); @@ -167,6 +171,18 @@ public: return *init_expr.value (); } + HIR::Expr &get_else_expr () + { + rust_assert (*else_expr); + return *else_expr.value (); + } + + const HIR::Expr &get_else_expr () const + { + rust_assert (*else_expr); + return *else_expr.value (); + } + HIR::Pattern &get_pattern () { return *variables_pattern; } bool is_item () const override final { return false; } |