Skip to content

Commit f536bd4

Browse files
powerboat9P-E-P
authored andcommitted
nr1.0: Remove support in privacy checker
gcc/rust/ChangeLog: * checks/errors/privacy/rust-privacy-check.cc: Adjust includes. (Resolver::resolve): Pass 2.0 name resolution context to VisibilityResolver and PrivacyReporter. * checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::PrivacyReporter): Change type of resolver parameter. (is_child_module): Remove static function. (PrivacyReporter::check_for_privacy_violation): Assume nr2.0 is enabled and handle removal of is_child_module. * checks/errors/privacy/rust-privacy-reporter.h: Adjust includes. (PrivacyReporter::PrivacyReporter): Change type of resolver parameter. (PrivacyReporter::resolver): Change member variable type. * checks/errors/privacy/rust-visibility-resolver.cc: Adjust includes. (VisibilityResolver::VisibilityResolver): Change type of resolver parameter. (VisibilityResolver::resolve_module_path): Assume nr2.0 is enabled. * checks/errors/privacy/rust-visibility-resolver.h: Adjust includes. (VisibilityResolver::VisibilityResolver): Change type of resolver parameter. (VisibilityResolver::resolver): Change member variable type. Signed-off-by: Owen Avery <[email protected]>
1 parent 7dba381 commit f536bd4

File tree

5 files changed

+29
-78
lines changed

5 files changed

+29
-78
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "rust-reachability.h"
2121
#include "rust-hir-type-check.h"
2222
#include "rust-hir-map.h"
23-
#include "rust-name-resolver.h"
23+
#include "rust-immutable-name-resolution-context.h"
2424
#include "rust-visibility-resolver.h"
2525
#include "rust-pub-restricted-visitor.h"
2626
#include "rust-privacy-reporter.h"
@@ -35,12 +35,13 @@ Resolver::resolve (HIR::Crate &crate)
3535
{
3636
PrivacyContext ctx;
3737
auto &mappings = Analysis::Mappings::get ();
38-
auto resolver = Rust::Resolver::Resolver::get ();
38+
auto &resolver
39+
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
3940
auto ty_ctx = ::Rust::Resolver::TypeCheckContext::get ();
4041

41-
VisibilityResolver (mappings, *resolver).go (crate);
42+
VisibilityResolver (mappings, resolver).go (crate);
4243
PubRestrictedVisitor (mappings).go (crate);
43-
PrivacyReporter (mappings, *resolver, *ty_ctx).go (crate);
44+
PrivacyReporter (mappings, resolver, *ty_ctx).go (crate);
4445

4546
auto visitor = ReachabilityVisitor (ctx, *ty_ctx);
4647

gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ namespace Rust {
2828
namespace Privacy {
2929

3030
PrivacyReporter::PrivacyReporter (
31-
Analysis::Mappings &mappings, Resolver::Resolver &resolver,
31+
Analysis::Mappings &mappings,
32+
const Resolver2_0::NameResolutionContext &resolver,
3233
const Rust::Resolver::TypeCheckContext &ty_ctx)
3334
: mappings (mappings), resolver (resolver), ty_ctx (ty_ctx),
3435
current_module (tl::nullopt)
@@ -90,59 +91,18 @@ PrivacyReporter::go (HIR::Crate &crate)
9091
}
9192
}
9293

93-
static bool
94-
is_child_module (Analysis::Mappings &mappings, NodeId parent,
95-
NodeId possible_child)
96-
{
97-
if (flag_name_resolution_2_0)
98-
{
99-
auto &nr_ctx
100-
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
101-
102-
return nr_ctx.values.is_module_descendant (parent, possible_child);
103-
}
104-
105-
auto children = mappings.lookup_module_children (parent);
106-
107-
if (!children)
108-
return false;
109-
110-
// Visit all toplevel children
111-
for (auto &child : *children)
112-
if (child == possible_child)
113-
return true;
114-
115-
// Now descend recursively in the child module tree
116-
for (auto &child : *children)
117-
if (is_child_module (mappings, child, possible_child))
118-
return true;
119-
120-
return false;
121-
}
122-
12394
// FIXME: This function needs a lot of refactoring
12495
void
12596
PrivacyReporter::check_for_privacy_violation (const NodeId &use_id,
12697
const location_t locus)
12798
{
128-
NodeId ref_node_id = UNKNOWN_NODEID;
129-
130-
if (flag_name_resolution_2_0)
131-
{
132-
auto &nr_ctx
133-
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
134-
135-
if (auto id = nr_ctx.lookup (use_id))
136-
ref_node_id = *id;
137-
}
138-
// FIXME: Don't assert here - we might be dealing with a type
139-
else if (!resolver.lookup_resolved_name (use_id, &ref_node_id))
140-
resolver.lookup_resolved_type (use_id, &ref_node_id);
99+
NodeId ref_node_id;
141100

142101
// FIXME: Assert here. For now, we return since this causes issues when
143102
// checking inferred types (#1260)
144-
// rust_assert (ref_node_id != UNKNOWN_NODEID);
145-
if (ref_node_id == UNKNOWN_NODEID)
103+
if (auto id = resolver.lookup (use_id))
104+
ref_node_id = *id;
105+
else
146106
return;
147107

148108
auto vis = mappings.lookup_visibility (ref_node_id);
@@ -175,7 +135,9 @@ PrivacyReporter::check_for_privacy_violation (const NodeId &use_id,
175135

176136
// FIXME: This needs a LOT of TLC: hinting about the definition, a
177137
// string to say if it's a module, function, type, etc...
178-
if (!is_child_module (mappings, mod_node_id, current_module.value ()))
138+
139+
if (!resolver.values.is_module_descendant (mod_node_id,
140+
current_module.value ()))
179141
valid = false;
180142
}
181143
break;

gcc/rust/checks/errors/privacy/rust-privacy-reporter.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
#include "rust-hir-expr.h"
2323
#include "rust-hir-map.h"
2424
#include "rust-hir-visitor.h"
25+
#include "rust-hir-type-check.h"
2526
#include "rust-mapping-common.h"
26-
#include "rust-name-resolver.h"
27+
#include "rust-name-resolution-context.h"
2728

2829
namespace Rust {
2930
namespace Privacy {
@@ -38,7 +39,7 @@ class PrivacyReporter : public HIR::HIRExpressionVisitor,
3839
{
3940
public:
4041
PrivacyReporter (Analysis::Mappings &mappings,
41-
Rust::Resolver::Resolver &resolver,
42+
const Resolver2_0::NameResolutionContext &resolver,
4243
const Rust::Resolver::TypeCheckContext &ty_ctx);
4344

4445
/**
@@ -157,7 +158,7 @@ types
157158
virtual void visit (HIR::ExprStmt &stmt);
158159

159160
Analysis::Mappings &mappings;
160-
Rust::Resolver::Resolver &resolver;
161+
const Resolver2_0::NameResolutionContext &resolver;
161162
const Rust::Resolver::TypeCheckContext &ty_ctx;
162163

163164
// `None` means we're in the root module - the crate

gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@
2020
#include "rust-ast.h"
2121
#include "rust-hir.h"
2222
#include "rust-hir-item.h"
23-
#include "rust-immutable-name-resolution-context.h"
24-
25-
// for flag_name_resolution_2_0
26-
#include "options.h"
23+
#include "rust-name-resolution-context.h"
2724

2825
namespace Rust {
2926
namespace Privacy {
3027

31-
VisibilityResolver::VisibilityResolver (Analysis::Mappings &mappings,
32-
Resolver::Resolver &resolver)
28+
VisibilityResolver::VisibilityResolver (
29+
Analysis::Mappings &mappings,
30+
const Resolver2_0::NameResolutionContext &resolver)
3331
: mappings (mappings), resolver (resolver)
3432
{}
3533

@@ -64,23 +62,12 @@ VisibilityResolver::resolve_module_path (const HIR::SimplePath &restriction,
6462
= Error (restriction.get_locus (),
6563
"cannot use non-module path as privacy restrictor");
6664

67-
NodeId ref_node_id = UNKNOWN_NODEID;
68-
if (flag_name_resolution_2_0)
65+
NodeId ref_node_id;
66+
if (auto id = resolver.lookup (ast_node_id))
6967
{
70-
auto &nr_ctx
71-
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
72-
73-
if (auto id = nr_ctx.lookup (ast_node_id))
74-
{
75-
ref_node_id = *id;
76-
}
77-
else
78-
{
79-
invalid_path.emit ();
80-
return false;
81-
}
68+
ref_node_id = *id;
8269
}
83-
else if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id))
70+
else
8471
{
8572
invalid_path.emit ();
8673
return false;

gcc/rust/checks/errors/privacy/rust-visibility-resolver.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "rust-hir-stmt.h"
2525
#include "rust-hir-item.h"
2626
#include "rust-hir-map.h"
27-
#include "rust-name-resolver.h"
27+
#include "rust-name-resolution-context.h"
2828
#include "rust-hir-visitor.h"
2929

3030
namespace Rust {
@@ -34,7 +34,7 @@ class VisibilityResolver : public HIR::HIRVisItemVisitor
3434
{
3535
public:
3636
VisibilityResolver (Analysis::Mappings &mappings,
37-
Rust::Resolver::Resolver &resolver);
37+
const Resolver2_0::NameResolutionContext &resolver);
3838

3939
/**
4040
* Perform visibility resolving on an entire crate
@@ -93,7 +93,7 @@ class VisibilityResolver : public HIR::HIRVisItemVisitor
9393

9494
private:
9595
Analysis::Mappings &mappings;
96-
Rust::Resolver::Resolver &resolver;
96+
const Resolver2_0::NameResolutionContext &resolver;
9797
DefId current_module;
9898
};
9999

0 commit comments

Comments
 (0)