Skip to content

Commit 19b8598

Browse files
committed
rustc: Implement "priv" for simple items.
Inherited privacy doesn't work yet. This probably requires a snapshot since it won't be backwards compatible. Additionally, two errors are printed instead of one. For this reason the test is XFAIL'd.
1 parent fcb055e commit 19b8598

File tree

2 files changed

+137
-60
lines changed

2 files changed

+137
-60
lines changed

src/rustc/middle/resolve3.rs

Lines changed: 124 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,22 @@ import syntax::ast::{expr_binary, expr_break, expr_cast, expr_field, expr_fn};
2626
import syntax::ast::{expr_fn_block, expr_index, expr_loop};
2727
import syntax::ast::{expr_path, expr_struct, expr_unary, fn_decl};
2828
import syntax::ast::{foreign_item, foreign_item_fn, ge, gt, ident, trait_ref};
29-
import syntax::ast::{impure_fn, item, item_class, item_const};
29+
import syntax::ast::{impure_fn, inherited, item, item_class, item_const};
3030
import syntax::ast::{item_enum, item_fn, item_mac, item_foreign_mod};
3131
import syntax::ast::{item_impl, item_mod, item_trait, item_ty, le, local};
3232
import syntax::ast::{local_crate, lt, method, mul, ne, neg, node_id, pat};
3333
import syntax::ast::{pat_enum, pat_ident, path, prim_ty, pat_box, pat_uniq};
3434
import syntax::ast::{pat_lit, pat_range, pat_rec, pat_struct, pat_tup};
35-
import syntax::ast::{pat_wild, provided, required, rem, self_ty_, shl};
36-
import syntax::ast::{stmt_decl, struct_field, struct_variant_kind};
35+
import syntax::ast::{pat_wild, private, provided, public, required, rem};
36+
import syntax::ast::{self_ty_};
37+
import syntax::ast::{shl, stmt_decl, struct_field, struct_variant_kind};
3738
import syntax::ast::{sty_static, subtract, tuple_variant_kind, ty};
3839
import syntax::ast::{ty_bool, ty_char, ty_f, ty_f32, ty_f64, ty_float, ty_i};
3940
import syntax::ast::{ty_i16, ty_i32, ty_i64, ty_i8, ty_int, ty_param};
4041
import syntax::ast::{ty_path, ty_str, ty_u, ty_u16, ty_u32, ty_u64, ty_u8};
4142
import syntax::ast::{ty_uint, variant, view_item, view_item_export};
4243
import syntax::ast::{view_item_import, view_item_use, view_path_glob};
43-
import syntax::ast::{view_path_list, view_path_simple};
44+
import syntax::ast::{view_path_list, view_path_simple, visibility};
4445
import syntax::ast_util::{def_id_of_def, dummy_sp, local_def, new_def_hash};
4546
import syntax::ast_util::{path_to_ident, walk_pat, trait_method_to_ty_method};
4647
import syntax::attr::{attr_metas, contains_name};
@@ -475,14 +476,26 @@ fn unused_import_lint_level(session: session) -> level {
475476
return allow;
476477
}
477478

479+
enum Privacy {
480+
Private,
481+
Public
482+
}
483+
484+
// Records a possibly-private definition.
485+
struct Definition {
486+
privacy: Privacy;
487+
def: def;
488+
}
489+
478490
// Records the definitions (at most one for each namespace) that a name is
479491
// bound to.
480492
struct NameBindings {
481-
let mut module_def: ModuleDef; //< Meaning in the module namespace.
482-
let mut type_def: option<def>; //< Meaning in the type namespace.
483-
let mut value_def: option<def>; //< Meaning in the value namespace.
493+
let mut module_def: ModuleDef; //< Meaning in module namespace.
494+
let mut type_def: option<Definition>; //< Meaning in type namespace.
495+
let mut value_def: option<Definition>; //< Meaning in value namespace.
484496

485497
// For error reporting
498+
// XXX: Merge me into Definition.
486499
let mut module_span: option<span>;
487500
let mut type_span: option<span>;
488501
let mut value_span: option<span>;
@@ -507,14 +520,14 @@ struct NameBindings {
507520
}
508521

509522
/// Records a type definition.
510-
fn define_type(def: def, sp: span) {
511-
self.type_def = some(def);
523+
fn define_type(privacy: Privacy, def: def, sp: span) {
524+
self.type_def = some(Definition { privacy: privacy, def: def });
512525
self.type_span = some(sp);
513526
}
514527

515528
/// Records a value definition.
516-
fn define_value(def: def, sp: span) {
517-
self.value_def = some(def);
529+
fn define_value(privacy: Privacy, def: def, sp: span) {
530+
self.value_def = some(Definition { privacy: privacy, def: def });
518531
self.value_span = some(sp);
519532
}
520533

@@ -550,16 +563,22 @@ struct NameBindings {
550563
}
551564
}
552565
553-
fn def_for_namespace(namespace: Namespace) -> option<def> {
566+
fn def_for_namespace(namespace: Namespace) -> option<Definition> {
554567
match namespace {
555568
TypeNS => return self.type_def,
556569
ValueNS => return self.value_def,
557570
ModuleNS => match self.module_def {
558571
NoModuleDef => return none,
559-
ModuleDef(module_) => match module_.def_id {
560-
none => return none,
561-
some(def_id) => return some(def_mod(def_id))
562-
}
572+
ModuleDef(module_) =>
573+
match module_.def_id {
574+
none => return none,
575+
some(def_id) => {
576+
return some(Definition {
577+
privacy: Public,
578+
def: def_mod(def_id)
579+
});
580+
}
581+
}
563582
}
564583
}
565584
}
@@ -762,6 +781,13 @@ struct Resolver {
762781
}));
763782
}
764783

784+
fn visibility_to_privacy(visibility: visibility) -> Privacy {
785+
match visibility {
786+
inherited | public => Public,
787+
privacy => Private
788+
}
789+
}
790+
765791
/// Returns the current module tracked by the reduced graph parent.
766792
fn get_module_from_parent(reduced_graph_parent: ReducedGraphParent)
767793
-> @Module {
@@ -915,15 +941,18 @@ struct Resolver {
915941
let (name_bindings, _) = self.add_child(atom, parent,
916942
~[ValueNS], sp);
917943

918-
(*name_bindings).define_value(def_const(local_def(item.id)),
919-
sp);
944+
(*name_bindings).define_value
945+
(self.visibility_to_privacy(item.vis),
946+
def_const(local_def(item.id)),
947+
sp);
920948
}
921949
item_fn(decl, _, _) => {
922950
let (name_bindings, new_parent) = self.add_child(atom, parent,
923951
~[ValueNS], sp);
924952

925953
let def = def_fn(local_def(item.id), decl.purity);
926-
(*name_bindings).define_value(def, sp);
954+
(*name_bindings).define_value
955+
(self.visibility_to_privacy(item.vis), def, sp);
927956
visit_item(item, new_parent, visitor);
928957
}
929958

@@ -932,15 +961,21 @@ struct Resolver {
932961
let (name_bindings, _) = self.add_child(atom, parent,
933962
~[TypeNS], sp);
934963

935-
(*name_bindings).define_type(def_ty(local_def(item.id)), sp);
964+
(*name_bindings).define_type
965+
(self.visibility_to_privacy(item.vis),
966+
def_ty(local_def(item.id)),
967+
sp);
936968
}
937969

938970
item_enum(enum_definition, _) => {
939971

940972
let (name_bindings, new_parent) = self.add_child(atom, parent,
941973
~[TypeNS], sp);
942974

943-
(*name_bindings).define_type(def_ty(local_def(item.id)), sp);
975+
(*name_bindings).define_type
976+
(self.visibility_to_privacy(item.vis),
977+
def_ty(local_def(item.id)),
978+
sp);
944979

945980
for enum_definition.variants.each |variant| {
946981
self.build_reduced_graph_for_variant(variant,
@@ -958,22 +993,26 @@ struct Resolver {
958993
let (name_bindings, new_parent) =
959994
self.add_child(atom, parent, ~[TypeNS], sp);
960995

961-
(*name_bindings).define_type(def_ty(
962-
local_def(item.id)), sp);
996+
(*name_bindings).define_type
997+
(self.visibility_to_privacy(item.vis),
998+
def_ty(local_def(item.id)),
999+
sp);
9631000
new_parent
9641001
}
9651002
some(ctor) => {
9661003
let (name_bindings, new_parent) =
9671004
self.add_child(atom, parent, ~[ValueNS, TypeNS],
9681005
sp);
9691006

970-
(*name_bindings).define_type(def_ty(
971-
local_def(item.id)), sp);
1007+
let privacy = self.visibility_to_privacy(item.vis);
1008+
1009+
(*name_bindings).define_type
1010+
(privacy, def_ty(local_def(item.id)), sp);
9721011

9731012
let purity = ctor.node.dec.purity;
9741013
let ctor_def = def_fn(local_def(ctor.node.id),
9751014
purity);
976-
(*name_bindings).define_value(ctor_def, sp);
1015+
(*name_bindings).define_value(privacy, ctor_def, sp);
9771016
new_parent
9781017
}
9791018
};
@@ -1009,7 +1048,8 @@ struct Resolver {
10091048
ty_m.span);
10101049
let def = def_static_method(local_def(ty_m.id),
10111050
ty_m.decl.purity);
1012-
(*method_name_bindings).define_value(def, ty_m.span);
1051+
(*method_name_bindings).define_value
1052+
(Public, def, ty_m.span);
10131053
}
10141054
_ => {
10151055
(*method_names).insert(atom, ());
@@ -1020,7 +1060,10 @@ struct Resolver {
10201060
let def_id = local_def(item.id);
10211061
self.trait_info.insert(def_id, method_names);
10221062

1023-
(*name_bindings).define_type(def_ty(def_id), sp);
1063+
(*name_bindings).define_type
1064+
(self.visibility_to_privacy(item.vis),
1065+
def_ty(def_id),
1066+
sp);
10241067
visit_item(item, new_parent, visitor);
10251068
}
10261069

@@ -1043,18 +1086,21 @@ struct Resolver {
10431086

10441087
match variant.node.kind {
10451088
tuple_variant_kind(_) => {
1046-
(*child).define_value(def_variant(item_id,
1089+
(*child).define_value(Public,
1090+
def_variant(item_id,
10471091
local_def(variant.node.id)),
10481092
variant.span);
10491093
}
10501094
struct_variant_kind(_) => {
1051-
(*child).define_type(def_variant(item_id,
1095+
(*child).define_type(Public,
1096+
def_variant(item_id,
10521097
local_def(variant.node.id)),
10531098
variant.span);
10541099
self.structs.insert(local_def(variant.node.id), false);
10551100
}
10561101
enum_variant_kind(enum_definition) => {
1057-
(*child).define_type(def_ty(local_def(variant.node.id)),
1102+
(*child).define_type(Public,
1103+
def_ty(local_def(variant.node.id)),
10581104
variant.span);
10591105
for enum_definition.variants.each |variant| {
10601106
self.build_reduced_graph_for_variant(variant, item_id,
@@ -1240,7 +1286,7 @@ struct Resolver {
12401286
~[ValueNS], foreign_item.span);
12411287

12421288
let def = def_fn(local_def(foreign_item.id), fn_decl.purity);
1243-
(*name_bindings).define_value(def, foreign_item.span);
1289+
(*name_bindings).define_value(Public, def, foreign_item.span);
12441290

12451291
do self.with_type_parameter_rib
12461292
(HasTypeParameters(&type_parameters,
@@ -1342,7 +1388,7 @@ struct Resolver {
13421388
def_const(def_id) | def_variant(_, def_id) => {
13431389
debug!("(building reduced graph for external \
13441390
crate) building value %s", final_ident);
1345-
(*child_name_bindings).define_value(def, dummy_sp());
1391+
(*child_name_bindings).define_value(Public, def, dummy_sp());
13461392
}
13471393
def_ty(def_id) => {
13481394
debug!("(building reduced graph for external \
@@ -1375,17 +1421,17 @@ struct Resolver {
13751421
}
13761422
}
13771423

1378-
child_name_bindings.define_type(def, dummy_sp());
1424+
child_name_bindings.define_type(Public, def, dummy_sp());
13791425
}
13801426
def_class(def_id, has_constructor) => {
13811427
debug!("(building reduced graph for external \
13821428
crate) building type %s (value? %d)",
13831429
final_ident,
13841430
if has_constructor { 1 } else { 0 });
1385-
child_name_bindings.define_type(def, dummy_sp());
1431+
child_name_bindings.define_type(Public, def, dummy_sp());
13861432

13871433
if has_constructor {
1388-
child_name_bindings.define_value(def, dummy_sp());
1434+
child_name_bindings.define_value(Public, def, dummy_sp());
13891435
}
13901436

13911437
self.structs.insert(def_id, has_constructor);
@@ -3751,14 +3797,18 @@ struct Resolver {
37513797
fail ~"resolved name in the value namespace to a set \
37523798
of name bindings with no def?!";
37533799
}
3754-
some(def @ def_variant(*)) => {
3755-
return FoundEnumVariant(def);
3756-
}
3757-
some(def_const(*)) => {
3758-
return FoundConst;
3759-
}
3760-
some(_) => {
3761-
return EnumVariantOrConstNotFound;
3800+
some(def) => {
3801+
match def.def {
3802+
def @ def_variant(*) => {
3803+
return FoundEnumVariant(def);
3804+
}
3805+
def_const(*) => {
3806+
return FoundConst;
3807+
}
3808+
_ => {
3809+
return EnumVariantOrConstNotFound;
3810+
}
3811+
}
37623812
}
37633813
}
37643814
}
@@ -3845,11 +3895,11 @@ struct Resolver {
38453895
match containing_module.children.find(name) {
38463896
some(child_name_bindings) => {
38473897
match (*child_name_bindings).def_for_namespace(namespace) {
3848-
some(def) => {
3898+
some(def) if def.privacy == Public => {
38493899
// Found it. Stop the search here.
3850-
return ChildNameDefinition(def);
3900+
return ChildNameDefinition(def.def);
38513901
}
3852-
none => {
3902+
some(_) | none => {
38533903
// Continue.
38543904
}
38553905
}
@@ -3866,12 +3916,12 @@ struct Resolver {
38663916
some(target) => {
38673917
match (*target.bindings)
38683918
.def_for_namespace(namespace) {
3869-
some(def) => {
3919+
some(def) if def.privacy == Public => {
38703920
// Found it.
38713921
import_resolution.used = true;
3872-
return ImportNameDefinition(def);
3922+
return ImportNameDefinition(def.def);
38733923
}
3874-
none => {
3924+
some(_) | none => {
38753925
// This can happen with external impls, due to
38763926
// the imperfect way we read the metadata.
38773927

@@ -4066,7 +4116,7 @@ struct Resolver {
40664116
debug!{"(resolving item path in lexical scope) \
40674117
resolved `%s` to item",
40684118
*(*self.atom_table).atom_to_str(name)};
4069-
return some(def);
4119+
return some(def.def);
40704120
}
40714121
}
40724122
}
@@ -4277,12 +4327,18 @@ struct Resolver {
42774327
// Look for trait children.
42784328
for search_module.children.each |_name, child_name_bindings| {
42794329
match child_name_bindings.def_for_namespace(TypeNS) {
4280-
some(def_ty(trait_def_id)) => {
4281-
self.add_trait_info_if_containing_method(found_traits,
4282-
trait_def_id,
4283-
name);
4330+
some(def) => {
4331+
match def.def {
4332+
def_ty(trait_def_id) => {
4333+
self.add_trait_info_if_containing_method
4334+
(found_traits, trait_def_id, name);
4335+
}
4336+
_ => {
4337+
// Continue.
4338+
}
4339+
}
42844340
}
4285-
some(_) | none => {
4341+
none => {
42864342
// Continue.
42874343
}
42884344
}
@@ -4298,11 +4354,19 @@ struct Resolver {
42984354
}
42994355
some(target) => {
43004356
match target.bindings.def_for_namespace(TypeNS) {
4301-
some(def_ty(trait_def_id)) => {
4302-
self.add_trait_info_if_containing_method
4303-
(found_traits, trait_def_id, name);
4357+
some(def) => {
4358+
match def.def {
4359+
def_ty(trait_def_id) => {
4360+
self.
4361+
add_trait_info_if_containing_method
4362+
(found_traits, trait_def_id, name);
4363+
}
4364+
_ => {
4365+
// Continue.
4366+
}
4367+
}
43044368
}
4305-
some(_) | none => {
4369+
none => {
43064370
// Continue.
43074371
}
43084372
}

0 commit comments

Comments
 (0)