Rollup merge of #35618 - jseyfried:ast_view_path_refactor, r=eddyb

Refactor `PathListItem`s

This refactors away variant `Mod` of `ast::PathListItemKind` and refactors the remaining variant `Ident` to a struct `ast::PathListItem_`.
This commit is contained in:
Jeffrey Seyfried 2016-08-28 10:31:15 +00:00
commit 37f30173a0
21 changed files with 82 additions and 192 deletions

View file

@ -271,16 +271,10 @@ pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<
ViewPathList(fld.fold_path(path), ViewPathList(fld.fold_path(path),
path_list_idents.move_map(|path_list_ident| { path_list_idents.move_map(|path_list_ident| {
Spanned { Spanned {
node: match path_list_ident.node { node: PathListItem_ {
PathListIdent { id, name, rename } => PathListIdent { id: fld.new_id(path_list_ident.node.id),
id: fld.new_id(id), name: path_list_ident.node.name,
name: name, rename: path_list_ident.node.rename,
rename: rename,
},
PathListMod { id, rename } => PathListMod {
id: fld.new_id(id),
rename: rename,
},
}, },
span: fld.new_span(path_list_ident.span), span: fld.new_span(path_list_ident.span),
} }

View file

@ -444,12 +444,12 @@ pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
} }
} }
pub fn walk_path_list_item<'v, V: Visitor<'v>>(visitor: &mut V, pub fn walk_path_list_item<'v, V>(visitor: &mut V, _prefix: &'v Path, item: &'v PathListItem)
_prefix: &'v Path, where V: Visitor<'v>,
item: &'v PathListItem) { {
visitor.visit_id(item.node.id()); visitor.visit_id(item.node.id);
walk_opt_name(visitor, item.span, item.node.name()); visitor.visit_name(item.span, item.node.name);
walk_opt_name(visitor, item.span, item.node.rename()); walk_opt_name(visitor, item.span, item.node.rename);
} }
pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V, pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,

View file

@ -218,16 +218,10 @@ impl<'a> LoweringContext<'a> {
fn lower_path_list_item(&mut self, path_list_ident: &PathListItem) -> hir::PathListItem { fn lower_path_list_item(&mut self, path_list_ident: &PathListItem) -> hir::PathListItem {
Spanned { Spanned {
node: match path_list_ident.node { node: hir::PathListItem_ {
PathListItemKind::Ident { id, name, rename } => hir::PathListIdent { id: path_list_ident.node.id,
id: id, name: path_list_ident.node.name.name,
name: name.name, rename: path_list_ident.node.rename.map(|rename| rename.name),
rename: rename.map(|x| x.name),
},
PathListItemKind::Mod { id, rename } => hir::PathListMod {
id: id,
rename: rename.map(|x| x.name),
},
}, },
span: path_list_ident.span, span: path_list_ident.span,
} }

View file

@ -120,7 +120,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
match view_path.node { match view_path.node {
ViewPathList(_, ref paths) => { ViewPathList(_, ref paths) => {
for path in paths { for path in paths {
this.insert(path.node.id(), NodeItem(i)); this.insert(path.node.id, NodeItem(i));
} }
} }
_ => () _ => ()

View file

@ -20,7 +20,6 @@ pub use self::FunctionRetTy::*;
pub use self::ForeignItem_::*; pub use self::ForeignItem_::*;
pub use self::Item_::*; pub use self::Item_::*;
pub use self::Mutability::*; pub use self::Mutability::*;
pub use self::PathListItem_::*;
pub use self::PrimTy::*; pub use self::PrimTy::*;
pub use self::Stmt_::*; pub use self::Stmt_::*;
pub use self::TraitItem_::*; pub use self::TraitItem_::*;
@ -1307,39 +1306,11 @@ pub struct Variant_ {
pub type Variant = Spanned<Variant_>; pub type Variant = Spanned<Variant_>;
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
pub enum PathListItem_ { pub struct PathListItem_ {
PathListIdent { pub name: Name,
name: Name, /// renamed in list, eg `use foo::{bar as baz};`
/// renamed in list, eg `use foo::{bar as baz};` pub rename: Option<Name>,
rename: Option<Name>, pub id: NodeId,
id: NodeId,
},
PathListMod {
/// renamed in list, eg `use foo::{self as baz};`
rename: Option<Name>,
id: NodeId,
},
}
impl PathListItem_ {
pub fn id(&self) -> NodeId {
match *self {
PathListIdent { id, .. } | PathListMod { id, .. } => id,
}
}
pub fn name(&self) -> Option<Name> {
match *self {
PathListIdent { name, .. } => Some(name),
PathListMod { .. } => None,
}
}
pub fn rename(&self) -> Option<Name> {
match *self {
PathListIdent { rename, .. } | PathListMod { rename, .. } => rename,
}
}
} }
pub type PathListItem = Spanned<PathListItem_>; pub type PathListItem = Spanned<PathListItem_>;

View file

@ -2134,16 +2134,7 @@ impl<'a> State<'a> {
self.print_path(path, false, 0)?; self.print_path(path, false, 0)?;
word(&mut self.s, "::{")?; word(&mut self.s, "::{")?;
} }
self.commasep(Inconsistent, &segments[..], |s, w| { self.commasep(Inconsistent, &segments[..], |s, w| s.print_name(w.node.name))?;
match w.node {
hir::PathListIdent { name, .. } => {
s.print_name(name)
}
hir::PathListMod { .. } => {
word(&mut s.s, "self")
}
}
})?;
word(&mut self.s, "}") word(&mut self.s, "}")
} }
} }

View file

@ -294,7 +294,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
} }
fn visit_path_list_item(&mut self, path: &hir::Path, item: &hir::PathListItem) { fn visit_path_list_item(&mut self, path: &hir::Path, item: &hir::PathListItem) {
self.lookup_and_handle_definition(item.node.id()); self.lookup_and_handle_definition(item.node.id);
intravisit::walk_path_list_item(self, path, item); intravisit::walk_path_list_item(self, path, item);
} }
} }

View file

@ -631,7 +631,7 @@ pub fn check_path_list_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
cb: &mut FnMut(DefId, Span, cb: &mut FnMut(DefId, Span,
&Option<&Stability>, &Option<&Stability>,
&Option<DeprecationEntry>)) { &Option<DeprecationEntry>)) {
match tcx.expect_def(item.node.id()) { match tcx.expect_def(item.node.id) {
Def::PrimTy(..) => {} Def::PrimTy(..) => {}
def => { def => {
maybe_do_stability_check(tcx, def.def_id(), item.span, cb); maybe_do_stability_check(tcx, def.def_id(), item.span, cb);

View file

@ -20,6 +20,7 @@ use std::collections::hash_map::Entry::{Occupied, Vacant};
use syntax::ast; use syntax::ast;
use syntax::attr::{self, AttrMetaMethods}; use syntax::attr::{self, AttrMetaMethods};
use syntax::feature_gate::{KNOWN_ATTRIBUTES, AttributeType}; use syntax::feature_gate::{KNOWN_ATTRIBUTES, AttributeType};
use syntax::parse::token::keywords;
use syntax::ptr::P; use syntax::ptr::P;
use syntax_pos::Span; use syntax_pos::Span;
@ -392,13 +393,9 @@ impl LateLintPass for UnusedImportBraces {
fn check_item(&mut self, cx: &LateContext, item: &hir::Item) { fn check_item(&mut self, cx: &LateContext, item: &hir::Item) {
if let hir::ItemUse(ref view_path) = item.node { if let hir::ItemUse(ref view_path) = item.node {
if let hir::ViewPathList(_, ref items) = view_path.node { if let hir::ViewPathList(_, ref items) = view_path.node {
if items.len() == 1 { if items.len() == 1 && items[0].node.name != keywords::SelfValue.name() {
if let hir::PathListIdent {ref name, ..} = items[0].node { let msg = format!("braces around {} is unnecessary", items[0].node.name);
let m = format!("braces around {} is unnecessary", cx.span_lint(UNUSED_IMPORT_BRACES, item.span, &msg);
name);
cx.span_lint(UNUSED_IMPORT_BRACES, item.span,
&m[..]);
}
} }
} }
} }

View file

@ -32,9 +32,9 @@ use syntax::parse::token;
use syntax::ast::{Block, Crate}; use syntax::ast::{Block, Crate};
use syntax::ast::{ForeignItem, ForeignItemKind, Item, ItemKind}; use syntax::ast::{ForeignItem, ForeignItemKind, Item, ItemKind};
use syntax::ast::{Mutability, PathListItemKind}; use syntax::ast::{Mutability, StmtKind, TraitItemKind};
use syntax::ast::{StmtKind, TraitItemKind};
use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple}; use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
use syntax::parse::token::keywords;
use syntax::visit::{self, Visitor}; use syntax::visit::{self, Visitor};
use syntax_pos::{Span, DUMMY_SP}; use syntax_pos::{Span, DUMMY_SP};
@ -130,9 +130,10 @@ impl<'b> Resolver<'b> {
ViewPathList(_, ref source_items) => { ViewPathList(_, ref source_items) => {
// Make sure there's at most one `mod` import in the list. // Make sure there's at most one `mod` import in the list.
let mod_spans = source_items.iter().filter_map(|item| { let mod_spans = source_items.iter().filter_map(|item| {
match item.node { if item.node.name.name == keywords::SelfValue.name() {
PathListItemKind::Mod { .. } => Some(item.span), Some(item.span)
_ => None, } else {
None
} }
}).collect::<Vec<Span>>(); }).collect::<Vec<Span>>();
@ -147,10 +148,12 @@ impl<'b> Resolver<'b> {
} }
for source_item in source_items { for source_item in source_items {
let (module_path, name, rename) = match source_item.node { let node = source_item.node;
PathListItemKind::Ident { name, rename, .. } => let (module_path, name, rename) = {
(module_path.clone(), name.name, rename.unwrap_or(name).name), if node.name.name != keywords::SelfValue.name() {
PathListItemKind::Mod { rename, .. } => { let rename = node.rename.unwrap_or(node.name).name;
(module_path.clone(), node.name.name, rename)
} else {
let name = match module_path.last() { let name = match module_path.last() {
Some(name) => *name, Some(name) => *name,
None => { None => {
@ -164,12 +167,12 @@ impl<'b> Resolver<'b> {
} }
}; };
let module_path = module_path.split_last().unwrap().1; let module_path = module_path.split_last().unwrap().1;
let rename = rename.map(|i| i.name).unwrap_or(name); let rename = node.rename.map(|i| i.name).unwrap_or(name);
(module_path.to_vec(), name, rename) (module_path.to_vec(), name, rename)
} }
}; };
let subclass = ImportDirectiveSubclass::single(rename, name); let subclass = ImportDirectiveSubclass::single(rename, name);
let (span, id) = (source_item.span, source_item.node.id()); let (span, id) = (source_item.span, source_item.node.id);
self.add_import_directive(module_path, subclass, span, id, vis); self.add_import_directive(module_path, subclass, span, id, vis);
} }
} }

View file

@ -101,7 +101,7 @@ impl<'a, 'b> Visitor for UnusedImportCheckVisitor<'a, 'b> {
ViewPathList(_, ref list) => { ViewPathList(_, ref list) => {
for i in list { for i in list {
self.check_import(i.node.id(), i.span); self.check_import(i.node.id, i.span);
} }
} }
ViewPathGlob(_) => { ViewPathGlob(_) => {

View file

@ -1102,18 +1102,11 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
} }
ast::ViewPathList(ref path, ref list) => { ast::ViewPathList(ref path, ref list) => {
for plid in list { for plid in list {
match plid.node { let scope = self.cur_scope;
ast::PathListItemKind::Ident { id, .. } => { let id = plid.node.id;
let scope = self.cur_scope; if let Some(def_id) = self.lookup_type_ref(id) {
if let Some(def_id) = self.lookup_type_ref(id) { let span = plid.span;
self.process_def_kind(id, self.process_def_kind(id, span, Some(span), def_id, scope);
plid.span,
Some(plid.span),
def_id,
scope);
}
}
ast::PathListItemKind::Mod { .. } => (),
} }
} }

View file

@ -49,7 +49,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for UnusedTraitImportVisitor<'a, 'tcx> {
} }
hir::ViewPathList(_, ref path_list) => { hir::ViewPathList(_, ref path_list) => {
for path_item in path_list { for path_item in path_list {
self.check_import(path_item.node.id(), path_item.span); self.check_import(path_item.node.id, path_item.span);
} }
} }
} }

View file

@ -2551,7 +2551,7 @@ impl Clean<Vec<Item>> for doctree::Import {
let remaining = if !denied { let remaining = if !denied {
let mut remaining = vec![]; let mut remaining = vec![];
for path in list { for path in list {
match inline::try_inline(cx, path.node.id(), path.node.rename()) { match inline::try_inline(cx, path.node.id, path.node.rename) {
Some(items) => { Some(items) => {
ret.extend(items); ret.extend(items);
} }
@ -2619,17 +2619,10 @@ pub struct ViewListIdent {
impl Clean<ViewListIdent> for hir::PathListItem { impl Clean<ViewListIdent> for hir::PathListItem {
fn clean(&self, cx: &DocContext) -> ViewListIdent { fn clean(&self, cx: &DocContext) -> ViewListIdent {
match self.node { ViewListIdent {
hir::PathListIdent { id, name, rename } => ViewListIdent { name: self.node.name.clean(cx),
name: name.clean(cx), rename: self.node.rename.map(|r| r.clean(cx)),
rename: rename.map(|r| r.clean(cx)), source: resolve_def(cx, self.node.id)
source: resolve_def(cx, id)
},
hir::PathListMod { id, rename } => ViewListIdent {
name: "self".to_string(),
rename: rename.map(|r| r.clean(cx)),
source: resolve_def(cx, id)
}
} }
} }
} }

View file

@ -189,7 +189,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
} }
hir::ViewPathList(p, paths) => { hir::ViewPathList(p, paths) => {
let mine = paths.into_iter().filter(|path| { let mine = paths.into_iter().filter(|path| {
!self.maybe_inline_local(path.node.id(), path.node.rename(), !self.maybe_inline_local(path.node.id, path.node.rename,
false, om, please_inline) false, om, please_inline)
}).collect::<hir::HirVec<hir::PathListItem>>(); }).collect::<hir::HirVec<hir::PathListItem>>();

View file

@ -1626,42 +1626,14 @@ pub struct Variant_ {
pub type Variant = Spanned<Variant_>; pub type Variant = Spanned<Variant_>;
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
pub enum PathListItemKind { pub struct PathListItem_ {
Ident { pub name: Ident,
name: Ident, /// renamed in list, e.g. `use foo::{bar as baz};`
/// renamed in list, e.g. `use foo::{bar as baz};` pub rename: Option<Ident>,
rename: Option<Ident>, pub id: NodeId,
id: NodeId
},
Mod {
/// renamed in list, e.g. `use foo::{self as baz};`
rename: Option<Ident>,
id: NodeId
}
} }
impl PathListItemKind { pub type PathListItem = Spanned<PathListItem_>;
pub fn id(&self) -> NodeId {
match *self {
PathListItemKind::Ident { id, .. } | PathListItemKind::Mod { id, .. } => id
}
}
pub fn name(&self) -> Option<Ident> {
match *self {
PathListItemKind::Ident { name, .. } => Some(name),
PathListItemKind::Mod { .. } => None,
}
}
pub fn rename(&self) -> Option<Ident> {
match *self {
PathListItemKind::Ident { rename, .. } | PathListItemKind::Mod { rename, .. } => rename
}
}
}
pub type PathListItem = Spanned<PathListItemKind>;
pub type ViewPath = Spanned<ViewPath_>; pub type ViewPath = Spanned<ViewPath_>;

View file

@ -1178,7 +1178,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
fn item_use_list(&self, sp: Span, vis: ast::Visibility, fn item_use_list(&self, sp: Span, vis: ast::Visibility,
path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> { path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
let imports = imports.iter().map(|id| { let imports = imports.iter().map(|id| {
let item = ast::PathListItemKind::Ident { let item = ast::PathListItem_ {
name: *id, name: *id,
rename: None, rename: None,
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,

View file

@ -307,18 +307,10 @@ pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<
ViewPathList(fld.fold_path(path), ViewPathList(fld.fold_path(path),
path_list_idents.move_map(|path_list_ident| { path_list_idents.move_map(|path_list_ident| {
Spanned { Spanned {
node: match path_list_ident.node { node: PathListItem_ {
PathListItemKind::Ident { id, name, rename } => id: fld.new_id(path_list_ident.node.id),
PathListItemKind::Ident { rename: path_list_ident.node.rename,
id: fld.new_id(id), name: path_list_ident.node.name,
rename: rename,
name: name
},
PathListItemKind::Mod { id, rename } =>
PathListItemKind::Mod {
id: fld.new_id(id),
rename: rename
}
}, },
span: fld.new_span(path_list_ident.span) span: fld.new_span(path_list_ident.span)
} }

View file

@ -6040,13 +6040,16 @@ impl<'a> Parser<'a> {
&token::CloseDelim(token::Brace), &token::CloseDelim(token::Brace),
SeqSep::trailing_allowed(token::Comma), |this| { SeqSep::trailing_allowed(token::Comma), |this| {
let lo = this.span.lo; let lo = this.span.lo;
let node = if this.eat_keyword(keywords::SelfValue) { let ident = if this.eat_keyword(keywords::SelfValue) {
let rename = this.parse_rename()?; keywords::SelfValue.ident()
ast::PathListItemKind::Mod { id: ast::DUMMY_NODE_ID, rename: rename }
} else { } else {
let ident = this.parse_ident()?; this.parse_ident()?
let rename = this.parse_rename()?; };
ast::PathListItemKind::Ident { name: ident, rename: rename, id: ast::DUMMY_NODE_ID } let rename = this.parse_rename()?;
let node = ast::PathListItem_ {
name: ident,
rename: rename,
id: ast::DUMMY_NODE_ID
}; };
let hi = this.last_span.hi; let hi = this.last_span.hi;
Ok(spanned(lo, hi, node)) Ok(spanned(lo, hi, node))

View file

@ -2879,26 +2879,13 @@ impl<'a> State<'a> {
try!(word(&mut self.s, "::{")); try!(word(&mut self.s, "::{"));
} }
try!(self.commasep(Inconsistent, &idents[..], |s, w| { try!(self.commasep(Inconsistent, &idents[..], |s, w| {
match w.node { try!(s.print_ident(w.node.name));
ast::PathListItemKind::Ident { name, rename, .. } => { if let Some(ident) = w.node.rename {
try!(s.print_ident(name)); try!(space(&mut s.s));
if let Some(ident) = rename { try!(s.word_space("as"));
try!(space(&mut s.s)); try!(s.print_ident(ident));
try!(s.word_space("as"));
try!(s.print_ident(ident));
}
Ok(())
},
ast::PathListItemKind::Mod { rename, .. } => {
try!(word(&mut s.s, "self"));
if let Some(ident) = rename {
try!(space(&mut s.s));
try!(s.word_space("as"));
try!(s.print_ident(ident));
}
Ok(())
}
} }
Ok(())
})); }));
word(&mut self.s, "}") word(&mut self.s, "}")
} }

View file

@ -367,8 +367,8 @@ pub fn walk_path<V: Visitor>(visitor: &mut V, path: &Path) {
} }
pub fn walk_path_list_item<V: Visitor>(visitor: &mut V, _prefix: &Path, item: &PathListItem) { pub fn walk_path_list_item<V: Visitor>(visitor: &mut V, _prefix: &Path, item: &PathListItem) {
walk_opt_ident(visitor, item.span, item.node.name()); visitor.visit_ident(item.span, item.node.name);
walk_opt_ident(visitor, item.span, item.node.rename()); walk_opt_ident(visitor, item.span, item.node.rename);
} }
pub fn walk_path_segment<V: Visitor>(visitor: &mut V, path_span: Span, segment: &PathSegment) { pub fn walk_path_segment<V: Visitor>(visitor: &mut V, path_span: Span, segment: &PathSegment) {