Auto merge of #32688 - jseyfried:ast_groundwork_for_1422, r=pnkfelix

[breaking-batch] Add support for `pub(restricted)` syntax in the AST

This PR allows the AST to represent the `pub(restricted)` syntax from RFC 1422 (cc #32409).

More specifically, it makes `ast::Visibility` non-`Copy` and adds two new variants, `Visibility::Crate` for `pub(crate)` and `Visitibility::Restricted { path: P<Path>, id: NodeId }` for `pub(path)`.

plugin-[breaking-change] cc #31645
r? @pnkfelix
This commit is contained in:
bors 2016-04-05 18:58:24 -07:00
commit 772c600d4d
7 changed files with 92 additions and 69 deletions

View File

@ -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,10 +1706,11 @@ 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,
_ => panic!(lctx.diagnostic().fatal("pub(restricted) is not implemented yet!"))
}
}

View File

@ -1868,21 +1868,14 @@ 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,
Crate,
Restricted { path: P<Path>, id: NodeId },
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,
@ -1902,7 +1895,7 @@ impl StructField_ {
pub type StructField = Spanned<StructField_>;
#[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
@ -1917,9 +1910,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
}
}
}

View File

@ -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 => {}

View File

@ -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<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) => {
@ -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)
}
}
@ -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)
}
}
@ -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;

View File

@ -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)?;
@ -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,9 +4968,10 @@ impl<'a> Parser<'a> {
})
}
fn complain_if_pub_macro(&mut self, visa: Visibility, span: Span) {
match visa {
Visibility::Public => {
fn complain_if_pub_macro(&mut self, visa: &Visibility, span: Span) {
match *visa {
Visibility::Inherited => (),
_ => {
let is_macro_rules: bool = match self.token {
token::Ident(sid, _) => sid.name == intern("macro_rules"),
_ => false,
@ -4988,12 +4989,11 @@ impl<'a> Parser<'a> {
.emit();
}
}
Visibility::Inherited => (),
}
}
/// 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::Attribute>, 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;
@ -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`"));
}

View File

@ -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,9 +432,11 @@ 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::Crate => format!("pub(crate) {}", s),
ast::Visibility::Restricted { ref path, .. } => format!("pub({}) {}", path, s),
ast::Visibility::Inherited => s.to_string()
}
}
@ -1052,13 +1054,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 +1078,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 +1120,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 +1138,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 +1161,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 +1183,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 +1206,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 +1224,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 +1251,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 +1289,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 +1314,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 +1357,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,9 +1383,12 @@ 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::Crate => self.word_nbsp("pub(crate)"),
ast::Visibility::Restricted { ref path, .. } =>
self.word_nbsp(&format!("pub({})", path)),
ast::Visibility::Inherited => Ok(())
}
}
@ -1404,7 +1409,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 +1434,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 +1533,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 +1555,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 +1587,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 +2660,7 @@ impl<'a> State<'a> {
name: Option<ast::Ident>,
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 +3042,7 @@ impl<'a> State<'a> {
name,
&generics,
opt_explicit_self,
ast::Visibility::Inherited)?;
&ast::Visibility::Inherited)?;
self.end()
}
@ -3112,7 +3117,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 {

View File

@ -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<Visibility>),
Method(Ident, &'a MethodSig, Option<&'a Visibility>),
/// |x, y| {}
Closure,
@ -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]
@ -260,7 +263,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 +549,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 +600,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) => {
@ -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),
_ => {}
}
}