Skip to content

Commit 8c13bbe

Browse files
committed
Split-up the AST to index it.
1 parent 0925219 commit 8c13bbe

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
@@ -3523,7 +3523,6 @@ version = "0.0.0"
35233523
dependencies = [
35243524
"rustc_abi",
35253525
"rustc_ast",
3526-
"rustc_ast_pretty",
35273526
"rustc_attr_parsing",
35283527
"rustc_data_structures",
35293528
"rustc_errors",

compiler/rustc_ast/src/ast.rs

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

41734173
#[derive(Debug)]
4174-
pub enum AstOwner<'a> {
4174+
pub enum AstOwner {
41754175
NonOwner,
41764176
Synthetic(rustc_span::def_id::LocalDefId),
4177-
Crate(&'a Crate),
4178-
Item(&'a Item),
4179-
TraitItem(&'a AssocItem),
4180-
ImplItem(&'a AssocItem),
4181-
ForeignItem(&'a ForeignItem),
4177+
Crate(Box<Crate>),
4178+
Item(Box<Item>),
4179+
TraitItem(Box<AssocItem>),
4180+
ImplItem(Box<AssocItem>),
4181+
ForeignItem(Box<ForeignItem>),
41824182
}
41834183

41844184
// 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
@@ -3,7 +3,6 @@ use std::ops::ControlFlow;
33
use std::sync::Arc;
44

55
use rustc_ast::*;
6-
use rustc_ast_pretty::pprust::expr_to_string;
76
use rustc_data_structures::stack::ensure_sufficient_stack;
87
use rustc_hir as hir;
98
use rustc_hir::attrs::AttributeKind;
@@ -447,13 +446,16 @@ impl<'hir> LoweringContext<'hir> {
447446
let mut invalid_expr_error = |tcx: TyCtxt<'_>, span| {
448447
// Avoid emitting the error multiple times.
449448
if error.is_none() {
449+
let sm = tcx.sess.source_map();
450450
let mut const_args = vec![];
451451
let mut other_args = vec![];
452452
for (idx, arg) in args.iter().enumerate() {
453-
if legacy_args_idx.contains(&idx) {
454-
const_args.push(format!("{{ {} }}", expr_to_string(arg)));
455-
} else {
456-
other_args.push(expr_to_string(arg));
453+
if let Ok(arg) = sm.span_to_snippet(arg.span) {
454+
if legacy_args_idx.contains(&idx) {
455+
const_args.push(format!("{{ {} }}", arg));
456+
} else {
457+
other_args.push(arg);
458+
}
457459
}
458460
}
459461
let suggestion = UseConstGenericArg {

compiler/rustc_ast_lowering/src/item.rs

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

@@ -27,6 +27,7 @@ use super::{
2727
pub(super) struct ItemLowerer<'hir> {
2828
pub(super) tcx: TyCtxt<'hir>,
2929
pub(super) resolver: &'hir ResolverAstLowering,
30+
pub(super) next_node_id: NodeId,
3031
}
3132

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

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

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

130115
fn generate_extra_attrs_for_item_kind(

0 commit comments

Comments
 (0)