Skip to content

Commit

Permalink
Change return type of lookup_defid
Browse files Browse the repository at this point in the history
Change the return type to an optional.

gcc/rust/ChangeLog:

	* backend/rust-compile-base.cc (HIRCompileBase::resolve_method_address):
	Change calling code to accomodate new return type.
	* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::check_for_privacy_violation):
	Likewise.
	* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::get_marker_predicate):
	Likewise.
	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
	Likewise.
	* typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::assemble_builtin_candidate):
	Likewise.
	* typecheck/rust-tyty.cc (ClosureType::setup_fn_once_output): Likewise.
	* util/rust-hir-map.cc (Mappings::lookup_defid): Change function's
	return type.
	* util/rust-hir-map.h: Update function's prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
  • Loading branch information
P-E-P committed May 1, 2024
1 parent f5e097e commit b5d74da
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 27 deletions.
7 changes: 3 additions & 4 deletions gcc/rust/backend/rust-compile-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -877,13 +877,12 @@ HIRCompileBase::resolve_method_address (TyTy::FnType *fntype,
// Now we can try and resolve the address since this might be a forward
// declared function, generic function which has not be compiled yet or
// its an not yet trait bound function
HIR::Item *resolved_item = ctx->get_mappings ().lookup_defid (id);
if (resolved_item != nullptr)
if (auto resolved_item = ctx->get_mappings ().lookup_defid (id))
{
if (!fntype->has_substitutions_defined ())
return CompileItem::compile (resolved_item, ctx);
return CompileItem::compile (*resolved_item, ctx);

return CompileItem::compile (resolved_item, ctx, fntype);
return CompileItem::compile (*resolved_item, ctx, fntype);
}

// it might be resolved to a trait item
Expand Down
3 changes: 1 addition & 2 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ PrivacyReporter::check_for_privacy_violation (const NodeId &use_id,
if (!current_module.has_value ())
return;

auto module = mappings.lookup_defid (vis.get_module_id ());
rust_assert (module != nullptr);
auto module = mappings.lookup_defid (vis.get_module_id ()).value ();

auto mod_node_id = module->get_mappings ().get_nodeid ();

Expand Down
3 changes: 1 addition & 2 deletions gcc/rust/typecheck/rust-hir-type-check-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,7 @@ TyTy::TypeBoundPredicate
TypeCheckBase::get_marker_predicate (LangItem::Kind item_type, location_t locus)
{
DefId item_id = mappings.get_lang_item (item_type, locus);
HIR::Item *item = mappings.lookup_defid (item_id);
rust_assert (item != nullptr);
HIR::Item *item = mappings.lookup_defid (item_id).value ();
rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);

HIR::Trait &trait = *static_cast<HIR::Trait *> (item);
Expand Down
17 changes: 6 additions & 11 deletions gcc/rust/typecheck/rust-hir-type-check-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,7 @@ TypeCheckExpr::visit (HIR::RangeFromToExpr &expr)
}

// look it up and it _must_ be a struct definition
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
rust_assert (item != nullptr);
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();

TyTy::BaseType *item_type = nullptr;
bool ok
Expand Down Expand Up @@ -697,8 +696,7 @@ TypeCheckExpr::visit (HIR::RangeFromExpr &expr)
}

// look it up and it _must_ be a struct definition
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
rust_assert (item != nullptr);
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();

TyTy::BaseType *item_type = nullptr;
bool ok
Expand Down Expand Up @@ -745,8 +743,7 @@ TypeCheckExpr::visit (HIR::RangeToExpr &expr)
}

// look it up and it _must_ be a struct definition
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
rust_assert (item != nullptr);
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();

TyTy::BaseType *item_type = nullptr;
bool ok
Expand Down Expand Up @@ -792,8 +789,7 @@ TypeCheckExpr::visit (HIR::RangeFullExpr &expr)
}

// look it up and it _must_ be a struct definition
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
rust_assert (item != nullptr);
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();

TyTy::BaseType *item_type = nullptr;
bool ok
Expand Down Expand Up @@ -823,8 +819,7 @@ TypeCheckExpr::visit (HIR::RangeFromToInclExpr &expr)
}

// look it up and it _must_ be a struct definition
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
rust_assert (item != nullptr);
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();

TyTy::BaseType *item_type = nullptr;
bool ok
Expand Down Expand Up @@ -1599,7 +1594,7 @@ TypeCheckExpr::visit (HIR::ClosureExpr &expr)
rust_assert (lang_item_defined);

// these lang items are always traits
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id);
HIR::Item *item = mappings.lookup_defid (respective_lang_item_id).value ();
rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
HIR::Trait *trait_item = static_cast<HIR::Trait *> (item);

Expand Down
5 changes: 3 additions & 2 deletions gcc/rust/typecheck/rust-tyty-bounds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ TypeBoundsProbe::assemble_builtin_candidate (LangItem::Kind lang_item)
if (!found_lang_item)
return;

HIR::Item *item = mappings.lookup_defid (id);
if (item == nullptr)
auto defid = mappings.lookup_defid (id);
if (!defid)
return;
auto item = defid.value ();

rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
HIR::Trait *trait = static_cast<HIR::Trait *> (item);
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/typecheck/rust-tyty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2229,7 +2229,7 @@ ClosureType::setup_fn_once_output () const
rust_assert (trait_item_lang_item_defined);

// resolve to the trait
HIR::Item *item = mappings.lookup_defid (trait_id);
HIR::Item *item = mappings.lookup_defid (trait_id).value ();
rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
HIR::Trait *trait = static_cast<HIR::Trait *> (item);

Expand Down
8 changes: 4 additions & 4 deletions gcc/rust/util/rust-hir-map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -312,20 +312,20 @@ Mappings::insert_defid_mapping (DefId id, HIR::Item *item)
CrateNum crate_num = id.crateNum;
LocalDefId local_def_id = id.localDefId;

rust_assert (lookup_defid (id) == nullptr);
rust_assert (!lookup_defid (id));
rust_assert (lookup_local_defid (crate_num, local_def_id) == nullptr);
rust_assert (lookup_trait_item_defid (id) == nullptr);

defIdMappings[id] = item;
insert_local_defid_mapping (crate_num, local_def_id, item);
}

HIR::Item *
tl::optional<HIR::Item *>
Mappings::lookup_defid (DefId id)
{
auto it = defIdMappings.find (id);
if (it == defIdMappings.end ())
return nullptr;
return tl::nullopt;

return it->second;
}
Expand All @@ -336,7 +336,7 @@ Mappings::insert_defid_mapping (DefId id, HIR::TraitItem *item)
CrateNum crate_num = id.crateNum;
LocalDefId local_def_id = id.localDefId;

rust_assert (lookup_defid (id) == nullptr);
rust_assert (!lookup_defid (id));
rust_assert (lookup_local_defid (crate_num, local_def_id) == nullptr);
rust_assert (lookup_trait_item_defid (id) == nullptr);

Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/util/rust-hir-map.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Mappings
bool is_local_hirid_crate (HirId crateNum);

void insert_defid_mapping (DefId id, HIR::Item *item);
HIR::Item *lookup_defid (DefId id);
tl::optional<HIR::Item *> lookup_defid (DefId id);
void insert_defid_mapping (DefId id, HIR::TraitItem *item);
HIR::TraitItem *lookup_trait_item_defid (DefId id);

Expand Down

0 comments on commit b5d74da

Please sign in to comment.