Skip to content

Commit 94b0b0e

Browse files
committed
Forbid pub/priv where it has no effect
Closes #5495
1 parent 2ba36ec commit 94b0b0e

34 files changed

+229
-116
lines changed

src/libextra/crypto/sha1.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl Sha1 {
159159
}
160160

161161
impl Digest for Sha1 {
162-
pub fn reset(&mut self) {
162+
fn reset(&mut self) {
163163
self.length_bits = 0;
164164
self.h[0] = 0x67452301u32;
165165
self.h[1] = 0xEFCDAB89u32;
@@ -169,9 +169,9 @@ impl Digest for Sha1 {
169169
self.buffer.reset();
170170
self.computed = false;
171171
}
172-
pub fn input(&mut self, msg: &[u8]) { add_input(self, msg); }
173-
pub fn result(&mut self, out: &mut [u8]) { return mk_result(self, out); }
174-
pub fn output_bits(&self) -> uint { 160 }
172+
fn input(&mut self, msg: &[u8]) { add_input(self, msg); }
173+
fn result(&mut self, out: &mut [u8]) { return mk_result(self, out); }
174+
fn output_bits(&self) -> uint { 160 }
175175
}
176176

177177
#[cfg(test)]

src/libextra/enum_set.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ pub struct EnumSet<E> {
2121
/// An iterface for casting C-like enum to uint and back.
2222
pub trait CLike {
2323
/// Converts C-like enum to uint.
24-
pub fn to_uint(&self) -> uint;
24+
fn to_uint(&self) -> uint;
2525
/// Converts uint to C-like enum.
26-
pub fn from_uint(uint) -> Self;
26+
fn from_uint(uint) -> Self;
2727
}
2828

2929
fn bit<E:CLike>(e: E) -> uint {
@@ -142,11 +142,11 @@ mod test {
142142
}
143143

144144
impl CLike for Foo {
145-
pub fn to_uint(&self) -> uint {
145+
fn to_uint(&self) -> uint {
146146
*self as uint
147147
}
148148

149-
pub fn from_uint(v: uint) -> Foo {
149+
fn from_uint(v: uint) -> Foo {
150150
unsafe { cast::transmute(v) }
151151
}
152152
}

src/libextra/num/bigint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ impl ToStrRadix for BigUint {
537537
impl FromStrRadix for BigUint {
538538
/// Creates and initializes an BigUint.
539539
540-
pub fn from_str_radix(s: &str, radix: uint)
540+
fn from_str_radix(s: &str, radix: uint)
541541
-> Option<BigUint> {
542542
BigUint::parse_bytes(s.as_bytes(), radix)
543543
}

src/libextra/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub struct Metric {
104104
pub struct MetricMap(TreeMap<~str,Metric>);
105105

106106
impl Clone for MetricMap {
107-
pub fn clone(&self) -> MetricMap {
107+
fn clone(&self) -> MetricMap {
108108
MetricMap((**self).clone())
109109
}
110110
}

src/libextra/treemap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ impl<K: TotalOrd, V, T: Iterator<(K, V)>> Extendable<(K, V), T> for TreeMap<K, V
895895
}
896896

897897
impl<T: TotalOrd, Iter: Iterator<T>> FromIterator<T, Iter> for TreeSet<T> {
898-
pub fn from_iterator(iter: &mut Iter) -> TreeSet<T> {
898+
fn from_iterator(iter: &mut Iter) -> TreeSet<T> {
899899
let mut set = TreeSet::new();
900900
set.extend(iter);
901901
set

src/libextra/url.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ pub fn to_str(url: &Url) -> ~str {
701701
}
702702

703703
impl ToStr for Url {
704-
pub fn to_str(&self) -> ~str {
704+
fn to_str(&self) -> ~str {
705705
to_str(self)
706706
}
707707
}

src/librustc/middle/privacy.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ pub fn check_crate<'mm>(tcx: ty::ctxt,
351351
// Do not check privacy inside items with the resolve_unexported
352352
// attribute. This is used for the test runner.
353353
if !attr::contains_name(item.attrs, "!resolve_unexported") {
354+
check_sane_privacy(tcx, item);
354355
oldvisit::visit_item(item, (method_map, visitor));
355356
}
356357
},
@@ -539,3 +540,81 @@ pub fn check_crate<'mm>(tcx: ty::ctxt,
539540
});
540541
oldvisit::visit_crate(crate, (method_map, visitor));
541542
}
543+
544+
/// Validates all of the visibility qualifers placed on the item given. This
545+
/// ensures that there are no extraneous qualifiers that don't actually do
546+
/// anything. In theory these qualifiers wouldn't parse, but that may happen
547+
/// later on down the road...
548+
fn check_sane_privacy(tcx: ty::ctxt, item: @ast::item) {
549+
match item.node {
550+
// implementations of traits don't need visibility qualifiers because
551+
// that's controlled by having the trait in scope.
552+
ast::item_impl(_, Some(*), _, ref methods) => {
553+
for m in methods.iter() {
554+
match m.vis {
555+
ast::private | ast::public => {
556+
tcx.sess.span_err(m.span, "unnecessary visibility")
557+
}
558+
ast::inherited => {}
559+
}
560+
}
561+
}
562+
563+
ast::item_enum(ref def, _) => {
564+
for v in def.variants.iter() {
565+
match v.node.vis {
566+
ast::public => {
567+
if item.vis == ast::public {
568+
tcx.sess.span_err(v.span, "unnecessary `pub` \
569+
visibility");
570+
}
571+
}
572+
ast::private => {
573+
if item.vis != ast::public {
574+
tcx.sess.span_err(v.span, "unnecessary `priv` \
575+
visibility");
576+
}
577+
}
578+
ast::inherited => {}
579+
}
580+
}
581+
}
582+
583+
ast::item_struct(ref def, _) => {
584+
for f in def.fields.iter() {
585+
match f.node.kind {
586+
ast::named_field(_, ast::public) => {
587+
tcx.sess.span_err(f.span, "unnecessary `pub` \
588+
visibility");
589+
}
590+
ast::named_field(_, ast::private) => {
591+
// Fields should really be private by default...
592+
}
593+
ast::named_field(*) | ast::unnamed_field => {}
594+
}
595+
}
596+
}
597+
598+
ast::item_trait(_, _, ref methods) => {
599+
for m in methods.iter() {
600+
match *m {
601+
ast::provided(ref m) => {
602+
match m.vis {
603+
ast::private | ast::public => {
604+
tcx.sess.span_err(m.span, "unnecessary \
605+
visibility");
606+
}
607+
ast::inherited => {}
608+
}
609+
}
610+
// this is warned about in the parser
611+
ast::required(*) => {}
612+
}
613+
}
614+
}
615+
616+
ast::item_impl(*) | ast::item_static(*) | ast::item_foreign_mod(*) |
617+
ast::item_fn(*) | ast::item_mod(*) | ast::item_ty(*) |
618+
ast::item_mac(*) => {}
619+
}
620+
}

src/librustc/middle/trans/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'self> StatRecorder<'self> {
165165

166166
#[unsafe_destructor]
167167
impl<'self> Drop for StatRecorder<'self> {
168-
pub fn drop(&self) {
168+
fn drop(&self) {
169169
if self.ccx.sess.trans_stats() {
170170
let end = time::precise_time_ns();
171171
let elapsed = ((end - self.start) / 1_000_000) as uint;

src/librustc/middle/ty.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,10 @@ pub fn AllBuiltinBounds() -> BuiltinBounds {
706706
}
707707

708708
impl CLike for BuiltinBound {
709-
pub fn to_uint(&self) -> uint {
709+
fn to_uint(&self) -> uint {
710710
*self as uint
711711
}
712-
pub fn from_uint(v: uint) -> BuiltinBound {
712+
fn from_uint(v: uint) -> BuiltinBound {
713713
unsafe { cast::transmute(v) }
714714
}
715715
}
@@ -4315,16 +4315,16 @@ pub fn normalize_ty(cx: ctxt, t: t) -> t {
43154315
}
43164316

43174317
pub trait ExprTyProvider {
4318-
pub fn expr_ty(&self, ex: &ast::expr) -> t;
4319-
pub fn ty_ctxt(&self) -> ctxt;
4318+
fn expr_ty(&self, ex: &ast::expr) -> t;
4319+
fn ty_ctxt(&self) -> ctxt;
43204320
}
43214321

43224322
impl ExprTyProvider for ctxt {
4323-
pub fn expr_ty(&self, ex: &ast::expr) -> t {
4323+
fn expr_ty(&self, ex: &ast::expr) -> t {
43244324
expr_ty(*self, ex)
43254325
}
43264326

4327-
pub fn ty_ctxt(&self) -> ctxt {
4327+
fn ty_ctxt(&self) -> ctxt {
43284328
*self
43294329
}
43304330
}

src/librustc/middle/typeck/check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,11 @@ pub fn blank_fn_ctxt(ccx: @mut CrateCtxt,
287287
}
288288

289289
impl ExprTyProvider for FnCtxt {
290-
pub fn expr_ty(&self, ex: &ast::expr) -> ty::t {
290+
fn expr_ty(&self, ex: &ast::expr) -> ty::t {
291291
self.expr_ty(ex)
292292
}
293293

294-
pub fn ty_ctxt(&self) -> ty::ctxt {
294+
fn ty_ctxt(&self) -> ty::ctxt {
295295
self.ccx.tcx
296296
}
297297
}

src/librustc/middle/typeck/infer/error_reporting.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ use util::ppaux::UserString;
7676
use util::ppaux::note_and_explain_region;
7777

7878
pub trait ErrorReporting {
79-
pub fn report_region_errors(@mut self,
80-
errors: &OptVec<RegionResolutionError>);
79+
fn report_region_errors(@mut self,
80+
errors: &OptVec<RegionResolutionError>);
8181

82-
pub fn report_and_explain_type_error(@mut self,
83-
trace: TypeTrace,
84-
terr: &ty::type_err);
82+
fn report_and_explain_type_error(@mut self,
83+
trace: TypeTrace,
84+
terr: &ty::type_err);
8585

8686
fn values_str(@mut self, values: &ValuePairs) -> Option<~str>;
8787

@@ -112,8 +112,8 @@ pub trait ErrorReporting {
112112

113113

114114
impl ErrorReporting for InferCtxt {
115-
pub fn report_region_errors(@mut self,
116-
errors: &OptVec<RegionResolutionError>) {
115+
fn report_region_errors(@mut self,
116+
errors: &OptVec<RegionResolutionError>) {
117117
for error in errors.iter() {
118118
match *error {
119119
ConcreteFailure(origin, sub, sup) => {
@@ -139,7 +139,7 @@ impl ErrorReporting for InferCtxt {
139139
}
140140
}
141141

142-
pub fn report_and_explain_type_error(@mut self,
142+
fn report_and_explain_type_error(@mut self,
143143
trace: TypeTrace,
144144
terr: &ty::type_err) {
145145
let tcx = self.tcx;

src/librustc/middle/typeck/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,11 @@ trait get_and_find_region {
294294
}
295295

296296
impl get_and_find_region for isr_alist {
297-
pub fn get(&self, br: ty::bound_region) -> ty::Region {
297+
fn get(&self, br: ty::bound_region) -> ty::Region {
298298
self.find(br).unwrap()
299299
}
300300

301-
pub fn find(&self, br: ty::bound_region) -> Option<ty::Region> {
301+
fn find(&self, br: ty::bound_region) -> Option<ty::Region> {
302302
let mut ret = None;
303303
do list::each(*self) |isr| {
304304
let (isr_br, isr_r) = *isr;

src/libstd/num/num.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,11 @@ impl_num_cast!(f64, to_f64)
413413
impl_num_cast!(float, to_float)
414414

415415
pub trait ToStrRadix {
416-
pub fn to_str_radix(&self, radix: uint) -> ~str;
416+
fn to_str_radix(&self, radix: uint) -> ~str;
417417
}
418418

419419
pub trait FromStrRadix {
420-
pub fn from_str_radix(str: &str, radix: uint) -> Option<Self>;
420+
fn from_str_radix(str: &str, radix: uint) -> Option<Self>;
421421
}
422422

423423
/// Calculates a power to a given radix, optimized for uint `pow` and `radix`.

src/libstd/ptr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<T, I: Int> Add<I, *T> for *T {
416416
/// Add an integer value to a pointer to get an offset pointer.
417417
/// Is calculated according to the size of the type pointed to.
418418
#[inline]
419-
pub fn add(&self, rhs: &I) -> *T {
419+
fn add(&self, rhs: &I) -> *T {
420420
self.offset(rhs.to_int() as int)
421421
}
422422
}
@@ -426,7 +426,7 @@ impl<T, I: Int> Sub<I, *T> for *T {
426426
/// Subtract an integer value from a pointer to get an offset pointer.
427427
/// Is calculated according to the size of the type pointed to.
428428
#[inline]
429-
pub fn sub(&self, rhs: &I) -> *T {
429+
fn sub(&self, rhs: &I) -> *T {
430430
self.offset(-rhs.to_int() as int)
431431
}
432432
}
@@ -436,7 +436,7 @@ impl<T, I: Int> Add<I, *mut T> for *mut T {
436436
/// Add an integer value to a pointer to get an offset pointer.
437437
/// Is calculated according to the size of the type pointed to.
438438
#[inline]
439-
pub fn add(&self, rhs: &I) -> *mut T {
439+
fn add(&self, rhs: &I) -> *mut T {
440440
self.offset(rhs.to_int() as int)
441441
}
442442
}
@@ -446,7 +446,7 @@ impl<T, I: Int> Sub<I, *mut T> for *mut T {
446446
/// Subtract an integer value from a pointer to get an offset pointer.
447447
/// Is calculated according to the size of the type pointed to.
448448
#[inline]
449-
pub fn sub(&self, rhs: &I) -> *mut T {
449+
fn sub(&self, rhs: &I) -> *mut T {
450450
self.offset(-rhs.to_int() as int)
451451
}
452452
}

src/libstd/rand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ pub mod rustrt {
260260
/// A random number generator
261261
pub trait Rng {
262262
/// Return the next random integer
263-
pub fn next(&mut self) -> u32;
263+
fn next(&mut self) -> u32;
264264
}
265265

266266
/// A value with a particular weight compared to other values
@@ -825,7 +825,7 @@ pub struct XorShiftRng {
825825

826826
impl Rng for XorShiftRng {
827827
#[inline]
828-
pub fn next(&mut self) -> u32 {
828+
fn next(&mut self) -> u32 {
829829
let x = self.x;
830830
let t = x ^ (x << 11);
831831
self.x = self.y;

src/libstd/rt/io/comm_adapters.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ impl<C: GenericChan<~[u8]>> ChanWriter<C> {
3131
}
3232

3333
impl<C: GenericChan<~[u8]>> Writer for ChanWriter<C> {
34-
pub fn write(&mut self, _buf: &[u8]) { fail!() }
34+
fn write(&mut self, _buf: &[u8]) { fail!() }
3535

36-
pub fn flush(&mut self) { fail!() }
36+
fn flush(&mut self) { fail!() }
3737
}
3838

3939
struct ReaderPort<R>;

0 commit comments

Comments
 (0)