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
}

0 commit comments

Comments
 (0)