Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Split-up the AST to index it.
  • Loading branch information
cjgillot committed Jul 20, 2025
commit 971544de9920f8d2edc2c5eaca7556f9c829de3d
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3242,7 +3242,6 @@ version = "0.0.0"
dependencies = [
"rustc_abi",
"rustc_ast",
"rustc_ast_pretty",
"rustc_attr_data_structures",
"rustc_attr_parsing",
"rustc_data_structures",
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4109,14 +4109,14 @@ impl TryFrom<ItemKind> for ForeignItemKind {
pub type ForeignItem = Item<ForeignItemKind>;

#[derive(Debug)]
pub enum AstOwner<'a> {
pub enum AstOwner {
NonOwner,
Synthetic(rustc_span::def_id::LocalDefId),
Crate(&'a Crate),
Item(&'a Item),
TraitItem(&'a AssocItem),
ImplItem(&'a AssocItem),
ForeignItem(&'a ForeignItem),
Crate(P<Crate>),
Item(P<Item>),
TraitItem(P<AssocItem>),
ImplItem(P<AssocItem>),
ForeignItem(P<ForeignItem>),
}

// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ pub fn walk_flat_map_stmt<T: MutVisitor>(
stmts
}

fn walk_flat_map_stmt_kind<T: MutVisitor>(vis: &mut T, kind: StmtKind) -> SmallVec<[StmtKind; 1]> {
pub fn walk_flat_map_stmt_kind<T: MutVisitor>(
vis: &mut T,
kind: StmtKind,
) -> SmallVec<[StmtKind; 1]> {
match kind {
StmtKind::Let(mut local) => smallvec![StmtKind::Let({
vis.visit_local(&mut local);
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_ast_lowering/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ doctest = false
# tidy-alphabetical-start
rustc_abi = { path = "../rustc_abi" }
rustc_ast = { path = "../rustc_ast" }
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
rustc_attr_data_structures = { path = "../rustc_attr_data_structures" }
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
rustc_data_structures = { path = "../rustc_data_structures" }
Expand Down
16 changes: 5 additions & 11 deletions compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,11 @@ impl<'hir> LoweringContext<'hir> {
stmts.push(hir::Stmt { hir_id, kind, span });
}
StmtKind::Item(it) => {
stmts.extend(self.lower_item_ref(it).into_iter().enumerate().map(
|(i, item_id)| {
let hir_id = match i {
0 => self.lower_node_id(s.id),
_ => self.next_id(),
};
let kind = hir::StmtKind::Item(item_id);
let span = self.lower_span(s.span);
hir::Stmt { hir_id, kind, span }
},
));
let item_id = self.lower_item_ref(it);
let hir_id = self.lower_node_id(s.id);
let kind = hir::StmtKind::Item(item_id);
let span = self.lower_span(s.span);
stmts.push(hir::Stmt { hir_id, kind, span });
}
StmtKind::Expr(e) => {
let e = self.lower_expr(e);
Expand Down
12 changes: 7 additions & 5 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::sync::Arc;

use rustc_ast::ptr::P as AstP;
use rustc_ast::*;
use rustc_ast_pretty::pprust::expr_to_string;
use rustc_attr_data_structures::{AttributeKind, find_attr};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir as hir;
Expand Down Expand Up @@ -463,13 +462,16 @@ impl<'hir> LoweringContext<'hir> {
let mut invalid_expr_error = |tcx: TyCtxt<'_>, span| {
// Avoid emitting the error multiple times.
if error.is_none() {
let sm = tcx.sess.source_map();
let mut const_args = vec![];
let mut other_args = vec![];
for (idx, arg) in args.iter().enumerate() {
if legacy_args_idx.contains(&idx) {
const_args.push(format!("{{ {} }}", expr_to_string(arg)));
} else {
other_args.push(expr_to_string(arg));
if let Ok(arg) = sm.span_to_snippet(arg.span) {
if legacy_args_idx.contains(&idx) {
const_args.push(format!("{{ {} }}", arg));
} else {
other_args.push(arg);
}
}
}
let suggestion = UseConstGenericArg {
Expand Down
27 changes: 6 additions & 21 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_middle::span_bug;
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::{DesugaringKind, Ident, Span, Symbol, kw, sym};
use smallvec::{SmallVec, smallvec};
use smallvec::SmallVec;
use thin_vec::ThinVec;
use tracing::instrument;

Expand All @@ -27,6 +27,7 @@ use super::{
pub(super) struct ItemLowerer<'hir> {
pub(super) tcx: TyCtxt<'hir>,
pub(super) resolver: &'hir ResolverAstLowering,
pub(super) next_node_id: NodeId,
}

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

let item = f(&mut lctx);
debug_assert_eq!(lctx.current_hir_id_owner, item.def_id());
Expand Down Expand Up @@ -105,28 +106,12 @@ impl<'hir> LoweringContext<'hir> {
inner_span: self.lower_span(spans.inner_span),
inject_use_span: self.lower_span(spans.inject_use_span),
},
item_ids: self.arena.alloc_from_iter(items.iter().flat_map(|x| self.lower_item_ref(x))),
item_ids: self.arena.alloc_from_iter(items.iter().map(|x| self.lower_item_ref(x))),
})
}

pub(super) fn lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
let mut node_ids = smallvec![hir::ItemId { owner_id: self.owner_id(i.id) }];
if let ItemKind::Use(use_tree) = &i.kind {
self.lower_item_id_use_tree(use_tree, &mut node_ids);
}
node_ids
}

fn lower_item_id_use_tree(&mut self, tree: &UseTree, vec: &mut SmallVec<[hir::ItemId; 1]>) {
match &tree.kind {
UseTreeKind::Nested { items, .. } => {
for &(ref nested, id) in items {
vec.push(hir::ItemId { owner_id: self.owner_id(id) });
self.lower_item_id_use_tree(nested, vec);
}
}
UseTreeKind::Simple(..) | UseTreeKind::Glob => {}
}
pub(super) fn lower_item_ref(&mut self, i: &Item) -> hir::ItemId {
hir::ItemId { owner_id: self.owner_id(i.id) }
}

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