Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

librustc: Enforce cross-crate method privacy #5156

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -2304,11 +2304,10 @@ mod farm {
farmer: Human
}

// Note - visibility modifiers on impls currently have no effect
impl Farm {
priv fn feed_chickens(&self) { ... }
priv fn feed_cows(&self) { ... }
fn add_chicken(&self, c: Chicken) { ... }
pub fn add_chicken(&self, c: Chicken) { ... }
}

pub fn feed_animals(farm: &Farm) {
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub pure fn empty_cell<T>() -> Cell<T> {
Cell { value: None }
}

impl<T> Cell<T> {
pub impl<T> Cell<T> {
/// Yields the value, failing if the cell is empty.
fn take() -> T {
if self.is_empty() {
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub fn PortSet<T: Owned>() -> PortSet<T>{
}
}

impl<T: Owned> PortSet<T> {
pub impl<T: Owned> PortSet<T> {

fn add(port: Port<T>) {
self.ports.push(port)
Expand Down Expand Up @@ -323,12 +323,12 @@ pub fn oneshot<T: Owned>() -> (PortOne<T>, ChanOne<T>) {
(port, chan)
}

impl<T: Owned> PortOne<T> {
pub impl<T: Owned> PortOne<T> {
fn recv(self) -> T { recv_one(self) }
fn try_recv(self) -> Option<T> { try_recv_one(self) }
}

impl<T: Owned> ChanOne<T> {
pub impl<T: Owned> ChanOne<T> {
fn send(self, data: T) { send_one(self, data) }
fn try_send(self, data: T) -> bool { try_send_one(self, data) }
}
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Condition<T, U> {
key: task::local_data::LocalDataKey<Handler<T, U>>
}

impl<T, U> Condition<T, U> {
pub impl<T, U> Condition<T, U> {
fn trap(&self, h: &self/fn(T) -> U) -> Trap/&self<T, U> {
unsafe {
let p : *RustClosure = ::cast::transmute(&h);
Expand Down Expand Up @@ -69,7 +69,7 @@ struct Trap<T, U> {
handler: @Handler<T, U>
}

impl<T, U> Trap<T, U> {
pub impl<T, U> Trap<T, U> {
fn in<V>(&self, inner: &self/fn() -> V) -> V {
unsafe {
let _g = Guard { cond: self.cond };
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ priv impl<T> DListNode<T> {
}
}

impl<T> DListNode<T> {
pub impl<T> DListNode<T> {
/// Get the next node in the list, if there is one.
pure fn next_link(@mut self) -> DListLink<T> {
self.assert_links();
Expand Down Expand Up @@ -208,7 +208,7 @@ priv impl<T> DList<T> {
}
}

impl<T> DList<T> {
pub impl<T> DList<T> {
/// Get the size of the list. O(1).
pure fn len(@mut self) -> uint { self.size }
/// Returns true if the list is empty. O(1).
Expand Down Expand Up @@ -457,7 +457,7 @@ impl<T> DList<T> {
}
}

impl<T:Copy> DList<T> {
pub impl<T:Copy> DList<T> {
/// Remove data from the head of the list. O(1).
fn pop(@mut self) -> Option<T> {
self.pop_n().map(|nobe| nobe.data)
Expand Down
27 changes: 14 additions & 13 deletions src/libcore/dvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,6 @@ priv impl<A> DVec<A> {
}
}

#[inline(always)]
fn check_out<B>(f: &fn(v: ~[A]) -> B) -> B {
unsafe {
let mut data = cast::reinterpret_cast(&null::<()>());
data <-> self.data;
let data_ptr: *() = cast::reinterpret_cast(&data);
if data_ptr.is_null() { fail!(~"Recursive use of dvec"); }
return f(data);
}
}

#[inline(always)]
fn give_back(data: ~[A]) {
unsafe {
Expand All @@ -117,7 +106,19 @@ priv impl<A> DVec<A> {
// In theory, most everything should work with any A, but in practice
// almost nothing works without the copy bound due to limitations
// around closures.
impl<A> DVec<A> {
pub impl<A> DVec<A> {
// FIXME (#3758): This should not need to be public.
#[inline(always)]
fn check_out<B>(f: &fn(v: ~[A]) -> B) -> B {
unsafe {
let mut data = cast::reinterpret_cast(&null::<()>());
data <-> self.data;
let data_ptr: *() = cast::reinterpret_cast(&data);
if data_ptr.is_null() { fail!(~"Recursive use of dvec"); }
return f(data);
}
}

/// Reserves space for N elements
fn reserve(count: uint) {
vec::reserve(&mut self.data, count)
Expand Down Expand Up @@ -215,7 +216,7 @@ impl<A> DVec<A> {
}
}

impl<A:Copy> DVec<A> {
pub impl<A:Copy> DVec<A> {
/**
* Append all elements of a vector to the end of the list
*
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn unwrap<T>(m: Mut<T>) -> T {
value
}

impl<T> Data<T> {
pub impl<T> Data<T> {
fn borrow_mut<R>(op: &fn(t: &mut T) -> R) -> R {
match self.mode {
Immutable => fail!(fmt!("%? currently immutable",
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ pub pure fn expect<T>(opt: Option<T>, reason: &str) -> T {
}
}

impl<T> Option<T> {
pub impl<T> Option<T> {
/// Returns true if the option equals `none`
#[inline(always)]
pure fn is_none(&self) -> bool { is_none(self) }
Expand Down Expand Up @@ -393,7 +393,7 @@ impl<T> Option<T> {
pure fn expect(self, reason: &str) -> T { expect(self, reason) }
}

impl<T:Copy> Option<T> {
pub impl<T:Copy> Option<T> {
/**
Gets the value out of an option

Expand Down Expand Up @@ -421,7 +421,7 @@ impl<T:Copy> Option<T> {
}
}

impl<T:Copy + Zero> Option<T> {
pub impl<T:Copy + Zero> Option<T> {
#[inline(always)]
pure fn get_or_zero(self) -> T { get_or_zero(self) }
}
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ mod stat {
}


impl Path {
pub impl Path {
fn stat(&self) -> Option<libc::stat> {
unsafe {
do str::as_c_str(self.to_str()) |buf| {
Expand Down Expand Up @@ -290,7 +290,7 @@ impl Path {
#[cfg(target_os = "freebsd")]
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
impl Path {
pub impl Path {
fn get_atime(&self) -> Option<(i64, int)> {
match self.stat() {
None => None,
Expand Down Expand Up @@ -324,7 +324,7 @@ impl Path {

#[cfg(target_os = "freebsd")]
#[cfg(target_os = "macos")]
impl Path {
pub impl Path {
fn get_birthtime(&self) -> Option<(i64, int)> {
match self.stat() {
None => None,
Expand All @@ -337,7 +337,7 @@ impl Path {
}

#[cfg(target_os = "win32")]
impl Path {
pub impl Path {
fn get_atime(&self) -> Option<(i64, int)> {
match self.stat() {
None => None,
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ pub fn SendPacketBuffered<T,Tbuffer>(p: *Packet<T>)
}
}

impl<T,Tbuffer> SendPacketBuffered<T,Tbuffer> {
pub impl<T,Tbuffer> SendPacketBuffered<T,Tbuffer> {
fn unwrap() -> *Packet<T> {
let mut p = None;
p <-> self.p;
Expand Down Expand Up @@ -857,7 +857,7 @@ impl<T:Owned,Tbuffer:Owned> ::ops::Drop for RecvPacketBuffered<T,Tbuffer> {
}
}

impl<T:Owned,Tbuffer:Owned> RecvPacketBuffered<T, Tbuffer> {
pub impl<T:Owned,Tbuffer:Owned> RecvPacketBuffered<T, Tbuffer> {
fn unwrap() -> *Packet<T> {
let mut p = None;
p <-> self.p;
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ fn LittleLock() -> LittleLock {
}
}

impl LittleLock {
pub impl LittleLock {
#[inline(always)]
unsafe fn lock<T>(f: fn() -> T) -> T {
struct Unlock {
Expand Down Expand Up @@ -381,7 +381,7 @@ impl<T:Owned> Clone for Exclusive<T> {
}
}

impl<T:Owned> Exclusive<T> {
pub impl<T:Owned> Exclusive<T> {
// Exactly like std::arc::mutex_arc,access(), but with the little_lock
// instead of a proper mutex. Same reason for being unsafe.
//
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/private/extfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub mod ct {
next: uint
}

impl<T> Parsed<T> {
pub impl<T> Parsed<T> {
static pure fn new(val: T, next: uint) -> Parsed<T> {
Parsed {val: val, next: next}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub struct Weighted<T> {
}

/// Extension methods for random number generators
impl Rng {
pub impl Rng {
/// Return a random value for a Rand type
fn gen<T:Rand>() -> T {
Rand::rand(self)
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn MovePtrAdaptor<V:TyVisitor + MovePtr>(v: V) -> MovePtrAdaptor<V> {
MovePtrAdaptor { inner: v }
}

impl<V:TyVisitor + MovePtr> MovePtrAdaptor<V> {
pub impl<V:TyVisitor + MovePtr> MovePtrAdaptor<V> {
#[inline(always)]
fn bump(sz: uint) {
do self.inner.move_ptr() |p| {
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl MovePtr for ReprVisitor {
}
}

impl ReprVisitor {
pub impl ReprVisitor {

// Various helpers for the TyVisitor impl

Expand Down
6 changes: 3 additions & 3 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub pure fn map_err<T:Copy,E,F:Copy>(res: &Result<T, E>, op: fn(&E) -> F)
}
}

impl<T, E> Result<T, E> {
pub impl<T, E> Result<T, E> {
#[inline(always)]
pure fn get_ref(&self) -> &self/T { get_ref(self) }

Expand Down Expand Up @@ -261,7 +261,7 @@ impl<T, E> Result<T, E> {
}
}

impl<T:Copy,E> Result<T, E> {
pub impl<T:Copy,E> Result<T, E> {
#[inline(always)]
pure fn get(&self) -> T { get(self) }

Expand All @@ -271,7 +271,7 @@ impl<T:Copy,E> Result<T, E> {
}
}

impl<T, E: Copy> Result<T, E> {
pub impl<T, E: Copy> Result<T, E> {
#[inline(always)]
pure fn get_err(&self) -> E { get_err(self) }

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ priv impl TaskBuilder {
}
}

impl TaskBuilder {
pub impl TaskBuilder {
/**
* Decouple the child task's failure from the parent's. If either fails,
* the other will not be killed.
Expand Down
1 change: 1 addition & 0 deletions src/librustc/metadata/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ pub const tag_lang_items_item_node_id: uint = 0x75;

pub const tag_item_unnamed_field: uint = 0x76;
pub const tag_items_data_item_struct_ctor: uint = 0x77;
pub const tag_items_data_item_visibility: uint = 0x78;

pub struct LinkMeta {
name: @str,
Expand Down
8 changes: 8 additions & 0 deletions src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ pub fn struct_dtor(cstore: @mut cstore::CStore, def: ast::def_id)
let cdata = cstore::get_crate_data(cstore, def.crate);
decoder::struct_dtor(cdata, def.node)
}

pub fn get_method_visibility(cstore: @mut cstore::CStore,
def_id: ast::def_id)
-> ast::visibility {
let cdata = cstore::get_crate_data(cstore, def_id.crate);
decoder::get_method_visibility(cdata, def_id.node)
}

// Local Variables:
// mode: rust
// fill-column: 78;
Expand Down
19 changes: 17 additions & 2 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ fn item_family(item: ebml::Doc) -> Family {
}
}

fn item_visibility(item: ebml::Doc) -> ast::visibility {
let visibility = reader::get_doc(item, tag_items_data_item_visibility);
match reader::doc_as_u8(visibility) as char {
'y' => ast::public,
'n' => ast::private,
'i' => ast::inherited,
_ => fail!(~"unknown visibility character"),
}
}

fn item_method_sort(item: ebml::Doc) -> char {
for reader::tagged_docs(item, tag_item_trait_method_sort) |doc| {
return str::from_bytes(reader::doc_data(doc))[0] as char;
Expand Down Expand Up @@ -860,7 +870,7 @@ pub fn get_item_attrs(cdata: cmd,
}
}

pure fn family_to_visibility(family: Family) -> ast::visibility {
pure fn struct_field_family_to_visibility(family: Family) -> ast::visibility {
match family {
PublicField => ast::public,
PrivateField => ast::private,
Expand All @@ -883,7 +893,7 @@ pub fn get_struct_fields(intr: @ident_interner, cdata: cmd, id: ast::node_id)
result.push(ty::field_ty {
ident: name,
id: did, vis:
family_to_visibility(f),
struct_field_family_to_visibility(f),
mutability: mt,
});
}
Expand All @@ -900,6 +910,11 @@ pub fn get_struct_fields(intr: @ident_interner, cdata: cmd, id: ast::node_id)
result
}

pub fn get_method_visibility(cdata: cmd, id: ast::node_id)
-> ast::visibility {
item_visibility(lookup_item(id, cdata.data))
}

fn family_has_type_params(fam: Family) -> bool {
match fam {
Const | ForeignType | Mod | ForeignMod | PublicField | PrivateField
Expand Down
Loading