From bc355244df5ae74eaabe9a1e19200b9160010505 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Wed, 23 Mar 2016 10:17:34 +0000 Subject: Make `ast::Visibility` non-copyable --- src/libsyntax/parse/parser.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 9027a5b1074..1c0fc5fda25 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; -- cgit 1.4.1-3-g733a5 From 432eb8a094322a3aa2e40c439f9b50c0d7947d61 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Thu, 31 Mar 2016 19:10:38 +0000 Subject: 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(-) (limited to 'src/libsyntax/parse') 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), + _ => {} + } +} -- cgit 1.4.1-3-g733a5