Skip to content

Commit

Permalink
use implicit insert and remove insert_type method from the context ob…
Browse files Browse the repository at this point in the history
…ject (#3395)

gcc/rust/ChangeLog:

	* typecheck/rust-typecheck-context.cc
	(TypeCheckContext::insert_type): Remove.
	* typecheck/rust-hir-type-check.h: Remove declaration of insert_type method.
	* typecheck/rust-tyty-subst.cc: Replace all insert_type with insert_implicit_type.
	* typecheck/rust-tyty-util.cc: Likewise.
	* typecheck/rust-tyty.cc: Likewise.
	* typecheck/rust-substitution-mapper.cc: Likewise.
	* typecheck/rust-hir-trait-resolve.cc: Likewise.
	* typecheck/rust-hir-type-check-base.cc: Likewise.
	* typecheck/rust-hir-type-check-enumitem.cc: Likewise.
	* typecheck/rust-hir-type-check-expr.cc: use Likewise.
	* typecheck/rust-hir-type-check-implitem.cc: Likewise.
	* typecheck/rust-hir-type-check-item.cc: use Likewise.
	* typecheck/rust-hir-type-check-pattern.cc: Likewise.
	* typecheck/rust-hir-type-check-stmt.cc: Likewise.
	* typecheck/rust-hir-type-check-struct.cc: Likewise.
	* typecheck/rust-hir-type-check-type.cc: Likewise.
	* typecheck/rust-hir-type-check.cc: Likewise.
	* typecheck/rust-unify.cc: Likewise.

Signed-off-by: sasa630 <mostafayounis630@gmail.com>
  • Loading branch information
mostafa630 committed Mar 7, 2025
1 parent bee59d6 commit 395d362
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 99 deletions.
5 changes: 3 additions & 2 deletions gcc/rust/typecheck/rust-hir-trait-resolve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference)
bool apply_sized = !is_self;
auto param_type
= TypeResolveGenericParam::Resolve (*generic_param, apply_sized);
context->insert_type (generic_param->get_mappings (), param_type);
context->insert_implicit_type (
generic_param->get_mappings ().get_hirid (), param_type);
substitutions.push_back (
TyTy::SubstitutionParamMapping (typaram, param_type));

Expand Down Expand Up @@ -361,7 +362,7 @@ TraitItemReference::resolve_item (HIR::TraitItemType &type)
= new TyTy::PlaceholderType (type.get_name ().as_string (),
type.get_mappings ().get_defid (),
type.get_mappings ().get_hirid ());
context->insert_type (type.get_mappings (), ty);
context->insert_implicit_type (type.get_mappings ().get_hirid (), ty);
}

void
Expand Down
12 changes: 7 additions & 5 deletions gcc/rust/typecheck/rust-hir-type-check-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ TypeCheckBase::resolve_literal (const Analysis::NodeMapping &expr_mappings,
TyTy::BaseType *expected_ty = nullptr;
ok = context->lookup_builtin ("usize", &expected_ty);
rust_assert (ok);
context->insert_type (capacity_mapping, expected_ty);
context->insert_implicit_type (capacity_mapping.get_hirid (),
expected_ty);

Analysis::NodeMapping array_mapping (crate_num, UNKNOWN_NODEID,
mappings.get_next_hir_id (
Expand All @@ -274,7 +275,7 @@ TypeCheckBase::resolve_literal (const Analysis::NodeMapping &expr_mappings,
= new TyTy::ArrayType (array_mapping.get_hirid (), locus,
*literal_capacity,
TyTy::TyVar (u8->get_ref ()));
context->insert_type (array_mapping, array);
context->insert_implicit_type (array_mapping.get_hirid (), array);

infered = new TyTy::ReferenceType (expr_mappings.get_hirid (),
TyTy::TyVar (array->get_ref ()),
Expand Down Expand Up @@ -401,14 +402,15 @@ TypeCheckBase::resolve_generic_params (
param.get_locus ());
}

context->insert_type (generic_param->get_mappings (),
specified_type);
context->insert_implicit_type (
generic_param->get_mappings ().get_hirid (), specified_type);
}
break;

case HIR::GenericParam::GenericKind::TYPE: {
auto param_type = TypeResolveGenericParam::Resolve (*generic_param);
context->insert_type (generic_param->get_mappings (), param_type);
context->insert_implicit_type (
generic_param->get_mappings ().get_hirid (), param_type);

substitutions.push_back (TyTy::SubstitutionParamMapping (
static_cast<HIR::TypeParam &> (*generic_param), param_type));
Expand Down
15 changes: 9 additions & 6 deletions gcc/rust/typecheck/rust-hir-type-check-enumitem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ TypeCheckEnumItem::visit (HIR::EnumItem &item)
TyTy::BaseType *isize = nullptr;
bool ok = context->lookup_builtin ("isize", &isize);
rust_assert (ok);
context->insert_type (mapping, isize);
context->insert_implicit_type (mapping.get_hirid (), isize);

tl::optional<CanonicalPath> canonical_path;

Expand Down Expand Up @@ -117,7 +117,8 @@ TypeCheckEnumItem::visit (HIR::EnumItemDiscriminant &item)

TyTy::ISizeType *expected_ty
= new TyTy::ISizeType (discriminant.get_mappings ().get_hirid ());
context->insert_type (discriminant.get_mappings (), expected_ty);
context->insert_implicit_type (discriminant.get_mappings ().get_hirid (),
expected_ty);

unify_site (item.get_mappings ().get_hirid (),
TyTy::TyWithLocation (expected_ty),
Expand Down Expand Up @@ -166,7 +167,8 @@ TypeCheckEnumItem::visit (HIR::EnumItemTuple &item)
std::to_string (idx), field_type,
field.get_locus ());
fields.push_back (ty_field);
context->insert_type (field.get_mappings (), ty_field->get_field_type ());
context->insert_implicit_type (field.get_mappings ().get_hirid (),
ty_field->get_field_type ());
idx++;
}

Expand All @@ -183,7 +185,7 @@ TypeCheckEnumItem::visit (HIR::EnumItemTuple &item)
TyTy::BaseType *isize = nullptr;
bool ok = context->lookup_builtin ("isize", &isize);
rust_assert (ok);
context->insert_type (mapping, isize);
context->insert_implicit_type (mapping.get_hirid (), isize);

tl::optional<CanonicalPath> canonical_path;

Expand Down Expand Up @@ -227,7 +229,8 @@ TypeCheckEnumItem::visit (HIR::EnumItemStruct &item)
field.get_field_name ().as_string (),
field_type, field.get_locus ());
fields.push_back (ty_field);
context->insert_type (field.get_mappings (), ty_field->get_field_type ());
context->insert_implicit_type (field.get_mappings ().get_hirid (),
ty_field->get_field_type ());
}

Analysis::NodeMapping mapping (item.get_mappings ().get_crate_num (),
Expand All @@ -243,7 +246,7 @@ TypeCheckEnumItem::visit (HIR::EnumItemStruct &item)
TyTy::BaseType *isize = nullptr;
bool ok = context->lookup_builtin ("isize", &isize);
rust_assert (ok);
context->insert_type (mapping, isize);
context->insert_implicit_type (mapping.get_hirid (), isize);

tl::optional<CanonicalPath> canonical_path;

Expand Down
13 changes: 8 additions & 5 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ TypeCheckExpr::Resolve (HIR::Expr &expr)

auto ref = expr.get_mappings ().get_hirid ();
resolver.infered->set_ref (ref);
resolver.context->insert_type (expr.get_mappings (), resolver.infered);
resolver.context->insert_implicit_type (expr.get_mappings ().get_hirid (),
resolver.infered);

return resolver.infered;
}
Expand Down Expand Up @@ -1007,8 +1008,9 @@ TypeCheckExpr::visit (HIR::ArrayExpr &expr)
TyTy::BaseType *expected_ty = nullptr;
bool ok = context->lookup_builtin ("usize", &expected_ty);
rust_assert (ok);
context->insert_type (elems.get_num_copies_expr ().get_mappings (),
expected_ty);
context->insert_implicit_type (
elems.get_num_copies_expr ().get_mappings ().get_hirid (),
expected_ty);

unify_site (expr.get_mappings ().get_hirid (),
TyTy::TyWithLocation (expected_ty),
Expand Down Expand Up @@ -1056,7 +1058,7 @@ TypeCheckExpr::visit (HIR::ArrayExpr &expr)
TyTy::BaseType *expected_ty = nullptr;
bool ok = context->lookup_builtin ("usize", &expected_ty);
rust_assert (ok);
context->insert_type (mapping, expected_ty);
context->insert_implicit_type (mapping.get_hirid (), expected_ty);
}
break;
}
Expand Down Expand Up @@ -1314,7 +1316,8 @@ TypeCheckExpr::visit (HIR::MethodCallExpr &expr)
}

// store the expected fntype
context->insert_type (expr.get_method_name ().get_mappings (), lookup);
context->insert_implicit_type (
expr.get_method_name ().get_mappings ().get_hirid (), lookup);

if (flag_name_resolution_2_0)
{
Expand Down
28 changes: 17 additions & 11 deletions gcc/rust/typecheck/rust-hir-type-check-implitem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalStaticItem &item)
{
TyTy::BaseType *actual_type = TypeCheckType::Resolve (item.get_item_type ());

context->insert_type (item.get_mappings (), actual_type);
context->insert_implicit_type (item.get_mappings ().get_hirid (),
actual_type);
resolved = actual_type;
}

Expand Down Expand Up @@ -91,8 +92,8 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function)
case HIR::GenericParam::GenericKind::TYPE: {
auto param_type
= TypeResolveGenericParam::Resolve (*generic_param);
context->insert_type (generic_param->get_mappings (),
param_type);
context->insert_implicit_type (
generic_param->get_mappings ().get_hirid (), param_type);

substitutions.push_back (TyTy::SubstitutionParamMapping (
static_cast<HIR::TypeParam &> (*generic_param), param_type));
Expand Down Expand Up @@ -149,7 +150,8 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function)

params.push_back (TyTy::FnParam (std::move (param_pattern), param_tyty));

context->insert_type (param.get_mappings (), param_tyty);
context->insert_implicit_type (param.get_mappings ().get_hirid (),
param_tyty);

// FIXME do we need error checking for patterns here?
// see https://github.com/Rust-GCC/gccrs/issues/995
Expand Down Expand Up @@ -181,7 +183,7 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function)
context->get_lifetime_resolver ().get_num_bound_regions ()),
region_constraints);

context->insert_type (function.get_mappings (), fnType);
context->insert_implicit_type (function.get_mappings ().get_hirid (), fnType);
resolved = fnType;
}

Expand Down Expand Up @@ -450,7 +452,8 @@ TypeCheckImplItem::visit (HIR::Function &function)
}
}

context->insert_type (self_param.get_mappings (), self_type);
context->insert_implicit_type (self_param.get_mappings ().get_hirid (),
self_type);
params.push_back (TyTy::FnParam (std::move (self_pattern), self_type));
}

Expand All @@ -459,7 +462,8 @@ TypeCheckImplItem::visit (HIR::Function &function)
// get the name as well required for later on
auto param_tyty = TypeCheckType::Resolve (param.get_type ());

context->insert_type (param.get_mappings (), param_tyty);
context->insert_implicit_type (param.get_mappings ().get_hirid (),
param_tyty);
TypeCheckPattern::Resolve (param.get_param_name (), param_tyty);

params.push_back (
Expand Down Expand Up @@ -496,7 +500,7 @@ TypeCheckImplItem::visit (HIR::Function &function)
context->get_lifetime_resolver ().get_num_bound_regions ()),
region_constraints);

context->insert_type (function.get_mappings (), fnType);
context->insert_implicit_type (function.get_mappings ().get_hirid (), fnType);
result = fnType;

// need to get the return type from this
Expand Down Expand Up @@ -530,7 +534,8 @@ TypeCheckImplItem::visit (HIR::ConstantItem &constant)
TyTy::TyWithLocation (type, constant.get_type ().get_locus ()),
TyTy::TyWithLocation (expr_type, constant.get_expr ().get_locus ()),
constant.get_locus ());
context->insert_type (constant.get_mappings (), unified);
context->insert_implicit_type (constant.get_mappings ().get_hirid (),
unified);
result = unified;
}

Expand All @@ -545,7 +550,8 @@ TypeCheckImplItem::visit (HIR::TypeAlias &alias)
TyTy::BaseType *actual_type
= TypeCheckType::Resolve (alias.get_type_aliased ());

context->insert_type (alias.get_mappings (), actual_type);
context->insert_implicit_type (alias.get_mappings ().get_hirid (),
actual_type);
result = actual_type;
TyTy::RegionConstraints region_constraints;
for (auto &where_clause_item : alias.get_where_clause ().get_items ())
Expand Down Expand Up @@ -688,7 +694,7 @@ TypeCheckImplItemWithTrait::visit (HIR::TypeAlias &type)
raw_trait_item->get_mappings ().get_defid (),
substitutions);

context->insert_type (type.get_mappings (), projection);
context->insert_implicit_type (type.get_mappings ().get_hirid (), projection);
raw_trait_item->associated_type_set (projection);
}

Expand Down
35 changes: 22 additions & 13 deletions gcc/rust/typecheck/rust-hir-type-check-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ TypeCheckItem::visit (HIR::TypeAlias &alias)
TyTy::BaseType *actual_type
= TypeCheckType::Resolve (alias.get_type_aliased ());

context->insert_type (alias.get_mappings (), actual_type);
context->insert_implicit_type (alias.get_mappings ().get_hirid (),
actual_type);

TyTy::RegionConstraints region_constraints;
for (auto &where_clause_item : alias.get_where_clause ().get_items ())
Expand Down Expand Up @@ -189,7 +190,8 @@ TypeCheckItem::visit (HIR::TupleStruct &struct_decl)
std::to_string (idx), field_type,
field.get_locus ());
fields.push_back (ty_field);
context->insert_type (field.get_mappings (), ty_field->get_field_type ());
context->insert_implicit_type (field.get_mappings ().get_hirid (),
ty_field->get_field_type ());
idx++;
}

Expand Down Expand Up @@ -241,7 +243,9 @@ TypeCheckItem::visit (HIR::TupleStruct &struct_decl)
context->get_lifetime_resolver ().get_num_bound_regions ()),
region_constraints);

context->insert_type (struct_decl.get_mappings (), type);
context->insert_implicit_type (struct_decl.get_mappings ().get_hirid (),
type);

infered = type;

context->get_variance_analysis_ctx ().add_type_constraints (*type);
Expand Down Expand Up @@ -272,7 +276,8 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl)
field.get_field_name ().as_string (),
field_type, field.get_locus ());
fields.push_back (ty_field);
context->insert_type (field.get_mappings (), ty_field->get_field_type ());
context->insert_implicit_type (field.get_mappings ().get_hirid (),
ty_field->get_field_type ());
}

auto path = CanonicalPath::create_empty ();
Expand Down Expand Up @@ -323,7 +328,8 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl)
context->get_lifetime_resolver ().get_num_bound_regions ()),
region_constraints);

context->insert_type (struct_decl.get_mappings (), type);
context->insert_implicit_type (struct_decl.get_mappings ().get_hirid (),
type);
infered = type;

context->get_variance_analysis_ctx ().add_type_constraints (*type);
Expand Down Expand Up @@ -382,7 +388,7 @@ TypeCheckItem::visit (HIR::Enum &enum_decl)
TyTy::ADTType::ADTKind::ENUM, std::move (variants),
std::move (substitutions), repr);

context->insert_type (enum_decl.get_mappings (), type);
context->insert_implicit_type (enum_decl.get_mappings ().get_hirid (), type);
infered = type;

context->get_variance_analysis_ctx ().add_type_constraints (*type);
Expand Down Expand Up @@ -412,8 +418,8 @@ TypeCheckItem::visit (HIR::Union &union_decl)
variant.get_field_name ().as_string (),
variant_type, variant.get_locus ());
fields.push_back (ty_variant);
context->insert_type (variant.get_mappings (),
ty_variant->get_field_type ());
context->insert_implicit_type (variant.get_mappings ().get_hirid (),
ty_variant->get_field_type ());
}

// get the path
Expand Down Expand Up @@ -453,7 +459,7 @@ TypeCheckItem::visit (HIR::Union &union_decl)
TyTy::ADTType::ADTKind::UNION, std::move (variants),
std::move (substitutions));

context->insert_type (union_decl.get_mappings (), type);
context->insert_implicit_type (union_decl.get_mappings ().get_hirid (), type);
infered = type;

context->get_variance_analysis_ctx ().add_type_constraints (*type);
Expand All @@ -471,7 +477,7 @@ TypeCheckItem::visit (HIR::StaticItem &var)
TyTy::TyWithLocation (expr_type,
var.get_expr ().get_locus ()),
var.get_locus ());
context->insert_type (var.get_mappings (), unified);
context->insert_implicit_type (var.get_mappings ().get_hirid (), unified);
infered = unified;
}

Expand All @@ -486,7 +492,8 @@ TypeCheckItem::visit (HIR::ConstantItem &constant)
TyTy::TyWithLocation (type, constant.get_type ().get_locus ()),
TyTy::TyWithLocation (expr_type, constant.get_expr ().get_locus ()),
constant.get_locus ());
context->insert_type (constant.get_mappings (), unified);
context->insert_implicit_type (constant.get_mappings ().get_hirid (),
unified);
infered = unified;
}

Expand Down Expand Up @@ -585,7 +592,8 @@ TypeCheckItem::visit (HIR::Function &function)
{
// get the name as well required for later on
auto param_tyty = TypeCheckType::Resolve (param.get_type ());
context->insert_type (param.get_mappings (), param_tyty);
context->insert_implicit_type (param.get_mappings ().get_hirid (),
param_tyty);
TypeCheckPattern::Resolve (param.get_param_name (), param_tyty);
params.push_back (
TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty));
Expand Down Expand Up @@ -622,7 +630,8 @@ TypeCheckItem::visit (HIR::Function &function)
context->get_lifetime_resolver ().get_num_bound_regions ()),
region_constraints);

context->insert_type (function.get_mappings (), fn_type);
context->insert_implicit_type (function.get_mappings ().get_hirid (),
fn_type);

// need to get the return type from this
TyTy::FnType *resolved_fn_type = fn_type;
Expand Down
Loading

0 comments on commit 395d362

Please sign in to comment.