Skip to content

Commit 4f4e26e

Browse files
committed
Split-up the AST to index it.
1 parent 6cea0cb commit 4f4e26e

File tree

16 files changed

+182
-102
lines changed

16 files changed

+182
-102
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3354,7 +3354,6 @@ version = "0.0.0"
33543354
dependencies = [
33553355
"rustc_abi",
33563356
"rustc_ast",
3357-
"rustc_ast_pretty",
33583357
"rustc_attr_data_structures",
33593358
"rustc_attr_parsing",
33603359
"rustc_data_structures",

compiler/rustc_ast/src/ast.rs

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

40574057
#[derive(Debug)]
4058-
pub enum AstOwner<'a> {
4058+
pub enum AstOwner {
40594059
NonOwner,
40604060
Synthetic(rustc_span::def_id::LocalDefId),
4061-
Crate(&'a Crate),
4062-
Item(&'a Item),
4063-
TraitItem(&'a AssocItem),
4064-
ImplItem(&'a AssocItem),
4065-
ForeignItem(&'a ForeignItem),
4061+
Crate(P<Crate>),
4062+
Item(P<Item>),
4063+
TraitItem(P<AssocItem>),
4064+
ImplItem(P<AssocItem>),
4065+
ForeignItem(P<ForeignItem>),
40664066
}
40674067

40684068
// 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
@@ -142,7 +142,10 @@ pub fn walk_flat_map_stmt<T: MutVisitor>(
142142
stmts
143143
}
144144

145-
fn walk_flat_map_stmt_kind<T: MutVisitor>(vis: &mut T, kind: StmtKind) -> SmallVec<[StmtKind; 1]> {
145+
pub fn walk_flat_map_stmt_kind<T: MutVisitor>(
146+
vis: &mut T,
147+
kind: StmtKind,
148+
) -> SmallVec<[StmtKind; 1]> {
146149
match kind {
147150
StmtKind::Let(mut local) => smallvec![StmtKind::Let({
148151
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_data_structures = { path = "../rustc_attr_data_structures" }
1514
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
1615
rustc_data_structures = { path = "../rustc_data_structures" }

compiler/rustc_ast_lowering/src/block.rs

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

44
use rustc_ast::ptr::P as AstP;
55
use rustc_ast::*;
6-
use rustc_ast_pretty::pprust::expr_to_string;
76
use rustc_attr_data_structures::{AttributeKind, find_attr};
87
use rustc_data_structures::stack::ensure_sufficient_stack;
98
use rustc_hir as hir;
@@ -463,13 +462,16 @@ impl<'hir> LoweringContext<'hir> {
463462
let mut invalid_expr_error = |tcx: TyCtxt<'_>, span| {
464463
// Avoid emitting the error multiple times.
465464
if error.is_none() {
465+
let sm = tcx.sess.source_map();
466466
let mut const_args = vec![];
467467
let mut other_args = vec![];
468468
for (idx, arg) in args.iter().enumerate() {
469-
if legacy_args_idx.contains(&idx) {
470-
const_args.push(format!("{{ {} }}", expr_to_string(arg)));
471-
} else {
472-
other_args.push(expr_to_string(arg));
469+
if let Ok(arg) = sm.span_to_snippet(arg.span) {
470+
if legacy_args_idx.contains(&idx) {
471+
const_args.push(format!("{{ {} }}", arg));
472+
} else {
473+
other_args.push(arg);
474+
}
473475
}
474476
}
475477
let suggestion = UseConstGenericArg {

compiler/rustc_ast_lowering/src/item.rs

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

@@ -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
@@ -56,7 +57,7 @@ impl<'hir> ItemLowerer<'hir> {
5657
owner: NodeId,
5758
f: impl FnOnce(&mut LoweringContext<'hir>) -> hir::OwnerNode<'hir>,
5859
) -> hir::MaybeOwner<'hir> {
59-
let mut lctx = LoweringContext::new(self.tcx, self.resolver, owner);
60+
let mut lctx = LoweringContext::new(self.tcx, self.resolver, owner, self.next_node_id);
6061

6162
let item = f(&mut lctx);
6263
debug_assert_eq!(lctx.current_hir_id_owner, item.def_id());
@@ -105,28 +106,12 @@ impl<'hir> LoweringContext<'hir> {
105106
inner_span: self.lower_span(spans.inner_span),
106107
inject_use_span: self.lower_span(spans.inject_use_span),
107108
},
108-
item_ids: self.arena.alloc_from_iter(items.iter().flat_map(|x| self.lower_item_ref(x))),
109+
item_ids: self.arena.alloc_from_iter(items.iter().map(|x| self.lower_item_ref(x))),
109110
})
110111
}
111112

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

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

0 commit comments

Comments
 (0)