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 | |
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')
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-generic-param.h | 6 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-item.h | 69 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-path.h | 18 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-stmt.cc | 26 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-stmt.h | 40 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir.cc | 4 |
6 files changed, 125 insertions, 38 deletions
diff --git a/gcc/rust/hir/tree/rust-hir-generic-param.h b/gcc/rust/hir/tree/rust-hir-generic-param.h index 73b93d4..a1c59bf 100644 --- a/gcc/rust/hir/tree/rust-hir-generic-param.h +++ b/gcc/rust/hir/tree/rust-hir-generic-param.h @@ -156,7 +156,11 @@ public: bool has_default_expression () { return default_expression != nullptr; } std::string get_name () { return name; } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } Expr &get_default_expression () { return *default_expression; } protected: diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 39097ce..bb300e9 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -24,6 +24,7 @@ #include "rust-common.h" #include "rust-hir-visibility.h" #include "rust-hir-generic-param.h" +#include "rust-system.h" namespace Rust { namespace HIR { @@ -141,7 +142,11 @@ public: Identifier get_type_representation () const { return type_representation; } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } Analysis::NodeMapping get_type_mappings () const; @@ -414,7 +419,11 @@ public: ImplicitSelfKind get_self_kind () const { return self_kind; } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } Analysis::NodeMapping get_mappings () { return mappings; } @@ -482,7 +491,11 @@ public: Pattern &get_param_name () { return *param_name; } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } const Analysis::NodeMapping &get_mappings () const { return mappings; } }; @@ -1112,7 +1125,11 @@ public: WhereClause &get_where_clause () { return where_clause; } - Type &get_type_aliased () { return *existing_type; } + Type &get_type_aliased () + { + rust_assert (existing_type); + return *existing_type; + } Identifier get_new_type_name () const { return new_type_name; } @@ -1766,7 +1783,11 @@ public: void accept_vis (HIRImplVisitor &vis) override; void accept_vis (HIRVisItemVisitor &vis) override; - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } Expr &get_expr () { return *const_expr; } @@ -1844,9 +1865,17 @@ public: bool is_mut () const { return mut == Mutability::Mut; } - Expr &get_expr () { return *expr; } + Expr &get_expr () + { + rust_assert (expr); + return *expr; + } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } ItemKind get_item_kind () const override { return ItemKind::Static; } @@ -2028,9 +2057,17 @@ public: bool has_expr () const { return expr != nullptr; } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } - Expr &get_expr () { return *expr; } + Expr &get_expr () + { + rust_assert (expr); + return *expr; + } const std::string trait_identifier () const override final { @@ -2274,9 +2311,13 @@ public: location_t get_locus () const override final { return locus; } - Type &get_type () { return *impl_type; }; + Type &get_type () + { + rust_assert (impl_type); + return *impl_type; + }; - bool has_type () { return impl_type == nullptr; } + bool has_type () { return impl_type != nullptr; } std::vector<std::unique_ptr<GenericParam>> &get_generic_params () { @@ -2436,7 +2477,11 @@ public: Identifier get_param_name () const { return name; } - Type &get_type () { return *param_type; } + Type &get_type () + { + rust_assert (param_type); + return *param_type; + } Analysis::NodeMapping get_mappings () const { return mappings; } }; diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h index 8b9814f..7ef66b8 100644 --- a/gcc/rust/hir/tree/rust-hir-path.h +++ b/gcc/rust/hir/tree/rust-hir-path.h @@ -99,8 +99,16 @@ public: Identifier &get_identifier () { return identifier; } const Identifier &get_identifier () const { return identifier; } - Type &get_type () { return *type; } - const Type &get_type () const { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } + const Type &get_type () const + { + rust_assert (type); + return *type; + } location_t get_locus () const { return locus; } }; @@ -633,7 +641,11 @@ public: Analysis::NodeMapping get_mappings () const { return mappings; } - Type &get_type () { return *type; } + Type &get_type () + { + rust_assert (type); + return *type; + } TypePath &get_trait () { return *trait; } diff --git a/gcc/rust/hir/tree/rust-hir-stmt.cc b/gcc/rust/hir/tree/rust-hir-stmt.cc index 764ea11..025f67e 100644 --- a/gcc/rust/hir/tree/rust-hir-stmt.cc +++ b/gcc/rust/hir/tree/rust-hir-stmt.cc @@ -17,13 +17,16 @@ // <http://www.gnu.org/licenses/>. #include "rust-hir-stmt.h" +#include "optional.h" +#include "rust-system.h" namespace Rust { namespace HIR { LetStmt::LetStmt (Analysis::NodeMapping mappings, std::unique_ptr<Pattern> variables_pattern, - std::unique_ptr<Expr> init_expr, std::unique_ptr<Type> type, + tl::optional<std::unique_ptr<Expr>> init_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)), @@ -38,10 +41,13 @@ LetStmt::LetStmt (LetStmt const &other) variables_pattern = other.variables_pattern->clone_pattern (); // guard to prevent null dereference (always required) - if (other.init_expr != nullptr) - init_expr = other.init_expr->clone_expr (); - if (other.type != nullptr) - type = other.type->clone_type (); + if (other.has_init_expr ()) + init_expr = other.get_init_expr ().clone_expr (); + + if (other.has_type ()) + type = other.get_type ().clone_type (); + else + type = tl::nullopt; } LetStmt & @@ -57,14 +63,14 @@ LetStmt::operator= (LetStmt const &other) variables_pattern = nullptr; // guard to prevent null dereference (always required) - if (other.init_expr != nullptr) - init_expr = other.init_expr->clone_expr (); + if (other.has_init_expr ()) + init_expr = other.get_init_expr ().clone_expr (); else init_expr = nullptr; - if (other.type != nullptr) - type = other.type->clone_type (); + if (other.has_type ()) + type = other.get_type ().clone_type (); else - type = nullptr; + type = tl::nullopt; return *this; } 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; } diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc index ea09111..bccfc52 100644 --- a/gcc/rust/hir/tree/rust-hir.cc +++ b/gcc/rust/hir/tree/rust-hir.cc @@ -2669,12 +2669,12 @@ LetStmt::as_string () const if (has_type ()) { - str += " : " + type->as_string (); + str += " : " + get_type ().as_string (); } if (has_init_expr ()) { - str += " = " + init_expr->as_string (); + str += " = " + get_init_expr ().as_string (); } return str; |