Skip to content

Commit

Permalink
Add Crate and Restricted variants to ast::Visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Apr 2, 2016
1 parent bc35524 commit 432eb8a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/librustc_front/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!"))
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,8 @@ pub struct PolyTraitRef {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum Visibility {
Public,
Crate,
Restricted { path: P<Path>, id: NodeId },
Inherited,
}

Expand Down
20 changes: 17 additions & 3 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -992,7 +996,7 @@ pub fn noop_fold_impl_item<T: Folder>(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) => {
Expand Down Expand Up @@ -1082,7 +1086,7 @@ pub fn noop_fold_item_simple<T: Folder>(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)
}
}
Expand All @@ -1100,7 +1104,7 @@ pub fn noop_fold_foreign_item<T: Folder>(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)
}
}
Expand Down Expand Up @@ -1391,6 +1395,16 @@ pub fn noop_fold_stmt<T: Folder>(Spanned {node, span}: Stmt, folder: &mut T)
}
}

pub fn noop_fold_vis<T: Folder>(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;
Expand Down
8 changes: 4 additions & 4 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3842,7 +3842,7 @@ impl<'a> Parser<'a> {
attrs: Vec<Attribute> ) -> 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)?;
Expand Down Expand Up @@ -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,
Expand All @@ -4988,7 +4989,6 @@ impl<'a> Parser<'a> {
.emit();
}
}
Visibility::Inherited => (),
}
}

Expand Down Expand Up @@ -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`"));
}
Expand Down
5 changes: 5 additions & 0 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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(())
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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),
_ => {}
}
}

0 comments on commit 432eb8a

Please sign in to comment.