Skip to content

Commit

Permalink
Fixes some tests
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* backend/rust-compile-expr.cc (CompileExpr::visit):
	* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::visit):
	* hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit):
	* hir/tree/rust-hir-expr-abstract.h:
	* hir/tree/rust-hir-generic-param.h:
	* hir/tree/rust-hir-item.h:
	* hir/tree/rust-hir-path.h:
	* hir/tree/rust-hir-stmt.cc (LetStmt::LetStmt):
	(LetStmt::operator=):
	* hir/tree/rust-hir-stmt.h (class LetStmt):
	* hir/tree/rust-hir.cc (LetStmt::as_string):
	* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit):
	* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit):
	* typecheck/rust-tyty.cc (VariantDef::VariantDef):
	(VariantDef::get_error_node):
	(VariantDef::is_error):
	(VariantDef::get_id):
	(VariantDef::get_defid):
	(VariantDef::get_variant_type):
	(VariantDef::is_data_variant):
	(VariantDef::is_dataless_variant):
	(VariantDef::get_identifier):
	(VariantDef::num_fields):
	(VariantDef::get_field_at_index):
	(VariantDef::get_fields):
	(VariantDef::lookup_field):
	(VariantDef::get_discriminant):
	(VariantDef::has_discriminant):
	(VariantDef::as_string):
	(VariantDef::is_equal):
	(VariantDef::clone):
	(VariantDef::monomorphized_clone):
	(VariantDef::get_ident):
	* typecheck/rust-tyty.h:

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
  • Loading branch information
P-E-P committed Oct 28, 2024
1 parent 3cb72c8 commit 285dfde
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 63 deletions.
2 changes: 1 addition & 1 deletion gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ CompileExpr::visit (HIR::CallExpr &expr)
// this assumes all fields are in order from type resolution and if a
// base struct was specified those fields are filed via accessors
std::vector<tree> arguments;
for (size_t i = 0; i < expr.get_arguments ().size (); i++)
for (size_t i = 0; i < expr.num_params (); i++)
{
auto &argument = expr.get_arguments ().at (i);
auto rvalue = CompileExpr::Compile (*argument, ctx);
Expand Down
6 changes: 4 additions & 2 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -747,9 +747,11 @@ PrivacyReporter::visit (HIR::EmptyStmt &)
void
PrivacyReporter::visit (HIR::LetStmt &stmt)
{
check_type_privacy (stmt.get_type ());
if (stmt.has_type ())
check_type_privacy (stmt.get_type ());

stmt.get_init_expr ().accept_vis (*this);
if (stmt.has_init_expr ())
stmt.get_init_expr ().accept_vis (*this);
}

void
Expand Down
20 changes: 12 additions & 8 deletions gcc/rust/hir/rust-ast-lower-stmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "optional.h"
#include "rust-ast-lower-item.h"
#include "rust-ast-lower-stmt.h"
#include "rust-ast-lower-type.h"
Expand Down Expand Up @@ -68,21 +69,24 @@ ASTLoweringStmt::visit (AST::LetStmt &stmt)
{
HIR::Pattern *variables
= ASTLoweringPattern::translate (stmt.get_pattern (), true);
HIR::Type *type = stmt.has_type ()
? ASTLoweringType::translate (stmt.get_type ())
: nullptr;
HIR::Expr *init_expression
= stmt.has_init_expr () ? ASTLoweringExpr::translate (stmt.get_init_expr ())
: nullptr;

auto type
= stmt.has_type () ? tl::optional<std::unique_ptr<Type>> (
std::unique_ptr<Type> (ASTLoweringType::translate (stmt.get_type ())))
: tl::nullopt;
auto init_expression
= stmt.has_init_expr ()
? tl::optional<std::unique_ptr<Expr>> (std::unique_ptr<HIR::Expr> (
ASTLoweringExpr::translate (stmt.get_init_expr ())))
: tl::nullopt;

auto crate_num = mappings.get_current_crate ();
Analysis::NodeMapping mapping (crate_num, stmt.get_node_id (),
mappings.get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);
translated
= new HIR::LetStmt (mapping, std::unique_ptr<HIR::Pattern> (variables),
std::unique_ptr<HIR::Expr> (init_expression),
std::unique_ptr<HIR::Type> (type),
std::move (init_expression), std::move (type),
stmt.get_outer_attrs (), stmt.get_locus ());
}

Expand Down
1 change: 1 addition & 0 deletions gcc/rust/hir/tree/rust-hir-expr-abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Expr : public Node, virtual public FullVisitable
// Unique pointer custom clone function
std::unique_ptr<Expr> clone_expr () const
{
rust_assert (this);
return std::unique_ptr<Expr> (clone_expr_impl ());
}

Expand Down
6 changes: 5 additions & 1 deletion gcc/rust/hir/tree/rust-hir-generic-param.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ class ConstGenericParam : public GenericParam
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:
Expand Down
69 changes: 57 additions & 12 deletions gcc/rust/hir/tree/rust-hir-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -141,7 +142,11 @@ class TypeParam : public GenericParam

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;

Expand Down Expand Up @@ -414,7 +419,11 @@ struct SelfParam

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; }

Expand Down Expand Up @@ -482,7 +491,11 @@ struct FunctionParam

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; }
};
Expand Down Expand Up @@ -1112,7 +1125,11 @@ class TypeAlias : public VisItem, public ImplItem

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; }

Expand Down Expand Up @@ -1766,7 +1783,11 @@ class ConstantItem : public VisItem, public ImplItem
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; }

Expand Down Expand Up @@ -1844,9 +1865,17 @@ class StaticItem : public VisItem

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; }

Expand Down Expand Up @@ -2028,9 +2057,17 @@ class TraitItemConst : public TraitItem

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
{
Expand Down Expand Up @@ -2274,9 +2311,13 @@ class ImplBlock : public VisItem, public WithInnerAttrs

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 ()
{
Expand Down Expand Up @@ -2436,7 +2477,11 @@ struct NamedFunctionParam

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; }
};
Expand Down
18 changes: 15 additions & 3 deletions gcc/rust/hir/tree/rust-hir-path.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,16 @@ class GenericArgsBinding
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; }
};
Expand Down Expand Up @@ -633,7 +641,11 @@ class QualifiedPathType

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; }

Expand Down
26 changes: 16 additions & 10 deletions gcc/rust/hir/tree/rust-hir-stmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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 &
Expand All @@ -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;
}
Expand Down
40 changes: 30 additions & 10 deletions gcc/rust/hir/tree/rust-hir-stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

Expand All @@ -110,17 +109,18 @@ class LetStmt : public Stmt
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);
Expand All @@ -143,9 +143,29 @@ class LetStmt : public Stmt
}
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; }

Expand Down
Loading

0 comments on commit 285dfde

Please sign in to comment.