Skip to content

Commit 7bebe80

Browse files
committed
syntax: dismantle ast_util.
1 parent ef4c724 commit 7bebe80

File tree

16 files changed

+117
-489
lines changed

16 files changed

+117
-489
lines changed

src/librustc/hir/fold.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -908,14 +908,6 @@ pub fn noop_fold_item<T: Folder>(item: Item, folder: &mut T) -> Item {
908908
let Item { id, name, attrs, node, vis, span } = item;
909909
let id = folder.new_id(id);
910910
let node = folder.fold_item_underscore(node);
911-
// FIXME: we should update the impl_pretty_name, but it uses pretty printing.
912-
// let ident = match node {
913-
// // The node may have changed, recompute the "pretty" impl name.
914-
// ItemImpl(_, _, _, ref maybe_trait, ref ty, _) => {
915-
// impl_pretty_name(maybe_trait, Some(&**ty))
916-
// }
917-
// _ => ident
918-
// };
919911

920912
Item {
921913
id: id,

src/librustc/hir/intravisit.rs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
2828
use syntax::abi::Abi;
2929
use syntax::ast::{NodeId, CRATE_NODE_ID, Name, Attribute};
30-
use syntax::ast_util;
3130
use syntax::attr::ThinAttributesExt;
3231
use syntax::codemap::Span;
3332
use hir::*;
3433

34+
use std::cmp;
35+
use std::u32;
36+
3537
#[derive(Copy, Clone, PartialEq, Eq)]
3638
pub enum FnKind<'a> {
3739
/// fn foo() or extern "Abi" fn foo()
@@ -837,6 +839,54 @@ pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
837839
walk_list!(visitor, visit_attribute, &arm.attrs);
838840
}
839841

842+
#[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)]
843+
pub struct IdRange {
844+
pub min: NodeId,
845+
pub max: NodeId,
846+
}
847+
848+
impl IdRange {
849+
pub fn max() -> IdRange {
850+
IdRange {
851+
min: u32::MAX,
852+
max: u32::MIN,
853+
}
854+
}
855+
856+
pub fn empty(&self) -> bool {
857+
self.min >= self.max
858+
}
859+
860+
pub fn add(&mut self, id: NodeId) {
861+
self.min = cmp::min(self.min, id);
862+
self.max = cmp::max(self.max, id + 1);
863+
}
864+
}
865+
866+
pub trait IdVisitingOperation {
867+
fn visit_id(&mut self, node_id: NodeId);
868+
}
869+
870+
pub struct IdRangeComputingVisitor {
871+
pub result: IdRange,
872+
}
873+
874+
impl IdRangeComputingVisitor {
875+
pub fn new() -> IdRangeComputingVisitor {
876+
IdRangeComputingVisitor { result: IdRange::max() }
877+
}
878+
879+
pub fn result(&self) -> IdRange {
880+
self.result
881+
}
882+
}
883+
884+
impl IdVisitingOperation for IdRangeComputingVisitor {
885+
fn visit_id(&mut self, id: NodeId) {
886+
self.result.add(id);
887+
}
888+
}
889+
840890
pub struct IdVisitor<'a, O: 'a> {
841891
operation: &'a mut O,
842892

@@ -853,7 +903,7 @@ pub struct IdVisitor<'a, O: 'a> {
853903
skip_members: bool,
854904
}
855905

856-
impl<'a, O: ast_util::IdVisitingOperation> IdVisitor<'a, O> {
906+
impl<'a, O: IdVisitingOperation> IdVisitor<'a, O> {
857907
pub fn new(operation: &'a mut O) -> IdVisitor<'a, O> {
858908
IdVisitor { operation: operation, skip_members: false }
859909
}
@@ -868,7 +918,7 @@ impl<'a, O: ast_util::IdVisitingOperation> IdVisitor<'a, O> {
868918
}
869919
}
870920

871-
impl<'a, 'v, O: ast_util::IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
921+
impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
872922
fn visit_mod(&mut self, module: &Mod, _: Span, node_id: NodeId) {
873923
self.operation.visit_id(node_id);
874924
walk_mod(self, module)
@@ -1012,8 +1062,8 @@ pub fn compute_id_range_for_fn_body(fk: FnKind,
10121062
body: &Block,
10131063
sp: Span,
10141064
id: NodeId)
1015-
-> ast_util::IdRange {
1016-
let mut visitor = ast_util::IdRangeComputingVisitor { result: ast_util::IdRange::max() };
1065+
-> IdRange {
1066+
let mut visitor = IdRangeComputingVisitor { result: IdRange::max() };
10171067
let mut id_visitor = IdVisitor::new(&mut visitor);
10181068
id_visitor.visit_fn(fk, decl, body, sp, id);
10191069
id_visitor.operation.result

src/librustc/lint/context.rs

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use std::cell::RefCell;
3939
use std::cmp;
4040
use std::default::Default as StdDefault;
4141
use std::mem;
42-
use syntax::ast_util::{self, IdVisitingOperation};
4342
use syntax::attr::{self, AttrMetaMethods};
4443
use syntax::codemap::Span;
4544
use syntax::errors::DiagnosticBuilder;
@@ -48,7 +47,7 @@ use syntax::ast;
4847
use syntax::attr::ThinAttributesExt;
4948
use hir;
5049
use hir::intravisit as hir_visit;
51-
use hir::intravisit::IdVisitor;
50+
use hir::intravisit::{IdVisitor, IdVisitingOperation};
5251
use syntax::visit as ast_visit;
5352

5453
/// Information about the registered lints.
@@ -654,16 +653,6 @@ impl<'a> EarlyContext<'a> {
654653
level_stack: vec![],
655654
}
656655
}
657-
658-
fn visit_ids<F>(&mut self, f: F)
659-
where F: FnOnce(&mut ast_util::IdVisitor<EarlyContext>)
660-
{
661-
let mut v = ast_util::IdVisitor {
662-
operation: self,
663-
visited_outermost: false,
664-
};
665-
f(&mut v);
666-
}
667656
}
668657

669658
impl<'a, 'tcx> LateContext<'a, 'tcx> {
@@ -928,7 +917,6 @@ impl<'a, 'v> ast_visit::Visitor<'v> for EarlyContext<'a> {
928917
fn visit_item(&mut self, it: &ast::Item) {
929918
self.with_lint_attrs(&it.attrs, |cx| {
930919
run_lints!(cx, check_item, early_passes, it);
931-
cx.visit_ids(|v| v.visit_item(it));
932920
ast_visit::walk_item(cx, it);
933921
run_lints!(cx, check_item_post, early_passes, it);
934922
})
@@ -1042,7 +1030,6 @@ impl<'a, 'v> ast_visit::Visitor<'v> for EarlyContext<'a> {
10421030
fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
10431031
self.with_lint_attrs(&trait_item.attrs, |cx| {
10441032
run_lints!(cx, check_trait_item, early_passes, trait_item);
1045-
cx.visit_ids(|v| v.visit_trait_item(trait_item));
10461033
ast_visit::walk_trait_item(cx, trait_item);
10471034
run_lints!(cx, check_trait_item_post, early_passes, trait_item);
10481035
});
@@ -1051,7 +1038,6 @@ impl<'a, 'v> ast_visit::Visitor<'v> for EarlyContext<'a> {
10511038
fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
10521039
self.with_lint_attrs(&impl_item.attrs, |cx| {
10531040
run_lints!(cx, check_impl_item, early_passes, impl_item);
1054-
cx.visit_ids(|v| v.visit_impl_item(impl_item));
10551041
ast_visit::walk_impl_item(cx, impl_item);
10561042
run_lints!(cx, check_impl_item_post, early_passes, impl_item);
10571043
});
@@ -1099,18 +1085,6 @@ impl<'a, 'tcx> IdVisitingOperation for LateContext<'a, 'tcx> {
10991085
}
11001086
}
11011087
}
1102-
impl<'a> IdVisitingOperation for EarlyContext<'a> {
1103-
fn visit_id(&mut self, id: ast::NodeId) {
1104-
match self.sess.lints.borrow_mut().remove(&id) {
1105-
None => {}
1106-
Some(lints) => {
1107-
for (lint_id, span, msg) in lints {
1108-
self.span_lint(lint_id.lint, span, &msg[..])
1109-
}
1110-
}
1111-
}
1112-
}
1113-
}
11141088

11151089
// This lint pass is defined here because it touches parts of the `LateContext`
11161090
// that we don't want to expose. It records the lint level at certain AST
@@ -1292,11 +1266,12 @@ pub fn check_ast_crate(sess: &Session, krate: &ast::Crate) {
12921266

12931267
// Visit the whole crate.
12941268
cx.with_lint_attrs(&krate.attrs, |cx| {
1295-
cx.visit_id(ast::CRATE_NODE_ID);
1296-
cx.visit_ids(|v| {
1297-
v.visited_outermost = true;
1298-
ast_visit::walk_crate(v, krate);
1299-
});
1269+
// Lints may be assigned to the whole crate.
1270+
if let Some(lints) = cx.sess.lints.borrow_mut().remove(&ast::CRATE_NODE_ID) {
1271+
for (lint_id, span, msg) in lints {
1272+
cx.span_lint(lint_id.lint, span, &msg[..])
1273+
}
1274+
}
13001275

13011276
// since the root module isn't visited as an item (because it isn't an
13021277
// item), warn for it here.

src/librustc/middle/cstore.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ use std::cell::RefCell;
3838
use std::rc::Rc;
3939
use std::path::PathBuf;
4040
use syntax::ast;
41-
use syntax::ast_util::{IdVisitingOperation};
4241
use syntax::attr;
4342
use syntax::codemap::Span;
4443
use syntax::ptr::P;
4544
use syntax::parse::token::InternedString;
4645
use rustc_back::target::Target;
4746
use hir;
48-
use hir::intravisit::{IdVisitor, Visitor};
47+
use hir::intravisit::{IdVisitor, IdVisitingOperation, Visitor};
4948

5049
pub use self::DefLike::{DlDef, DlField, DlImpl};
5150
pub use self::NativeLibraryKind::{NativeStatic, NativeFramework, NativeUnknown};

src/librustc/middle/dataflow.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ use std::io;
2121
use std::mem;
2222
use std::usize;
2323
use syntax::ast;
24-
use syntax::ast_util::IdRange;
2524
use syntax::print::pp;
2625
use syntax::print::pprust::PrintState;
2726
use util::nodemap::NodeMap;
2827
use hir;
29-
use hir::intravisit;
28+
use hir::intravisit::{self, IdRange};
3029
use hir::print as pprust;
3130

3231

src/librustc_borrowck/borrowck/move_data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ use std::cell::RefCell;
2828
use std::rc::Rc;
2929
use std::usize;
3030
use syntax::ast;
31-
use syntax::ast_util;
3231
use syntax::codemap::Span;
3332
use rustc::hir;
33+
use rustc::hir::intravisit::IdRange;
3434

3535
#[path="fragments.rs"]
3636
pub mod fragments;
@@ -602,7 +602,7 @@ impl<'a, 'tcx> FlowedMoveData<'a, 'tcx> {
602602
pub fn new(move_data: MoveData<'tcx>,
603603
tcx: &'a TyCtxt<'tcx>,
604604
cfg: &cfg::CFG,
605-
id_range: ast_util::IdRange,
605+
id_range: IdRange,
606606
decl: &hir::FnDecl,
607607
body: &hir::Block)
608608
-> FlowedMoveData<'a, 'tcx> {

src/librustc_const_eval/check_match.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ use std::iter::{FromIterator, IntoIterator, repeat};
3434

3535
use rustc::hir;
3636
use rustc::hir::{Pat, PatKind};
37-
use rustc::hir::intravisit::{self, IdVisitor, Visitor, FnKind};
37+
use rustc::hir::intravisit::{self, IdVisitor, IdVisitingOperation, Visitor, FnKind};
3838
use rustc_back::slice;
3939

4040
use syntax::ast::{self, DUMMY_NODE_ID, NodeId};
41-
use syntax::ast_util;
4241
use syntax::codemap::{Span, Spanned, DUMMY_SP};
4342
use rustc::hir::fold::{Folder, noop_fold_pat};
4443
use rustc::hir::print::pat_to_string;
@@ -460,7 +459,7 @@ struct RenamingRecorder<'map> {
460459
renaming_map: &'map mut FnvHashMap<(NodeId, Span), NodeId>
461460
}
462461

463-
impl<'map> ast_util::IdVisitingOperation for RenamingRecorder<'map> {
462+
impl<'map> IdVisitingOperation for RenamingRecorder<'map> {
464463
fn visit_id(&mut self, node_id: NodeId) {
465464
let key = (node_id, self.origin_span);
466465
self.renaming_map.insert(key, self.substituted_node_id);

src/librustc_metadata/astencode.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc::session::Session;
1818
use rustc::hir;
1919
use rustc::hir::fold;
2020
use rustc::hir::fold::Folder;
21+
use rustc::hir::intravisit::{IdRange, IdRangeComputingVisitor, IdVisitingOperation};
2122

2223
use common as c;
2324
use cstore;
@@ -36,7 +37,7 @@ use middle::region;
3637
use rustc::ty::subst;
3738
use rustc::ty::{self, Ty, TyCtxt};
3839

39-
use syntax::{ast, ast_util, codemap};
40+
use syntax::{ast, codemap};
4041
use syntax::ast::NodeIdAssigner;
4142
use syntax::ptr::P;
4243

@@ -61,8 +62,8 @@ use serialize::EncoderHelpers;
6162
struct DecodeContext<'a, 'b, 'tcx: 'a> {
6263
tcx: &'a TyCtxt<'tcx>,
6364
cdata: &'b cstore::crate_metadata,
64-
from_id_range: ast_util::IdRange,
65-
to_id_range: ast_util::IdRange,
65+
from_id_range: IdRange,
66+
to_id_range: IdRange,
6667
// Cache the last used filemap for translating spans as an optimization.
6768
last_filemap_index: Cell<usize>,
6869
}
@@ -178,13 +179,13 @@ pub fn decode_inlined_item<'tcx>(cdata: &cstore::crate_metadata,
178179
// Enumerating the IDs which appear in an AST
179180

180181
fn reserve_id_range(sess: &Session,
181-
from_id_range: ast_util::IdRange) -> ast_util::IdRange {
182+
from_id_range: IdRange) -> IdRange {
182183
// Handle the case of an empty range:
183184
if from_id_range.empty() { return from_id_range; }
184185
let cnt = from_id_range.max - from_id_range.min;
185186
let to_id_min = sess.reserve_node_ids(cnt);
186187
let to_id_max = to_id_min + cnt;
187-
ast_util::IdRange { min: to_id_min, max: to_id_max }
188+
IdRange { min: to_id_min, max: to_id_max }
188189
}
189190

190191
impl<'a, 'b, 'tcx> DecodeContext<'a, 'b, 'tcx> {
@@ -705,7 +706,7 @@ struct SideTableEncodingIdVisitor<'a, 'b:'a, 'c:'a, 'tcx:'c> {
705706
rbml_w: &'a mut Encoder<'b>,
706707
}
707708

708-
impl<'a, 'b, 'c, 'tcx> ast_util::IdVisitingOperation for
709+
impl<'a, 'b, 'c, 'tcx> IdVisitingOperation for
709710
SideTableEncodingIdVisitor<'a, 'b, 'c, 'tcx> {
710711
fn visit_id(&mut self, id: ast::NodeId) {
711712
encode_side_tables_for_id(self.ecx, self.rbml_w, id)
@@ -1261,8 +1262,8 @@ fn copy_item_types(dcx: &DecodeContext, ii: &InlinedItem, orig_did: DefId) {
12611262
}
12621263
}
12631264

1264-
fn inlined_item_id_range(v: &InlinedItem) -> ast_util::IdRange {
1265-
let mut visitor = ast_util::IdRangeComputingVisitor::new();
1265+
fn inlined_item_id_range(v: &InlinedItem) -> IdRange {
1266+
let mut visitor = IdRangeComputingVisitor::new();
12661267
v.visit_ids(&mut visitor);
12671268
visitor.result()
12681269
}

src/librustc_save_analysis/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use std::fs::{self, File};
3939
use std::path::{Path, PathBuf};
4040

4141
use syntax::ast::{self, NodeId, PatKind};
42-
use syntax::ast_util;
4342
use syntax::codemap::*;
4443
use syntax::parse::token::{self, keywords};
4544
use syntax::visit::{self, Visitor};
@@ -670,7 +669,7 @@ impl<'v> Visitor<'v> for PathCollector {
670669
ast::BindingMode::ByValue(mt) => mt,
671670
};
672671
// collect path for either visit_local or visit_arm
673-
let path = ast_util::ident_to_path(path1.span, path1.node);
672+
let path = ast::Path::from_ident(path1.span, path1.node);
674673
self.collected_paths.push((p.id, path, immut, recorder::VarRef));
675674
}
676675
_ => {}

src/libsyntax/ast.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,23 @@ impl fmt::Display for Path {
205205
}
206206
}
207207

208+
impl Path {
209+
// convert a span and an identifier to the corresponding
210+
// 1-segment path
211+
pub fn from_ident(s: Span, identifier: Ident) -> Path {
212+
Path {
213+
span: s,
214+
global: false,
215+
segments: vec!(
216+
PathSegment {
217+
identifier: identifier,
218+
parameters: PathParameters::none()
219+
}
220+
),
221+
}
222+
}
223+
}
224+
208225
/// A segment of a path: an identifier, an optional lifetime, and a set of
209226
/// types.
210227
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]

0 commit comments

Comments
 (0)