Skip to content

Commit 09a2b4e

Browse files
committed
librustc: Make methods private if the impl is private
1 parent 107bf96 commit 09a2b4e

File tree

3 files changed

+63
-13
lines changed

3 files changed

+63
-13
lines changed

src/libcore/dvec.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,6 @@ priv impl<A> DVec<A> {
9292
}
9393
}
9494
95-
#[inline(always)]
96-
fn check_out<B>(f: &fn(v: ~[A]) -> B) -> B {
97-
unsafe {
98-
let mut data = cast::reinterpret_cast(&null::<()>());
99-
data <-> self.data;
100-
let data_ptr: *() = cast::reinterpret_cast(&data);
101-
if data_ptr.is_null() { fail!(~"Recursive use of dvec"); }
102-
return f(data);
103-
}
104-
}
105-
10695
#[inline(always)]
10796
fn give_back(data: ~[A]) {
10897
unsafe {
@@ -118,6 +107,18 @@ priv impl<A> DVec<A> {
118107
// almost nothing works without the copy bound due to limitations
119108
// around closures.
120109
pub impl<A> DVec<A> {
110+
// FIXME (#3758): This should not need to be public.
111+
#[inline(always)]
112+
fn check_out<B>(f: &fn(v: ~[A]) -> B) -> B {
113+
unsafe {
114+
let mut data = cast::reinterpret_cast(&null::<()>());
115+
data <-> self.data;
116+
let data_ptr: *() = cast::reinterpret_cast(&data);
117+
if data_ptr.is_null() { fail!(~"Recursive use of dvec"); }
118+
return f(data);
119+
}
120+
}
121+
121122
/// Reserves space for N elements
122123
fn reserve(count: uint) {
123124
vec::reserve(&mut self.data, count)

src/librustc/middle/privacy.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use core::util::ignore;
2525
use syntax::ast::{def_variant, expr_field, expr_method_call, expr_struct};
2626
use syntax::ast::{expr_unary, ident, item_struct, item_enum, item_impl};
2727
use syntax::ast::{item_trait, local_crate, node_id, pat_struct, private};
28-
use syntax::ast::{provided, required};
28+
use syntax::ast::{provided, public, required};
2929
use syntax::ast;
3030
use syntax::ast_map::{node_item, node_method};
3131
use syntax::ast_map;
@@ -107,7 +107,41 @@ pub fn check_crate(tcx: ty::ctxt,
107107
if method_id.crate == local_crate {
108108
match tcx.items.find(&method_id.node) {
109109
Some(node_method(method, impl_id, _)) => {
110-
if method.vis == private &&
110+
let mut is_private = false;
111+
if method.vis == private {
112+
is_private = true;
113+
} else {
114+
// Look up the enclosing impl.
115+
if impl_id.crate != local_crate {
116+
tcx.sess.span_bug(span,
117+
~"local method isn't \
118+
in local impl?!");
119+
}
120+
121+
match tcx.items.find(&impl_id.node) {
122+
Some(node_item(item, _)) => {
123+
match item.node {
124+
item_impl(_, None, _, _)
125+
if item.vis != public => {
126+
is_private = true;
127+
}
128+
_ => {}
129+
}
130+
}
131+
Some(_) => {
132+
tcx.sess.span_bug(span,
133+
~"impl wasn't an \
134+
item?!");
135+
}
136+
None => {
137+
tcx.sess.span_bug(span,
138+
~"impl wasn't in \
139+
AST map?!");
140+
}
141+
}
142+
}
143+
144+
if is_private &&
111145
(impl_id.crate != local_crate ||
112146
!privileged_items
113147
.contains(&(impl_id.node))) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Tests that inherited visibility applies to methods.
2+
3+
mod a {
4+
pub struct Foo;
5+
6+
impl Foo {
7+
fn f(self) {}
8+
}
9+
}
10+
11+
fn main() {
12+
let x = a::Foo;
13+
x.f(); //~ ERROR method `f` is private
14+
}
15+

0 commit comments

Comments
 (0)