Skip to content

Commit

Permalink
Make TyTy::TupleType::get_unit_type cache its return value
Browse files Browse the repository at this point in the history
This removes a usage of Resolver::get_unit_type_node_id in
rust-hir-type-check-expr.cc (the HIR::TupleExpr overload of
TypeCheckExpr::visit).

gcc/rust/ChangeLog:

	* typecheck/rust-tyty.cc
	(TupleType::get_unit_type): Remove parameter, cache return
	value.
	* typecheck/rust-tyty.h
	(TupleType::get_unit_type): Remove parameter.
	* resolve/rust-late-name-resolver-2.0.cc
	(Late::setup_builtin_types): Adjust calls to get_unit_type.
	* resolve/rust-name-resolver.cc
	(Resolver::generate_builtins): Likewise.
	* typecheck/rust-hir-type-check-expr.cc
	(TypeCheckExpr::visit): Likewise.
	* typecheck/rust-hir-type-check-implitem.cc
	(TypeCheckTopLevelExternItem::visit): Likewise.
	(TypeCheckImplItem::visit): Likewise.
	* typecheck/rust-hir-type-check-item.cc
	(TypeCheckItem::visit): Likewise.
	* typecheck/rust-hir-type-check-stmt.cc
	(TypeCheckStmt::visit): Likewise.
	* typecheck/rust-hir-type-check-type.cc
	(TypeCheckType::visit): Likewise.
	* typecheck/rust-hir-type-check.cc
	(TraitItemReference::get_type_from_fn): Likewise.

Signed-off-by: Owen Avery <[email protected]>
  • Loading branch information
powerboat9 committed Oct 28, 2024
1 parent 5fe9110 commit bb873f5
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 38 deletions.
2 changes: 1 addition & 1 deletion gcc/rust/resolve/rust-late-name-resolver-2.0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Late::setup_builtin_types ()
}

// ...here!
auto *unit_type = TyTy::TupleType::get_unit_type (next_hir_id ());
auto *unit_type = TyTy::TupleType::get_unit_type ();
ty_ctx.insert_builtin (unit_type->get_ref (), next_node_id (), unit_type);
}

Expand Down
3 changes: 1 addition & 2 deletions gcc/rust/resolve/rust-name-resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,7 @@ Resolver::generate_builtins ()
set_never_type_node_id (never_node_id);

// unit type ()
TyTy::TupleType *unit_tyty
= TyTy::TupleType::get_unit_type (mappings.get_next_hir_id ());
TyTy::TupleType *unit_tyty = TyTy::TupleType::get_unit_type ();
std::vector<std::unique_ptr<AST::Type> > elems;
AST::TupleType *unit_type
= new AST::TupleType (std::move (elems), BUILTINS_LOCATION);
Expand Down
34 changes: 12 additions & 22 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,7 @@ TypeCheckExpr::visit (HIR::TupleExpr &expr)
{
if (expr.is_unit ())
{
auto unit_node_id = resolver->get_unit_type_node_id ();
if (!context->lookup_builtin (unit_node_id, &infered))
{
rust_error_at (expr.get_locus (),
"failed to lookup builtin unit type");
}
infered = TyTy::TupleType::get_unit_type ();
return;
}

Expand Down Expand Up @@ -168,7 +163,7 @@ TypeCheckExpr::visit (HIR::ReturnExpr &expr)
TyTy::BaseType *expr_ty
= expr.has_return_expr ()
? TypeCheckExpr::Resolve (expr.get_expr ().get ())
: TyTy::TupleType::get_unit_type (expr.get_mappings ().get_hirid ());
: TyTy::TupleType::get_unit_type ();

coercion_site (expr.get_mappings ().get_hirid (),
TyTy::TyWithLocation (fn_return_tyty),
Expand Down Expand Up @@ -242,7 +237,7 @@ TypeCheckExpr::visit (HIR::CallExpr &expr)
void
TypeCheckExpr::visit (HIR::AssignmentExpr &expr)
{
infered = TyTy::TupleType::get_unit_type (expr.get_mappings ().get_hirid ());
infered = TyTy::TupleType::get_unit_type ();

auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ().get ());
auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ().get ());
Expand All @@ -256,7 +251,7 @@ TypeCheckExpr::visit (HIR::AssignmentExpr &expr)
void
TypeCheckExpr::visit (HIR::CompoundAssignmentExpr &expr)
{
infered = TyTy::TupleType::get_unit_type (expr.get_mappings ().get_hirid ());
infered = TyTy::TupleType::get_unit_type ();

auto lhs = TypeCheckExpr::Resolve (expr.get_lhs ().get ());
auto rhs = TypeCheckExpr::Resolve (expr.get_rhs ().get ());
Expand Down Expand Up @@ -465,7 +460,7 @@ TypeCheckExpr::visit (HIR::IfExpr &expr)

TypeCheckExpr::Resolve (expr.get_if_block ().get ());

infered = TyTy::TupleType::get_unit_type (expr.get_mappings ().get_hirid ());
infered = TyTy::TupleType::get_unit_type ();
}

void
Expand Down Expand Up @@ -538,8 +533,7 @@ TypeCheckExpr::visit (HIR::BlockExpr &expr)

if (s->is_unit_check_needed () && !resolved->is_unit ())
{
auto unit
= TyTy::TupleType::get_unit_type (s->get_mappings ().get_hirid ());
auto unit = TyTy::TupleType::get_unit_type ();
resolved
= unify_site (s->get_mappings ().get_hirid (),
TyTy::TyWithLocation (unit),
Expand All @@ -550,8 +544,7 @@ TypeCheckExpr::visit (HIR::BlockExpr &expr)
if (expr.has_expr ())
infered = TypeCheckExpr::Resolve (expr.get_final_expr ().get ())->clone ();
else if (expr.is_tail_reachable ())
infered
= TyTy::TupleType::get_unit_type (expr.get_mappings ().get_hirid ());
infered = TyTy::TupleType::get_unit_type ();
else if (expr.has_label ())
{
TyTy::BaseType *loop_context_type = context->pop_loop_context ();
Expand All @@ -563,8 +556,7 @@ TypeCheckExpr::visit (HIR::BlockExpr &expr)
!= TyTy::InferType::GENERAL));

infered = loop_context_type_infered ? loop_context_type
: TyTy::TupleType::get_unit_type (
expr.get_mappings ().get_hirid ());
: TyTy::TupleType::get_unit_type ();
}
else
{
Expand Down Expand Up @@ -773,8 +765,7 @@ TypeCheckExpr::visit (HIR::InlineAsm &expr)
if (expr.options.count (AST::InlineAsmOption::NORETURN) == 1)
infered = new TyTy::NeverType (expr.get_mappings ().get_hirid ());
else
infered
= TyTy::TupleType::get_unit_type (expr.get_mappings ().get_hirid ());
infered = TyTy::TupleType::get_unit_type ();
}

void
Expand Down Expand Up @@ -1298,7 +1289,7 @@ TypeCheckExpr::visit (HIR::LoopExpr &expr)
infered
= loop_context_type_infered
? loop_context_type
: TyTy::TupleType::get_unit_type (expr.get_mappings ().get_hirid ());
: TyTy::TupleType::get_unit_type ();
}

void
Expand All @@ -1319,7 +1310,7 @@ TypeCheckExpr::visit (HIR::WhileLoopExpr &expr)
}

context->pop_loop_context ();
infered = TyTy::TupleType::get_unit_type (expr.get_mappings ().get_hirid ());
infered = TyTy::TupleType::get_unit_type ();
}

void
Expand Down Expand Up @@ -1506,8 +1497,7 @@ TypeCheckExpr::visit (HIR::MatchExpr &expr)

if (kase_block_tys.size () == 0)
{
infered
= TyTy::TupleType::get_unit_type (expr.get_mappings ().get_hirid ());
infered = TyTy::TupleType::get_unit_type ();
return;
}

Expand Down
6 changes: 2 additions & 4 deletions gcc/rust/typecheck/rust-hir-type-check-implitem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function)

TyTy::BaseType *ret_type = nullptr;
if (!function.has_return_type ())
ret_type
= TyTy::TupleType::get_unit_type (function.get_mappings ().get_hirid ());
ret_type = TyTy::TupleType::get_unit_type ();
else
{
auto resolved
Expand Down Expand Up @@ -359,8 +358,7 @@ TypeCheckImplItem::visit (HIR::Function &function)

TyTy::BaseType *ret_type = nullptr;
if (!function.has_function_return_type ())
ret_type
= TyTy::TupleType::get_unit_type (function.get_mappings ().get_hirid ());
ret_type = TyTy::TupleType::get_unit_type ();
else
{
auto resolved
Expand Down
3 changes: 1 addition & 2 deletions gcc/rust/typecheck/rust-hir-type-check-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,7 @@ TypeCheckItem::visit (HIR::Function &function)

TyTy::BaseType *ret_type = nullptr;
if (!function.has_function_return_type ())
ret_type
= TyTy::TupleType::get_unit_type (function.get_mappings ().get_hirid ());
ret_type = TyTy::TupleType::get_unit_type ();
else
{
auto resolved
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-stmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ TypeCheckStmt::visit (HIR::ExprStmt &stmt)
void
TypeCheckStmt::visit (HIR::EmptyStmt &stmt)
{
infered = TyTy::TupleType::get_unit_type (stmt.get_mappings ().get_hirid ());
infered = TyTy::TupleType::get_unit_type ();
}

void
Expand Down Expand Up @@ -74,7 +74,7 @@ TypeCheckStmt::visit (HIR::ConstantItem &constant)
void
TypeCheckStmt::visit (HIR::LetStmt &stmt)
{
infered = TyTy::TupleType::get_unit_type (stmt.get_mappings ().get_hirid ());
infered = TyTy::TupleType::get_unit_type ();

HIR::Pattern &stmt_pattern = *stmt.get_pattern ();
TyTy::BaseType *init_expr_ty = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/typecheck/rust-hir-type-check-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ TypeCheckType::visit (HIR::BareFunctionType &fntype)
{
// needs a new implicit ID
HirId ref = mappings.get_next_hir_id ();
return_type = TyTy::TupleType::get_unit_type (ref);
return_type = TyTy::TupleType::get_unit_type ();
context->insert_implicit_type (ref, return_type);
}

Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/typecheck/rust-hir-type-check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const

TyTy::BaseType *ret_type = nullptr;
if (!function.has_return_type ())
ret_type = TyTy::TupleType::get_unit_type (fn.get_mappings ().get_hirid ());
ret_type = TyTy::TupleType::get_unit_type ();
else
{
auto resolved
Expand Down
7 changes: 5 additions & 2 deletions gcc/rust/typecheck/rust-tyty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1747,9 +1747,12 @@ TupleType::TupleType (HirId ref, HirId ty_ref, location_t locus,
{}

TupleType *
TupleType::get_unit_type (HirId ref)
TupleType::get_unit_type ()
{
return new TupleType (ref, BUILTINS_LOCATION);
static TupleType *ret = nullptr;
if (ret == nullptr)
ret = new TupleType (Analysis::Mappings::get ().get_next_hir_id (), BUILTINS_LOCATION);
return ret;
}

size_t
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/typecheck/rust-tyty.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ class TupleType : public BaseType
std::vector<TyVar> fields = std::vector<TyVar> (),
std::set<HirId> refs = std::set<HirId> ());

static TupleType *get_unit_type (HirId ref);
static TupleType *get_unit_type ();

void accept_vis (TyVisitor &vis) override;
void accept_vis (TyConstVisitor &vis) const override;
Expand Down

0 comments on commit bb873f5

Please sign in to comment.