Skip to content
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

Fix incorrect trait privacy error #32073

Merged
merged 4 commits into from
Mar 9, 2016
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
2 changes: 2 additions & 0 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub trait CrateStore<'tcx> : Any {
// item info
fn stability(&self, def: DefId) -> Option<attr::Stability>;
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation>;
fn visibility(&self, def: DefId) -> hir::Visibility;
fn closure_kind(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
-> ty::ClosureKind;
fn closure_ty(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
Expand Down Expand Up @@ -302,6 +303,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
// item info
fn stability(&self, def: DefId) -> Option<attr::Stability> { unimplemented!() }
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> { unimplemented!() }
fn visibility(&self, def: DefId) -> hir::Visibility { unimplemented!() }
fn closure_kind(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
-> ty::ClosureKind { unimplemented!() }
fn closure_ty(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
Expand Down
5 changes: 1 addition & 4 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,6 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
freevars,
export_map,
trait_map,
external_exports,
glob_map,
} = time(time_passes,
"resolution",
Expand Down Expand Up @@ -822,9 +821,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,

analysis.access_levels =
time(time_passes, "privacy checking", || {
rustc_privacy::check_crate(tcx,
&analysis.export_map,
external_exports)
rustc_privacy::check_crate(tcx, &analysis.export_map)
});

// Do not move this check past lint
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
decoder::get_deprecation(&cdata, def.index)
}

fn visibility(&self, def: DefId) -> hir::Visibility {
let cdata = self.get_crate_data(def.krate);
decoder::get_visibility(&cdata, def.index)
}

fn closure_kind(&self, _tcx: &TyCtxt<'tcx>, def_id: DefId) -> ty::ClosureKind
{
assert!(!def_id.is_local());
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ pub fn get_deprecation(cdata: Cmd, id: DefIndex) -> Option<attr::Deprecation> {
})
}

pub fn get_visibility(cdata: Cmd, id: DefIndex) -> hir::Visibility {
item_visibility(cdata.lookup_item(id))
}

pub fn get_repr_attrs(cdata: Cmd, id: DefIndex) -> Vec<attr::ReprAttr> {
let item = cdata.lookup_item(id);
match reader::maybe_get_doc(item, tag_items_data_item_repr).map(|doc| {
Expand Down
11 changes: 3 additions & 8 deletions src/librustc_privacy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ use rustc_front::intravisit::{self, Visitor};

use rustc::dep_graph::DepNode;
use rustc::lint;
use rustc::middle::cstore::CrateStore;
use rustc::middle::def::{self, Def};
use rustc::middle::def_id::DefId;
use rustc::middle::privacy::{AccessLevel, AccessLevels};
use rustc::middle::privacy::ExternalExports;
use rustc::middle::ty::{self, TyCtxt};
use rustc::util::nodemap::{NodeMap, NodeSet};
use rustc::front::map as ast_map;
Expand Down Expand Up @@ -476,7 +476,6 @@ struct PrivacyVisitor<'a, 'tcx: 'a> {
curitem: ast::NodeId,
in_foreign: bool,
parents: NodeMap<ast::NodeId>,
external_exports: ExternalExports,
}

#[derive(Debug)]
Expand All @@ -498,7 +497,7 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
let node_id = if let Some(node_id) = self.tcx.map.as_local_node_id(did) {
node_id
} else {
if self.external_exports.contains(&did) {
if self.tcx.sess.cstore.visibility(did) == hir::Public {
debug!("privacy - {:?} was externally exported", did);
return Allowable;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terrible triplication below this line. Could you clean it up?

Expand Down Expand Up @@ -1567,10 +1566,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivateItemsInPublicInterfacesVisitor<'a, 'tc
}
}

pub fn check_crate(tcx: &TyCtxt,
export_map: &def::ExportMap,
external_exports: ExternalExports)
-> AccessLevels {
pub fn check_crate(tcx: &TyCtxt, export_map: &def::ExportMap) -> AccessLevels {
let _task = tcx.dep_graph.in_task(DepNode::Privacy);

let krate = tcx.map.krate();
Expand All @@ -1593,7 +1589,6 @@ pub fn check_crate(tcx: &TyCtxt,
in_foreign: false,
tcx: tcx,
parents: visitor.parents,
external_exports: external_exports,
};
intravisit::walk_crate(&mut visitor, krate);

Expand Down
14 changes: 0 additions & 14 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
krate: crate_id,
index: CRATE_DEF_INDEX,
};
self.external_exports.insert(def_id);
let parent_link = ModuleParentLink(parent, name);
let def = Def::Mod(def_id);
let module = self.new_extern_crate_module(parent_link, def, is_public, item.id);
Expand Down Expand Up @@ -495,15 +494,6 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
modifiers = modifiers | DefModifiers::IMPORTABLE;
}

let is_exported = is_public &&
match new_parent.def_id() {
None => true,
Some(did) => self.external_exports.contains(&did),
};
if is_exported {
self.external_exports.insert(def.def_id());
}

match def {
Def::Mod(_) | Def::ForeignMod(_) | Def::Enum(..) | Def::TyAlias(..) => {
debug!("(building reduced graph for external crate) building module {} {}",
Expand Down Expand Up @@ -552,10 +542,6 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
trait_item_name);

self.trait_item_map.insert((trait_item_name, def_id), trait_item_def.def_id());

if is_exported {
self.external_exports.insert(trait_item_def.def_id());
}
}

let parent_link = ModuleParentLink(new_parent, name);
Expand Down
7 changes: 1 addition & 6 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ use rustc::middle::cstore::{CrateStore, DefLike, DlDef};
use rustc::middle::def::*;
use rustc::middle::def_id::DefId;
use rustc::middle::pat_util::pat_bindings;
use rustc::middle::privacy::ExternalExports;
use rustc::middle::subst::{ParamSpace, FnSpace, TypeSpace};
use rustc::middle::ty::{Freevar, FreevarMap, TraitMap, GlobMap};
use rustc::util::nodemap::{NodeMap, DefIdSet, FnvHashMap};
use rustc::util::nodemap::{NodeMap, FnvHashMap};

use syntax::ast::{self, FloatTy};
use syntax::ast::{CRATE_NODE_ID, Name, NodeId, CrateNum, IntTy, UintTy};
Expand Down Expand Up @@ -1093,7 +1092,6 @@ pub struct Resolver<'a, 'tcx: 'a> {
freevars_seen: NodeMap<NodeMap<usize>>,
export_map: ExportMap,
trait_map: TraitMap,
external_exports: ExternalExports,

// Whether or not to print error messages. Can be set to true
// when getting additional info for error message suggestions,
Expand Down Expand Up @@ -1184,7 +1182,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
trait_map: NodeMap(),
used_imports: HashSet::new(),
used_crates: HashSet::new(),
external_exports: DefIdSet(),

emit_errors: true,
make_glob_map: make_glob_map == MakeGlobMap::Yes,
Expand Down Expand Up @@ -3716,7 +3713,6 @@ pub struct CrateMap {
pub freevars: FreevarMap,
pub export_map: ExportMap,
pub trait_map: TraitMap,
pub external_exports: ExternalExports,
pub glob_map: Option<GlobMap>,
}

Expand Down Expand Up @@ -3754,7 +3750,6 @@ pub fn resolve_crate<'a, 'tcx>(session: &'a Session,
freevars: resolver.freevars,
export_map: resolver.export_map,
trait_map: resolver.trait_map,
external_exports: resolver.external_exports,
glob_map: if resolver.make_glob_map {
Some(resolver.glob_map)
} else {
Expand Down
7 changes: 6 additions & 1 deletion src/test/compile-fail/trait-privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(rustc_attrs)]
#![feature(rustc_attrs, get_type_id)]
#![allow(dead_code)]

mod foo {
Expand All @@ -26,5 +26,10 @@ fn g() {
().f(); // Check that this does not trigger a privacy error
}

fn f() {
let error = ::std::thread::spawn(|| {}).join().unwrap_err();
error.get_type_id(); // Regression test for #21670
}

#[rustc_error]
fn main() {} //~ ERROR compilation successful