Skip to content

Commit 432eb8a

Browse files
committed
Add Crate and Restricted variants to ast::Visibility
1 parent bc35524 commit 432eb8a

File tree

6 files changed

+40
-8
lines changed

6 files changed

+40
-8
lines changed

src/librustc_front/lowering.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1706,10 +1706,11 @@ pub fn lower_capture_clause(_lctx: &LoweringContext, c: CaptureBy) -> hir::Captu
17061706
}
17071707
}
17081708

1709-
pub fn lower_visibility(_lctx: &LoweringContext, v: &Visibility) -> hir::Visibility {
1709+
pub fn lower_visibility(lctx: &LoweringContext, v: &Visibility) -> hir::Visibility {
17101710
match *v {
17111711
Visibility::Public => hir::Public,
17121712
Visibility::Inherited => hir::Inherited,
1713+
_ => panic!(lctx.diagnostic().fatal("pub(restricted) is not implemented yet!"))
17131714
}
17141715
}
17151716

src/libsyntax/ast.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,8 @@ pub struct PolyTraitRef {
18711871
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
18721872
pub enum Visibility {
18731873
Public,
1874+
Crate,
1875+
Restricted { path: P<Path>, id: NodeId },
18741876
Inherited,
18751877
}
18761878

src/libsyntax/fold.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ pub trait Folder : Sized {
288288
noop_fold_where_predicate(where_predicate, self)
289289
}
290290

291+
fn fold_vis(&mut self, vis: Visibility) -> Visibility {
292+
noop_fold_vis(vis, self)
293+
}
294+
291295
fn new_id(&mut self, i: NodeId) -> NodeId {
292296
i
293297
}
@@ -992,7 +996,7 @@ pub fn noop_fold_impl_item<T: Folder>(i: ImplItem, folder: &mut T)
992996
id: folder.new_id(i.id),
993997
ident: folder.fold_ident(i.ident),
994998
attrs: fold_attrs(i.attrs, folder),
995-
vis: i.vis,
999+
vis: folder.fold_vis(i.vis),
9961000
defaultness: i.defaultness,
9971001
node: match i.node {
9981002
ast::ImplItemKind::Const(ty, expr) => {
@@ -1082,7 +1086,7 @@ pub fn noop_fold_item_simple<T: Folder>(Item {id, ident, attrs, node, vis, span}
10821086
ident: folder.fold_ident(ident),
10831087
attrs: fold_attrs(attrs, folder),
10841088
node: node,
1085-
vis: vis,
1089+
vis: folder.fold_vis(vis),
10861090
span: folder.new_span(span)
10871091
}
10881092
}
@@ -1100,7 +1104,7 @@ pub fn noop_fold_foreign_item<T: Folder>(ni: ForeignItem, folder: &mut T) -> For
11001104
ForeignItemKind::Static(folder.fold_ty(t), m)
11011105
}
11021106
},
1103-
vis: ni.vis,
1107+
vis: folder.fold_vis(ni.vis),
11041108
span: folder.new_span(ni.span)
11051109
}
11061110
}
@@ -1391,6 +1395,16 @@ pub fn noop_fold_stmt<T: Folder>(Spanned {node, span}: Stmt, folder: &mut T)
13911395
}
13921396
}
13931397

1398+
pub fn noop_fold_vis<T: Folder>(vis: Visibility, folder: &mut T) -> Visibility {
1399+
match vis {
1400+
Visibility::Restricted { path, id } => Visibility::Restricted {
1401+
path: path.map(|path| folder.fold_path(path)),
1402+
id: folder.new_id(id)
1403+
},
1404+
_ => vis,
1405+
}
1406+
}
1407+
13941408
#[cfg(test)]
13951409
mod tests {
13961410
use std::io;

src/libsyntax/parse/parser.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3842,7 +3842,7 @@ impl<'a> Parser<'a> {
38423842
attrs: Vec<Attribute> ) -> PResult<'a, StructField> {
38433843
let lo = match pr {
38443844
Visibility::Inherited => self.span.lo,
3845-
Visibility::Public => self.last_span.lo,
3845+
_ => self.last_span.lo,
38463846
};
38473847
let name = self.parse_ident()?;
38483848
self.expect(&token::Colon)?;
@@ -4970,7 +4970,8 @@ impl<'a> Parser<'a> {
49704970

49714971
fn complain_if_pub_macro(&mut self, visa: &Visibility, span: Span) {
49724972
match *visa {
4973-
Visibility::Public => {
4973+
Visibility::Inherited => (),
4974+
_ => {
49744975
let is_macro_rules: bool = match self.token {
49754976
token::Ident(sid, _) => sid.name == intern("macro_rules"),
49764977
_ => false,
@@ -4988,7 +4989,6 @@ impl<'a> Parser<'a> {
49884989
.emit();
49894990
}
49904991
}
4991-
Visibility::Inherited => (),
49924992
}
49934993
}
49944994

@@ -6096,7 +6096,7 @@ impl<'a> Parser<'a> {
60966096
// FAILURE TO PARSE ITEM
60976097
match visibility {
60986098
Visibility::Inherited => {}
6099-
Visibility::Public => {
6099+
_ => {
61006100
let last_span = self.last_span;
61016101
return Err(self.span_fatal(last_span, "unmatched visibility `pub`"));
61026102
}

src/libsyntax/print/pprust.rs

+5
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ pub fn mac_to_string(arg: &ast::Mac) -> String {
435435
pub fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
436436
match *vis {
437437
ast::Visibility::Public => format!("pub {}", s),
438+
ast::Visibility::Crate => format!("pub(crate) {}", s),
439+
ast::Visibility::Restricted { ref path, .. } => format!("pub({}) {}", path, s),
438440
ast::Visibility::Inherited => s.to_string()
439441
}
440442
}
@@ -1384,6 +1386,9 @@ impl<'a> State<'a> {
13841386
pub fn print_visibility(&mut self, vis: &ast::Visibility) -> io::Result<()> {
13851387
match *vis {
13861388
ast::Visibility::Public => self.word_nbsp("pub"),
1389+
ast::Visibility::Crate => self.word_nbsp("pub(crate)"),
1390+
ast::Visibility::Restricted { ref path, .. } =>
1391+
self.word_nbsp(&format!("pub({})", path)),
13871392
ast::Visibility::Inherited => Ok(())
13881393
}
13891394
}

src/libsyntax/visit.rs

+10
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ pub trait Visitor<'v> : Sized {
129129
fn visit_macro_def(&mut self, macro_def: &'v MacroDef) {
130130
walk_macro_def(self, macro_def)
131131
}
132+
fn visit_vis(&mut self, vis: &'v Visibility) {
133+
walk_vis(self, vis)
134+
}
132135
}
133136

134137
#[macro_export]
@@ -807,3 +810,10 @@ pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
807810
visitor.visit_expr(&arm.body);
808811
walk_list!(visitor, visit_attribute, &arm.attrs);
809812
}
813+
814+
pub fn walk_vis<'v, V: Visitor<'v>>(visitor: &mut V, vis: &'v Visibility) {
815+
match *vis {
816+
Visibility::Restricted { ref path, id } => visitor.visit_path(path, id),
817+
_ => {}
818+
}
819+
}

0 commit comments

Comments
 (0)