Skip to content

Commit cdfb5b3

Browse files
committed
privacy: reachability: Cleanup Struct definition visitor
1 parent a196568 commit cdfb5b3

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

gcc/rust/privacy/rust-reachability.cc

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,38 @@ maybe_get_vis_item (std::unique_ptr<HIR::Item> &item)
3131
return static_cast<HIR::VisItem *> (item.get ());
3232
}
3333

34+
void
35+
ReachabilityVisitor::visit_generic_predicates (
36+
const std::vector<std::unique_ptr<HIR::GenericParam>> &generics,
37+
ReachLevel item_reach)
38+
{
39+
if (item_reach == ReachLevel::Unreachable)
40+
return;
41+
42+
for (auto &generic : generics)
43+
{
44+
if (generic->get_kind () == HIR::GenericParam::TYPE)
45+
{
46+
TyTy::BaseType *generic_ty = nullptr;
47+
rust_assert (
48+
ty_ctx.lookup_type (generic->get_mappings ().get_hirid (),
49+
&generic_ty));
50+
51+
// FIXME: Can we really get anything else than a TyTy::PARAM here?
52+
// Should we change this to an assertion instead?
53+
if (generic_ty->get_kind () == TyTy::PARAM)
54+
{
55+
auto generic_param = static_cast<TyTy::ParamType *> (generic_ty);
56+
for (const auto &bound : generic_param->get_specified_bounds ())
57+
{
58+
const auto trait = bound.get ()->get_hir_trait_ref ();
59+
ctx.update_reachability (trait->get_mappings (), item_reach);
60+
}
61+
}
62+
}
63+
}
64+
}
65+
3466
void
3567
ReachabilityVisitor::visit (HIR::Module &mod)
3668
{
@@ -75,41 +107,10 @@ ReachabilityVisitor::visit (HIR::StructStruct &struct_item)
75107
auto old_level = current_level;
76108
current_level = struct_reach;
77109

110+
visit_generic_predicates (struct_item.get_generic_params (), struct_reach);
111+
78112
if (struct_reach != ReachLevel::Unreachable)
79113
{
80-
for (auto &field : struct_item.get_fields ())
81-
if (field.get_visibility ().is_public ())
82-
ctx.update_reachability (field.get_mappings (), struct_reach);
83-
84-
for (auto &generic : struct_item.get_generic_params ())
85-
{
86-
switch (generic->get_kind ())
87-
{
88-
case HIR::GenericParam::LIFETIME:
89-
break;
90-
case HIR::GenericParam::TYPE:
91-
TyTy::BaseType *generic_ty = nullptr;
92-
rust_assert (
93-
ty_ctx.lookup_type (generic->get_mappings ().get_hirid (),
94-
&generic_ty));
95-
96-
if (generic_ty->get_kind () == TyTy::PARAM)
97-
{
98-
auto generic_param
99-
= static_cast<TyTy::ParamType *> (generic_ty);
100-
for (const auto &bound :
101-
generic_param->get_specified_bounds ())
102-
{
103-
const auto trait = bound.get ()->get_hir_trait_ref ();
104-
ctx.update_reachability (trait->get_mappings (),
105-
struct_reach);
106-
}
107-
}
108-
109-
break;
110-
}
111-
}
112-
113114
for (auto &field : struct_item.get_fields ())
114115
if (field.get_visibility ().is_public ())
115116
ctx.update_reachability (field.get_field_type ()->get_mappings (),

gcc/rust/privacy/rust-reachability.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ class ReachabilityVisitor : public HIR::HIRVisItemVisitor
4646
: current_level (ReachLevel::Reachable), ctx (ctx), ty_ctx (ty_ctx)
4747
{}
4848

49+
/**
50+
* Visit all the predicates of all the generic types of a given item, marking
51+
* them as reachable or not.
52+
*/
53+
void visit_generic_predicates (
54+
const std::vector<std::unique_ptr<HIR::GenericParam>> &generics,
55+
ReachLevel item_reach);
56+
4957
virtual void visit (HIR::Module &mod);
5058
virtual void visit (HIR::ExternCrate &crate);
5159
virtual void visit (HIR::UseDeclaration &use_decl);

0 commit comments

Comments
 (0)