From 74240b22d92bbbdf4afac8cf14f81fd337b97a46 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Sat, 2 Apr 2016 20:21:02 +0000 Subject: [PATCH 1/3] Remove `ast::Visibility::inherit_from` (it is unused and has obsolete semantics) --- src/libsyntax/ast.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index e096aa9902476..e043478f5a808 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1874,15 +1874,6 @@ pub enum Visibility { Inherited, } -impl Visibility { - pub fn inherit_from(&self, parent_visibility: Visibility) -> Visibility { - match *self { - Visibility::Inherited => parent_visibility, - Visibility::Public => *self - } - } -} - #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct StructField_ { pub kind: StructFieldKind, From bc355244df5ae74eaabe9a1e19200b9160010505 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Wed, 23 Mar 2016 10:17:34 +0000 Subject: [PATCH 2/3] Make `ast::Visibility` non-copyable --- src/librustc_front/lowering.rs | 10 +++--- src/libsyntax/ast.rs | 8 ++--- src/libsyntax/ast_util.rs | 2 +- src/libsyntax/parse/parser.rs | 12 +++---- src/libsyntax/print/pprust.rs | 64 +++++++++++++++++----------------- src/libsyntax/visit.rs | 10 +++--- 6 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/librustc_front/lowering.rs b/src/librustc_front/lowering.rs index be1841794512b..3636953cb0595 100644 --- a/src/librustc_front/lowering.rs +++ b/src/librustc_front/lowering.rs @@ -761,7 +761,7 @@ pub fn lower_impl_item(lctx: &LoweringContext, i: &ImplItem) -> hir::ImplItem { id: i.id, name: i.ident.name, attrs: lower_attrs(lctx, &i.attrs), - vis: lower_visibility(lctx, i.vis), + vis: lower_visibility(lctx, &i.vis), defaultness: lower_defaultness(lctx, i.defaultness), node: match i.node { ImplItemKind::Const(ref ty, ref expr) => { @@ -839,7 +839,7 @@ pub fn lower_item(lctx: &LoweringContext, i: &Item) -> hir::Item { name: i.ident.name, attrs: lower_attrs(lctx, &i.attrs), node: node, - vis: lower_visibility(lctx, i.vis), + vis: lower_visibility(lctx, &i.vis), span: i.span, } } @@ -857,7 +857,7 @@ pub fn lower_foreign_item(lctx: &LoweringContext, i: &ForeignItem) -> hir::Forei hir::ForeignItemStatic(lower_ty(lctx, t), m) } }, - vis: lower_visibility(lctx, i.vis), + vis: lower_visibility(lctx, &i.vis), span: i.span, } } @@ -1706,8 +1706,8 @@ pub fn lower_capture_clause(_lctx: &LoweringContext, c: CaptureBy) -> hir::Captu } } -pub fn lower_visibility(_lctx: &LoweringContext, v: Visibility) -> hir::Visibility { - match v { +pub fn lower_visibility(_lctx: &LoweringContext, v: &Visibility) -> hir::Visibility { + match *v { Visibility::Public => hir::Public, Visibility::Inherited => hir::Inherited, } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index e043478f5a808..30dc3d00c182b 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1868,7 +1868,7 @@ pub struct PolyTraitRef { pub span: Span, } -#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub enum Visibility { Public, Inherited, @@ -1893,7 +1893,7 @@ impl StructField_ { pub type StructField = Spanned; -#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub enum StructFieldKind { NamedField(Ident, Visibility), /// Element of a tuple-like struct @@ -1908,9 +1908,9 @@ impl StructFieldKind { } } - pub fn visibility(&self) -> Visibility { + pub fn visibility(&self) -> &Visibility { match *self { - NamedField(_, vis) | UnnamedField(vis) => vis + NamedField(_, ref vis) | UnnamedField(ref vis) => vis } } } diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 5d55b6f8f88d3..852b153044fea 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -247,7 +247,7 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> { FnKind::ItemFn(_, generics, _, _, _, _) => { self.visit_generics_helper(generics) } - FnKind::Method(_, sig, _) => { + FnKind::Method(_, ref sig, _) => { self.visit_generics_helper(&sig.generics) } FnKind::Closure => {} diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 9027a5b107452..1c0fc5fda2514 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -4952,7 +4952,7 @@ impl<'a> Parser<'a> { self.commit_expr_expecting(&expr, token::Semi)?; (name, ast::ImplItemKind::Const(typ, expr)) } else { - let (name, inner_attrs, node) = self.parse_impl_method(vis)?; + let (name, inner_attrs, node) = self.parse_impl_method(&vis)?; attrs.extend(inner_attrs); (name, node) }; @@ -4968,8 +4968,8 @@ impl<'a> Parser<'a> { }) } - fn complain_if_pub_macro(&mut self, visa: Visibility, span: Span) { - match visa { + fn complain_if_pub_macro(&mut self, visa: &Visibility, span: Span) { + match *visa { Visibility::Public => { let is_macro_rules: bool = match self.token { token::Ident(sid, _) => sid.name == intern("macro_rules"), @@ -4993,7 +4993,7 @@ impl<'a> Parser<'a> { } /// Parse a method or a macro invocation in a trait impl. - fn parse_impl_method(&mut self, vis: Visibility) + fn parse_impl_method(&mut self, vis: &Visibility) -> PResult<'a, (Ident, Vec, ast::ImplItemKind)> { // code copied from parse_macro_use_or_failure... abstraction! if !self.token.is_any_keyword() @@ -5003,7 +5003,7 @@ impl<'a> Parser<'a> { // method macro. let last_span = self.last_span; - self.complain_if_pub_macro(vis, last_span); + self.complain_if_pub_macro(&vis, last_span); let lo = self.span.lo; let pth = self.parse_path(NoTypesAllowed)?; @@ -6045,7 +6045,7 @@ impl<'a> Parser<'a> { // MACRO INVOCATION ITEM let last_span = self.last_span; - self.complain_if_pub_macro(visibility, last_span); + self.complain_if_pub_macro(&visibility, last_span); let mac_lo = self.span.lo; diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 5b06eb026d696..83f4d78cb1911 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -388,7 +388,7 @@ pub fn fun_to_string(decl: &ast::FnDecl, to_string(|s| { s.head("")?; s.print_fn(decl, unsafety, constness, Abi::Rust, Some(name), - generics, opt_explicit_self, ast::Visibility::Inherited)?; + generics, opt_explicit_self, &ast::Visibility::Inherited)?; s.end()?; // Close the head box s.end() // Close the outer box }) @@ -432,8 +432,8 @@ pub fn mac_to_string(arg: &ast::Mac) -> String { to_string(|s| s.print_mac(arg, ::parse::token::Paren)) } -pub fn visibility_qualified(vis: ast::Visibility, s: &str) -> String { - match vis { +pub fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String { + match *vis { ast::Visibility::Public => format!("pub {}", s), ast::Visibility::Inherited => s.to_string() } @@ -1052,13 +1052,13 @@ impl<'a> State<'a> { self.print_fn(decl, ast::Unsafety::Normal, ast::Constness::NotConst, Abi::Rust, Some(item.ident), - generics, None, item.vis)?; + generics, None, &item.vis)?; self.end()?; // end head-ibox word(&mut self.s, ";")?; self.end() // end the outer fn box } ast::ForeignItemKind::Static(ref t, m) => { - self.head(&visibility_qualified(item.vis, "static"))?; + self.head(&visibility_qualified(&item.vis, "static"))?; if m { self.word_space("mut")?; } @@ -1076,7 +1076,7 @@ impl<'a> State<'a> { ident: ast::Ident, ty: &ast::Ty, default: Option<&ast::Expr>, - vis: ast::Visibility) + vis: &ast::Visibility) -> io::Result<()> { word(&mut self.s, &visibility_qualified(vis, ""))?; @@ -1118,7 +1118,7 @@ impl<'a> State<'a> { self.ann.pre(self, NodeItem(item))?; match item.node { ast::ItemKind::ExternCrate(ref optional_path) => { - self.head(&visibility_qualified(item.vis, "extern crate"))?; + self.head(&visibility_qualified(&item.vis, "extern crate"))?; if let Some(p) = *optional_path { let val = p.as_str(); if val.contains("-") { @@ -1136,14 +1136,14 @@ impl<'a> State<'a> { self.end()?; // end outer head-block } ast::ItemKind::Use(ref vp) => { - self.head(&visibility_qualified(item.vis, "use"))?; + self.head(&visibility_qualified(&item.vis, "use"))?; self.print_view_path(&vp)?; word(&mut self.s, ";")?; self.end()?; // end inner head-block self.end()?; // end outer head-block } ast::ItemKind::Static(ref ty, m, ref expr) => { - self.head(&visibility_qualified(item.vis, "static"))?; + self.head(&visibility_qualified(&item.vis, "static"))?; if m == ast::Mutability::Mutable { self.word_space("mut")?; } @@ -1159,7 +1159,7 @@ impl<'a> State<'a> { self.end()?; // end the outer cbox } ast::ItemKind::Const(ref ty, ref expr) => { - self.head(&visibility_qualified(item.vis, "const"))?; + self.head(&visibility_qualified(&item.vis, "const"))?; self.print_ident(item.ident)?; self.word_space(":")?; self.print_type(&ty)?; @@ -1181,13 +1181,13 @@ impl<'a> State<'a> { Some(item.ident), typarams, None, - item.vis + &item.vis )?; word(&mut self.s, " ")?; self.print_block_with_attrs(&body, &item.attrs)?; } ast::ItemKind::Mod(ref _mod) => { - self.head(&visibility_qualified(item.vis, "mod"))?; + self.head(&visibility_qualified(&item.vis, "mod"))?; self.print_ident(item.ident)?; self.nbsp()?; self.bopen()?; @@ -1204,7 +1204,7 @@ impl<'a> State<'a> { ast::ItemKind::Ty(ref ty, ref params) => { self.ibox(INDENT_UNIT)?; self.ibox(0)?; - self.word_nbsp(&visibility_qualified(item.vis, "type"))?; + self.word_nbsp(&visibility_qualified(&item.vis, "type"))?; self.print_ident(item.ident)?; self.print_generics(params)?; self.end()?; // end the inner ibox @@ -1222,17 +1222,17 @@ impl<'a> State<'a> { params, item.ident, item.span, - item.vis + &item.vis )?; } ast::ItemKind::Struct(ref struct_def, ref generics) => { - self.head(&visibility_qualified(item.vis,"struct"))?; + self.head(&visibility_qualified(&item.vis, "struct"))?; self.print_struct(&struct_def, generics, item.ident, item.span, true)?; } ast::ItemKind::DefaultImpl(unsafety, ref trait_ref) => { self.head("")?; - self.print_visibility(item.vis)?; + self.print_visibility(&item.vis)?; self.print_unsafety(unsafety)?; self.word_nbsp("impl")?; self.print_trait_ref(trait_ref)?; @@ -1249,7 +1249,7 @@ impl<'a> State<'a> { ref ty, ref impl_items) => { self.head("")?; - self.print_visibility(item.vis)?; + self.print_visibility(&item.vis)?; self.print_unsafety(unsafety)?; self.word_nbsp("impl")?; @@ -1287,7 +1287,7 @@ impl<'a> State<'a> { } ast::ItemKind::Trait(unsafety, ref generics, ref bounds, ref trait_items) => { self.head("")?; - self.print_visibility(item.vis)?; + self.print_visibility(&item.vis)?; self.print_unsafety(unsafety)?; self.word_nbsp("trait")?; self.print_ident(item.ident)?; @@ -1312,7 +1312,7 @@ impl<'a> State<'a> { self.bclose(item.span)?; } ast::ItemKind::Mac(codemap::Spanned { ref node, .. }) => { - self.print_visibility(item.vis)?; + self.print_visibility(&item.vis)?; self.print_path(&node.path, false, 0)?; word(&mut self.s, "! ")?; self.print_ident(item.ident)?; @@ -1355,7 +1355,7 @@ impl<'a> State<'a> { pub fn print_enum_def(&mut self, enum_definition: &ast::EnumDef, generics: &ast::Generics, ident: ast::Ident, span: codemap::Span, - visibility: ast::Visibility) -> io::Result<()> { + visibility: &ast::Visibility) -> io::Result<()> { self.head(&visibility_qualified(visibility, "enum"))?; self.print_ident(ident)?; self.print_generics(generics)?; @@ -1381,8 +1381,8 @@ impl<'a> State<'a> { self.bclose(span) } - pub fn print_visibility(&mut self, vis: ast::Visibility) -> io::Result<()> { - match vis { + pub fn print_visibility(&mut self, vis: &ast::Visibility) -> io::Result<()> { + match *vis { ast::Visibility::Public => self.word_nbsp("pub"), ast::Visibility::Inherited => Ok(()) } @@ -1404,7 +1404,7 @@ impl<'a> State<'a> { |s, field| { match field.node.kind { ast::NamedField(..) => panic!("unexpected named field"), - ast::UnnamedField(vis) => { + ast::UnnamedField(ref vis) => { s.print_visibility(vis)?; s.maybe_print_comment(field.span.lo)?; s.print_type(&field.node.ty) @@ -1429,7 +1429,7 @@ impl<'a> State<'a> { for field in struct_def.fields() { match field.node.kind { ast::UnnamedField(..) => panic!("unexpected unnamed field"), - ast::NamedField(ident, visibility) => { + ast::NamedField(ident, ref visibility) => { self.hardbreak_if_not_bol()?; self.maybe_print_comment(field.span.lo)?; self.print_outer_attributes(&field.node.attrs)?; @@ -1528,7 +1528,7 @@ impl<'a> State<'a> { pub fn print_method_sig(&mut self, ident: ast::Ident, m: &ast::MethodSig, - vis: ast::Visibility) + vis: &ast::Visibility) -> io::Result<()> { self.print_fn(&m.decl, m.unsafety, @@ -1550,13 +1550,13 @@ impl<'a> State<'a> { ast::TraitItemKind::Const(ref ty, ref default) => { self.print_associated_const(ti.ident, &ty, default.as_ref().map(|expr| &**expr), - ast::Visibility::Inherited)?; + &ast::Visibility::Inherited)?; } ast::TraitItemKind::Method(ref sig, ref body) => { if body.is_some() { self.head("")?; } - self.print_method_sig(ti.ident, sig, ast::Visibility::Inherited)?; + self.print_method_sig(ti.ident, sig, &ast::Visibility::Inherited)?; if let Some(ref body) = *body { self.nbsp()?; self.print_block_with_attrs(body, &ti.attrs)?; @@ -1582,11 +1582,11 @@ impl<'a> State<'a> { } match ii.node { ast::ImplItemKind::Const(ref ty, ref expr) => { - self.print_associated_const(ii.ident, &ty, Some(&expr), ii.vis)?; + self.print_associated_const(ii.ident, &ty, Some(&expr), &ii.vis)?; } ast::ImplItemKind::Method(ref sig, ref body) => { self.head("")?; - self.print_method_sig(ii.ident, sig, ii.vis)?; + self.print_method_sig(ii.ident, sig, &ii.vis)?; self.nbsp()?; self.print_block_with_attrs(body, &ii.attrs)?; } @@ -2655,7 +2655,7 @@ impl<'a> State<'a> { name: Option, generics: &ast::Generics, opt_explicit_self: Option<&ast::SelfKind>, - vis: ast::Visibility) -> io::Result<()> { + vis: &ast::Visibility) -> io::Result<()> { self.print_fn_header_info(unsafety, constness, abi, vis)?; if let Some(name) = name { @@ -3037,7 +3037,7 @@ impl<'a> State<'a> { name, &generics, opt_explicit_self, - ast::Visibility::Inherited)?; + &ast::Visibility::Inherited)?; self.end() } @@ -3112,7 +3112,7 @@ impl<'a> State<'a> { unsafety: ast::Unsafety, constness: ast::Constness, abi: Abi, - vis: ast::Visibility) -> io::Result<()> { + vis: &ast::Visibility) -> io::Result<()> { word(&mut self.s, &visibility_qualified(vis, ""))?; match constness { diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 25aee09e26c8e..f0eb42d1741e4 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -31,10 +31,10 @@ use codemap::Span; #[derive(Copy, Clone, PartialEq, Eq)] pub enum FnKind<'a> { /// fn foo() or extern "Abi" fn foo() - ItemFn(Ident, &'a Generics, Unsafety, Constness, Abi, Visibility), + ItemFn(Ident, &'a Generics, Unsafety, Constness, Abi, &'a Visibility), /// fn foo(&self) - Method(Ident, &'a MethodSig, Option), + Method(Ident, &'a MethodSig, Option<&'a Visibility>), /// |x, y| {} Closure, @@ -260,7 +260,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) { } ItemKind::Fn(ref declaration, unsafety, constness, abi, ref generics, ref body) => { visitor.visit_fn(FnKind::ItemFn(item.ident, generics, unsafety, - constness, abi, item.vis), + constness, abi, &item.vis), declaration, body, item.span, @@ -546,7 +546,7 @@ pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, FnKind::ItemFn(_, generics, _, _, _, _) => { visitor.visit_generics(generics); } - FnKind::Method(_, sig, _) => { + FnKind::Method(_, ref sig, _) => { visitor.visit_generics(&sig.generics); visitor.visit_explicit_self(&sig.explicit_self); } @@ -597,7 +597,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt visitor.visit_expr(expr); } ImplItemKind::Method(ref sig, ref body) => { - visitor.visit_fn(FnKind::Method(impl_item.ident, sig, Some(impl_item.vis)), &sig.decl, + visitor.visit_fn(FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis)), &sig.decl, body, impl_item.span, impl_item.id); } ImplItemKind::Type(ref ty) => { From 432eb8a094322a3aa2e40c439f9b50c0d7947d61 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Thu, 31 Mar 2016 19:10:38 +0000 Subject: [PATCH 3/3] Add `Crate` and `Restricted` variants to `ast::Visibility` --- src/librustc_front/lowering.rs | 3 ++- src/libsyntax/ast.rs | 2 ++ src/libsyntax/fold.rs | 20 +++++++++++++++++--- src/libsyntax/parse/parser.rs | 8 ++++---- src/libsyntax/print/pprust.rs | 5 +++++ src/libsyntax/visit.rs | 10 ++++++++++ 6 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/librustc_front/lowering.rs b/src/librustc_front/lowering.rs index 3636953cb0595..66b9e217bd387 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 30dc3d00c182b..a441f2990cd1a 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 cd8998a211ae7..46bcb8067a3b0 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 1c0fc5fda2514..aff1f77665a77 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 83f4d78cb1911..a8f28ed3d9e0d 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 f0eb42d1741e4..1251f9bfe1350 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), + _ => {} + } +}