From 930885d5e5f817e3d7609f93d5ba89b1abebfaf4 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 9 Aug 2013 01:25:24 -0700 Subject: [PATCH] Forbid pub/priv where it has no effect Closes #5495 --- src/libextra/crypto/sha1.rs | 8 +- src/libextra/enum_set.rs | 8 +- src/libextra/num/bigint.rs | 2 +- src/libextra/test.rs | 2 +- src/libextra/treemap.rs | 2 +- src/libextra/url.rs | 2 +- src/librustc/middle/privacy.rs | 79 +++++++++++++++++++ src/librustc/middle/trans/base.rs | 2 +- src/librustc/middle/ty.rs | 12 +-- src/librustc/middle/typeck/check/mod.rs | 4 +- .../middle/typeck/infer/error_reporting.rs | 16 ++-- src/librustc/middle/typeck/mod.rs | 4 +- src/librustdoc/config.rs | 8 +- src/libstd/num/num.rs | 4 +- src/libstd/ptr.rs | 8 +- src/libstd/rand.rs | 4 +- src/libstd/rt/io/comm_adapters.rs | 4 +- src/libstd/rt/uv/mod.rs | 12 +-- src/libstd/str.rs | 12 +-- src/libstd/vec.rs | 16 ++-- src/libsyntax/ast_util.rs | 2 +- src/libsyntax/attr.rs | 6 +- src/libsyntax/codemap.rs | 4 +- src/libsyntax/ext/deriving/generic.rs | 2 +- src/libsyntax/ext/quote.rs | 6 +- src/libsyntax/opt_vec.rs | 4 +- src/libsyntax/parse/obsolete.rs | 21 +++-- src/libsyntax/parse/parser.rs | 23 ++---- src/test/auxiliary/private_variant_xc.rs | 2 +- .../auxiliary/reexported_static_methods.rs | 8 +- src/test/compile-fail/lint-missing-doc.rs | 12 ++- src/test/compile-fail/useless-priv.rs | 25 ++++++ src/test/compile-fail/useless-priv2.rs | 19 +++++ src/test/run-pass/bug-7295.rs | 4 +- src/test/run-pass/issue-7712.rs | 2 +- src/test/run-pass/static-methods-in-traits.rs | 6 +- 36 files changed, 234 insertions(+), 121 deletions(-) create mode 100644 src/test/compile-fail/useless-priv.rs create mode 100644 src/test/compile-fail/useless-priv2.rs diff --git a/src/libextra/crypto/sha1.rs b/src/libextra/crypto/sha1.rs index aa52902c27d..8ee9006f613 100644 --- a/src/libextra/crypto/sha1.rs +++ b/src/libextra/crypto/sha1.rs @@ -159,7 +159,7 @@ impl Sha1 { } impl Digest for Sha1 { - pub fn reset(&mut self) { + fn reset(&mut self) { self.length_bits = 0; self.h[0] = 0x67452301u32; self.h[1] = 0xEFCDAB89u32; @@ -169,9 +169,9 @@ impl Digest for Sha1 { self.buffer.reset(); self.computed = false; } - pub fn input(&mut self, msg: &[u8]) { add_input(self, msg); } - pub fn result(&mut self, out: &mut [u8]) { return mk_result(self, out); } - pub fn output_bits(&self) -> uint { 160 } + fn input(&mut self, msg: &[u8]) { add_input(self, msg); } + fn result(&mut self, out: &mut [u8]) { return mk_result(self, out); } + fn output_bits(&self) -> uint { 160 } } #[cfg(test)] diff --git a/src/libextra/enum_set.rs b/src/libextra/enum_set.rs index 25501faa02e..28ff16c43e8 100644 --- a/src/libextra/enum_set.rs +++ b/src/libextra/enum_set.rs @@ -21,9 +21,9 @@ pub struct EnumSet { /// An iterface for casting C-like enum to uint and back. pub trait CLike { /// Converts C-like enum to uint. - pub fn to_uint(&self) -> uint; + fn to_uint(&self) -> uint; /// Converts uint to C-like enum. - pub fn from_uint(uint) -> Self; + fn from_uint(uint) -> Self; } fn bit(e: E) -> uint { @@ -142,11 +142,11 @@ mod test { } impl CLike for Foo { - pub fn to_uint(&self) -> uint { + fn to_uint(&self) -> uint { *self as uint } - pub fn from_uint(v: uint) -> Foo { + fn from_uint(v: uint) -> Foo { unsafe { cast::transmute(v) } } } diff --git a/src/libextra/num/bigint.rs b/src/libextra/num/bigint.rs index 27dfc090f88..354696ef420 100644 --- a/src/libextra/num/bigint.rs +++ b/src/libextra/num/bigint.rs @@ -537,7 +537,7 @@ impl ToStrRadix for BigUint { impl FromStrRadix for BigUint { /// Creates and initializes an BigUint. - pub fn from_str_radix(s: &str, radix: uint) + fn from_str_radix(s: &str, radix: uint) -> Option { BigUint::parse_bytes(s.as_bytes(), radix) } diff --git a/src/libextra/test.rs b/src/libextra/test.rs index 8b7332ff545..a9c3bf98cb6 100644 --- a/src/libextra/test.rs +++ b/src/libextra/test.rs @@ -104,7 +104,7 @@ pub struct Metric { pub struct MetricMap(TreeMap<~str,Metric>); impl Clone for MetricMap { - pub fn clone(&self) -> MetricMap { + fn clone(&self) -> MetricMap { MetricMap((**self).clone()) } } diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs index f5a61692509..486a7dab5c1 100644 --- a/src/libextra/treemap.rs +++ b/src/libextra/treemap.rs @@ -853,7 +853,7 @@ impl> Extendable<(K, V), T> for TreeMap> FromIterator for TreeSet { - pub fn from_iterator(iter: &mut Iter) -> TreeSet { + fn from_iterator(iter: &mut Iter) -> TreeSet { let mut set = TreeSet::new(); set.extend(iter); set diff --git a/src/libextra/url.rs b/src/libextra/url.rs index 581bdf2a294..3f23cbd02ab 100644 --- a/src/libextra/url.rs +++ b/src/libextra/url.rs @@ -701,7 +701,7 @@ pub fn to_str(url: &Url) -> ~str { } impl ToStr for Url { - pub fn to_str(&self) -> ~str { + fn to_str(&self) -> ~str { to_str(self) } } diff --git a/src/librustc/middle/privacy.rs b/src/librustc/middle/privacy.rs index aa7b2e55cdc..54e7c79e97c 100644 --- a/src/librustc/middle/privacy.rs +++ b/src/librustc/middle/privacy.rs @@ -351,6 +351,7 @@ pub fn check_crate<'mm>(tcx: ty::ctxt, // Do not check privacy inside items with the resolve_unexported // attribute. This is used for the test runner. if !attr::contains_name(item.attrs, "!resolve_unexported") { + check_sane_privacy(tcx, item); oldvisit::visit_item(item, (method_map, visitor)); } }, @@ -540,3 +541,81 @@ pub fn check_crate<'mm>(tcx: ty::ctxt, }); oldvisit::visit_crate(crate, (method_map, visitor)); } + +/// Validates all of the visibility qualifers placed on the item given. This +/// ensures that there are no extraneous qualifiers that don't actually do +/// anything. In theory these qualifiers wouldn't parse, but that may happen +/// later on down the road... +fn check_sane_privacy(tcx: ty::ctxt, item: @ast::item) { + match item.node { + // implementations of traits don't need visibility qualifiers because + // that's controlled by having the trait in scope. + ast::item_impl(_, Some(*), _, ref methods) => { + for m in methods.iter() { + match m.vis { + ast::private | ast::public => { + tcx.sess.span_err(m.span, "unnecessary visibility") + } + ast::inherited => {} + } + } + } + + ast::item_enum(ref def, _) => { + for v in def.variants.iter() { + match v.node.vis { + ast::public => { + if item.vis == ast::public { + tcx.sess.span_err(v.span, "unnecessary `pub` \ + visibility"); + } + } + ast::private => { + if item.vis != ast::public { + tcx.sess.span_err(v.span, "unnecessary `priv` \ + visibility"); + } + } + ast::inherited => {} + } + } + } + + ast::item_struct(ref def, _) => { + for f in def.fields.iter() { + match f.node.kind { + ast::named_field(_, ast::public) => { + tcx.sess.span_err(f.span, "unnecessary `pub` \ + visibility"); + } + ast::named_field(_, ast::private) => { + // Fields should really be private by default... + } + ast::named_field(*) | ast::unnamed_field => {} + } + } + } + + ast::item_trait(_, _, ref methods) => { + for m in methods.iter() { + match *m { + ast::provided(ref m) => { + match m.vis { + ast::private | ast::public => { + tcx.sess.span_err(m.span, "unnecessary \ + visibility"); + } + ast::inherited => {} + } + } + // this is warned about in the parser + ast::required(*) => {} + } + } + } + + ast::item_impl(*) | ast::item_static(*) | ast::item_foreign_mod(*) | + ast::item_fn(*) | ast::item_mod(*) | ast::item_ty(*) | + ast::item_mac(*) => {} + } +} diff --git a/src/librustc/middle/trans/base.rs b/src/librustc/middle/trans/base.rs index 1291a586682..7331b50dd10 100644 --- a/src/librustc/middle/trans/base.rs +++ b/src/librustc/middle/trans/base.rs @@ -165,7 +165,7 @@ impl<'self> StatRecorder<'self> { #[unsafe_destructor] impl<'self> Drop for StatRecorder<'self> { - pub fn drop(&self) { + fn drop(&self) { if self.ccx.sess.trans_stats() { let end = time::precise_time_ns(); let elapsed = ((end - self.start) / 1_000_000) as uint; diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 2ba6930d9c5..80d6ce6ab08 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -709,10 +709,10 @@ pub fn AllBuiltinBounds() -> BuiltinBounds { } impl CLike for BuiltinBound { - pub fn to_uint(&self) -> uint { + fn to_uint(&self) -> uint { *self as uint } - pub fn from_uint(v: uint) -> BuiltinBound { + fn from_uint(v: uint) -> BuiltinBound { unsafe { cast::transmute(v) } } } @@ -4345,16 +4345,16 @@ pub fn normalize_ty(cx: ctxt, t: t) -> t { } pub trait ExprTyProvider { - pub fn expr_ty(&self, ex: &ast::expr) -> t; - pub fn ty_ctxt(&self) -> ctxt; + fn expr_ty(&self, ex: &ast::expr) -> t; + fn ty_ctxt(&self) -> ctxt; } impl ExprTyProvider for ctxt { - pub fn expr_ty(&self, ex: &ast::expr) -> t { + fn expr_ty(&self, ex: &ast::expr) -> t { expr_ty(*self, ex) } - pub fn ty_ctxt(&self) -> ctxt { + fn ty_ctxt(&self) -> ctxt { *self } } diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index e7ef30c4576..d27b7f07a12 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -287,11 +287,11 @@ pub fn blank_fn_ctxt(ccx: @mut CrateCtxt, } impl ExprTyProvider for FnCtxt { - pub fn expr_ty(&self, ex: &ast::expr) -> ty::t { + fn expr_ty(&self, ex: &ast::expr) -> ty::t { self.expr_ty(ex) } - pub fn ty_ctxt(&self) -> ty::ctxt { + fn ty_ctxt(&self) -> ty::ctxt { self.ccx.tcx } } diff --git a/src/librustc/middle/typeck/infer/error_reporting.rs b/src/librustc/middle/typeck/infer/error_reporting.rs index 1b325dd8a4b..2c8e7b28059 100644 --- a/src/librustc/middle/typeck/infer/error_reporting.rs +++ b/src/librustc/middle/typeck/infer/error_reporting.rs @@ -76,12 +76,12 @@ use util::ppaux::UserString; use util::ppaux::note_and_explain_region; pub trait ErrorReporting { - pub fn report_region_errors(@mut self, - errors: &OptVec); + fn report_region_errors(@mut self, + errors: &OptVec); - pub fn report_and_explain_type_error(@mut self, - trace: TypeTrace, - terr: &ty::type_err); + fn report_and_explain_type_error(@mut self, + trace: TypeTrace, + terr: &ty::type_err); fn values_str(@mut self, values: &ValuePairs) -> Option<~str>; @@ -112,8 +112,8 @@ pub trait ErrorReporting { impl ErrorReporting for InferCtxt { - pub fn report_region_errors(@mut self, - errors: &OptVec) { + fn report_region_errors(@mut self, + errors: &OptVec) { for error in errors.iter() { match *error { ConcreteFailure(origin, sub, sup) => { @@ -139,7 +139,7 @@ impl ErrorReporting for InferCtxt { } } - pub fn report_and_explain_type_error(@mut self, + fn report_and_explain_type_error(@mut self, trace: TypeTrace, terr: &ty::type_err) { let tcx = self.tcx; diff --git a/src/librustc/middle/typeck/mod.rs b/src/librustc/middle/typeck/mod.rs index 53ae80f19fa..61027519b5b 100644 --- a/src/librustc/middle/typeck/mod.rs +++ b/src/librustc/middle/typeck/mod.rs @@ -294,11 +294,11 @@ trait get_and_find_region { } impl get_and_find_region for isr_alist { - pub fn get(&self, br: ty::bound_region) -> ty::Region { + fn get(&self, br: ty::bound_region) -> ty::Region { self.find(br).unwrap() } - pub fn find(&self, br: ty::bound_region) -> Option { + fn find(&self, br: ty::bound_region) -> Option { let mut ret = None; do list::each(*self) |isr| { let (isr_br, isr_r) = *isr; diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index 877338902cc..dd43e22fc0c 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -22,18 +22,18 @@ use extra::getopts; #[deriving(Clone, Eq)] pub enum OutputFormat { /// Markdown - pub Markdown, + Markdown, /// HTML, via markdown and pandoc - pub PandocHtml + PandocHtml } /// How to organize the output #[deriving(Clone, Eq)] pub enum OutputStyle { /// All in a single document - pub DocPerCrate, + DocPerCrate, /// Each module in its own document - pub DocPerMod + DocPerMod } /// The configuration for a rustdoc session diff --git a/src/libstd/num/num.rs b/src/libstd/num/num.rs index 3af1666b4da..9e72b355bf9 100644 --- a/src/libstd/num/num.rs +++ b/src/libstd/num/num.rs @@ -414,11 +414,11 @@ impl_num_cast!(f64, to_f64) impl_num_cast!(float, to_float) pub trait ToStrRadix { - pub fn to_str_radix(&self, radix: uint) -> ~str; + fn to_str_radix(&self, radix: uint) -> ~str; } pub trait FromStrRadix { - pub fn from_str_radix(str: &str, radix: uint) -> Option; + fn from_str_radix(str: &str, radix: uint) -> Option; } /// Calculates a power to a given radix, optimized for uint `pow` and `radix`. diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index 96f3e480617..b13d46d540d 100644 --- a/src/libstd/ptr.rs +++ b/src/libstd/ptr.rs @@ -394,7 +394,7 @@ impl Add for *T { /// Add an integer value to a pointer to get an offset pointer. /// Is calculated according to the size of the type pointed to. #[inline] - pub fn add(&self, rhs: &I) -> *T { + fn add(&self, rhs: &I) -> *T { self.offset(rhs.to_int() as int) } } @@ -404,7 +404,7 @@ impl Sub for *T { /// Subtract an integer value from a pointer to get an offset pointer. /// Is calculated according to the size of the type pointed to. #[inline] - pub fn sub(&self, rhs: &I) -> *T { + fn sub(&self, rhs: &I) -> *T { self.offset(-rhs.to_int() as int) } } @@ -414,7 +414,7 @@ impl Add for *mut T { /// Add an integer value to a pointer to get an offset pointer. /// Is calculated according to the size of the type pointed to. #[inline] - pub fn add(&self, rhs: &I) -> *mut T { + fn add(&self, rhs: &I) -> *mut T { self.offset(rhs.to_int() as int) } } @@ -424,7 +424,7 @@ impl Sub for *mut T { /// Subtract an integer value from a pointer to get an offset pointer. /// Is calculated according to the size of the type pointed to. #[inline] - pub fn sub(&self, rhs: &I) -> *mut T { + fn sub(&self, rhs: &I) -> *mut T { self.offset(-rhs.to_int() as int) } } diff --git a/src/libstd/rand.rs b/src/libstd/rand.rs index 5f8fa9fddbc..500278fddb0 100644 --- a/src/libstd/rand.rs +++ b/src/libstd/rand.rs @@ -260,7 +260,7 @@ pub mod rustrt { /// A random number generator pub trait Rng { /// Return the next random integer - pub fn next(&mut self) -> u32; + fn next(&mut self) -> u32; } /// A value with a particular weight compared to other values @@ -825,7 +825,7 @@ pub struct XorShiftRng { impl Rng for XorShiftRng { #[inline] - pub fn next(&mut self) -> u32 { + fn next(&mut self) -> u32 { let x = self.x; let t = x ^ (x << 11); self.x = self.y; diff --git a/src/libstd/rt/io/comm_adapters.rs b/src/libstd/rt/io/comm_adapters.rs index 6a3e44b2a4d..06424fee8bc 100644 --- a/src/libstd/rt/io/comm_adapters.rs +++ b/src/libstd/rt/io/comm_adapters.rs @@ -31,9 +31,9 @@ impl> ChanWriter { } impl> Writer for ChanWriter { - pub fn write(&mut self, _buf: &[u8]) { fail!() } + fn write(&mut self, _buf: &[u8]) { fail!() } - pub fn flush(&mut self) { fail!() } + fn flush(&mut self) { fail!() } } struct ReaderPort; diff --git a/src/libstd/rt/uv/mod.rs b/src/libstd/rt/uv/mod.rs index ae5e7dd27b5..6c5a28b31b1 100644 --- a/src/libstd/rt/uv/mod.rs +++ b/src/libstd/rt/uv/mod.rs @@ -90,8 +90,8 @@ pub trait Request { } /// A type that wraps a native handle pub trait NativeHandle { - pub fn from_native_handle(T) -> Self; - pub fn native_handle(&self) -> T; + fn from_native_handle(T) -> Self; + fn native_handle(&self) -> T; } impl Loop { @@ -155,7 +155,7 @@ pub trait WatcherInterop { impl> WatcherInterop for W { /// Get the uv event loop from a Watcher - pub fn event_loop(&self) -> Loop { + fn event_loop(&self) -> Loop { unsafe { let handle = self.native_handle(); let loop_ = uvll::get_loop_for_uv_handle(handle); @@ -163,7 +163,7 @@ impl> WatcherInterop for W { } } - pub fn install_watcher_data(&mut self) { + fn install_watcher_data(&mut self) { unsafe { let data = ~WatcherData { read_cb: None, @@ -182,7 +182,7 @@ impl> WatcherInterop for W { } } - pub fn get_watcher_data<'r>(&'r mut self) -> &'r mut WatcherData { + fn get_watcher_data<'r>(&'r mut self) -> &'r mut WatcherData { unsafe { let data = uvll::get_data_for_uv_handle(self.native_handle()); let data = transmute::<&*c_void, &mut ~WatcherData>(&data); @@ -190,7 +190,7 @@ impl> WatcherInterop for W { } } - pub fn drop_watcher_data(&mut self) { + fn drop_watcher_data(&mut self) { unsafe { let data = uvll::get_data_for_uv_handle(self.native_handle()); let _data = transmute::<*c_void, ~WatcherData>(data); diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 9e5f2db4092..886e4d86ab6 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -142,13 +142,13 @@ pub fn push_str(lhs: &mut ~str, rhs: &str) { #[allow(missing_doc)] pub trait StrVector { - pub fn concat(&self) -> ~str; - pub fn connect(&self, sep: &str) -> ~str; + fn concat(&self) -> ~str; + fn connect(&self, sep: &str) -> ~str; } impl<'self, S: Str> StrVector for &'self [S] { /// Concatenate a vector of strings. - pub fn concat(&self) -> ~str { + fn concat(&self) -> ~str { if self.is_empty() { return ~""; } let len = self.iter().map(|s| s.as_slice().len()).sum(); @@ -171,7 +171,7 @@ impl<'self, S: Str> StrVector for &'self [S] { } /// Concatenate a vector of strings, placing a given separator between each. - pub fn connect(&self, sep: &str) -> ~str { + fn connect(&self, sep: &str) -> ~str { if self.is_empty() { return ~""; } // concat is faster @@ -1554,7 +1554,7 @@ impl<'self> StrSlice<'self> for &'self str { /// # Return value /// /// The original string with all occurances of `from` replaced with `to` - pub fn replace(&self, from: &str, to: &str) -> ~str { + fn replace(&self, from: &str, to: &str) -> ~str { let mut result = ~""; let mut last_end = 0; for (start, end) in self.matches_index_iter(from) { @@ -2081,7 +2081,7 @@ impl OwnedStr for ~str { /// * s - A string /// * n - The number of bytes to reserve space for #[inline] - pub fn reserve(&mut self, n: uint) { + fn reserve(&mut self, n: uint) { unsafe { let v: &mut ~[u8] = cast::transmute(self); (*v).reserve(n); diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 2228922d9e4..27e09d85479 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -313,18 +313,18 @@ pub fn connect_slices(v: &[&[T]], sep: &T) -> ~[T] { v.connect_vec(sep) pub trait VectorVector { // FIXME #5898: calling these .concat and .connect conflicts with // StrVector::con{cat,nect}, since they have generic contents. - pub fn concat_vec(&self) -> ~[T]; - pub fn connect_vec(&self, sep: &T) -> ~[T]; + fn concat_vec(&self) -> ~[T]; + fn connect_vec(&self, sep: &T) -> ~[T]; } impl<'self, T:Clone> VectorVector for &'self [~[T]] { /// Flattens a vector of slices of T into a single vector of T. - pub fn concat_vec(&self) -> ~[T] { + fn concat_vec(&self) -> ~[T] { self.flat_map(|inner| (*inner).clone()) } /// Concatenate a vector of vectors, placing a given separator between each. - pub fn connect_vec(&self, sep: &T) -> ~[T] { + fn connect_vec(&self, sep: &T) -> ~[T] { let mut r = ~[]; let mut first = true; for inner in self.iter() { @@ -337,12 +337,12 @@ impl<'self, T:Clone> VectorVector for &'self [~[T]] { impl<'self,T:Clone> VectorVector for &'self [&'self [T]] { /// Flattens a vector of slices of T into a single vector of T. - pub fn concat_vec(&self) -> ~[T] { + fn concat_vec(&self) -> ~[T] { self.flat_map(|&inner| inner.to_owned()) } /// Concatenate a vector of slices, placing a given separator between each. - pub fn connect_vec(&self, sep: &T) -> ~[T] { + fn connect_vec(&self, sep: &T) -> ~[T] { let mut r = ~[]; let mut first = true; for &inner in self.iter() { @@ -1649,7 +1649,7 @@ impl OwnedEqVector for ~[T] { * Remove consecutive repeated elements from a vector; if the vector is * sorted, this removes all duplicates. */ - pub fn dedup(&mut self) { + fn dedup(&mut self) { unsafe { // Although we have a mutable reference to `self`, we cannot make // *arbitrary* changes. There exists the possibility that this @@ -2079,7 +2079,7 @@ pub mod bytes { /// A trait for operations on mutable operations on `[u8]` pub trait MutableByteVector { /// Sets all bytes of the receiver to the given value. - pub fn set_memory(self, value: u8); + fn set_memory(self, value: u8); } impl<'self> MutableByteVector for &'self mut [u8] { diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index d99f8cab493..02eaf432b54 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -672,7 +672,7 @@ pub fn walk_pat(pat: @pat, it: &fn(@pat) -> bool) -> bool { } pub trait EachViewItem { - pub fn each_view_item(&self, f: @fn(&ast::view_item) -> bool) -> bool; + fn each_view_item(&self, f: @fn(&ast::view_item) -> bool) -> bool; } struct EachViewItemData { diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 47d8ebecca0..51df08f5228 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -82,7 +82,7 @@ impl AttrMetaMethods for MetaItem { } } - pub fn name_str_pair(&self) -> Option<(@str, @str)> { + fn name_str_pair(&self) -> Option<(@str, @str)> { self.value_str().map_move(|s| (self.name(), s)) } } @@ -105,14 +105,14 @@ pub trait AttributeMethods { impl AttributeMethods for Attribute { /// Extract the MetaItem from inside this Attribute. - pub fn meta(&self) -> @MetaItem { + fn meta(&self) -> @MetaItem { self.node.value } /// Convert self to a normal #[doc="foo"] comment, if it is a /// comment like `///` or `/** */`. (Returns self unchanged for /// non-sugared doc attributes.) - pub fn desugar_doc(&self) -> Attribute { + fn desugar_doc(&self) -> Attribute { if self.node.is_sugared_doc { let comment = self.value_str().unwrap(); let meta = mk_name_value_item_str(@"doc", diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 8c70f128d9a..d4337523cfb 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -194,10 +194,10 @@ pub struct FileLines // represents the origin of a file: pub enum FileSubstr { // indicates that this is a normal standalone file: - pub FssNone, + FssNone, // indicates that this "file" is actually a substring // of another file that appears earlier in the codemap - pub FssInternal(span), + FssInternal(span), } /// Identifies an offset of a multi-byte character in a FileMap diff --git a/src/libsyntax/ext/deriving/generic.rs b/src/libsyntax/ext/deriving/generic.rs index a1abe47e090..6b028e25c0f 100644 --- a/src/libsyntax/ext/deriving/generic.rs +++ b/src/libsyntax/ext/deriving/generic.rs @@ -542,7 +542,7 @@ impl<'self> MethodDef<'self> { id: cx.next_id(), span: span, self_id: cx.next_id(), - vis: ast::public + vis: ast::inherited, } } diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index d218be5e476..9d82bb9c4f8 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -40,11 +40,11 @@ pub mod rt { pub use codemap::{BytePos, span, dummy_spanned}; pub trait ToTokens { - pub fn to_tokens(&self, _cx: @ExtCtxt) -> ~[token_tree]; + fn to_tokens(&self, _cx: @ExtCtxt) -> ~[token_tree]; } impl ToTokens for ~[token_tree] { - pub fn to_tokens(&self, _cx: @ExtCtxt) -> ~[token_tree] { + fn to_tokens(&self, _cx: @ExtCtxt) -> ~[token_tree] { (*self).clone() } } @@ -65,7 +65,7 @@ pub mod rt { pub trait ToSource { // Takes a thing and generates a string containing rust code for it. - pub fn to_source(&self) -> @str; + fn to_source(&self) -> @str; } impl ToSource for ast::ident { diff --git a/src/libsyntax/opt_vec.rs b/src/libsyntax/opt_vec.rs index 3a10206b513..2d7801a22de 100644 --- a/src/libsyntax/opt_vec.rs +++ b/src/libsyntax/opt_vec.rs @@ -124,7 +124,7 @@ impl OptVec { } impl Eq for OptVec { - pub fn eq(&self, other: &OptVec) -> bool { + fn eq(&self, other: &OptVec) -> bool { // Note: cannot use #[deriving(Eq)] here because // (Empty, Vec(~[])) ought to be equal. match (self, other) { @@ -135,7 +135,7 @@ impl Eq for OptVec { } } - pub fn ne(&self, other: &OptVec) -> bool { + fn ne(&self, other: &OptVec) -> bool { !self.eq(other) } } diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index dda5e990221..01c1af7464d 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -65,6 +65,7 @@ pub enum ObsoleteSyntax { ObsoleteExternVisibility, ObsoleteUnsafeExternFn, ObsoletePrivVisibility, + ObsoleteTraitFuncVisibility, } impl to_bytes::IterBytes for ObsoleteSyntax { @@ -95,7 +96,7 @@ pub trait ParserObsoleteMethods { impl ParserObsoleteMethods for Parser { /// Reports an obsolete syntax non-fatal error. - pub fn obsolete(&self, sp: span, kind: ObsoleteSyntax) { + fn obsolete(&self, sp: span, kind: ObsoleteSyntax) { let (kind_str, desc) = match kind { ObsoleteLet => ( "`let` in field declaration", @@ -258,6 +259,10 @@ impl ParserObsoleteMethods for Parser { "`priv` not necessary", "an item without a visibility qualifier is private by default" ), + ObsoleteTraitFuncVisibility => ( + "visibility not necessary", + "trait functions inherit the visibility of the trait itself" + ), }; self.report(sp, kind, kind_str, desc); @@ -265,7 +270,7 @@ impl ParserObsoleteMethods for Parser { // Reports an obsolete syntax non-fatal error, and returns // a placeholder expression - pub fn obsolete_expr(&self, sp: span, kind: ObsoleteSyntax) -> @expr { + fn obsolete_expr(&self, sp: span, kind: ObsoleteSyntax) -> @expr { self.obsolete(sp, kind); self.mk_expr(sp.lo, sp.hi, expr_lit(@respan(sp, lit_nil))) } @@ -283,7 +288,7 @@ impl ParserObsoleteMethods for Parser { } } - pub fn token_is_obsolete_ident(&self, ident: &str, token: &Token) + fn token_is_obsolete_ident(&self, ident: &str, token: &Token) -> bool { match *token { token::IDENT(sid, _) => { @@ -293,11 +298,11 @@ impl ParserObsoleteMethods for Parser { } } - pub fn is_obsolete_ident(&self, ident: &str) -> bool { + fn is_obsolete_ident(&self, ident: &str) -> bool { self.token_is_obsolete_ident(ident, self.token) } - pub fn eat_obsolete_ident(&self, ident: &str) -> bool { + fn eat_obsolete_ident(&self, ident: &str) -> bool { if self.is_obsolete_ident(ident) { self.bump(); true @@ -306,7 +311,7 @@ impl ParserObsoleteMethods for Parser { } } - pub fn try_parse_obsolete_struct_ctor(&self) -> bool { + fn try_parse_obsolete_struct_ctor(&self) -> bool { if self.eat_obsolete_ident("new") { self.obsolete(*self.last_span, ObsoleteStructCtor); self.parse_fn_decl(); @@ -317,7 +322,7 @@ impl ParserObsoleteMethods for Parser { } } - pub fn try_parse_obsolete_with(&self) -> bool { + fn try_parse_obsolete_with(&self) -> bool { if *self.token == token::COMMA && self.look_ahead(1, |t| self.token_is_obsolete_ident("with", t)) { @@ -332,7 +337,7 @@ impl ParserObsoleteMethods for Parser { } } - pub fn try_parse_obsolete_priv_section(&self, attrs: &[Attribute]) + fn try_parse_obsolete_priv_section(&self, attrs: &[Attribute]) -> bool { if self.is_keyword(keywords::Priv) && self.look_ahead(1, |t| *t == token::LBRACE) { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 7e18c440d81..b38de31c56a 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -70,24 +70,7 @@ use parse::common::{SeqSep, seq_sep_none}; use parse::common::{seq_sep_trailing_disallowed, seq_sep_trailing_allowed}; use parse::lexer::reader; use parse::lexer::TokenAndSpan; -use parse::obsolete::{ObsoleteClassTraits}; -use parse::obsolete::{ObsoleteLet, ObsoleteFieldTerminator}; -use parse::obsolete::{ObsoleteMoveInit, ObsoleteBinaryMove, ObsoleteSwap}; -use parse::obsolete::ObsoleteSyntax; -use parse::obsolete::{ObsoleteUnsafeBlock, ObsoleteImplSyntax}; -use parse::obsolete::{ObsoleteMutOwnedPointer}; -use parse::obsolete::{ObsoleteMutVector, ObsoleteImplVisibility}; -use parse::obsolete::{ObsoleteRecordType, ObsoleteRecordPattern}; -use parse::obsolete::{ObsoletePostFnTySigil}; -use parse::obsolete::{ObsoleteBareFnType, ObsoleteNewtypeEnum}; -use parse::obsolete::ObsoleteMode; -use parse::obsolete::{ObsoleteLifetimeNotation, ObsoleteConstManagedPointer}; -use parse::obsolete::{ObsoletePurity, ObsoleteStaticMethod}; -use parse::obsolete::{ObsoleteConstItem, ObsoleteFixedLengthVectorType}; -use parse::obsolete::{ObsoleteNamedExternModule, ObsoleteMultipleLocalDecl}; -use parse::obsolete::{ObsoleteMutWithMultipleBindings}; -use parse::obsolete::{ObsoleteExternVisibility, ObsoleteUnsafeExternFn}; -use parse::obsolete::{ParserObsoleteMethods, ObsoletePrivVisibility}; +use parse::obsolete::*; use parse::token::{can_begin_expr, get_ident_interner, ident_to_str, is_ident}; use parse::token::{is_ident_or_path}; use parse::token::{is_plain_ident, INTERPOLATED, keywords, special_idents}; @@ -932,6 +915,10 @@ impl Parser { debug!("parse_trait_methods(): parsing required method"); // NB: at the moment, visibility annotations on required // methods are ignored; this could change. + if vis != ast::inherited { + self.obsolete(*self.last_span, + ObsoleteTraitFuncVisibility); + } required(TypeMethod { ident: ident, attrs: attrs, diff --git a/src/test/auxiliary/private_variant_xc.rs b/src/test/auxiliary/private_variant_xc.rs index a3a604d9e78..e1ecbf8c543 100644 --- a/src/test/auxiliary/private_variant_xc.rs +++ b/src/test/auxiliary/private_variant_xc.rs @@ -1,4 +1,4 @@ pub enum Foo { - pub Bar, + Bar, priv Baz, } diff --git a/src/test/auxiliary/reexported_static_methods.rs b/src/test/auxiliary/reexported_static_methods.rs index 811bf082ae8..bb20b04762d 100644 --- a/src/test/auxiliary/reexported_static_methods.rs +++ b/src/test/auxiliary/reexported_static_methods.rs @@ -14,20 +14,20 @@ pub use sub_foo::Boz; pub use sub_foo::Bort; pub trait Bar { - pub fn bar() -> Self; + fn bar() -> Self; } impl Bar for int { - pub fn bar() -> int { 84 } + fn bar() -> int { 84 } } pub mod sub_foo { pub trait Foo { - pub fn foo() -> Self; + fn foo() -> Self; } impl Foo for int { - pub fn foo() -> int { 42 } + fn foo() -> int { 42 } } pub struct Boz { diff --git a/src/test/compile-fail/lint-missing-doc.rs b/src/test/compile-fail/lint-missing-doc.rs index 2350ca68b97..75bc93c6d0b 100644 --- a/src/test/compile-fail/lint-missing-doc.rs +++ b/src/test/compile-fail/lint-missing-doc.rs @@ -15,19 +15,17 @@ struct Foo { a: int, priv b: int, - pub c: int, // doesn't matter, Foo is private } pub struct PubFoo { //~ ERROR: missing documentation a: int, //~ ERROR: missing documentation priv b: int, - pub c: int, //~ ERROR: missing documentation } #[allow(missing_doc)] pub struct PubFoo2 { a: int, - pub c: int, + c: int, } /// dox @@ -44,9 +42,9 @@ pub trait C {} //~ ERROR: missing documentation trait Bar { /// dox - pub fn foo(); + fn foo(); fn foo2(); //~ ERROR: missing documentation - pub fn foo3(); //~ ERROR: missing documentation + fn foo3(); //~ ERROR: missing documentation } impl Foo { @@ -59,13 +57,13 @@ impl Foo { #[allow(missing_doc)] trait F { - pub fn a(); + fn a(); fn b(&self); } // should need to redefine documentation for implementations of traits impl F for Foo { - pub fn a() {} + fn a() {} fn b(&self) {} } diff --git a/src/test/compile-fail/useless-priv.rs b/src/test/compile-fail/useless-priv.rs new file mode 100644 index 00000000000..3d6841c919a --- /dev/null +++ b/src/test/compile-fail/useless-priv.rs @@ -0,0 +1,25 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct A { pub i: int } //~ ERROR: unnecessary `pub` +struct B { priv i: int } // don't warn b/c B can still be returned +pub enum C { pub Variant } //~ ERROR: unnecessary `pub` +enum D { priv Variant2 } //~ ERROR: unnecessary `priv` + +pub trait E { + pub fn foo() {} //~ ERROR: unnecessary visibility +} +trait F { pub fn foo() {} } //~ ERROR: unnecessary visibility + +impl E for A { + pub fn foo() {} //~ ERROR: unnecessary visibility +} + +fn main() {} diff --git a/src/test/compile-fail/useless-priv2.rs b/src/test/compile-fail/useless-priv2.rs new file mode 100644 index 00000000000..0d94300329c --- /dev/null +++ b/src/test/compile-fail/useless-priv2.rs @@ -0,0 +1,19 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub trait E { + pub fn foo(); //~ ERROR: obsolete syntax +} +trait F { pub fn foo(); } //~ ERROR: obsolete syntax + +struct B; +impl E for B { + priv fn foo() {} //~ ERROR: obsolete syntax +} diff --git a/src/test/run-pass/bug-7295.rs b/src/test/run-pass/bug-7295.rs index 2a1a0e35c56..ea711d78dd2 100644 --- a/src/test/run-pass/bug-7295.rs +++ b/src/test/run-pass/bug-7295.rs @@ -9,9 +9,9 @@ // except according to those terms. pub trait Foo { - pub fn func1(&self, t: U); + fn func1(&self, t: U); - pub fn func2(&self, t: U) { + fn func2(&self, t: U) { self.func1(t); } } diff --git a/src/test/run-pass/issue-7712.rs b/src/test/run-pass/issue-7712.rs index 41c4af86bac..1ea8312ddd9 100644 --- a/src/test/run-pass/issue-7712.rs +++ b/src/test/run-pass/issue-7712.rs @@ -11,7 +11,7 @@ // compile-flags:-Z debug-info pub trait TraitWithDefaultMethod { - pub fn method(self) { + fn method(self) { () } } diff --git a/src/test/run-pass/static-methods-in-traits.rs b/src/test/run-pass/static-methods-in-traits.rs index 8cd7b78312b..180cd115c96 100644 --- a/src/test/run-pass/static-methods-in-traits.rs +++ b/src/test/run-pass/static-methods-in-traits.rs @@ -10,17 +10,17 @@ mod a { pub trait Foo { - pub fn foo() -> Self; + fn foo() -> Self; } impl Foo for int { - pub fn foo() -> int { + fn foo() -> int { 3 } } impl Foo for uint { - pub fn foo() -> uint { + fn foo() -> uint { 5u } }