Skip to content

Commit c4443ca

Browse files
committed
privacy: reachability: Visit all variants of an Enum and their fields
1 parent 126f7ae commit c4443ca

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

gcc/rust/privacy/rust-reachability.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,38 @@ ReachabilityVisitor::visit (HIR::Enum &enum_item)
150150

151151
enum_reach = ctx.update_reachability (enum_item.get_mappings (), enum_reach);
152152
visit_generic_predicates (enum_item.get_generic_params (), enum_reach);
153+
154+
for (const auto &variant : enum_item.get_variants ())
155+
{
156+
auto variant_reach
157+
= ctx.update_reachability (variant->get_mappings (), enum_reach);
158+
159+
switch (variant->get_enum_item_kind ())
160+
{
161+
case HIR::EnumItem::Tuple: {
162+
// Should we update the fields only if they are public? Similarly to
163+
// what we do in the ReachabilityVisitor for HIR::TupleStruct?
164+
auto tuple_variant
165+
= static_cast<HIR::EnumItemTuple *> (variant.get ());
166+
for (const auto &field : tuple_variant->get_tuple_fields ())
167+
ctx.update_reachability (field.get_mappings (), variant_reach);
168+
break;
169+
}
170+
case HIR::EnumItem::Struct: {
171+
// Should we update the fields only if they are public? Similarly to
172+
// what we do in the ReachabilityVisitor for HIR::StructStruct?
173+
auto struct_variant
174+
= static_cast<HIR::EnumItemStruct *> (variant.get ());
175+
for (const auto &field : struct_variant->get_struct_fields ())
176+
ctx.update_reachability (field.get_mappings (), variant_reach);
177+
break;
178+
}
179+
// Nothing nested to visit in that case
180+
case HIR::EnumItem::Named:
181+
case HIR::EnumItem::Discriminant:
182+
break;
183+
}
184+
}
153185
}
154186

155187
void

0 commit comments

Comments
 (0)