Skip to content

Commit

Permalink
Auto merge of #32688 - jseyfried:ast_groundwork_for_1422, r=pnkfelix
Browse files Browse the repository at this point in the history
[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
  • Loading branch information
bors committed Apr 6, 2016
2 parents 241a9d0 + 432eb8a commit 772c600
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 69 deletions.
11 changes: 6 additions & 5 deletions src/librustc_front/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
}
}
Expand All @@ -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,
}
}
Expand Down 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 {
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!"))
}
}

Expand Down
19 changes: 6 additions & 13 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {}
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
20 changes: 10 additions & 10 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 @@ -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)
};
Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -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)?;
Expand Down Expand Up @@ -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;

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
Loading

0 comments on commit 772c600

Please sign in to comment.