Remove unused trace module

This commit is contained in:
Lukas Wirth 2024-07-25 14:08:48 +02:00
parent 0ba6f4eda0
commit d32e60467f
4 changed files with 46 additions and 127 deletions

View file

@ -5,14 +5,10 @@ use bitflags::bitflags;
use cfg::CfgOptions;
use either::Either;
use hir_expand::{
name::{AsName, Name},
InFile,
};
use hir_expand::name::Name;
use intern::{sym, Interned};
use la_arena::Arena;
use rustc_abi::{Align, Integer, IntegerType, ReprFlags, ReprOptions};
use syntax::ast::{self, HasName, HasVisibility};
use triomphe::Arc;
use crate::{
@ -22,9 +18,7 @@ use crate::{
AttrOwner, Field, FieldParent, FieldsShape, ItemTree, ModItem, RawVisibilityId, TreeId,
},
lang_item::LangItem,
lower::LowerCtx,
nameres::diagnostics::{DefDiagnostic, DefDiagnostics},
trace::Trace,
tt::{Delimiter, DelimiterKind, Leaf, Subtree, TokenTree},
type_ref::TypeRef,
visibility::RawVisibility,
@ -409,64 +403,6 @@ pub enum StructKind {
Unit,
}
// FIXME This is only used for mapping back source now?
pub(crate) fn lower_struct(
db: &dyn DefDatabase,
trace: &mut Trace<FieldData, Either<ast::TupleField, ast::RecordField>>,
ast: &InFile<ast::StructKind>,
krate: CrateId,
item_tree: &ItemTree,
parent: FieldParent,
) -> StructKind {
let ctx = LowerCtx::new(db, ast.file_id);
match &ast.value {
ast::StructKind::Tuple(fl) => {
let cfg_options = &db.crate_graph()[krate].cfg_options;
for (i, fd) in fl.fields().enumerate() {
let attrs = item_tree.attrs(db, krate, AttrOwner::make_field_indexed(parent, i));
if !attrs.is_cfg_enabled(cfg_options) {
continue;
}
trace.alloc(
|| Either::Left(fd.clone()),
|| FieldData {
name: Name::new_tuple_field(i),
type_ref: Interned::new(TypeRef::from_ast_opt(&ctx, fd.ty())),
visibility: RawVisibility::from_ast(db, fd.visibility(), &mut |range| {
ctx.span_map().span_for_range(range).ctx
}),
},
);
}
StructKind::Tuple
}
ast::StructKind::Record(fl) => {
let cfg_options = &db.crate_graph()[krate].cfg_options;
for (i, fd) in fl.fields().enumerate() {
let attrs = item_tree.attrs(db, krate, AttrOwner::make_field_indexed(parent, i));
if !attrs.is_cfg_enabled(cfg_options) {
continue;
}
trace.alloc(
|| Either::Right(fd.clone()),
|| FieldData {
name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
type_ref: Interned::new(TypeRef::from_ast_opt(&ctx, fd.ty())),
visibility: RawVisibility::from_ast(db, fd.visibility(), &mut |range| {
ctx.span_map().span_for_range(range).ctx
}),
},
);
}
StructKind::Record
}
_ => StructKind::Unit,
}
}
fn lower_fields(
db: &dyn DefDatabase,
krate: CrateId,

View file

@ -46,7 +46,6 @@ pub mod body;
pub mod resolver;
pub mod nameres;
mod trace;
pub mod child_by_source;
pub mod src;

View file

@ -6,10 +6,8 @@ use la_arena::ArenaMap;
use syntax::{ast, AstNode, AstPtr};
use crate::{
data::adt::lower_struct,
db::DefDatabase,
item_tree::{FieldParent, ItemTreeNode},
trace::Trace,
item_tree::{AttrOwner, FieldParent, ItemTreeNode},
GenericDefId, ItemTreeLoc, LocalFieldId, LocalLifetimeParamId, LocalTypeOrConstParamId, Lookup,
UseId, VariantId,
};
@ -156,8 +154,49 @@ impl HasChildSource<LocalFieldId> for VariantId {
)
}
};
let mut trace = Trace::new_for_map();
lower_struct(db, &mut trace, &src, container.krate, &item_tree, parent);
src.with_value(trace.into_map())
let mut map = ArenaMap::new();
match &src.value {
ast::StructKind::Tuple(fl) => {
let cfg_options = &db.crate_graph()[container.krate].cfg_options;
let mut idx = 0;
for (i, fd) in fl.fields().enumerate() {
let attrs = item_tree.attrs(
db,
container.krate,
AttrOwner::make_field_indexed(parent, i),
);
if !attrs.is_cfg_enabled(cfg_options) {
continue;
}
map.insert(
LocalFieldId::from_raw(la_arena::RawIdx::from(idx)),
Either::Left(fd.clone()),
);
idx += 1;
}
}
ast::StructKind::Record(fl) => {
let cfg_options = &db.crate_graph()[container.krate].cfg_options;
let mut idx = 0;
for (i, fd) in fl.fields().enumerate() {
let attrs = item_tree.attrs(
db,
container.krate,
AttrOwner::make_field_indexed(parent, i),
);
if !attrs.is_cfg_enabled(cfg_options) {
continue;
}
map.insert(
LocalFieldId::from_raw(la_arena::RawIdx::from(idx)),
Either::Right(fd.clone()),
);
idx += 1;
}
}
_ => (),
}
InFile::new(src.file_id, map)
}
}

View file

@ -1,55 +0,0 @@
//! Trace is a pretty niche data structure which is used when lowering a CST
//! into HIR.
//!
//! Lowering process calculates two bits of information:
//! * the lowered syntax itself
//! * a mapping between lowered syntax and original syntax
//!
//! Due to the way salsa works, the mapping is usually hot lava, as it contains
//! absolute offsets. The `Trace` structure (inspired, at least in name, by
//! Kotlin's `BindingTrace`) allows use the same code to compute both
//! projections.
use la_arena::{Arena, ArenaMap, Idx, RawIdx};
// FIXME: This isn't really used anymore, at least not in a way where it does anything useful.
// Check if we should get rid of this or make proper use of it instead.
pub(crate) struct Trace<T, V> {
arena: Option<Arena<T>>,
map: Option<ArenaMap<Idx<T>, V>>,
len: u32,
}
impl<T, V> Trace<T, V> {
#[allow(dead_code)]
pub(crate) fn new_for_arena() -> Trace<T, V> {
Trace { arena: Some(Arena::default()), map: None, len: 0 }
}
pub(crate) fn new_for_map() -> Trace<T, V> {
Trace { arena: None, map: Some(ArenaMap::default()), len: 0 }
}
pub(crate) fn alloc(&mut self, value: impl FnOnce() -> V, data: impl FnOnce() -> T) -> Idx<T> {
let id = if let Some(arena) = &mut self.arena {
arena.alloc(data())
} else {
let id = Idx::<T>::from_raw(RawIdx::from(self.len));
self.len += 1;
id
};
if let Some(map) = &mut self.map {
map.insert(id, value());
}
id
}
#[allow(dead_code)]
pub(crate) fn into_arena(mut self) -> Arena<T> {
self.arena.take().unwrap()
}
pub(crate) fn into_map(mut self) -> ArenaMap<Idx<T>, V> {
self.map.take().unwrap()
}
}