Skip to content

Commit 1f727e5

Browse files
committed
Split-up the AST to index it.
1 parent 856f75e commit 1f727e5

File tree

17 files changed

+182
-103
lines changed

17 files changed

+182
-103
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3399,7 +3399,6 @@ version = "0.0.0"
33993399
dependencies = [
34003400
"rustc_abi",
34013401
"rustc_ast",
3402-
"rustc_ast_pretty",
34033402
"rustc_attr_parsing",
34043403
"rustc_data_structures",
34053404
"rustc_errors",

compiler/rustc_ast/src/ast.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4028,14 +4028,14 @@ impl TryFrom<ItemKind> for ForeignItemKind {
40284028
pub type ForeignItem = Item<ForeignItemKind>;
40294029

40304030
#[derive(Debug)]
4031-
pub enum AstOwner<'a> {
4031+
pub enum AstOwner {
40324032
NonOwner,
40334033
Synthetic(rustc_span::def_id::LocalDefId),
4034-
Crate(&'a Crate),
4035-
Item(&'a Item),
4036-
TraitItem(&'a AssocItem),
4037-
ImplItem(&'a AssocItem),
4038-
ForeignItem(&'a ForeignItem),
4034+
Crate(Box<Crate>),
4035+
Item(Box<Item>),
4036+
TraitItem(Box<AssocItem>),
4037+
ImplItem(Box<AssocItem>),
4038+
ForeignItem(Box<ForeignItem>),
40394039
}
40404040

40414041
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,10 @@ pub fn walk_flat_map_stmt<T: MutVisitor>(
362362
stmts
363363
}
364364

365-
fn walk_flat_map_stmt_kind<T: MutVisitor>(vis: &mut T, kind: StmtKind) -> SmallVec<[StmtKind; 1]> {
365+
pub fn walk_flat_map_stmt_kind<T: MutVisitor>(
366+
vis: &mut T,
367+
kind: StmtKind,
368+
) -> SmallVec<[StmtKind; 1]> {
366369
match kind {
367370
StmtKind::Let(mut local) => smallvec![StmtKind::Let({
368371
vis.visit_local(&mut local);

compiler/rustc_ast_lowering/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ doctest = false
1010
# tidy-alphabetical-start
1111
rustc_abi = { path = "../rustc_abi" }
1212
rustc_ast = { path = "../rustc_ast" }
13-
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1413
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
1514
rustc_data_structures = { path = "../rustc_data_structures" }
1615
rustc_errors = { path = "../rustc_errors" }

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,11 @@ impl<'hir> LoweringContext<'hir> {
4444
stmts.push(hir::Stmt { hir_id, kind, span });
4545
}
4646
StmtKind::Item(it) => {
47-
stmts.extend(self.lower_item_ref(it).into_iter().enumerate().map(
48-
|(i, item_id)| {
49-
let hir_id = match i {
50-
0 => self.lower_node_id(s.id),
51-
_ => self.next_id(),
52-
};
53-
let kind = hir::StmtKind::Item(item_id);
54-
let span = self.lower_span(s.span);
55-
hir::Stmt { hir_id, kind, span }
56-
},
57-
));
47+
let item_id = self.lower_item_ref(it);
48+
let hir_id = self.lower_node_id(s.id);
49+
let kind = hir::StmtKind::Item(item_id);
50+
let span = self.lower_span(s.span);
51+
stmts.push(hir::Stmt { hir_id, kind, span });
5852
}
5953
StmtKind::Expr(e) => {
6054
let e = self.lower_expr(e);

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::ops::ControlFlow;
22
use std::sync::Arc;
33

44
use rustc_ast::*;
5-
use rustc_ast_pretty::pprust::expr_to_string;
65
use rustc_data_structures::stack::ensure_sufficient_stack;
76
use rustc_hir as hir;
87
use rustc_hir::attrs::AttributeKind;
@@ -436,13 +435,16 @@ impl<'hir> LoweringContext<'hir> {
436435
let mut invalid_expr_error = |tcx: TyCtxt<'_>, span| {
437436
// Avoid emitting the error multiple times.
438437
if error.is_none() {
438+
let sm = tcx.sess.source_map();
439439
let mut const_args = vec![];
440440
let mut other_args = vec![];
441441
for (idx, arg) in args.iter().enumerate() {
442-
if legacy_args_idx.contains(&idx) {
443-
const_args.push(format!("{{ {} }}", expr_to_string(arg)));
444-
} else {
445-
other_args.push(expr_to_string(arg));
442+
if let Ok(arg) = sm.span_to_snippet(arg.span) {
443+
if legacy_args_idx.contains(&idx) {
444+
const_args.push(format!("{{ {} }}", arg));
445+
} else {
446+
other_args.push(arg);
447+
}
446448
}
447449
}
448450
let suggestion = UseConstGenericArg {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::span_bug;
1212
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1313
use rustc_span::edit_distance::find_best_match_for_name;
1414
use rustc_span::{DesugaringKind, Ident, Span, Symbol, kw, sym};
15-
use smallvec::{SmallVec, smallvec};
15+
use smallvec::SmallVec;
1616
use thin_vec::ThinVec;
1717
use tracing::instrument;
1818

@@ -26,6 +26,7 @@ use super::{
2626
pub(super) struct ItemLowerer<'hir> {
2727
pub(super) tcx: TyCtxt<'hir>,
2828
pub(super) resolver: &'hir ResolverAstLowering,
29+
pub(super) next_node_id: NodeId,
2930
}
3031

3132
/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
@@ -53,7 +54,7 @@ impl<'hir> ItemLowerer<'hir> {
5354
owner: NodeId,
5455
f: impl FnOnce(&mut LoweringContext<'hir>) -> hir::OwnerNode<'hir>,
5556
) -> hir::MaybeOwner<'hir> {
56-
let mut lctx = LoweringContext::new(self.tcx, self.resolver, owner);
57+
let mut lctx = LoweringContext::new(self.tcx, self.resolver, owner, self.next_node_id);
5758

5859
let item = f(&mut lctx);
5960
debug_assert_eq!(lctx.current_hir_id_owner, item.def_id());
@@ -102,28 +103,12 @@ impl<'hir> LoweringContext<'hir> {
102103
inner_span: self.lower_span(spans.inner_span),
103104
inject_use_span: self.lower_span(spans.inject_use_span),
104105
},
105-
item_ids: self.arena.alloc_from_iter(items.iter().flat_map(|x| self.lower_item_ref(x))),
106+
item_ids: self.arena.alloc_from_iter(items.iter().map(|x| self.lower_item_ref(x))),
106107
})
107108
}
108109

109-
pub(super) fn lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
110-
let mut node_ids = smallvec![hir::ItemId { owner_id: self.owner_id(i.id) }];
111-
if let ItemKind::Use(use_tree) = &i.kind {
112-
self.lower_item_id_use_tree(use_tree, &mut node_ids);
113-
}
114-
node_ids
115-
}
116-
117-
fn lower_item_id_use_tree(&mut self, tree: &UseTree, vec: &mut SmallVec<[hir::ItemId; 1]>) {
118-
match &tree.kind {
119-
UseTreeKind::Nested { items, .. } => {
120-
for &(ref nested, id) in items {
121-
vec.push(hir::ItemId { owner_id: self.owner_id(id) });
122-
self.lower_item_id_use_tree(nested, vec);
123-
}
124-
}
125-
UseTreeKind::Simple(..) | UseTreeKind::Glob => {}
126-
}
110+
pub(super) fn lower_item_ref(&mut self, i: &Item) -> hir::ItemId {
111+
hir::ItemId { owner_id: self.owner_id(i.id) }
127112
}
128113

129114
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {

0 commit comments

Comments
 (0)