Skip to content

Commit 4a78364

Browse files
committed
Forbid unnecessary visibility on view items
For `use` statements, this means disallowing qualifiers when in functions and disallowing `priv` outside of functions. For `extern mod` statements, this means disallowing everything everywhere. It may have been envisioned for `pub extern mod foo` to be a thing, but it currently doesn't do anything (resolve doesn't pick it up), so better to err on the side of forwards-compatibility and forbid it entirely for now. Closes #9957
1 parent 9434e7c commit 4a78364

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

src/librustc/front/std_inject.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl fold::Folder for StandardLibraryInjector {
7373
with_version("std"),
7474
ast::DUMMY_NODE_ID),
7575
attrs: ~[],
76-
vis: ast::Private,
76+
vis: ast::Inherited,
7777
span: DUMMY_SP
7878
}];
7979

@@ -83,15 +83,15 @@ impl fold::Folder for StandardLibraryInjector {
8383
with_version("green"),
8484
ast::DUMMY_NODE_ID),
8585
attrs: ~[],
86-
vis: ast::Private,
86+
vis: ast::Inherited,
8787
span: DUMMY_SP
8888
});
8989
vis.push(ast::ViewItem {
9090
node: ast::ViewItemExternMod(self.sess.ident_of("rustuv"),
9191
with_version("rustuv"),
9292
ast::DUMMY_NODE_ID),
9393
attrs: ~[],
94-
vis: ast::Private,
94+
vis: ast::Inherited,
9595
span: DUMMY_SP
9696
});
9797
}
@@ -147,7 +147,7 @@ impl fold::Folder for StandardLibraryInjector {
147147
let vi2 = ast::ViewItem {
148148
node: ast::ViewItemUse(~[vp]),
149149
attrs: ~[],
150-
vis: ast::Private,
150+
vis: ast::Inherited,
151151
span: DUMMY_SP,
152152
};
153153

src/librustc/front/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ fn mk_std(cx: &TestCtxt) -> ast::ViewItem {
299299
ast::ViewItem {
300300
node: vi,
301301
attrs: ~[],
302-
vis: ast::Public,
302+
vis: ast::Inherited,
303303
span: DUMMY_SP
304304
}
305305
}

src/librustc/middle/privacy.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,32 @@ impl Visitor<()> for SanePrivacyVisitor {
839839
visit::walk_fn(self, fk, fd, b, s, n, ());
840840
self.in_fn = orig_in_fn;
841841
}
842+
843+
fn visit_view_item(&mut self, i: &ast::ViewItem, _: ()) {
844+
match i.vis {
845+
ast::Inherited => {}
846+
ast::Private => {
847+
self.tcx.sess.span_err(i.span, "unnecessary visibility \
848+
qualifier");
849+
}
850+
ast::Public => {
851+
if self.in_fn {
852+
self.tcx.sess.span_err(i.span, "unnecessary `pub`, imports \
853+
in functions are never \
854+
reachable");
855+
} else {
856+
match i.node {
857+
ast::ViewItemExternMod(..) => {
858+
self.tcx.sess.span_err(i.span, "`pub` visibility \
859+
is not allowed");
860+
}
861+
_ => {}
862+
}
863+
}
864+
}
865+
}
866+
visit::walk_view_item(self, i, ());
867+
}
842868
}
843869

844870
impl SanePrivacyVisitor {

src/libsyntax/ext/quote.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ fn expand_wrapper(cx: &ExtCtxt,
648648
sp: Span,
649649
cx_expr: @ast::Expr,
650650
expr: @ast::Expr) -> @ast::Expr {
651-
let uses = ~[ cx.view_use_glob(sp, ast::Public,
651+
let uses = ~[ cx.view_use_glob(sp, ast::Inherited,
652652
ids_ext(~[~"syntax",
653653
~"ext",
654654
~"quote",

src/test/compile-fail/issue-9957.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2014 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+
pub extern mod std; //~ ERROR: `pub` visibility is not allowed
12+
priv extern mod std; //~ ERROR: unnecessary visibility qualifier
13+
extern mod std;
14+
15+
pub use std::bool;
16+
priv use std::bool; //~ ERROR: unnecessary visibility qualifier
17+
use std::bool;
18+
19+
fn main() {
20+
pub use std::bool; //~ ERROR: imports in functions are never reachable
21+
priv use std::bool; //~ ERROR: unnecessary visibility qualifier
22+
use std::bool;
23+
}

0 commit comments

Comments
 (0)