diff --git a/src/librustc_front/lowering.rs b/src/librustc_front/lowering.rs index 3636953cb05..66b9e217bd3 100644 --- a/src/librustc_front/lowering.rs +++ b/src/librustc_front/lowering.rs @@ -1706,10 +1706,11 @@ pub fn lower_capture_clause(_lctx: &LoweringContext, c: CaptureBy) -> hir::Captu } } -pub fn lower_visibility(_lctx: &LoweringContext, v: &Visibility) -> hir::Visibility { +pub fn lower_visibility(lctx: &LoweringContext, v: &Visibility) -> hir::Visibility { match *v { Visibility::Public => hir::Public, Visibility::Inherited => hir::Inherited, + _ => panic!(lctx.diagnostic().fatal("pub(restricted) is not implemented yet!")) } } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 30dc3d00c18..a441f2990cd 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1871,6 +1871,8 @@ pub struct PolyTraitRef { #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub enum Visibility { Public, + Crate, + Restricted { path: P, id: NodeId }, Inherited, } diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index cd8998a211a..46bcb8067a3 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -288,6 +288,10 @@ pub trait Folder : Sized { noop_fold_where_predicate(where_predicate, self) } + fn fold_vis(&mut self, vis: Visibility) -> Visibility { + noop_fold_vis(vis, self) + } + fn new_id(&mut self, i: NodeId) -> NodeId { i } @@ -992,7 +996,7 @@ pub fn noop_fold_impl_item(i: ImplItem, folder: &mut T) id: folder.new_id(i.id), ident: folder.fold_ident(i.ident), attrs: fold_attrs(i.attrs, folder), - vis: i.vis, + vis: folder.fold_vis(i.vis), defaultness: i.defaultness, node: match i.node { ast::ImplItemKind::Const(ty, expr) => { @@ -1082,7 +1086,7 @@ pub fn noop_fold_item_simple(Item {id, ident, attrs, node, vis, span} ident: folder.fold_ident(ident), attrs: fold_attrs(attrs, folder), node: node, - vis: vis, + vis: folder.fold_vis(vis), span: folder.new_span(span) } } @@ -1100,7 +1104,7 @@ pub fn noop_fold_foreign_item(ni: ForeignItem, folder: &mut T) -> For ForeignItemKind::Static(folder.fold_ty(t), m) } }, - vis: ni.vis, + vis: folder.fold_vis(ni.vis), span: folder.new_span(ni.span) } } @@ -1391,6 +1395,16 @@ pub fn noop_fold_stmt(Spanned {node, span}: Stmt, folder: &mut T) } } +pub fn noop_fold_vis(vis: Visibility, folder: &mut T) -> Visibility { + match vis { + Visibility::Restricted { path, id } => Visibility::Restricted { + path: path.map(|path| folder.fold_path(path)), + id: folder.new_id(id) + }, + _ => vis, + } +} + #[cfg(test)] mod tests { use std::io; diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 1c0fc5fda25..aff1f77665a 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3842,7 +3842,7 @@ impl<'a> Parser<'a> { attrs: Vec ) -> PResult<'a, StructField> { let lo = match pr { Visibility::Inherited => self.span.lo, - Visibility::Public => self.last_span.lo, + _ => self.last_span.lo, }; let name = self.parse_ident()?; self.expect(&token::Colon)?; @@ -4970,7 +4970,8 @@ impl<'a> Parser<'a> { fn complain_if_pub_macro(&mut self, visa: &Visibility, span: Span) { match *visa { - Visibility::Public => { + Visibility::Inherited => (), + _ => { let is_macro_rules: bool = match self.token { token::Ident(sid, _) => sid.name == intern("macro_rules"), _ => false, @@ -4988,7 +4989,6 @@ impl<'a> Parser<'a> { .emit(); } } - Visibility::Inherited => (), } } @@ -6096,7 +6096,7 @@ impl<'a> Parser<'a> { // FAILURE TO PARSE ITEM match visibility { Visibility::Inherited => {} - Visibility::Public => { + _ => { let last_span = self.last_span; return Err(self.span_fatal(last_span, "unmatched visibility `pub`")); } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 83f4d78cb19..a8f28ed3d9e 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -435,6 +435,8 @@ pub fn mac_to_string(arg: &ast::Mac) -> String { pub fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String { match *vis { ast::Visibility::Public => format!("pub {}", s), + ast::Visibility::Crate => format!("pub(crate) {}", s), + ast::Visibility::Restricted { ref path, .. } => format!("pub({}) {}", path, s), ast::Visibility::Inherited => s.to_string() } } @@ -1384,6 +1386,9 @@ impl<'a> State<'a> { pub fn print_visibility(&mut self, vis: &ast::Visibility) -> io::Result<()> { match *vis { ast::Visibility::Public => self.word_nbsp("pub"), + ast::Visibility::Crate => self.word_nbsp("pub(crate)"), + ast::Visibility::Restricted { ref path, .. } => + self.word_nbsp(&format!("pub({})", path)), ast::Visibility::Inherited => Ok(()) } } diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index f0eb42d1741..1251f9bfe13 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -129,6 +129,9 @@ pub trait Visitor<'v> : Sized { fn visit_macro_def(&mut self, macro_def: &'v MacroDef) { walk_macro_def(self, macro_def) } + fn visit_vis(&mut self, vis: &'v Visibility) { + walk_vis(self, vis) + } } #[macro_export] @@ -807,3 +810,10 @@ pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) { visitor.visit_expr(&arm.body); walk_list!(visitor, visit_attribute, &arm.attrs); } + +pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility) { + match *vis { + Visibility::Restricted { ref path, id } => visitor.visit_path(path, id), + _ => {} + } +}