From: Pierre-Emmanuel Patry <[email protected]>
Wrap the return type within an optional. Now return the parent id within
a pair instead of taking an out reference.
gcc/rust/ChangeLog:
* backend/rust-compile-item.cc (CompileItem::visit): Change call site
to accept new return type.
* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile):
Likewise.
* backend/rust-mangle-v0.cc (v0_path): Likewise.
* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit):
Likewise.
* checks/lints/rust-lint-marklive.cc (MarkLive::go): Likewise.
(MarkLive::visit): Likewise.
* typecheck/rust-type-util.cc (query_type): Likewise.
* util/rust-hir-map.cc (Mappings::insert_hir_implitem): Likewise.
(Mappings::lookup_hir_implitem): Change return type.
* util/rust-hir-map.h: Update the function's prototype.
Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
---
gcc/rust/backend/rust-compile-item.cc | 7 ++--
gcc/rust/backend/rust-compile-resolve-path.cc | 12 +++----
gcc/rust/backend/rust-mangle-v0.cc | 10 +++---
gcc/rust/checks/errors/rust-unsafe-checker.cc | 6 ++--
gcc/rust/checks/lints/rust-lint-marklive.cc | 33 ++++---------------
gcc/rust/typecheck/rust-type-util.cc | 16 ++++-----
gcc/rust/util/rust-hir-map.cc | 14 +++-----
gcc/rust/util/rust-hir-map.h | 10 +++---
8 files changed, 35 insertions(+), 73 deletions(-)
diff --git a/gcc/rust/backend/rust-compile-item.cc
b/gcc/rust/backend/rust-compile-item.cc
index 9d65b610ec5..e7b3a17edf7 100644
--- a/gcc/rust/backend/rust-compile-item.cc
+++ b/gcc/rust/backend/rust-compile-item.cc
@@ -152,15 +152,12 @@ CompileItem::visit (HIR::Function &function)
{
// if this is part of a trait impl block which is not generic we need to
// ensure associated types are setup
- HirId parent_impl_block = UNKNOWN_HIRID;
HirId id = function.get_mappings ().get_hirid ();
- HIR::ImplItem *impl_item
- = ctx->get_mappings ().lookup_hir_implitem (id, &parent_impl_block);
- if (impl_item != nullptr)
+ if (auto impl_item = ctx->get_mappings ().lookup_hir_implitem (id))
{
Resolver::AssociatedImplTrait *impl = nullptr;
bool found = ctx->get_tyctx ()->lookup_associated_trait_impl (
- parent_impl_block, &impl);
+ impl_item->second, &impl);
if (found)
impl->setup_raw_associated_types ();
}
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc
b/gcc/rust/backend/rust-compile-resolve-path.cc
index c27074f89a4..7c9b303b851 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -238,18 +238,14 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType
*lookup,
}
}
- HirId parent_impl_id = UNKNOWN_HIRID;
- HIR::ImplItem *resolved_item
- = ctx->get_mappings ().lookup_hir_implitem (ref, &parent_impl_id);
- bool is_impl_item = resolved_item != nullptr;
- if (is_impl_item)
+ if (auto resolved_item = ctx->get_mappings ().lookup_hir_implitem (ref))
{
if (!lookup->has_substitutions_defined ())
- return CompileInherentImplItem::Compile (resolved_item, ctx,
+ return CompileInherentImplItem::Compile (resolved_item->first, ctx,
nullptr, true, expr_locus);
else
- return CompileInherentImplItem::Compile (resolved_item, ctx, lookup,
- true, expr_locus);
+ return CompileInherentImplItem::Compile (resolved_item->first, ctx,
+ lookup, true, expr_locus);
}
else
{
diff --git a/gcc/rust/backend/rust-mangle-v0.cc
b/gcc/rust/backend/rust-mangle-v0.cc
index 0b6d9455242..261e84405d5 100644
--- a/gcc/rust/backend/rust-mangle-v0.cc
+++ b/gcc/rust/backend/rust-mangle-v0.cc
@@ -384,17 +384,15 @@ v0_path (Rust::Compile::Context *ctx, const
TyTy::BaseType *ty,
auto hir_id = hid.value ();
- HirId parent_impl_id = UNKNOWN_HIRID;
- HIR::ImplItem *impl_item
- = mappings.lookup_hir_implitem (hir_id, &parent_impl_id);
HIR::Expr *expr = mappings.lookup_hir_expr (hir_id);
- if (impl_item != nullptr)
+ if (auto impl_item = mappings.lookup_hir_implitem (hir_id))
{
- switch (impl_item->get_impl_item_type ())
+ switch (impl_item->first->get_impl_item_type ())
{
case HIR::ImplItem::FUNCTION: {
- HIR::Function *fn = static_cast<HIR::Function *> (impl_item);
+ HIR::Function *fn
+ = static_cast<HIR::Function *> (impl_item->first);
v0path
= v0_function_path (v0path, ctx, ty, fn->get_generic_params (),
v0_identifier (seg.get ()));
diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc
b/gcc/rust/checks/errors/rust-unsafe-checker.cc
index 19a0489297f..8d986c72eda 100644
--- a/gcc/rust/checks/errors/rust-unsafe-checker.cc
+++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc
@@ -449,11 +449,11 @@ UnsafeChecker::visit (MethodCallExpr &expr)
&method_type);
auto fn = *static_cast<TyTy::FnType *> (method_type);
- auto method = mappings.lookup_hir_implitem (fn.get_ref (), nullptr);
+ auto method = mappings.lookup_hir_implitem (fn.get_ref ());
if (!unsafe_context.is_in_context () && method)
- check_unsafe_call (static_cast<Function *> (method), expr.get_locus (),
- "method");
+ check_unsafe_call (static_cast<Function *> (method->first),
+ expr.get_locus (), "method");
expr.get_receiver ()->accept_vis (*this);
diff --git a/gcc/rust/checks/lints/rust-lint-marklive.cc
b/gcc/rust/checks/lints/rust-lint-marklive.cc
index f64e8b59462..24df933c805 100644
--- a/gcc/rust/checks/lints/rust-lint-marklive.cc
+++ b/gcc/rust/checks/lints/rust-lint-marklive.cc
@@ -88,17 +88,9 @@ MarkLive::go (HIR::Crate &)
scannedSymbols.emplace (hirId);
liveSymbols.emplace (hirId);
if (auto item = mappings.lookup_hir_item (hirId))
- {
- item.value ()->accept_vis (*this);
- }
- else
- { // the item maybe inside a trait impl
- HirId parent_impl_id = UNKNOWN_HIRID;
- HIR::ImplItem *implItem
- = mappings.lookup_hir_implitem (hirId, &parent_impl_id);
- if (implItem != nullptr)
- implItem->accept_vis (*this);
- }
+ item.value ()->accept_vis (*this);
+ else if (auto implItem = mappings.lookup_hir_implitem (hirId))
+ implItem->first->accept_vis (*this);
}
}
@@ -123,21 +115,10 @@ MarkLive::visit (HIR::PathInExpression &expr)
auto ref = hid.value ();
// it must resolve to some kind of HIR::Item or HIR::InheritImplItem
- tl::optional<HIR::Item *> resolved_item = mappings.lookup_hir_item (ref);
- if (resolved_item)
- {
- mark_hir_id (resolved_item.value ()->get_mappings ().get_hirid ());
- }
- else
- {
- HirId parent_impl_id = UNKNOWN_HIRID;
- HIR::ImplItem *resolved_item
- = mappings.lookup_hir_implitem (ref, &parent_impl_id);
- if (resolved_item != nullptr)
- {
- mark_hir_id (resolved_item->get_impl_mappings ().get_hirid ());
- }
- }
+ if (auto resolved_item = mappings.lookup_hir_item (ref))
+ mark_hir_id (resolved_item.value ()->get_mappings ().get_hirid ());
+ else if (auto resolved_item = mappings.lookup_hir_implitem (ref))
+ mark_hir_id (resolved_item->first->get_impl_mappings ().get_hirid ());
}
void
diff --git a/gcc/rust/typecheck/rust-type-util.cc
b/gcc/rust/typecheck/rust-type-util.cc
index 05ac58bc18c..0ed71247353 100644
--- a/gcc/rust/typecheck/rust-type-util.cc
+++ b/gcc/rust/typecheck/rust-type-util.cc
@@ -70,20 +70,16 @@ query_type (HirId reference, TyTy::BaseType **result)
return true;
}
- HirId parent_impl_id = UNKNOWN_HIRID;
- HIR::ImplItem *impl_item
- = mappings.lookup_hir_implitem (reference, &parent_impl_id);
- if (impl_item != nullptr)
+ if (auto impl_item = mappings.lookup_hir_implitem (reference))
{
- auto impl_block = mappings.lookup_hir_impl_block (parent_impl_id);
- rust_assert (impl_block);
+ auto impl_block
+ = mappings.lookup_hir_impl_block (impl_item->second).value ();
// found an impl item
- rust_debug_loc (impl_item->get_locus (), "resolved impl-item {%u} to",
- reference);
+ rust_debug_loc (impl_item->first->get_locus (),
+ "resolved impl-item {%u} to", reference);
- *result
- = TypeCheckItem::ResolveImplItem (*impl_block.value (), *impl_item);
+ *result = TypeCheckItem::ResolveImplItem (*impl_block,
*impl_item->first);
context->query_completed (reference);
return true;
}
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 4a78ae15525..89c990d965f 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -509,25 +509,21 @@ void
Mappings::insert_hir_implitem (HirId parent_impl_id, HIR::ImplItem *item)
{
auto id = item->get_impl_mappings ().get_hirid ();
- rust_assert (lookup_hir_implitem (id, nullptr) == nullptr);
+ rust_assert (!lookup_hir_implitem (id));
hirImplItemMappings[id]
= std::pair<HirId, HIR::ImplItem *> (parent_impl_id, item);
insert_node_to_hir (item->get_impl_mappings ().get_nodeid (), id);
}
-HIR::ImplItem *
-Mappings::lookup_hir_implitem (HirId id, HirId *parent_impl_id)
+tl::optional<std::pair<HIR::ImplItem *, HirId>>
+Mappings::lookup_hir_implitem (HirId id)
{
auto it = hirImplItemMappings.find (id);
if (it == hirImplItemMappings.end ())
- return nullptr;
-
- std::pair<HirId, HIR::ImplItem *> &ref = it->second;
- if (parent_impl_id != nullptr)
- *parent_impl_id = ref.first;
+ return tl::nullopt;
- return ref.second;
+ return std::make_pair (it->second.second, it->second.first);
}
void
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 3ca980ea3a9..a587ccca753 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -141,7 +141,9 @@ public:
tl::optional<HIR::Module *> lookup_module (HirId id);
void insert_hir_implitem (HirId parent_impl_id, HIR::ImplItem *item);
- HIR::ImplItem *lookup_hir_implitem (HirId id, HirId *parent_impl_id);
+ // Optional<ImpItem, ParentImpl Hir id>
+ tl::optional<std::pair<HIR::ImplItem *, HirId>>
+ lookup_hir_implitem (HirId id);
void insert_hir_expr (HIR::Expr *expr);
HIR::Expr *lookup_hir_expr (HirId id);
@@ -209,11 +211,7 @@ public:
void iterate_trait_items (
std::function<bool (HIR::TraitItem *item, HIR::Trait *)> cb);
- bool is_impl_item (HirId id)
- {
- HirId parent_impl_block_id = UNKNOWN_HIRID;
- return lookup_hir_implitem (id, &parent_impl_block_id) != nullptr;
- }
+ bool is_impl_item (HirId id) { return lookup_hir_implitem (id).has_value ();
}
void insert_trait_item_mapping (HirId trait_item_id, HIR::Trait *trait)
{
--
2.45.2