Skip to content

Commit 2c9922a

Browse files
Enable privacy check for enum methods.
1 parent 4da1cfe commit 2c9922a

File tree

5 files changed

+59
-26
lines changed

5 files changed

+59
-26
lines changed

src/librustc/middle/privacy.rs

+1
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ pub fn check_crate<'mm>(tcx: ty::ctxt,
403403
// Ditto
404404
match ty::get(ty::type_autoderef(tcx, ty::expr_ty(tcx,
405405
base))).sty {
406+
ty_enum(id, _) |
406407
ty_struct(id, _)
407408
if id.crate != LOCAL_CRATE ||
408409
!privileged_items.iter().any(|x| x == &(id.node)) => {

src/libsyntax/ext/base.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,12 @@ pub enum MapChain<K,V> {
417417
// get the map from an env frame
418418
impl <K: Eq + Hash + IterBytes + 'static, V: 'static> MapChain<K,V>{
419419
// Constructor. I don't think we need a zero-arg one.
420-
fn new(init: ~HashMap<K,@V>) -> @mut MapChain<K,V> {
420+
pub fn new(init: ~HashMap<K,@V>) -> @mut MapChain<K,V> {
421421
@mut BaseMapChain(init)
422422
}
423423

424424
// add a new frame to the environment (functionally)
425-
fn push_frame (@mut self) -> @mut MapChain<K,V> {
425+
pub fn push_frame (@mut self) -> @mut MapChain<K,V> {
426426
@mut ConsMapChain(~HashMap::new() ,self)
427427
}
428428

@@ -432,7 +432,7 @@ impl <K: Eq + Hash + IterBytes + 'static, V: 'static> MapChain<K,V>{
432432

433433
// ugh: can't get this to compile with mut because of the
434434
// lack of flow sensitivity.
435-
fn get_map<'a>(&'a self) -> &'a HashMap<K,@V> {
435+
pub fn get_map<'a>(&'a self) -> &'a HashMap<K,@V> {
436436
match *self {
437437
BaseMapChain (~ref map) => map,
438438
ConsMapChain (~ref map,_) => map
@@ -442,7 +442,7 @@ impl <K: Eq + Hash + IterBytes + 'static, V: 'static> MapChain<K,V>{
442442
// traits just don't work anywhere...?
443443
//impl Map<Name,SyntaxExtension> for MapChain {
444444

445-
fn contains_key (&self, key: &K) -> bool {
445+
pub fn contains_key (&self, key: &K) -> bool {
446446
match *self {
447447
BaseMapChain (ref map) => map.contains_key(key),
448448
ConsMapChain (ref map,ref rest) =>
@@ -453,17 +453,17 @@ impl <K: Eq + Hash + IterBytes + 'static, V: 'static> MapChain<K,V>{
453453
// should each_key and each_value operate on shadowed
454454
// names? I think not.
455455
// delaying implementing this....
456-
fn each_key (&self, _f: &fn (&K)->bool) {
456+
pub fn each_key (&self, _f: &fn (&K)->bool) {
457457
fail!("unimplemented 2013-02-15T10:01");
458458
}
459459

460-
fn each_value (&self, _f: &fn (&V) -> bool) {
460+
pub fn each_value (&self, _f: &fn (&V) -> bool) {
461461
fail!("unimplemented 2013-02-15T10:02");
462462
}
463463

464464
// Returns a copy of the value that the name maps to.
465465
// Goes down the chain 'til it finds one (or bottom out).
466-
fn find (&self, key: &K) -> Option<@V> {
466+
pub fn find (&self, key: &K) -> Option<@V> {
467467
match self.get_map().find (key) {
468468
Some(ref v) => Some(**v),
469469
None => match *self {
@@ -473,7 +473,7 @@ impl <K: Eq + Hash + IterBytes + 'static, V: 'static> MapChain<K,V>{
473473
}
474474
}
475475

476-
fn find_in_topmost_frame(&self, key: &K) -> Option<@V> {
476+
pub fn find_in_topmost_frame(&self, key: &K) -> Option<@V> {
477477
let map = match *self {
478478
BaseMapChain(ref map) => map,
479479
ConsMapChain(ref map,_) => map
@@ -483,7 +483,7 @@ impl <K: Eq + Hash + IterBytes + 'static, V: 'static> MapChain<K,V>{
483483
}
484484

485485
// insert the binding into the top-level map
486-
fn insert (&mut self, key: K, ext: @V) -> bool {
486+
pub fn insert (&mut self, key: K, ext: @V) -> bool {
487487
// can't abstract over get_map because of flow sensitivity...
488488
match *self {
489489
BaseMapChain (~ref mut map) => map.insert(key, ext),
@@ -495,7 +495,7 @@ impl <K: Eq + Hash + IterBytes + 'static, V: 'static> MapChain<K,V>{
495495
// ... there are definitely some opportunities for abstraction
496496
// here that I'm ignoring. (e.g., manufacturing a predicate on
497497
// the maps in the chain, and using an abstract "find".
498-
fn insert_into_frame(&mut self, key: K, ext: @V, n: K, pred: &fn(&@V)->bool) {
498+
pub fn insert_into_frame(&mut self, key: K, ext: @V, n: K, pred: &fn(&@V)->bool) {
499499
match *self {
500500
BaseMapChain (~ref mut map) => {
501501
if satisfies_pred(map,&n,pred) {

src/libsyntax/opt_vec.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn from<T>(t: ~[T]) -> OptVec<T> {
3636
}
3737

3838
impl<T> OptVec<T> {
39-
fn push(&mut self, t: T) {
39+
pub fn push(&mut self, t: T) {
4040
match *self {
4141
Vec(ref mut v) => {
4242
v.push(t);
@@ -50,52 +50,52 @@ impl<T> OptVec<T> {
5050
*self = Vec(~[t]);
5151
}
5252

53-
fn map<U>(&self, op: &fn(&T) -> U) -> OptVec<U> {
53+
pub fn map<U>(&self, op: &fn(&T) -> U) -> OptVec<U> {
5454
match *self {
5555
Empty => Empty,
5656
Vec(ref v) => Vec(v.map(op))
5757
}
5858
}
5959

60-
fn map_consume<U>(self, op: &fn(T) -> U) -> OptVec<U> {
60+
pub fn map_consume<U>(self, op: &fn(T) -> U) -> OptVec<U> {
6161
match self {
6262
Empty => Empty,
6363
Vec(v) => Vec(v.consume_iter().transform(op).collect())
6464
}
6565
}
6666

67-
fn get<'a>(&'a self, i: uint) -> &'a T {
67+
pub fn get<'a>(&'a self, i: uint) -> &'a T {
6868
match *self {
6969
Empty => fail!("Invalid index %u", i),
7070
Vec(ref v) => &v[i]
7171
}
7272
}
7373

74-
fn is_empty(&self) -> bool {
74+
pub fn is_empty(&self) -> bool {
7575
self.len() == 0
7676
}
7777

78-
fn len(&self) -> uint {
78+
pub fn len(&self) -> uint {
7979
match *self {
8080
Empty => 0,
8181
Vec(ref v) => v.len()
8282
}
8383
}
8484

8585
#[inline]
86-
fn iter<'r>(&'r self) -> OptVecIterator<'r, T> {
86+
pub fn iter<'r>(&'r self) -> OptVecIterator<'r, T> {
8787
match *self {
8888
Empty => OptVecIterator{iter: None},
8989
Vec(ref v) => OptVecIterator{iter: Some(v.iter())}
9090
}
9191
}
9292

9393
#[inline]
94-
fn map_to_vec<B>(&self, op: &fn(&T) -> B) -> ~[B] {
94+
pub fn map_to_vec<B>(&self, op: &fn(&T) -> B) -> ~[B] {
9595
self.iter().transform(op).collect()
9696
}
9797

98-
fn mapi_to_vec<B>(&self, op: &fn(uint, &T) -> B) -> ~[B] {
98+
pub fn mapi_to_vec<B>(&self, op: &fn(uint, &T) -> B) -> ~[B] {
9999
let mut index = 0;
100100
self.map_to_vec(|a| {
101101
let i = index;
@@ -113,7 +113,7 @@ pub fn take_vec<T>(v: OptVec<T>) -> ~[T] {
113113
}
114114

115115
impl<T:Clone> OptVec<T> {
116-
fn prepend(&self, t: T) -> OptVec<T> {
116+
pub fn prepend(&self, t: T) -> OptVec<T> {
117117
let mut v0 = ~[t];
118118
match *self {
119119
Empty => {}
@@ -124,7 +124,7 @@ impl<T:Clone> OptVec<T> {
124124
}
125125

126126
impl<A:Eq> Eq for OptVec<A> {
127-
fn eq(&self, other: &OptVec<A>) -> bool {
127+
pub fn eq(&self, other: &OptVec<A>) -> bool {
128128
// Note: cannot use #[deriving(Eq)] here because
129129
// (Empty, Vec(~[])) ought to be equal.
130130
match (self, other) {
@@ -135,7 +135,7 @@ impl<A:Eq> Eq for OptVec<A> {
135135
}
136136
}
137137

138-
fn ne(&self, other: &OptVec<A>) -> bool {
138+
pub fn ne(&self, other: &OptVec<A>) -> bool {
139139
!self.eq(other)
140140
}
141141
}
+27-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
#[crate_type="lib"];
22

3-
pub struct Foo {
3+
pub struct Struct {
44
x: int
55
}
66

7-
impl Foo {
8-
fn new() -> Foo { Foo { x: 1 } }
7+
impl Struct {
8+
fn static_meth_struct() -> Struct {
9+
Struct { x: 1 }
10+
}
11+
12+
fn meth_struct(&self) -> int {
13+
self.x
14+
}
15+
}
16+
17+
pub enum Enum {
18+
Variant1(int),
19+
Variant2(int)
20+
}
21+
22+
impl Enum {
23+
fn static_meth_enum() -> Enum {
24+
Variant2(10)
25+
}
26+
27+
fn meth_enum(&self) -> int {
28+
match *self {
29+
Variant1(x) |
30+
Variant2(x) => x
31+
}
32+
}
933
}

src/test/compile-fail/xc-private-method.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,13 @@
44
extern mod xc_private_method_lib;
55

66
fn main() {
7-
let _ = xc_private_method_lib::Foo::new(); //~ ERROR function `new` is private
7+
// normal method on struct
8+
let _ = xc_private_method_lib::Struct{ x: 10 }.meth_struct(); //~ ERROR method `meth_struct` is private
9+
// static method on struct
10+
let _ = xc_private_method_lib::Struct::static_meth_struct(); //~ ERROR function `static_meth_struct` is private
11+
12+
// normal method on enum
13+
let _ = xc_private_method_lib::Variant1(20).meth_enum(); //~ ERROR method `meth_enum` is private
14+
// static method on enum
15+
let _ = xc_private_method_lib::Enum::static_meth_enum(); //~ ERROR function `static_meth_enum` is private
816
}

0 commit comments

Comments
 (0)