Shrink ModItem by usize

This commit is contained in:
Lukas Wirth 2025-06-12 11:51:33 +02:00
parent 83a7ecdead
commit 23eef0c4ea
4 changed files with 34 additions and 24 deletions

View File

@ -51,7 +51,7 @@ use hir_expand::{
name::Name, name::Name,
}; };
use intern::Interned; use intern::Interned;
use la_arena::Idx; use la_arena::{Idx, RawIdx};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use span::{AstIdNode, Edition, FileAstId, SyntaxContext}; use span::{AstIdNode, Edition, FileAstId, SyntaxContext};
use stdx::never; use stdx::never;
@ -437,10 +437,10 @@ pub struct Use {
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct UseTree { pub struct UseTree {
pub index: Idx<ast::UseTree>,
kind: UseTreeKind, kind: UseTreeKind,
} }
// FIXME: Would be nice to encode `None` into this
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum ImportAlias { pub enum ImportAlias {
/// Unnamed alias, as in `use Foo as _;` /// Unnamed alias, as in `use Foo as _;`
@ -655,15 +655,17 @@ pub enum ImportKind {
TypeOnly, TypeOnly,
} }
impl UseTree { impl Use {
/// Expands the `UseTree` into individually imported `ModPath`s. /// Expands the `UseTree` into individually imported `ModPath`s.
pub fn expand( pub fn expand(
&self, &self,
mut cb: impl FnMut(Idx<ast::UseTree>, ModPath, ImportKind, Option<ImportAlias>), mut cb: impl FnMut(Idx<ast::UseTree>, ModPath, ImportKind, Option<ImportAlias>),
) { ) {
self.expand_impl(None, &mut cb) self.use_tree.expand_impl(None, &mut 0, &mut cb)
} }
}
impl UseTree {
/// The [`UseTreeKind`] of this `UseTree`. /// The [`UseTreeKind`] of this `UseTree`.
pub fn kind(&self) -> &UseTreeKind { pub fn kind(&self) -> &UseTreeKind {
&self.kind &self.kind
@ -672,6 +674,7 @@ impl UseTree {
fn expand_impl( fn expand_impl(
&self, &self,
prefix: Option<ModPath>, prefix: Option<ModPath>,
counting_index: &mut u32,
cb: &mut impl FnMut(Idx<ast::UseTree>, ModPath, ImportKind, Option<ImportAlias>), cb: &mut impl FnMut(Idx<ast::UseTree>, ModPath, ImportKind, Option<ImportAlias>),
) { ) {
fn concat_mod_paths( fn concat_mod_paths(
@ -707,17 +710,27 @@ impl UseTree {
match &self.kind { match &self.kind {
UseTreeKind::Single { path, alias } => { UseTreeKind::Single { path, alias } => {
if let Some((path, kind)) = concat_mod_paths(prefix, path) { if let Some((path, kind)) = concat_mod_paths(prefix, path) {
cb(self.index, path, kind, alias.clone()); cb(Idx::from_raw(RawIdx::from_u32(*counting_index)), path, kind, alias.clone());
} }
} }
UseTreeKind::Glob { path: Some(path) } => { UseTreeKind::Glob { path: Some(path) } => {
if let Some((path, _)) = concat_mod_paths(prefix, path) { if let Some((path, _)) = concat_mod_paths(prefix, path) {
cb(self.index, path, ImportKind::Glob, None); cb(
Idx::from_raw(RawIdx::from_u32(*counting_index)),
path,
ImportKind::Glob,
None,
);
} }
} }
UseTreeKind::Glob { path: None } => { UseTreeKind::Glob { path: None } => {
if let Some(prefix) = prefix { if let Some(prefix) = prefix {
cb(self.index, prefix, ImportKind::Glob, None); cb(
Idx::from_raw(RawIdx::from_u32(*counting_index)),
prefix,
ImportKind::Glob,
None,
);
} }
} }
UseTreeKind::Prefixed { prefix: additional_prefix, list } => { UseTreeKind::Prefixed { prefix: additional_prefix, list } => {
@ -729,7 +742,8 @@ impl UseTree {
None => prefix, None => prefix,
}; };
for tree in &**list { for tree in &**list {
tree.expand_impl(prefix.clone(), cb); *counting_index += 1;
tree.expand_impl(prefix.clone(), counting_index, cb);
} }
} }
} }

View File

@ -424,17 +424,15 @@ impl UseTreeLowering<'_> {
} }
}; };
self.mapping.alloc(tree.clone());
let list = use_tree_list let list = use_tree_list
.use_trees() .use_trees()
.filter_map(|tree| self.lower_use_tree(tree, span_for_range)) .filter_map(|tree| self.lower_use_tree(tree, span_for_range))
.collect(); .collect();
Some( Some(UseTree {
self.use_tree( kind: UseTreeKind::Prefixed { prefix: prefix.map(Interned::new), list },
UseTreeKind::Prefixed { prefix: prefix.map(Interned::new), list }, })
tree,
),
)
} else { } else {
let is_glob = tree.star_token().is_some(); let is_glob = tree.star_token().is_some();
let path = match tree.path() { let path = match tree.path() {
@ -453,23 +451,20 @@ impl UseTreeLowering<'_> {
if path.is_none() { if path.is_none() {
cov_mark::hit!(glob_enum_group); cov_mark::hit!(glob_enum_group);
} }
Some(self.use_tree(UseTreeKind::Glob { path: path.map(Interned::new) }, tree)) self.mapping.alloc(tree.clone());
Some(UseTree { kind: UseTreeKind::Glob { path: path.map(Interned::new) } })
} }
// Globs can't be renamed // Globs can't be renamed
(_, Some(_), true) | (None, None, false) => None, (_, Some(_), true) | (None, None, false) => None,
// `bla::{ as Name}` is invalid // `bla::{ as Name}` is invalid
(None, Some(_), false) => None, (None, Some(_), false) => None,
(Some(path), alias, false) => Some( (Some(path), alias, false) => {
self.use_tree(UseTreeKind::Single { path: Interned::new(path), alias }, tree), self.mapping.alloc(tree.clone());
), Some(UseTree { kind: UseTreeKind::Single { path: Interned::new(path), alias } })
}
} }
} }
} }
fn use_tree(&mut self, kind: UseTreeKind, ast: ast::UseTree) -> UseTree {
let index = self.mapping.alloc(ast);
UseTree { index, kind }
}
} }
pub(crate) fn lower_use_tree( pub(crate) fn lower_use_tree(

View File

@ -163,7 +163,7 @@ impl Import {
) { ) {
let it = &tree[item]; let it = &tree[item];
let visibility = &tree[it.visibility]; let visibility = &tree[it.visibility];
it.use_tree.expand(|idx, path, kind, alias| { it.expand(|idx, path, kind, alias| {
cb(Self { cb(Self {
path, path,
alias, alias,

View File

@ -112,6 +112,7 @@ impl TaggedArcPtr {
} }
} }
// FIXME: This should have more than one niche
#[derive(PartialEq, Eq, Hash)] #[derive(PartialEq, Eq, Hash)]
pub struct Symbol { pub struct Symbol {
repr: TaggedArcPtr, repr: TaggedArcPtr,