diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2024-10-28 18:08:52 +0100 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2025-03-21 12:32:57 +0100 |
commit | 6246f658336099f328078f92f277ed461c3bb7e5 (patch) | |
tree | b20779c9fbb63ddd90e95811704c7c268fbe2ee0 /gcc/rust/hir/tree/rust-hir-stmt.h | |
parent | 417f4bd3e0bb82e74664417a6c9a7a878e4df462 (diff) | |
download | gcc-6246f658336099f328078f92f277ed461c3bb7e5.zip gcc-6246f658336099f328078f92f277ed461c3bb7e5.tar.gz gcc-6246f658336099f328078f92f277ed461c3bb7e5.tar.bz2 |
gccrs: Fixes some tests appearing with a moved variant
A variant being moved lead to a null being created and a segfault later
down the line.
gcc/rust/ChangeLog:
* backend/rust-compile-expr.cc (CompileExpr::visit): Call getter
instead of size function.
* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::visit):
Only check privacy if the type is present.
* hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): Use an optional.
* hir/tree/rust-hir-generic-param.h: Assert type before getting it.
* hir/tree/rust-hir-item.h: Assert pointers before dereference, fix
has_type condition.
* hir/tree/rust-hir-path.h: Add more assertions.
* hir/tree/rust-hir-stmt.cc: Change constructor with optionals.
* hir/tree/rust-hir-stmt.h: Use optionals over smart pointers to
emphasize these fields might be missing.
* hir/tree/rust-hir.cc (LetStmt::as_string): Use getters.
* typecheck/rust-hir-type-check-expr.cc: Clone structures to prevent
parent's fields from being nulled by the move operation.
* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Use
optionals.
* typecheck/rust-tyty.cc: Likewise.
* typecheck/rust-tyty.h: Likewise.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc/rust/hir/tree/rust-hir-stmt.h')
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-stmt.h | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/gcc/rust/hir/tree/rust-hir-stmt.h b/gcc/rust/hir/tree/rust-hir-stmt.h index 7540dfa..3db1728 100644 --- a/gcc/rust/hir/tree/rust-hir-stmt.h +++ b/gcc/rust/hir/tree/rust-hir-stmt.h @@ -22,6 +22,7 @@ #include "rust-hir.h" #include "rust-hir-path.h" #include "rust-hir-expr.h" +#include "rust-system.h" namespace Rust { namespace HIR { @@ -97,11 +98,9 @@ class LetStmt : public Stmt std::unique_ptr<Pattern> variables_pattern; - // bool has_type; - std::unique_ptr<Type> type; + tl::optional<std::unique_ptr<Type>> type; - // bool has_init_expr; - std::unique_ptr<Expr> init_expr; + tl::optional<std::unique_ptr<Expr>> init_expr; location_t locus; @@ -110,17 +109,18 @@ public: bool has_outer_attrs () const { return !outer_attrs.empty (); } // Returns whether let statement has a given return type. - bool has_type () const { return type != nullptr; } + bool has_type () const { return type.has_value (); } // Returns whether let statement has an initialisation expression. - bool has_init_expr () const { return init_expr != nullptr; } + bool has_init_expr () const { return init_expr.has_value (); } std::string as_string () const override; LetStmt (Analysis::NodeMapping mappings, std::unique_ptr<Pattern> variables_pattern, - std::unique_ptr<Expr> init_expr, std::unique_ptr<Type> type, - AST::AttrVec outer_attrs, location_t locus); + tl::optional<std::unique_ptr<Expr>> init_expr, + tl::optional<std::unique_ptr<Type>> type, AST::AttrVec outer_attrs, + location_t locus); // Copy constructor with clone LetStmt (LetStmt const &other); @@ -143,9 +143,29 @@ public: } std::vector<AST::Attribute> &get_outer_attrs () { return outer_attrs; } - HIR::Type &get_type () { return *type; } + HIR::Type &get_type () + { + rust_assert (*type); + return *type.value (); + } + + const HIR::Type &get_type () const + { + rust_assert (*type); + return *type.value (); + } - HIR::Expr &get_init_expr () { return *init_expr; } + HIR::Expr &get_init_expr () + { + rust_assert (*init_expr); + return *init_expr.value (); + } + + const HIR::Expr &get_init_expr () const + { + rust_assert (*init_expr); + return *init_expr.value (); + } HIR::Pattern &get_pattern () { return *variables_pattern; } |