Skip to content

Fix some pub use privacy errors and a bit of resolve refactoring #23400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 17, 2015
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 5 additions & 3 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1961,16 +1961,18 @@ module through the rules above. It essentially allows public access into the
re-exported item. For example, this program is valid:

```
pub use self::implementation as api;
pub use self::implementation::api;

mod implementation {
pub fn f() {}
pub mod api {
pub fn f() {}
}
}

# fn main() {}
```

This means that any external crate referencing `implementation::f` would
This means that any external crate referencing `implementation::api::f` would
receive a privacy violation, while the path `api::f` would be allowed.

When re-exporting a private item, it can be thought of as allowing the "privacy
Expand Down
41 changes: 18 additions & 23 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
//! any imports resolved.

use {DefModifiers, PUBLIC, IMPORTABLE};
use ImportDirective;
use ImportDirectiveSubclass::{self, SingleImport, GlobImport};
use ImportResolution;
use resolve_imports::ImportDirective;
use resolve_imports::ImportDirectiveSubclass::{self, SingleImport, GlobImport};
use resolve_imports::ImportResolution;
use Module;
use ModuleKind::*;
use Namespace::{TypeNS, ValueNS};
use NameBindings;
use {names_to_string, module_to_string};
use ParentLink::{self, ModuleParentLink, BlockParentLink};
use Resolver;
use Shadowable;
use resolve_imports::Shadowable;
use TypeNsDef;

use self::DuplicateCheckingMode::*;
Expand Down Expand Up @@ -371,8 +372,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {

ItemExternCrate(_) => {
// n.b. we don't need to look at the path option here, because cstore already did
for &crate_id in self.session.cstore
.find_extern_mod_stmt_cnum(item.id).iter() {
for &crate_id in self.session.cstore.find_extern_mod_stmt_cnum(item.id).iter() {
let def_id = DefId { krate: crate_id, node: 0 };
self.external_exports.insert(def_id);
let parent_link = ModuleParentLink(parent.downgrade(), name);
Expand All @@ -382,7 +382,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
false,
true));
debug!("(build reduced graph for item) found extern `{}`",
self.module_to_string(&*external_module));
module_to_string(&*external_module));
self.check_for_conflicts_between_external_crates(&**parent, name, sp);
parent.external_module_children.borrow_mut()
.insert(name, external_module.clone());
Expand All @@ -400,7 +400,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
Some(def_id),
NormalModuleKind,
false,
item.vis == ast::Public,
is_public,
sp);

name_bindings.get_module()
Expand Down Expand Up @@ -432,8 +432,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
// These items live in the type namespace.
ItemTy(..) => {
let name_bindings =
self.add_child(name, parent, ForbidDuplicateTypesAndModules,
sp);
self.add_child(name, parent, ForbidDuplicateTypesAndModules, sp);

name_bindings.define_type(DefTy(local_def(item.id), false), sp,
modifiers);
Expand Down Expand Up @@ -517,7 +516,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
Some(local_def(item.id)),
TraitModuleKind,
false,
item.vis == ast::Public,
is_public,
sp);
let module_parent = name_bindings.get_module();

Expand Down Expand Up @@ -636,8 +635,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
name: Name,
new_parent: &Rc<Module>) {
debug!("(building reduced graph for \
external crate) building external def, priv {:?}",
vis);
external crate) building external def {}, priv {:?}",
final_ident, vis);
let is_public = vis == ast::Public;
let modifiers = if is_public { PUBLIC } else { DefModifiers::empty() } | IMPORTABLE;
let is_exported = is_public && match new_parent.def_id.get() {
Expand Down Expand Up @@ -667,7 +666,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
Some(_) | None => {
debug!("(building reduced graph for \
external crate) building module \
{}", final_ident);
{} {}", final_ident, is_public);
let parent_link = self.get_parent_link(new_parent, name);

child_name_bindings.define_module(parent_link,
Expand Down Expand Up @@ -838,7 +837,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
/// Builds the reduced graph rooted at the given external module.
fn populate_external_module(&mut self, module: &Rc<Module>) {
debug!("(populating external module) attempting to populate {}",
self.module_to_string(&**module));
module_to_string(&**module));

let def_id = match module.def_id.get() {
None => {
Expand Down Expand Up @@ -904,18 +903,14 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {

match subclass {
SingleImport(target, _) => {
debug!("(building import directive) building import \
directive: {}::{}",
self.names_to_string(&module_.imports.borrow().last().unwrap().
module_path),
debug!("(building import directive) building import directive: {}::{}",
names_to_string(&module_.imports.borrow().last().unwrap().module_path),
token::get_name(target));

let mut import_resolutions = module_.import_resolutions
.borrow_mut();
let mut import_resolutions = module_.import_resolutions.borrow_mut();
match import_resolutions.get_mut(&target) {
Some(resolution) => {
debug!("(building import directive) bumping \
reference");
debug!("(building import directive) bumping reference");
resolution.outstanding_references += 1;

// the source of this name is different now
Expand Down
4 changes: 3 additions & 1 deletion src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ register_diagnostics! {
E0258, // import conflicts with existing submodule
E0259, // an extern crate has already been imported into this module
E0260, // name conflicts with an external crate that has been imported into this module
E0317 // user-defined types or type parameters cannot shadow the primitive types
E0317, // user-defined types or type parameters cannot shadow the primitive types
E0364, // item is private
E0365 // item is private
}

__build_diagnostic_array! { DIAGNOSTICS }
Loading