From 4a78364d491ee2d3a49f4ed5e62f63cd5748d898 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 16 Jan 2014 13:27:27 -0800 Subject: [PATCH] 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 --- src/librustc/front/std_inject.rs | 8 ++++---- src/librustc/front/test.rs | 2 +- src/librustc/middle/privacy.rs | 26 ++++++++++++++++++++++++++ src/libsyntax/ext/quote.rs | 2 +- src/test/compile-fail/issue-9957.rs | 23 +++++++++++++++++++++++ 5 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 src/test/compile-fail/issue-9957.rs diff --git a/src/librustc/front/std_inject.rs b/src/librustc/front/std_inject.rs index f2bc03b21ba..c1c31dc310e 100644 --- a/src/librustc/front/std_inject.rs +++ b/src/librustc/front/std_inject.rs @@ -73,7 +73,7 @@ fn fold_crate(&mut self, crate: ast::Crate) -> ast::Crate { with_version("std"), ast::DUMMY_NODE_ID), attrs: ~[], - vis: ast::Private, + vis: ast::Inherited, span: DUMMY_SP }]; @@ -83,7 +83,7 @@ fn fold_crate(&mut self, crate: ast::Crate) -> ast::Crate { with_version("green"), ast::DUMMY_NODE_ID), attrs: ~[], - vis: ast::Private, + vis: ast::Inherited, span: DUMMY_SP }); vis.push(ast::ViewItem { @@ -91,7 +91,7 @@ fn fold_crate(&mut self, crate: ast::Crate) -> ast::Crate { with_version("rustuv"), ast::DUMMY_NODE_ID), attrs: ~[], - vis: ast::Private, + vis: ast::Inherited, span: DUMMY_SP }); } @@ -147,7 +147,7 @@ fn fold_mod(&mut self, module: &ast::Mod) -> ast::Mod { let vi2 = ast::ViewItem { node: ast::ViewItemUse(~[vp]), attrs: ~[], - vis: ast::Private, + vis: ast::Inherited, span: DUMMY_SP, }; diff --git a/src/librustc/front/test.rs b/src/librustc/front/test.rs index b9a98e7b15a..67229b0550b 100644 --- a/src/librustc/front/test.rs +++ b/src/librustc/front/test.rs @@ -299,7 +299,7 @@ fn mk_std(cx: &TestCtxt) -> ast::ViewItem { ast::ViewItem { node: vi, attrs: ~[], - vis: ast::Public, + vis: ast::Inherited, span: DUMMY_SP } } diff --git a/src/librustc/middle/privacy.rs b/src/librustc/middle/privacy.rs index 767ca7fcc70..4318fde9b3a 100644 --- a/src/librustc/middle/privacy.rs +++ b/src/librustc/middle/privacy.rs @@ -839,6 +839,32 @@ fn visit_fn(&mut self, fk: &visit::FnKind, fd: &ast::FnDecl, visit::walk_fn(self, fk, fd, b, s, n, ()); self.in_fn = orig_in_fn; } + + fn visit_view_item(&mut self, i: &ast::ViewItem, _: ()) { + match i.vis { + ast::Inherited => {} + ast::Private => { + self.tcx.sess.span_err(i.span, "unnecessary visibility \ + qualifier"); + } + ast::Public => { + if self.in_fn { + self.tcx.sess.span_err(i.span, "unnecessary `pub`, imports \ + in functions are never \ + reachable"); + } else { + match i.node { + ast::ViewItemExternMod(..) => { + self.tcx.sess.span_err(i.span, "`pub` visibility \ + is not allowed"); + } + _ => {} + } + } + } + } + visit::walk_view_item(self, i, ()); + } } impl SanePrivacyVisitor { diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index e66e394d639..7c2175503e0 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -648,7 +648,7 @@ fn expand_wrapper(cx: &ExtCtxt, sp: Span, cx_expr: @ast::Expr, expr: @ast::Expr) -> @ast::Expr { - let uses = ~[ cx.view_use_glob(sp, ast::Public, + let uses = ~[ cx.view_use_glob(sp, ast::Inherited, ids_ext(~[~"syntax", ~"ext", ~"quote", diff --git a/src/test/compile-fail/issue-9957.rs b/src/test/compile-fail/issue-9957.rs new file mode 100644 index 00000000000..b8292d61752 --- /dev/null +++ b/src/test/compile-fail/issue-9957.rs @@ -0,0 +1,23 @@ +// Copyright 2014 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 extern mod std; //~ ERROR: `pub` visibility is not allowed +priv extern mod std; //~ ERROR: unnecessary visibility qualifier +extern mod std; + +pub use std::bool; +priv use std::bool; //~ ERROR: unnecessary visibility qualifier +use std::bool; + +fn main() { + pub use std::bool; //~ ERROR: imports in functions are never reachable + priv use std::bool; //~ ERROR: unnecessary visibility qualifier + use std::bool; +} -- GitLab