Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions gcc/rust/checks/errors/privacy/rust-privacy-check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "rust-reachability.h"
#include "rust-hir-type-check.h"
#include "rust-hir-map.h"
#include "rust-name-resolver.h"
#include "rust-immutable-name-resolution-context.h"
#include "rust-visibility-resolver.h"
#include "rust-pub-restricted-visitor.h"
#include "rust-privacy-reporter.h"
Expand All @@ -35,12 +35,13 @@ Resolver::resolve (HIR::Crate &crate)
{
PrivacyContext ctx;
auto &mappings = Analysis::Mappings::get ();
auto resolver = Rust::Resolver::Resolver::get ();
auto &resolver
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
auto ty_ctx = ::Rust::Resolver::TypeCheckContext::get ();

VisibilityResolver (mappings, *resolver).go (crate);
VisibilityResolver (mappings, resolver).go (crate);
PubRestrictedVisitor (mappings).go (crate);
PrivacyReporter (mappings, *resolver, *ty_ctx).go (crate);
PrivacyReporter (mappings, resolver, *ty_ctx).go (crate);

auto visitor = ReachabilityVisitor (ctx, *ty_ctx);

Expand Down
56 changes: 9 additions & 47 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace Rust {
namespace Privacy {

PrivacyReporter::PrivacyReporter (
Analysis::Mappings &mappings, Resolver::Resolver &resolver,
Analysis::Mappings &mappings,
const Resolver2_0::NameResolutionContext &resolver,
const Rust::Resolver::TypeCheckContext &ty_ctx)
: mappings (mappings), resolver (resolver), ty_ctx (ty_ctx),
current_module (tl::nullopt)
Expand Down Expand Up @@ -90,59 +91,18 @@ PrivacyReporter::go (HIR::Crate &crate)
}
}

static bool
is_child_module (Analysis::Mappings &mappings, NodeId parent,
NodeId possible_child)
{
if (flag_name_resolution_2_0)
{
auto &nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

return nr_ctx.values.is_module_descendant (parent, possible_child);
}

auto children = mappings.lookup_module_children (parent);

if (!children)
return false;

// Visit all toplevel children
for (auto &child : *children)
if (child == possible_child)
return true;

// Now descend recursively in the child module tree
for (auto &child : *children)
if (is_child_module (mappings, child, possible_child))
return true;

return false;
}

// FIXME: This function needs a lot of refactoring
void
PrivacyReporter::check_for_privacy_violation (const NodeId &use_id,
const location_t locus)
{
NodeId ref_node_id = UNKNOWN_NODEID;

if (flag_name_resolution_2_0)
{
auto &nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

if (auto id = nr_ctx.lookup (use_id))
ref_node_id = *id;
}
// FIXME: Don't assert here - we might be dealing with a type
else if (!resolver.lookup_resolved_name (use_id, &ref_node_id))
resolver.lookup_resolved_type (use_id, &ref_node_id);
NodeId ref_node_id;

// FIXME: Assert here. For now, we return since this causes issues when
// checking inferred types (#1260)
// rust_assert (ref_node_id != UNKNOWN_NODEID);
if (ref_node_id == UNKNOWN_NODEID)
if (auto id = resolver.lookup (use_id))
ref_node_id = *id;
else
return;

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

// FIXME: This needs a LOT of TLC: hinting about the definition, a
// string to say if it's a module, function, type, etc...
if (!is_child_module (mappings, mod_node_id, current_module.value ()))

if (!resolver.values.is_module_descendant (mod_node_id,
current_module.value ()))
valid = false;
}
break;
Expand Down
7 changes: 4 additions & 3 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
#include "rust-hir-expr.h"
#include "rust-hir-map.h"
#include "rust-hir-visitor.h"
#include "rust-hir-type-check.h"
#include "rust-mapping-common.h"
#include "rust-name-resolver.h"
#include "rust-name-resolution-context.h"

namespace Rust {
namespace Privacy {
Expand All @@ -38,7 +39,7 @@ class PrivacyReporter : public HIR::HIRExpressionVisitor,
{
public:
PrivacyReporter (Analysis::Mappings &mappings,
Rust::Resolver::Resolver &resolver,
const Resolver2_0::NameResolutionContext &resolver,
const Rust::Resolver::TypeCheckContext &ty_ctx);

/**
Expand Down Expand Up @@ -157,7 +158,7 @@ types
virtual void visit (HIR::ExprStmt &stmt);

Analysis::Mappings &mappings;
Rust::Resolver::Resolver &resolver;
const Resolver2_0::NameResolutionContext &resolver;
const Rust::Resolver::TypeCheckContext &ty_ctx;

// `None` means we're in the root module - the crate
Expand Down
29 changes: 8 additions & 21 deletions gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
#include "rust-ast.h"
#include "rust-hir.h"
#include "rust-hir-item.h"
#include "rust-immutable-name-resolution-context.h"

// for flag_name_resolution_2_0
#include "options.h"
#include "rust-name-resolution-context.h"

namespace Rust {
namespace Privacy {

VisibilityResolver::VisibilityResolver (Analysis::Mappings &mappings,
Resolver::Resolver &resolver)
VisibilityResolver::VisibilityResolver (
Analysis::Mappings &mappings,
const Resolver2_0::NameResolutionContext &resolver)
: mappings (mappings), resolver (resolver)
{}

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

NodeId ref_node_id = UNKNOWN_NODEID;
if (flag_name_resolution_2_0)
NodeId ref_node_id;
if (auto id = resolver.lookup (ast_node_id))
{
auto &nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

if (auto id = nr_ctx.lookup (ast_node_id))
{
ref_node_id = *id;
}
else
{
invalid_path.emit ();
return false;
}
ref_node_id = *id;
}
else if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id))
else
{
invalid_path.emit ();
return false;
Expand Down
6 changes: 3 additions & 3 deletions gcc/rust/checks/errors/privacy/rust-visibility-resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "rust-hir-stmt.h"
#include "rust-hir-item.h"
#include "rust-hir-map.h"
#include "rust-name-resolver.h"
#include "rust-name-resolution-context.h"
#include "rust-hir-visitor.h"

namespace Rust {
Expand All @@ -34,7 +34,7 @@ class VisibilityResolver : public HIR::HIRVisItemVisitor
{
public:
VisibilityResolver (Analysis::Mappings &mappings,
Rust::Resolver::Resolver &resolver);
const Resolver2_0::NameResolutionContext &resolver);

/**
* Perform visibility resolving on an entire crate
Expand Down Expand Up @@ -93,7 +93,7 @@ class VisibilityResolver : public HIR::HIRVisItemVisitor

private:
Analysis::Mappings &mappings;
Rust::Resolver::Resolver &resolver;
const Resolver2_0::NameResolutionContext &resolver;
DefId current_module;
};

Expand Down
Loading