From 3bb4d746a037f9ebea8f91a32b8785223c4e2d33 Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Tue, 12 Apr 2022 15:25:37 +0200 Subject: privacy: reachability: Add `maybe_get_vis_item` helper static function --- gcc/rust/privacy/rust-reachability.cc | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/privacy/rust-reachability.cc b/gcc/rust/privacy/rust-reachability.cc index 1876222..bbd0434 100644 --- a/gcc/rust/privacy/rust-reachability.cc +++ b/gcc/rust/privacy/rust-reachability.cc @@ -20,20 +20,28 @@ namespace Rust { namespace Privacy { + +static HIR::VisItem * +maybe_get_vis_item (std::unique_ptr &item) +{ + if (item->get_hir_kind () != HIR::VIS_ITEM) + return nullptr; + + return static_cast (item.get ()); +} + void ReachabilityVisitor::visit (HIR::Module &mod) { for (auto &item : mod.get_items ()) { - // FIXME: How do we refactor this pattern into something more ergonomic? - // FIXME: Add helper functions // FIXME: Is that what we want to do? Yes? Only visit the items with // visibility? - if (item->get_hir_kind () == HIR::VIS_ITEM) - { - auto vis_item = static_cast (item.get ()); - vis_item->accept_vis (*this); - } + // + // Imagine if we had `maybe_get_vis_item(item)?->accept_vis(*this)` ;) + auto vis_item = maybe_get_vis_item (item); + if (vis_item) + vis_item->accept_vis (*this); } } @@ -60,7 +68,7 @@ ReachabilityVisitor::visit (HIR::StructStruct &struct_item) // FIXME: This feels very wrong. Should we check for `has_visibility` // beforehand? Is it just private otherwise? Should the `HIR::Visibility` also // keep variants for private items? - if (struct_item.get_visibility ().get_vis_type () == HIR::Visibility::NONE) + if (struct_item.get_visibility ().is_public ()) struct_reach = ReachLevel::Reachable; struct_reach -- cgit v1.1