Skip to content

Commit 3058b55

Browse files
committed
privacy: reachability: Visit all struct generic predicates
1 parent 415586f commit 3058b55

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

gcc/rust/privacy/rust-privacy-check.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "rust-privacy-check.h"
2020
#include "rust-reachability.h"
21+
#include "rust-hir-type-check.h"
2122

2223
extern bool
2324
saw_errors (void);
@@ -28,7 +29,8 @@ void
2829
Resolver::resolve (HIR::Crate &crate)
2930
{
3031
PrivacyContext ctx;
31-
auto visitor = ReachabilityVisitor (ctx);
32+
auto ty_ctx = ::Rust::Resolver::TypeCheckContext::get ();
33+
auto visitor = ReachabilityVisitor (ctx, *ty_ctx);
3234

3335
const auto &items = crate.items;
3436
for (auto &item : items)

gcc/rust/privacy/rust-privacy-check.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "rust-hir-expr.h"
2525
#include "rust-hir-stmt.h"
2626
#include "rust-hir-item.h"
27+
#include "rust-hir-type-check.h"
2728

2829
namespace Rust {
2930
namespace Privacy {

gcc/rust/privacy/rust-reachability.cc

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// <http://www.gnu.org/licenses/>.
1818

1919
#include "rust-reachability.h"
20+
#include "rust-tyty.h"
2021

2122
namespace Rust {
2223
namespace Privacy {
@@ -80,12 +81,34 @@ ReachabilityVisitor::visit (HIR::StructStruct &struct_item)
8081
if (field.get_visibility ().is_public ())
8182
ctx.update_reachability (field.get_mappings (), struct_reach);
8283

83-
// for (auto &generic : struct_item.get_generic_params ())
84-
// FIXME: How do we visit these generics's predicates with the
85-
// reachability visitor?
86-
87-
// FIXME: How do we get each generic's predicates from here?
88-
// TypeContext?
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+
}
89112

90113
// for (auto &field : struct_item.get_fields ())
91114
// if (field.get_visibility ().is_public ())

gcc/rust/privacy/rust-reachability.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "rust-hir-expr.h"
2626
#include "rust-hir-stmt.h"
2727
#include "rust-hir-item.h"
28+
#include "rust-hir-type-check.h"
2829

2930
namespace Rust {
3031
namespace Privacy {
@@ -40,8 +41,9 @@ namespace Privacy {
4041
class ReachabilityVisitor : public HIR::HIRVisItemVisitor
4142
{
4243
public:
43-
ReachabilityVisitor (PrivacyContext &ctx)
44-
: current_level (ReachLevel::Reachable), ctx (ctx)
44+
ReachabilityVisitor (PrivacyContext &ctx,
45+
const ::Rust::Resolver::TypeCheckContext &ty_ctx)
46+
: current_level (ReachLevel::Reachable), ctx (ctx), ty_ctx (ty_ctx)
4547
{}
4648

4749
virtual void visit (HIR::Module &mod);
@@ -62,6 +64,7 @@ class ReachabilityVisitor : public HIR::HIRVisItemVisitor
6264
private:
6365
ReachLevel current_level;
6466
PrivacyContext &ctx;
67+
const ::Rust::Resolver::TypeCheckContext &ty_ctx;
6568
};
6669
} // namespace Privacy
6770
} // namespace Rust

0 commit comments

Comments
 (0)