Skip to content

Commit 09f4c9a

Browse files
committed
Merge branch 'enum-method-privacy' of https://github.com/michaelwoerister/rust into rollup
Conflicts: src/libsyntax/opt_vec.rs
2 parents c8a93ef + 2c9922a commit 09f4c9a

File tree

5 files changed

+59
-26
lines changed

5 files changed

+59
-26
lines changed

src/librustc/middle/privacy.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,12 @@ pub enum MapChain<K,V> {
421421
// get the map from an env frame
422422
impl <K: Eq + Hash + IterBytes + 'static, V: 'static> MapChain<K,V>{
423423
// Constructor. I don't think we need a zero-arg one.
424-
fn new(init: ~HashMap<K,@V>) -> @mut MapChain<K,V> {
424+
pub fn new(init: ~HashMap<K,@V>) -> @mut MapChain<K,V> {
425425
@mut BaseMapChain(init)
426426
}
427427

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

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

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

449-
fn contains_key (&self, key: &K) -> bool {
449+
pub fn contains_key (&self, key: &K) -> bool {
450450
match *self {
451451
BaseMapChain (ref map) => map.contains_key(key),
452452
ConsMapChain (ref map,ref rest) =>
@@ -457,17 +457,17 @@ impl <K: Eq + Hash + IterBytes + 'static, V: 'static> MapChain<K,V>{
457457
// should each_key and each_value operate on shadowed
458458
// names? I think not.
459459
// delaying implementing this....
460-
fn each_key (&self, _f: &fn (&K)->bool) {
460+
pub fn each_key (&self, _f: &fn (&K)->bool) {
461461
fail!("unimplemented 2013-02-15T10:01");
462462
}
463463

464-
fn each_value (&self, _f: &fn (&V) -> bool) {
464+
pub fn each_value (&self, _f: &fn (&V) -> bool) {
465465
fail!("unimplemented 2013-02-15T10:02");
466466
}
467467

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

480-
fn find_in_topmost_frame(&self, key: &K) -> Option<@V> {
480+
pub fn find_in_topmost_frame(&self, key: &K) -> Option<@V> {
481481
let map = match *self {
482482
BaseMapChain(ref map) => map,
483483
ConsMapChain(ref map,_) => map
@@ -487,7 +487,7 @@ impl <K: Eq + Hash + IterBytes + 'static, V: 'static> MapChain<K,V>{
487487
}
488488

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

src/libsyntax/opt_vec.rs

Lines changed: 12 additions & 12 deletions
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_move<U>(self, op: &fn(T) -> U) -> OptVec<U> {
60+
pub fn map_move<U>(self, op: &fn(T) -> U) -> OptVec<U> {
6161
match self {
6262
Empty => Empty,
6363
Vec(v) => Vec(v.move_iter().map(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().map(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
}
Lines changed: 27 additions & 3 deletions
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

Lines changed: 9 additions & 1 deletion
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)