Skip to content

Commit 10a583c

Browse files
committed
Correctly encode item visibility in metadata
This fixes private statics and functions from being usable cross-crates, along with some bad privacy error messages. This is a reopening of #8365 with all the privacy checks in privacy.rs instead of resolve.rs (where they should be anyway). These maps of exported items will hopefully get used for generating documentation by rustdoc Closes #8592
1 parent 7535479 commit 10a583c

27 files changed

+494
-161
lines changed

src/libextra/arc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ struct MutexArcInner<T> { priv lock: Mutex, priv failed: bool, priv data: T }
162162

163163
/// An Arc with mutable data protected by a blocking mutex.
164164
#[no_freeze]
165-
struct MutexArc<T> { priv x: UnsafeArc<MutexArcInner<T>> }
165+
pub struct MutexArc<T> { priv x: UnsafeArc<MutexArcInner<T>> }
166166

167167

168168
impl<T:Send> Clone for MutexArc<T> {
@@ -343,7 +343,7 @@ struct RWArcInner<T> { priv lock: RWLock, priv failed: bool, priv data: T }
343343
* Unlike mutex_arcs, rw_arcs are safe, because they cannot be nested.
344344
*/
345345
#[no_freeze]
346-
struct RWArc<T> {
346+
pub struct RWArc<T> {
347347
priv x: UnsafeArc<RWArcInner<T>>,
348348
}
349349

src/libextra/workcache.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl WorkMap {
127127
}
128128
}
129129

130-
struct Database {
130+
pub struct Database {
131131
db_filename: Path,
132132
db_cache: TreeMap<~str, ~str>,
133133
db_dirty: bool
@@ -207,7 +207,7 @@ impl Drop for Database {
207207
}
208208
}
209209

210-
struct Logger {
210+
pub struct Logger {
211211
// FIXME #4432: Fill in
212212
a: ()
213213
}
@@ -223,10 +223,10 @@ impl Logger {
223223
}
224224
}
225225
226-
type FreshnessMap = TreeMap<~str,extern fn(&str,&str)->bool>;
226+
pub type FreshnessMap = TreeMap<~str,extern fn(&str,&str)->bool>;
227227
228228
#[deriving(Clone)]
229-
struct Context {
229+
pub struct Context {
230230
db: RWArc<Database>,
231231
logger: RWArc<Logger>,
232232
cfg: Arc<json::Object>,
@@ -239,13 +239,13 @@ struct Context {
239239
freshness: Arc<FreshnessMap>
240240
}
241241
242-
struct Prep<'self> {
242+
pub struct Prep<'self> {
243243
ctxt: &'self Context,
244244
fn_name: &'self str,
245245
declared_inputs: WorkMap,
246246
}
247247
248-
struct Exec {
248+
pub struct Exec {
249249
discovered_inputs: WorkMap,
250250
discovered_outputs: WorkMap
251251
}

src/librustc/driver/driver.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ pub fn phase_2_configure_and_expand(sess: Session,
197197

198198
pub struct CrateAnalysis {
199199
exp_map2: middle::resolve::ExportMap2,
200+
exported_items: @middle::privacy::ExportedItems,
200201
ty_cx: ty::ctxt,
201202
maps: astencode::Maps,
202203
reachable: @mut HashSet<ast::NodeId>
@@ -258,8 +259,9 @@ pub fn phase_3_run_analysis_passes(sess: Session,
258259
middle::check_const::check_crate(sess, crate, ast_map, def_map,
259260
method_map, ty_cx));
260261

261-
time(time_passes, ~"privacy checking", ||
262-
middle::privacy::check_crate(ty_cx, &method_map, crate));
262+
let exported_items =
263+
time(time_passes, ~"privacy checking", ||
264+
middle::privacy::check_crate(ty_cx, &method_map, &exp_map2, crate));
263265

264266
time(time_passes, ~"effect checking", ||
265267
middle::effect::check_crate(ty_cx, method_map, crate));
@@ -301,6 +303,7 @@ pub fn phase_3_run_analysis_passes(sess: Session,
301303

302304
CrateAnalysis {
303305
exp_map2: exp_map2,
306+
exported_items: @exported_items,
304307
ty_cx: ty_cx,
305308
maps: astencode::Maps {
306309
root_map: root_map,

src/librustc/metadata/csearch.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ use syntax::diagnostic::expect;
2727
pub struct StaticMethodInfo {
2828
ident: ast::Ident,
2929
def_id: ast::DefId,
30-
purity: ast::purity
30+
purity: ast::purity,
31+
vis: ast::visibility,
3132
}
3233

3334
pub fn get_symbol(cstore: @mut cstore::CStore, def: ast::DefId) -> ~str {
@@ -52,7 +53,8 @@ pub fn each_lang_item(cstore: @mut cstore::CStore,
5253
/// Iterates over each child of the given item.
5354
pub fn each_child_of_item(cstore: @mut cstore::CStore,
5455
def_id: ast::DefId,
55-
callback: &fn(decoder::DefLike, ast::Ident)) {
56+
callback: &fn(decoder::DefLike, ast::Ident,
57+
ast::visibility)) {
5658
let crate_data = cstore::get_crate_data(cstore, def_id.crate);
5759
let get_crate_data: decoder::GetCrateDataCb = |cnum| {
5860
cstore::get_crate_data(cstore, cnum)
@@ -68,7 +70,8 @@ pub fn each_child_of_item(cstore: @mut cstore::CStore,
6870
pub fn each_top_level_item_of_crate(cstore: @mut cstore::CStore,
6971
cnum: ast::CrateNum,
7072
callback: &fn(decoder::DefLike,
71-
ast::Ident)) {
73+
ast::Ident,
74+
ast::visibility)) {
7275
let crate_data = cstore::get_crate_data(cstore, cnum);
7376
let get_crate_data: decoder::GetCrateDataCb = |cnum| {
7477
cstore::get_crate_data(cstore, cnum)

src/librustc/metadata/decoder.rs

+26-17
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn find_item(item_id: int, items: ebml::Doc) -> ebml::Doc {
9696

9797
// Looks up an item in the given metadata and returns an ebml doc pointing
9898
// to the item data.
99-
fn lookup_item(item_id: int, data: @~[u8]) -> ebml::Doc {
99+
pub fn lookup_item(item_id: int, data: @~[u8]) -> ebml::Doc {
100100
let items = reader::get_doc(reader::Doc(data), tag_items);
101101
find_item(item_id, items)
102102
}
@@ -291,7 +291,7 @@ fn enum_variant_ids(item: ebml::Doc, cdata: Cmd) -> ~[ast::DefId] {
291291
return ids;
292292
}
293293

294-
fn item_path(item_doc: ebml::Doc) -> ast_map::path {
294+
pub fn item_path(item_doc: ebml::Doc) -> ast_map::path {
295295
let path_doc = reader::get_doc(item_doc, tag_path);
296296

297297
let len_doc = reader::get_doc(path_doc, tag_path_len);
@@ -332,7 +332,7 @@ fn item_name(intr: @ident_interner, item: ebml::Doc) -> ast::Ident {
332332
}
333333
}
334334

335-
fn item_to_def_like(item: ebml::Doc, did: ast::DefId, cnum: ast::CrateNum)
335+
pub fn item_to_def_like(item: ebml::Doc, did: ast::DefId, cnum: ast::CrateNum)
336336
-> DefLike {
337337
let fam = item_family(item);
338338
match fam {
@@ -491,7 +491,7 @@ pub enum DefLike {
491491
DlField
492492
}
493493

494-
fn def_like_to_def(def_like: DefLike) -> ast::Def {
494+
pub fn def_like_to_def(def_like: DefLike) -> ast::Def {
495495
match def_like {
496496
DlDef(def) => return def,
497497
DlImpl(*) => fail!("found impl in def_like_to_def"),
@@ -544,7 +544,8 @@ impl<'self> EachItemContext<'self> {
544544
fn process_item_and_pop_name(&mut self,
545545
doc: ebml::Doc,
546546
def_id: ast::DefId,
547-
old_len: uint)
547+
old_len: uint,
548+
vis: ast::visibility)
548549
-> bool {
549550
let def_like = item_to_def_like(doc, def_id, self.cdata.cnum);
550551
match def_like {
@@ -563,8 +564,6 @@ impl<'self> EachItemContext<'self> {
563564
}
564565
}
565566

566-
let vis = item_visibility(doc);
567-
568567
let mut continue = (self.callback)(*self.path_builder, def_like, vis);
569568

570569
let family = item_family(doc);
@@ -653,9 +652,12 @@ impl<'self> EachItemContext<'self> {
653652
self.push_name(token::ident_to_str(&child_name));
654653

655654
// Process this item.
655+
656+
let vis = item_visibility(child_item_doc);
656657
continue = self.process_item_and_pop_name(child_item_doc,
657658
child_def_id,
658-
old_len);
659+
old_len,
660+
vis);
659661
}
660662
}
661663
continue
@@ -701,12 +703,13 @@ impl<'self> EachItemContext<'self> {
701703

702704
// Get the item.
703705
match maybe_find_item(def_id.node, other_crates_items) {
704-
None => {}
706+
None => { self.pop_name(old_len); }
705707
Some(reexported_item_doc) => {
706708
continue = self.process_item_and_pop_name(
707709
reexported_item_doc,
708710
def_id,
709-
old_len);
711+
old_len,
712+
ast::public);
710713
}
711714
}
712715

@@ -721,7 +724,8 @@ fn each_child_of_item_or_crate(intr: @ident_interner,
721724
cdata: Cmd,
722725
item_doc: ebml::Doc,
723726
get_crate_data: GetCrateDataCb,
724-
callback: &fn(DefLike, ast::Ident)) {
727+
callback: &fn(DefLike, ast::Ident,
728+
ast::visibility)) {
725729
// Iterate over all children.
726730
let _ = do reader::tagged_docs(item_doc, tag_mod_child) |child_info_doc| {
727731
let child_def_id = reader::with_doc_data(child_info_doc,
@@ -746,7 +750,8 @@ fn each_child_of_item_or_crate(intr: @ident_interner,
746750
let def_like = item_to_def_like(child_item_doc,
747751
child_def_id,
748752
cdata.cnum);
749-
callback(def_like, child_name);
753+
let visibility = item_visibility(child_item_doc);
754+
callback(def_like, child_name, visibility);
750755

751756
}
752757
}
@@ -788,7 +793,8 @@ fn each_child_of_item_or_crate(intr: @ident_interner,
788793
impl_method_def_id,
789794
cdata.cnum);
790795
callback(static_method_def_like,
791-
static_method_name);
796+
static_method_name,
797+
item_visibility(impl_method_doc));
792798
}
793799
_ => {}
794800
}
@@ -831,7 +837,8 @@ fn each_child_of_item_or_crate(intr: @ident_interner,
831837
let def_like = item_to_def_like(child_item_doc,
832838
child_def_id,
833839
cdata.cnum);
834-
callback(def_like, token::str_to_ident(name));
840+
callback(def_like, token::str_to_ident(name),
841+
item_visibility(child_item_doc));
835842
}
836843
}
837844

@@ -844,7 +851,7 @@ pub fn each_child_of_item(intr: @ident_interner,
844851
cdata: Cmd,
845852
id: ast::NodeId,
846853
get_crate_data: GetCrateDataCb,
847-
callback: &fn(DefLike, ast::Ident)) {
854+
callback: &fn(DefLike, ast::Ident, ast::visibility)) {
848855
// Find the item.
849856
let root_doc = reader::Doc(cdata.data);
850857
let items = reader::get_doc(root_doc, tag_items);
@@ -864,7 +871,8 @@ pub fn each_child_of_item(intr: @ident_interner,
864871
pub fn each_top_level_item_of_crate(intr: @ident_interner,
865872
cdata: Cmd,
866873
get_crate_data: GetCrateDataCb,
867-
callback: &fn(DefLike, ast::Ident)) {
874+
callback: &fn(DefLike, ast::Ident,
875+
ast::visibility)) {
868876
let root_doc = reader::Doc(cdata.data);
869877
let misc_info_doc = reader::get_doc(root_doc, tag_misc_info);
870878
let crate_items_doc = reader::get_doc(misc_info_doc,
@@ -1161,7 +1169,8 @@ pub fn get_static_methods_if_impl(intr: @ident_interner,
11611169
static_impl_methods.push(StaticMethodInfo {
11621170
ident: item_name(intr, impl_method_doc),
11631171
def_id: item_def_id(impl_method_doc, cdata),
1164-
purity: purity
1172+
purity: purity,
1173+
vis: item_visibility(impl_method_doc),
11651174
});
11661175
}
11671176
_ => {}

src/librustc/metadata/encoder.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use metadata::cstore;
1616
use metadata::decoder;
1717
use metadata::tyencode;
1818
use middle::ty::{node_id_to_type, lookup_item_type};
19+
use middle::astencode;
1920
use middle::ty;
2021
use middle::typeck;
21-
use middle::astencode;
2222
use middle;
2323

2424
use std::hashmap::{HashMap, HashSet};
@@ -58,6 +58,7 @@ pub struct EncodeParams<'self> {
5858
diag: @mut span_handler,
5959
tcx: ty::ctxt,
6060
reexports2: middle::resolve::ExportMap2,
61+
exported_items: @middle::privacy::ExportedItems,
6162
item_symbols: &'self HashMap<ast::NodeId, ~str>,
6263
discrim_symbols: &'self HashMap<ast::NodeId, @str>,
6364
non_inlineable_statics: &'self HashSet<ast::NodeId>,
@@ -88,6 +89,7 @@ pub struct EncodeContext<'self> {
8889
tcx: ty::ctxt,
8990
stats: @mut Stats,
9091
reexports2: middle::resolve::ExportMap2,
92+
exported_items: @middle::privacy::ExportedItems,
9193
item_symbols: &'self HashMap<ast::NodeId, ~str>,
9294
discrim_symbols: &'self HashMap<ast::NodeId, @str>,
9395
non_inlineable_statics: &'self HashSet<ast::NodeId>,
@@ -881,7 +883,8 @@ fn encode_info_for_item(ecx: &EncodeContext,
881883
ebml_w: &mut writer::Encoder,
882884
item: @item,
883885
index: @mut ~[entry<i64>],
884-
path: &[ast_map::path_elt]) {
886+
path: &[ast_map::path_elt],
887+
vis: ast::visibility) {
885888
let tcx = ecx.tcx;
886889

887890
fn add_to_index_(item: @item, ebml_w: &writer::Encoder,
@@ -912,6 +915,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
912915
if !ecx.non_inlineable_statics.contains(&item.id) {
913916
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item));
914917
}
918+
encode_visibility(ebml_w, vis);
915919
ebml_w.end_tag();
916920
}
917921
item_fn(_, purity, _, ref generics, _) => {
@@ -929,6 +933,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
929933
} else {
930934
encode_symbol(ecx, ebml_w, item.id);
931935
}
936+
encode_visibility(ebml_w, vis);
932937
ebml_w.end_tag();
933938
}
934939
item_mod(ref m) => {
@@ -955,7 +960,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
955960
ebml_w.wr_str(def_to_str(local_def(foreign_item.id)));
956961
ebml_w.end_tag();
957962
}
958-
963+
encode_visibility(ebml_w, vis);
959964
ebml_w.end_tag();
960965
}
961966
item_ty(*) => {
@@ -967,6 +972,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
967972
encode_name(ecx, ebml_w, item.ident);
968973
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
969974
encode_region_param(ecx, ebml_w, item);
975+
encode_visibility(ebml_w, vis);
970976
ebml_w.end_tag();
971977
}
972978
item_enum(ref enum_definition, ref generics) => {
@@ -987,6 +993,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
987993
// Encode inherent implementations for this enumeration.
988994
encode_inherent_implementations(ecx, ebml_w, def_id);
989995

996+
encode_visibility(ebml_w, vis);
990997
ebml_w.end_tag();
991998

992999
encode_enum_variant_info(ecx,
@@ -1018,6 +1025,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
10181025
encode_attributes(ebml_w, item.attrs);
10191026
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
10201027
encode_region_param(ecx, ebml_w, item);
1028+
encode_visibility(ebml_w, vis);
10211029

10221030
/* Encode def_ids for each field and method
10231031
for methods, write all the stuff get_trait_method
@@ -1264,7 +1272,12 @@ fn my_visit_item(i:@item, items: ast_map::map, ebml_w:&writer::Encoder,
12641272
let mut ebml_w = ebml_w.clone();
12651273
// See above
12661274
let ecx : &EncodeContext = unsafe { cast::transmute(ecx_ptr) };
1267-
encode_info_for_item(ecx, &mut ebml_w, i, index, *pt);
1275+
let vis = if ecx.exported_items.contains(&i.id) {
1276+
ast::public
1277+
} else {
1278+
ast::inherited
1279+
};
1280+
encode_info_for_item(ecx, &mut ebml_w, i, index, *pt, vis);
12681281
}
12691282
_ => fail!("bad item")
12701283
}
@@ -1727,6 +1740,7 @@ pub fn encode_metadata(parms: EncodeParams, crate: &Crate) -> ~[u8] {
17271740
diag,
17281741
tcx,
17291742
reexports2,
1743+
exported_items,
17301744
discrim_symbols,
17311745
cstore,
17321746
encode_inlined_item,
@@ -1742,6 +1756,7 @@ pub fn encode_metadata(parms: EncodeParams, crate: &Crate) -> ~[u8] {
17421756
tcx: tcx,
17431757
stats: stats,
17441758
reexports2: reexports2,
1759+
exported_items: exported_items,
17451760
item_symbols: item_symbols,
17461761
discrim_symbols: discrim_symbols,
17471762
non_inlineable_statics: non_inlineable_statics,

0 commit comments

Comments
 (0)