Skip to content

Commit ef3d051

Browse files
committed
Auto merge of #32073 - jseyfried:fix_another_trait_privacy_error, r=nikomatsakis
Fix incorrect trait privacy error This PR fixes #21670 by using the crate metadata instead of `ExternalExports` to determine if an external item is public. r? @nikomatsakis
2 parents cbbd3d9 + b06a1cc commit ef3d051

File tree

8 files changed

+22
-33
lines changed

8 files changed

+22
-33
lines changed

src/librustc/middle/cstore.rs

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ pub trait CrateStore<'tcx> : Any {
137137
// item info
138138
fn stability(&self, def: DefId) -> Option<attr::Stability>;
139139
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation>;
140+
fn visibility(&self, def: DefId) -> hir::Visibility;
140141
fn closure_kind(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
141142
-> ty::ClosureKind;
142143
fn closure_ty(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
@@ -302,6 +303,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
302303
// item info
303304
fn stability(&self, def: DefId) -> Option<attr::Stability> { unimplemented!() }
304305
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> { unimplemented!() }
306+
fn visibility(&self, def: DefId) -> hir::Visibility { unimplemented!() }
305307
fn closure_kind(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
306308
-> ty::ClosureKind { unimplemented!() }
307309
fn closure_ty(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)

src/librustc_driver/driver.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,6 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
768768
freevars,
769769
export_map,
770770
trait_map,
771-
external_exports,
772771
glob_map,
773772
} = time(time_passes,
774773
"resolution",
@@ -829,9 +828,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
829828

830829
analysis.access_levels =
831830
time(time_passes, "privacy checking", || {
832-
rustc_privacy::check_crate(tcx,
833-
&analysis.export_map,
834-
external_exports)
831+
rustc_privacy::check_crate(tcx, &analysis.export_map)
835832
});
836833

837834
// Do not move this check past lint

src/librustc_metadata/csearch.rs

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
4949
decoder::get_deprecation(&cdata, def.index)
5050
}
5151

52+
fn visibility(&self, def: DefId) -> hir::Visibility {
53+
let cdata = self.get_crate_data(def.krate);
54+
decoder::get_visibility(&cdata, def.index)
55+
}
56+
5257
fn closure_kind(&self, _tcx: &TyCtxt<'tcx>, def_id: DefId) -> ty::ClosureKind
5358
{
5459
assert!(!def_id.is_local());

src/librustc_metadata/decoder.rs

+4
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,10 @@ pub fn get_deprecation(cdata: Cmd, id: DefIndex) -> Option<attr::Deprecation> {
545545
})
546546
}
547547

548+
pub fn get_visibility(cdata: Cmd, id: DefIndex) -> hir::Visibility {
549+
item_visibility(cdata.lookup_item(id))
550+
}
551+
548552
pub fn get_repr_attrs(cdata: Cmd, id: DefIndex) -> Vec<attr::ReprAttr> {
549553
let item = cdata.lookup_item(id);
550554
match reader::maybe_get_doc(item, tag_items_data_item_repr).map(|doc| {

src/librustc_privacy/lib.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ use rustc_front::intravisit::{self, Visitor};
3838

3939
use rustc::dep_graph::DepNode;
4040
use rustc::lint;
41+
use rustc::middle::cstore::CrateStore;
4142
use rustc::middle::def::{self, Def};
4243
use rustc::middle::def_id::DefId;
4344
use rustc::middle::privacy::{AccessLevel, AccessLevels};
44-
use rustc::middle::privacy::ExternalExports;
4545
use rustc::middle::ty::{self, TyCtxt};
4646
use rustc::util::nodemap::{NodeMap, NodeSet};
4747
use rustc::front::map as ast_map;
@@ -476,7 +476,6 @@ struct PrivacyVisitor<'a, 'tcx: 'a> {
476476
curitem: ast::NodeId,
477477
in_foreign: bool,
478478
parents: NodeMap<ast::NodeId>,
479-
external_exports: ExternalExports,
480479
}
481480

482481
#[derive(Debug)]
@@ -498,7 +497,7 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
498497
let node_id = if let Some(node_id) = self.tcx.map.as_local_node_id(did) {
499498
node_id
500499
} else {
501-
if self.external_exports.contains(&did) {
500+
if self.tcx.sess.cstore.visibility(did) == hir::Public {
502501
debug!("privacy - {:?} was externally exported", did);
503502
return Allowable;
504503
}
@@ -1567,10 +1566,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivateItemsInPublicInterfacesVisitor<'a, 'tc
15671566
}
15681567
}
15691568

1570-
pub fn check_crate(tcx: &TyCtxt,
1571-
export_map: &def::ExportMap,
1572-
external_exports: ExternalExports)
1573-
-> AccessLevels {
1569+
pub fn check_crate(tcx: &TyCtxt, export_map: &def::ExportMap) -> AccessLevels {
15741570
let _task = tcx.dep_graph.in_task(DepNode::Privacy);
15751571

15761572
let krate = tcx.map.krate();
@@ -1593,7 +1589,6 @@ pub fn check_crate(tcx: &TyCtxt,
15931589
in_foreign: false,
15941590
tcx: tcx,
15951591
parents: visitor.parents,
1596-
external_exports: external_exports,
15971592
};
15981593
intravisit::walk_crate(&mut visitor, krate);
15991594

src/librustc_resolve/build_reduced_graph.rs

-14
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
289289
krate: crate_id,
290290
index: CRATE_DEF_INDEX,
291291
};
292-
self.external_exports.insert(def_id);
293292
let parent_link = ModuleParentLink(parent, name);
294293
let def = Def::Mod(def_id);
295294
let module = self.new_extern_crate_module(parent_link, def, is_public, item.id);
@@ -495,15 +494,6 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
495494
modifiers = modifiers | DefModifiers::IMPORTABLE;
496495
}
497496

498-
let is_exported = is_public &&
499-
match new_parent.def_id() {
500-
None => true,
501-
Some(did) => self.external_exports.contains(&did),
502-
};
503-
if is_exported {
504-
self.external_exports.insert(def.def_id());
505-
}
506-
507497
match def {
508498
Def::Mod(_) | Def::ForeignMod(_) | Def::Enum(..) | Def::TyAlias(..) => {
509499
debug!("(building reduced graph for external crate) building module {} {}",
@@ -552,10 +542,6 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
552542
trait_item_name);
553543

554544
self.trait_item_map.insert((trait_item_name, def_id), trait_item_def.def_id());
555-
556-
if is_exported {
557-
self.external_exports.insert(trait_item_def.def_id());
558-
}
559545
}
560546

561547
let parent_link = ModuleParentLink(new_parent, name);

src/librustc_resolve/lib.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ use rustc::middle::cstore::{CrateStore, DefLike, DlDef};
5555
use rustc::middle::def::*;
5656
use rustc::middle::def_id::DefId;
5757
use rustc::middle::pat_util::pat_bindings;
58-
use rustc::middle::privacy::ExternalExports;
5958
use rustc::middle::subst::{ParamSpace, FnSpace, TypeSpace};
6059
use rustc::middle::ty::{Freevar, FreevarMap, TraitMap, GlobMap};
61-
use rustc::util::nodemap::{NodeMap, DefIdSet, FnvHashMap};
60+
use rustc::util::nodemap::{NodeMap, FnvHashMap};
6261

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

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

11891186
emit_errors: true,
11901187
make_glob_map: make_glob_map == MakeGlobMap::Yes,
@@ -3716,7 +3713,6 @@ pub struct CrateMap {
37163713
pub freevars: FreevarMap,
37173714
pub export_map: ExportMap,
37183715
pub trait_map: TraitMap,
3719-
pub external_exports: ExternalExports,
37203716
pub glob_map: Option<GlobMap>,
37213717
}
37223718

@@ -3754,7 +3750,6 @@ pub fn resolve_crate<'a, 'tcx>(session: &'a Session,
37543750
freevars: resolver.freevars,
37553751
export_map: resolver.export_map,
37563752
trait_map: resolver.trait_map,
3757-
external_exports: resolver.external_exports,
37583753
glob_map: if resolver.make_glob_map {
37593754
Some(resolver.glob_map)
37603755
} else {

src/test/compile-fail/trait-privacy.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(rustc_attrs)]
11+
#![feature(rustc_attrs, get_type_id)]
1212
#![allow(dead_code)]
1313

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

29+
fn f() {
30+
let error = ::std::thread::spawn(|| {}).join().unwrap_err();
31+
error.get_type_id(); // Regression test for #21670
32+
}
33+
2934
#[rustc_error]
3035
fn main() {} //~ ERROR compilation successful

0 commit comments

Comments
 (0)