Skip to content

Commit 8eda5d8

Browse files
committed
auto merge of #10443 : alexcrichton/rust/meaninless-pub-priv, r=cmr
Closes #10111
2 parents 727b70d + dab8fec commit 8eda5d8

File tree

10 files changed

+121
-13
lines changed

10 files changed

+121
-13
lines changed

doc/rust.md

+1
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,7 @@ keyword for struct fields and enum variants). When an item is declared as `pub`,
15501550
it can be thought of as being accessible to the outside world. For example:
15511551

15521552
~~~~
1553+
# fn main() {}
15531554
// Declare a private struct
15541555
struct Foo;
15551556

doc/tutorial-container.md

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ Reaching the end of the iterator is signalled by returning `None` instead of
8787
`Some(item)`:
8888
8989
~~~
90+
# fn main() {}
9091
/// A stream of N zeroes
9192
struct ZeroStream {
9293
priv remaining: uint
@@ -301,6 +302,7 @@ the iterator can provide better information.
301302
The `ZeroStream` from earlier can provide an exact lower and upper bound:
302303

303304
~~~
305+
# fn main() {}
304306
/// A stream of N zeroes
305307
struct ZeroStream {
306308
priv remaining: uint

doc/tutorial-macros.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Now consider code like the following:
216216

217217
~~~~
218218
# enum t1 { good_1(t2, uint), bad_1 };
219-
# pub struct t2 { body: t3 }
219+
# struct t2 { body: t3 }
220220
# enum t3 { good_2(uint), bad_2};
221221
# fn f(x: t1) -> uint {
222222
match x {
@@ -262,7 +262,7 @@ macro_rules! biased_match (
262262
)
263263
264264
# enum t1 { good_1(t2, uint), bad_1 };
265-
# pub struct t2 { body: t3 }
265+
# struct t2 { body: t3 }
266266
# enum t3 { good_2(uint), bad_2};
267267
# fn f(x: t1) -> uint {
268268
biased_match!((x) ~ (good_1(g1, val)) else { return 0 };
@@ -364,7 +364,7 @@ macro_rules! biased_match (
364364
365365
366366
# enum t1 { good_1(t2, uint), bad_1 };
367-
# pub struct t2 { body: t3 }
367+
# struct t2 { body: t3 }
368368
# enum t3 { good_2(uint), bad_2};
369369
# fn f(x: t1) -> uint {
370370
biased_match!(

doc/tutorial.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -2568,6 +2568,7 @@ pub fn foo() { bar(); }
25682568
~~~
25692569
// c.rs
25702570
pub fn bar() { println("Baz!"); }
2571+
# fn main() {}
25712572
~~~
25722573

25732574
There also exist two short forms for importing multiple names at once:
@@ -2743,7 +2744,7 @@ Therefore, if you plan to compile your crate as a library, you should annotate i
27432744
#[link(name = "farm", vers = "2.5")];
27442745
27452746
// ...
2746-
# pub fn farm() {}
2747+
# fn farm() {}
27472748
~~~~
27482749

27492750
You can also in turn require in a `extern mod` statement that certain link metadata items match some criteria.
@@ -2769,7 +2770,7 @@ or setting the crate type (library or executable) explicitly:
27692770
27702771
// Turn on a warning
27712772
#[warn(non_camel_case_types)]
2772-
# pub fn farm() {}
2773+
# fn farm() {}
27732774
~~~~
27742775

27752776
If you're compiling your crate with `rustpkg`,
@@ -2790,7 +2791,9 @@ We define two crates, and use one of them as a library in the other.
27902791
~~~~
27912792
// world.rs
27922793
#[link(name = "world", vers = "0.42")];
2794+
# extern mod extra;
27932795
pub fn explore() -> &'static str { "world" }
2796+
# fn main() {}
27942797
~~~~
27952798

27962799
~~~~ {.xfail-test}

src/libextra/sort.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ mod tests {
846846

847847
fn check_sort(v1: &[int], v2: &[int]) {
848848
let len = v1.len();
849-
pub fn le(a: &int, b: &int) -> bool { *a <= *b }
849+
fn le(a: &int, b: &int) -> bool { *a <= *b }
850850
let f = le;
851851
let v3 = merge_sort::<int>(v1, f);
852852
let mut i = 0u;
@@ -876,7 +876,7 @@ mod tests {
876876

877877
#[test]
878878
fn test_merge_sort_mutable() {
879-
pub fn le(a: &int, b: &int) -> bool { *a <= *b }
879+
fn le(a: &int, b: &int) -> bool { *a <= *b }
880880
let v1 = ~[3, 2, 1];
881881
let v2 = merge_sort(v1, le);
882882
assert_eq!(v2, ~[1, 2, 3]);

src/librustc/middle/privacy.rs

+79-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! which are available for use externally when compiled as a library.
1414
1515
use std::hashmap::{HashSet, HashMap};
16+
use std::util;
1617

1718
use middle::resolve;
1819
use middle::ty;
@@ -275,6 +276,7 @@ impl<'self> Visitor<()> for EmbargoVisitor<'self> {
275276
struct PrivacyVisitor<'self> {
276277
tcx: ty::ctxt,
277278
curitem: ast::NodeId,
279+
in_fn: bool,
278280

279281
// See comments on the same field in `EmbargoVisitor`.
280282
path_all_public_items: &'self ExportedItems,
@@ -688,6 +690,63 @@ impl<'self> PrivacyVisitor<'self> {
688690
}
689691
}
690692
}
693+
694+
/// When inside of something like a function or a method, visibility has no
695+
/// control over anything so this forbids any mention of any visibility
696+
fn check_all_inherited(&self, item: @ast::item) {
697+
let tcx = self.tcx;
698+
let check_inherited = |sp: Span, vis: ast::visibility| {
699+
if vis != ast::inherited {
700+
tcx.sess.span_err(sp, "visibility has no effect inside functions");
701+
}
702+
};
703+
let check_struct = |def: &@ast::struct_def| {
704+
for f in def.fields.iter() {
705+
match f.node.kind {
706+
ast::named_field(_, p) => check_inherited(f.span, p),
707+
ast::unnamed_field => {}
708+
}
709+
}
710+
};
711+
check_inherited(item.span, item.vis);
712+
match item.node {
713+
ast::item_impl(_, _, _, ref methods) => {
714+
for m in methods.iter() {
715+
check_inherited(m.span, m.vis);
716+
}
717+
}
718+
ast::item_foreign_mod(ref fm) => {
719+
for i in fm.items.iter() {
720+
check_inherited(i.span, i.vis);
721+
}
722+
}
723+
ast::item_enum(ref def, _) => {
724+
for v in def.variants.iter() {
725+
check_inherited(v.span, v.node.vis);
726+
727+
match v.node.kind {
728+
ast::struct_variant_kind(ref s) => check_struct(s),
729+
ast::tuple_variant_kind(*) => {}
730+
}
731+
}
732+
}
733+
734+
ast::item_struct(ref def, _) => check_struct(def),
735+
736+
ast::item_trait(_, _, ref methods) => {
737+
for m in methods.iter() {
738+
match *m {
739+
ast::required(*) => {}
740+
ast::provided(ref m) => check_inherited(m.span, m.vis),
741+
}
742+
}
743+
}
744+
745+
ast::item_static(*) |
746+
ast::item_fn(*) | ast::item_mod(*) | ast::item_ty(*) |
747+
ast::item_mac(*) => {}
748+
}
749+
}
691750
}
692751

693752
impl<'self> Visitor<()> for PrivacyVisitor<'self> {
@@ -699,12 +758,28 @@ impl<'self> Visitor<()> for PrivacyVisitor<'self> {
699758
}
700759

701760
// Disallow unnecessary visibility qualifiers
702-
self.check_sane_privacy(item);
761+
if self.in_fn {
762+
self.check_all_inherited(item);
763+
} else {
764+
self.check_sane_privacy(item);
765+
}
703766

704-
let orig_curitem = self.curitem;
705-
self.curitem = item.id;
767+
let orig_curitem = util::replace(&mut self.curitem, item.id);
768+
let orig_in_fn = util::replace(&mut self.in_fn, match item.node {
769+
ast::item_mod(*) => false, // modules turn privacy back on
770+
_ => self.in_fn, // otherwise we inherit
771+
});
706772
visit::walk_item(self, item, ());
707773
self.curitem = orig_curitem;
774+
self.in_fn = orig_in_fn;
775+
}
776+
777+
fn visit_fn(&mut self, fk: &visit::fn_kind, fd: &ast::fn_decl,
778+
b: &ast::Block, s: Span, n: ast::NodeId, _: ()) {
779+
// This catches both functions and methods
780+
let orig_in_fn = util::replace(&mut self.in_fn, true);
781+
visit::walk_fn(self, fk, fd, b, s, n, ());
782+
self.in_fn = orig_in_fn;
708783
}
709784

710785
fn visit_expr(&mut self, expr: @ast::Expr, _: ()) {
@@ -907,6 +982,7 @@ pub fn check_crate(tcx: ty::ctxt,
907982
{
908983
let mut visitor = PrivacyVisitor {
909984
curitem: ast::DUMMY_NODE_ID,
985+
in_fn: false,
910986
tcx: tcx,
911987
path_all_public_items: &path_all_public_items,
912988
parents: &parents,

src/librustc/middle/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@ pub fn subst_tps(tcx: ctxt, tps: &[t], self_ty_opt: Option<t>, typ: t) -> t {
14091409
let mut subst = TpsSubst { tcx: tcx, self_ty_opt: self_ty_opt, tps: tps };
14101410
return subst.fold_ty(typ);
14111411

1412-
pub struct TpsSubst<'self> {
1412+
struct TpsSubst<'self> {
14131413
tcx: ctxt,
14141414
self_ty_opt: Option<t>,
14151415
tps: &'self [t],

src/libsyntax/ext/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv,
138138

139139
let span = e.span;
140140

141-
pub fn mk_expr(_: @ExtCtxt, span: Span, node: Expr_)
141+
fn mk_expr(_: @ExtCtxt, span: Span, node: Expr_)
142142
-> @ast::Expr {
143143
@ast::Expr {
144144
id: ast::DUMMY_NODE_ID,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
pub struct A; //~ ERROR: visibility has no effect
13+
pub enum B {} //~ ERROR: visibility has no effect
14+
pub trait C { //~ ERROR: visibility has no effect
15+
pub fn foo() {} //~ ERROR: visibility has no effect
16+
}
17+
impl A {
18+
pub fn foo() {} //~ ERROR: visibility has no effect
19+
}
20+
21+
struct D {
22+
priv foo: int, //~ ERROR: visibility has no effect
23+
}
24+
pub fn foo() {} //~ ERROR: visibility has no effect
25+
pub mod bar {} //~ ERROR: visibility has no effect
26+
}

src/test/run-pass/nested-class.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn main() {
1414
}
1515

1616
impl b {
17-
pub fn do_stuff(&self) -> int { return 37; }
17+
fn do_stuff(&self) -> int { return 37; }
1818
}
1919

2020
fn b(i:int) -> b {

0 commit comments

Comments
 (0)