Skip to content

Commit 37f3017

Browse files
committed
Rollup merge of rust-lang#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_`.
2 parents b833e8d + 9d99fe9 commit 37f3017

File tree

21 files changed

+82
-192
lines changed

21 files changed

+82
-192
lines changed

src/librustc/hir/fold.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -271,16 +271,10 @@ pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<
271271
ViewPathList(fld.fold_path(path),
272272
path_list_idents.move_map(|path_list_ident| {
273273
Spanned {
274-
node: match path_list_ident.node {
275-
PathListIdent { id, name, rename } => PathListIdent {
276-
id: fld.new_id(id),
277-
name: name,
278-
rename: rename,
279-
},
280-
PathListMod { id, rename } => PathListMod {
281-
id: fld.new_id(id),
282-
rename: rename,
283-
},
274+
node: PathListItem_ {
275+
id: fld.new_id(path_list_ident.node.id),
276+
name: path_list_ident.node.name,
277+
rename: path_list_ident.node.rename,
284278
},
285279
span: fld.new_span(path_list_ident.span),
286280
}

src/librustc/hir/intravisit.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,12 @@ pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
444444
}
445445
}
446446

447-
pub fn walk_path_list_item<'v, V: Visitor<'v>>(visitor: &mut V,
448-
_prefix: &'v Path,
449-
item: &'v PathListItem) {
450-
visitor.visit_id(item.node.id());
451-
walk_opt_name(visitor, item.span, item.node.name());
452-
walk_opt_name(visitor, item.span, item.node.rename());
447+
pub fn walk_path_list_item<'v, V>(visitor: &mut V, _prefix: &'v Path, item: &'v PathListItem)
448+
where V: Visitor<'v>,
449+
{
450+
visitor.visit_id(item.node.id);
451+
visitor.visit_name(item.span, item.node.name);
452+
walk_opt_name(visitor, item.span, item.node.rename);
453453
}
454454

455455
pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,

src/librustc/hir/lowering.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,10 @@ impl<'a> LoweringContext<'a> {
218218

219219
fn lower_path_list_item(&mut self, path_list_ident: &PathListItem) -> hir::PathListItem {
220220
Spanned {
221-
node: match path_list_ident.node {
222-
PathListItemKind::Ident { id, name, rename } => hir::PathListIdent {
223-
id: id,
224-
name: name.name,
225-
rename: rename.map(|x| x.name),
226-
},
227-
PathListItemKind::Mod { id, rename } => hir::PathListMod {
228-
id: id,
229-
rename: rename.map(|x| x.name),
230-
},
221+
node: hir::PathListItem_ {
222+
id: path_list_ident.node.id,
223+
name: path_list_ident.node.name.name,
224+
rename: path_list_ident.node.rename.map(|rename| rename.name),
231225
},
232226
span: path_list_ident.span,
233227
}

src/librustc/hir/map/collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
120120
match view_path.node {
121121
ViewPathList(_, ref paths) => {
122122
for path in paths {
123-
this.insert(path.node.id(), NodeItem(i));
123+
this.insert(path.node.id, NodeItem(i));
124124
}
125125
}
126126
_ => ()

src/librustc/hir/mod.rs

+5-34
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub use self::FunctionRetTy::*;
2020
pub use self::ForeignItem_::*;
2121
pub use self::Item_::*;
2222
pub use self::Mutability::*;
23-
pub use self::PathListItem_::*;
2423
pub use self::PrimTy::*;
2524
pub use self::Stmt_::*;
2625
pub use self::TraitItem_::*;
@@ -1307,39 +1306,11 @@ pub struct Variant_ {
13071306
pub type Variant = Spanned<Variant_>;
13081307

13091308
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1310-
pub enum PathListItem_ {
1311-
PathListIdent {
1312-
name: Name,
1313-
/// renamed in list, eg `use foo::{bar as baz};`
1314-
rename: Option<Name>,
1315-
id: NodeId,
1316-
},
1317-
PathListMod {
1318-
/// renamed in list, eg `use foo::{self as baz};`
1319-
rename: Option<Name>,
1320-
id: NodeId,
1321-
},
1322-
}
1323-
1324-
impl PathListItem_ {
1325-
pub fn id(&self) -> NodeId {
1326-
match *self {
1327-
PathListIdent { id, .. } | PathListMod { id, .. } => id,
1328-
}
1329-
}
1330-
1331-
pub fn name(&self) -> Option<Name> {
1332-
match *self {
1333-
PathListIdent { name, .. } => Some(name),
1334-
PathListMod { .. } => None,
1335-
}
1336-
}
1337-
1338-
pub fn rename(&self) -> Option<Name> {
1339-
match *self {
1340-
PathListIdent { rename, .. } | PathListMod { rename, .. } => rename,
1341-
}
1342-
}
1309+
pub struct PathListItem_ {
1310+
pub name: Name,
1311+
/// renamed in list, eg `use foo::{bar as baz};`
1312+
pub rename: Option<Name>,
1313+
pub id: NodeId,
13431314
}
13441315

13451316
pub type PathListItem = Spanned<PathListItem_>;

src/librustc/hir/print.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -2134,16 +2134,7 @@ impl<'a> State<'a> {
21342134
self.print_path(path, false, 0)?;
21352135
word(&mut self.s, "::{")?;
21362136
}
2137-
self.commasep(Inconsistent, &segments[..], |s, w| {
2138-
match w.node {
2139-
hir::PathListIdent { name, .. } => {
2140-
s.print_name(name)
2141-
}
2142-
hir::PathListMod { .. } => {
2143-
word(&mut s.s, "self")
2144-
}
2145-
}
2146-
})?;
2137+
self.commasep(Inconsistent, &segments[..], |s, w| s.print_name(w.node.name))?;
21472138
word(&mut self.s, "}")
21482139
}
21492140
}

src/librustc/middle/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
294294
}
295295

296296
fn visit_path_list_item(&mut self, path: &hir::Path, item: &hir::PathListItem) {
297-
self.lookup_and_handle_definition(item.node.id());
297+
self.lookup_and_handle_definition(item.node.id);
298298
intravisit::walk_path_list_item(self, path, item);
299299
}
300300
}

src/librustc/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ pub fn check_path_list_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
631631
cb: &mut FnMut(DefId, Span,
632632
&Option<&Stability>,
633633
&Option<DeprecationEntry>)) {
634-
match tcx.expect_def(item.node.id()) {
634+
match tcx.expect_def(item.node.id) {
635635
Def::PrimTy(..) => {}
636636
def => {
637637
maybe_do_stability_check(tcx, def.def_id(), item.span, cb);

src/librustc_lint/unused.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::collections::hash_map::Entry::{Occupied, Vacant};
2020
use syntax::ast;
2121
use syntax::attr::{self, AttrMetaMethods};
2222
use syntax::feature_gate::{KNOWN_ATTRIBUTES, AttributeType};
23+
use syntax::parse::token::keywords;
2324
use syntax::ptr::P;
2425
use syntax_pos::Span;
2526

@@ -392,13 +393,9 @@ impl LateLintPass for UnusedImportBraces {
392393
fn check_item(&mut self, cx: &LateContext, item: &hir::Item) {
393394
if let hir::ItemUse(ref view_path) = item.node {
394395
if let hir::ViewPathList(_, ref items) = view_path.node {
395-
if items.len() == 1 {
396-
if let hir::PathListIdent {ref name, ..} = items[0].node {
397-
let m = format!("braces around {} is unnecessary",
398-
name);
399-
cx.span_lint(UNUSED_IMPORT_BRACES, item.span,
400-
&m[..]);
401-
}
396+
if items.len() == 1 && items[0].node.name != keywords::SelfValue.name() {
397+
let msg = format!("braces around {} is unnecessary", items[0].node.name);
398+
cx.span_lint(UNUSED_IMPORT_BRACES, item.span, &msg);
402399
}
403400
}
404401
}

src/librustc_resolve/build_reduced_graph.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ use syntax::parse::token;
3232

3333
use syntax::ast::{Block, Crate};
3434
use syntax::ast::{ForeignItem, ForeignItemKind, Item, ItemKind};
35-
use syntax::ast::{Mutability, PathListItemKind};
36-
use syntax::ast::{StmtKind, TraitItemKind};
35+
use syntax::ast::{Mutability, StmtKind, TraitItemKind};
3736
use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
37+
use syntax::parse::token::keywords;
3838
use syntax::visit::{self, Visitor};
3939

4040
use syntax_pos::{Span, DUMMY_SP};
@@ -130,9 +130,10 @@ impl<'b> Resolver<'b> {
130130
ViewPathList(_, ref source_items) => {
131131
// Make sure there's at most one `mod` import in the list.
132132
let mod_spans = source_items.iter().filter_map(|item| {
133-
match item.node {
134-
PathListItemKind::Mod { .. } => Some(item.span),
135-
_ => None,
133+
if item.node.name.name == keywords::SelfValue.name() {
134+
Some(item.span)
135+
} else {
136+
None
136137
}
137138
}).collect::<Vec<Span>>();
138139

@@ -147,10 +148,12 @@ impl<'b> Resolver<'b> {
147148
}
148149

149150
for source_item in source_items {
150-
let (module_path, name, rename) = match source_item.node {
151-
PathListItemKind::Ident { name, rename, .. } =>
152-
(module_path.clone(), name.name, rename.unwrap_or(name).name),
153-
PathListItemKind::Mod { rename, .. } => {
151+
let node = source_item.node;
152+
let (module_path, name, rename) = {
153+
if node.name.name != keywords::SelfValue.name() {
154+
let rename = node.rename.unwrap_or(node.name).name;
155+
(module_path.clone(), node.name.name, rename)
156+
} else {
154157
let name = match module_path.last() {
155158
Some(name) => *name,
156159
None => {
@@ -164,12 +167,12 @@ impl<'b> Resolver<'b> {
164167
}
165168
};
166169
let module_path = module_path.split_last().unwrap().1;
167-
let rename = rename.map(|i| i.name).unwrap_or(name);
170+
let rename = node.rename.map(|i| i.name).unwrap_or(name);
168171
(module_path.to_vec(), name, rename)
169172
}
170173
};
171174
let subclass = ImportDirectiveSubclass::single(rename, name);
172-
let (span, id) = (source_item.span, source_item.node.id());
175+
let (span, id) = (source_item.span, source_item.node.id);
173176
self.add_import_directive(module_path, subclass, span, id, vis);
174177
}
175178
}

src/librustc_resolve/check_unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<'a, 'b> Visitor for UnusedImportCheckVisitor<'a, 'b> {
101101

102102
ViewPathList(_, ref list) => {
103103
for i in list {
104-
self.check_import(i.node.id(), i.span);
104+
self.check_import(i.node.id, i.span);
105105
}
106106
}
107107
ViewPathGlob(_) => {

src/librustc_save_analysis/dump_visitor.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -1102,18 +1102,11 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
11021102
}
11031103
ast::ViewPathList(ref path, ref list) => {
11041104
for plid in list {
1105-
match plid.node {
1106-
ast::PathListItemKind::Ident { id, .. } => {
1107-
let scope = self.cur_scope;
1108-
if let Some(def_id) = self.lookup_type_ref(id) {
1109-
self.process_def_kind(id,
1110-
plid.span,
1111-
Some(plid.span),
1112-
def_id,
1113-
scope);
1114-
}
1115-
}
1116-
ast::PathListItemKind::Mod { .. } => (),
1105+
let scope = self.cur_scope;
1106+
let id = plid.node.id;
1107+
if let Some(def_id) = self.lookup_type_ref(id) {
1108+
let span = plid.span;
1109+
self.process_def_kind(id, span, Some(span), def_id, scope);
11171110
}
11181111
}
11191112

src/librustc_typeck/check_unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for UnusedTraitImportVisitor<'a, 'tcx> {
4949
}
5050
hir::ViewPathList(_, ref path_list) => {
5151
for path_item in path_list {
52-
self.check_import(path_item.node.id(), path_item.span);
52+
self.check_import(path_item.node.id, path_item.span);
5353
}
5454
}
5555
}

src/librustdoc/clean/mod.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -2551,7 +2551,7 @@ impl Clean<Vec<Item>> for doctree::Import {
25512551
let remaining = if !denied {
25522552
let mut remaining = vec![];
25532553
for path in list {
2554-
match inline::try_inline(cx, path.node.id(), path.node.rename()) {
2554+
match inline::try_inline(cx, path.node.id, path.node.rename) {
25552555
Some(items) => {
25562556
ret.extend(items);
25572557
}
@@ -2619,17 +2619,10 @@ pub struct ViewListIdent {
26192619

26202620
impl Clean<ViewListIdent> for hir::PathListItem {
26212621
fn clean(&self, cx: &DocContext) -> ViewListIdent {
2622-
match self.node {
2623-
hir::PathListIdent { id, name, rename } => ViewListIdent {
2624-
name: name.clean(cx),
2625-
rename: rename.map(|r| r.clean(cx)),
2626-
source: resolve_def(cx, id)
2627-
},
2628-
hir::PathListMod { id, rename } => ViewListIdent {
2629-
name: "self".to_string(),
2630-
rename: rename.map(|r| r.clean(cx)),
2631-
source: resolve_def(cx, id)
2632-
}
2622+
ViewListIdent {
2623+
name: self.node.name.clean(cx),
2624+
rename: self.node.rename.map(|r| r.clean(cx)),
2625+
source: resolve_def(cx, self.node.id)
26332626
}
26342627
}
26352628
}

src/librustdoc/visit_ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
189189
}
190190
hir::ViewPathList(p, paths) => {
191191
let mine = paths.into_iter().filter(|path| {
192-
!self.maybe_inline_local(path.node.id(), path.node.rename(),
192+
!self.maybe_inline_local(path.node.id, path.node.rename,
193193
false, om, please_inline)
194194
}).collect::<hir::HirVec<hir::PathListItem>>();
195195

src/libsyntax/ast.rs

+6-34
Original file line numberDiff line numberDiff line change
@@ -1626,42 +1626,14 @@ pub struct Variant_ {
16261626
pub type Variant = Spanned<Variant_>;
16271627

16281628
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
1629-
pub enum PathListItemKind {
1630-
Ident {
1631-
name: Ident,
1632-
/// renamed in list, e.g. `use foo::{bar as baz};`
1633-
rename: Option<Ident>,
1634-
id: NodeId
1635-
},
1636-
Mod {
1637-
/// renamed in list, e.g. `use foo::{self as baz};`
1638-
rename: Option<Ident>,
1639-
id: NodeId
1640-
}
1641-
}
1642-
1643-
impl PathListItemKind {
1644-
pub fn id(&self) -> NodeId {
1645-
match *self {
1646-
PathListItemKind::Ident { id, .. } | PathListItemKind::Mod { id, .. } => id
1647-
}
1648-
}
1649-
1650-
pub fn name(&self) -> Option<Ident> {
1651-
match *self {
1652-
PathListItemKind::Ident { name, .. } => Some(name),
1653-
PathListItemKind::Mod { .. } => None,
1654-
}
1655-
}
1656-
1657-
pub fn rename(&self) -> Option<Ident> {
1658-
match *self {
1659-
PathListItemKind::Ident { rename, .. } | PathListItemKind::Mod { rename, .. } => rename
1660-
}
1661-
}
1629+
pub struct PathListItem_ {
1630+
pub name: Ident,
1631+
/// renamed in list, e.g. `use foo::{bar as baz};`
1632+
pub rename: Option<Ident>,
1633+
pub id: NodeId,
16621634
}
16631635

1664-
pub type PathListItem = Spanned<PathListItemKind>;
1636+
pub type PathListItem = Spanned<PathListItem_>;
16651637

16661638
pub type ViewPath = Spanned<ViewPath_>;
16671639

src/libsyntax/ext/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
11781178
fn item_use_list(&self, sp: Span, vis: ast::Visibility,
11791179
path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
11801180
let imports = imports.iter().map(|id| {
1181-
let item = ast::PathListItemKind::Ident {
1181+
let item = ast::PathListItem_ {
11821182
name: *id,
11831183
rename: None,
11841184
id: ast::DUMMY_NODE_ID,

0 commit comments

Comments
 (0)