From c4443ca3b2e1e0847bcb1a7d858d4bb0de16fdb3 Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Thu, 14 Apr 2022 10:50:02 +0200 Subject: privacy: reachability: Visit all variants of an Enum and their fields --- gcc/rust/privacy/rust-reachability.cc | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'gcc') diff --git a/gcc/rust/privacy/rust-reachability.cc b/gcc/rust/privacy/rust-reachability.cc index 826a465..bf2480b 100644 --- a/gcc/rust/privacy/rust-reachability.cc +++ b/gcc/rust/privacy/rust-reachability.cc @@ -150,6 +150,38 @@ ReachabilityVisitor::visit (HIR::Enum &enum_item) enum_reach = ctx.update_reachability (enum_item.get_mappings (), enum_reach); visit_generic_predicates (enum_item.get_generic_params (), enum_reach); + + for (const auto &variant : enum_item.get_variants ()) + { + auto variant_reach + = ctx.update_reachability (variant->get_mappings (), enum_reach); + + switch (variant->get_enum_item_kind ()) + { + case HIR::EnumItem::Tuple: { + // Should we update the fields only if they are public? Similarly to + // what we do in the ReachabilityVisitor for HIR::TupleStruct? + auto tuple_variant + = static_cast (variant.get ()); + for (const auto &field : tuple_variant->get_tuple_fields ()) + ctx.update_reachability (field.get_mappings (), variant_reach); + break; + } + case HIR::EnumItem::Struct: { + // Should we update the fields only if they are public? Similarly to + // what we do in the ReachabilityVisitor for HIR::StructStruct? + auto struct_variant + = static_cast (variant.get ()); + for (const auto &field : struct_variant->get_struct_fields ()) + ctx.update_reachability (field.get_mappings (), variant_reach); + break; + } + // Nothing nested to visit in that case + case HIR::EnumItem::Named: + case HIR::EnumItem::Discriminant: + break; + } + } } void -- cgit v1.1