Skip to content

Provide better incrementality when items are changed #19837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extend-ignore-re = [
"INOUT",
"optin",
"=Pn",
"\\[[0-9A-F]{4},", # AstId hex hashes
# ignore `// spellchecker:off` until `// spellchecker:on`
"(?s)(#|//)\\s*spellchecker:off.*?\\n\\s*(#|//)\\s*spellchecker:on",
]
Expand Down
158 changes: 92 additions & 66 deletions crates/hir-def/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use intern::{Symbol, sym};
use la_arena::{ArenaMap, Idx, RawIdx};
use mbe::DelimiterKind;
use rustc_abi::ReprOptions;
use span::AstIdNode;
use syntax::{
AstPtr,
ast::{self, HasAttrs},
Expand All @@ -22,10 +23,10 @@ use triomphe::Arc;
use tt::iter::{TtElement, TtIter};

use crate::{
AdtId, AttrDefId, GenericParamId, HasModule, ItemTreeLoc, LocalFieldId, Lookup, MacroId,
AdtId, AstIdLoc, AttrDefId, GenericParamId, HasModule, LocalFieldId, Lookup, MacroId,
VariantId,
db::DefDatabase,
item_tree::{AttrOwner, FieldParent, ItemTreeNode},
item_tree::AttrOwner,
lang_item::LangItem,
nameres::{ModuleOrigin, ModuleSource},
src::{HasChildSource, HasSource},
Expand All @@ -42,6 +43,15 @@ pub struct AttrsWithOwner {
}

impl Attrs {
pub fn new(
db: &dyn DefDatabase,
owner: &dyn ast::HasAttrs,
span_map: SpanMapRef<'_>,
cfg_options: &CfgOptions,
) -> Self {
Attrs(RawAttrs::new_expanded(db, owner, span_map, cfg_options))
}

pub fn get(&self, id: AttrId) -> Option<&Attr> {
(**self).iter().find(|attr| attr.id == id)
}
Expand Down Expand Up @@ -94,44 +104,64 @@ impl Attrs {
v: VariantId,
) -> Arc<ArenaMap<LocalFieldId, Attrs>> {
let _p = tracing::info_span!("fields_attrs_query").entered();
// FIXME: There should be some proper form of mapping between item tree field ids and hir field ids
let mut res = ArenaMap::default();
let item_tree;
let (parent, fields, krate) = match v {
let (fields, file_id, krate) = match v {
VariantId::EnumVariantId(it) => {
let loc = it.lookup(db);
let krate = loc.parent.lookup(db).container.krate;
item_tree = loc.id.item_tree(db);
let variant = &item_tree[loc.id.value];
(FieldParent::EnumVariant(loc.id.value), &variant.fields, krate)
let source = loc.source(db);
(source.value.field_list(), source.file_id, krate)
}
VariantId::StructId(it) => {
let loc = it.lookup(db);
let krate = loc.container.krate;
item_tree = loc.id.item_tree(db);
let struct_ = &item_tree[loc.id.value];
(FieldParent::Struct(loc.id.value), &struct_.fields, krate)
let source = loc.source(db);
(source.value.field_list(), source.file_id, krate)
}
VariantId::UnionId(it) => {
let loc = it.lookup(db);
let krate = loc.container.krate;
item_tree = loc.id.item_tree(db);
let union_ = &item_tree[loc.id.value];
(FieldParent::Union(loc.id.value), &union_.fields, krate)
let source = loc.source(db);
(
source.value.record_field_list().map(ast::FieldList::RecordFieldList),
source.file_id,
krate,
)
}
};
let Some(fields) = fields else {
return Arc::new(res);
};

let cfg_options = krate.cfg_options(db);

let mut idx = 0;
for (id, _field) in fields.iter().enumerate() {
let attrs = item_tree.attrs(db, krate, AttrOwner::make_field_indexed(parent, id));
if attrs.is_cfg_enabled(cfg_options) {
res.insert(Idx::from_raw(RawIdx::from(idx)), attrs);
idx += 1;
let span_map = db.span_map(file_id);

match fields {
ast::FieldList::RecordFieldList(fields) => {
let mut idx = 0;
for field in fields.fields() {
let attrs =
Attrs(RawAttrs::new_expanded(db, &field, span_map.as_ref(), cfg_options));
if attrs.is_cfg_enabled(cfg_options).is_ok() {
res.insert(Idx::from_raw(RawIdx::from(idx)), attrs);
idx += 1;
}
}
}
ast::FieldList::TupleFieldList(fields) => {
let mut idx = 0;
for field in fields.fields() {
let attrs =
Attrs(RawAttrs::new_expanded(db, &field, span_map.as_ref(), cfg_options));
if attrs.is_cfg_enabled(cfg_options).is_ok() {
res.insert(Idx::from_raw(RawIdx::from(idx)), attrs);
idx += 1;
}
}
}
}

res.shrink_to_fit();
Arc::new(res)
}
}
Expand Down Expand Up @@ -167,11 +197,10 @@ impl Attrs {
}

#[inline]
pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool {
match self.cfg() {
None => true,
Some(cfg) => cfg_options.check(&cfg) != Some(false),
}
pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> Result<(), CfgExpr> {
self.cfgs().try_for_each(|cfg| {
if cfg_options.check(&cfg) != Some(false) { Ok(()) } else { Err(cfg) }
})
}

#[inline]
Expand Down Expand Up @@ -488,12 +517,12 @@ impl AttrsWithOwner {
pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Attrs {
let _p = tracing::info_span!("attrs_query").entered();
// FIXME: this should use `Trace` to avoid duplication in `source_map` below
let raw_attrs = match def {
match def {
AttrDefId::ModuleId(module) => {
let def_map = module.def_map(db);
let mod_data = &def_map[module.local_id];

match mod_data.origin {
let raw_attrs = match mod_data.origin {
ModuleOrigin::File { definition, declaration_tree_id, .. } => {
let decl_attrs = declaration_tree_id
.item_tree(db)
Expand All @@ -515,77 +544,73 @@ impl AttrsWithOwner {
let tree = db.block_item_tree(id);
tree.raw_attrs(AttrOwner::TopLevel).clone()
}
}
}
AttrDefId::FieldId(it) => {
return db.fields_attrs(it.parent)[it.local_id].clone();
};
Attrs::expand_cfg_attr(db, module.krate, raw_attrs)
}
AttrDefId::EnumVariantId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::FieldId(it) => db.fields_attrs(it.parent)[it.local_id].clone(),
AttrDefId::EnumVariantId(it) => attrs_from_ast_id_loc(db, it),
AttrDefId::AdtId(it) => match it {
AdtId::StructId(it) => attrs_from_item_tree_loc(db, it),
AdtId::EnumId(it) => attrs_from_item_tree_loc(db, it),
AdtId::UnionId(it) => attrs_from_item_tree_loc(db, it),
AdtId::StructId(it) => attrs_from_ast_id_loc(db, it),
AdtId::EnumId(it) => attrs_from_ast_id_loc(db, it),
AdtId::UnionId(it) => attrs_from_ast_id_loc(db, it),
},
AttrDefId::TraitId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::TraitAliasId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::TraitId(it) => attrs_from_ast_id_loc(db, it),
AttrDefId::TraitAliasId(it) => attrs_from_ast_id_loc(db, it),
AttrDefId::MacroId(it) => match it {
MacroId::Macro2Id(it) => attrs_from_item_tree_loc(db, it),
MacroId::MacroRulesId(it) => attrs_from_item_tree_loc(db, it),
MacroId::ProcMacroId(it) => attrs_from_item_tree_loc(db, it),
MacroId::Macro2Id(it) => attrs_from_ast_id_loc(db, it),
MacroId::MacroRulesId(it) => attrs_from_ast_id_loc(db, it),
MacroId::ProcMacroId(it) => attrs_from_ast_id_loc(db, it),
},
AttrDefId::ImplId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::ConstId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::StaticId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::FunctionId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::TypeAliasId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::ImplId(it) => attrs_from_ast_id_loc(db, it),
AttrDefId::ConstId(it) => attrs_from_ast_id_loc(db, it),
AttrDefId::StaticId(it) => attrs_from_ast_id_loc(db, it),
AttrDefId::FunctionId(it) => attrs_from_ast_id_loc(db, it),
AttrDefId::TypeAliasId(it) => attrs_from_ast_id_loc(db, it),
AttrDefId::GenericParamId(it) => match it {
GenericParamId::ConstParamId(it) => {
let src = it.parent().child_source(db);
// FIXME: We should be never getting `None` here.
return Attrs(match src.value.get(it.local_id()) {
Attrs(match src.value.get(it.local_id()) {
Some(val) => RawAttrs::new_expanded(
db,
val,
db.span_map(src.file_id).as_ref(),
def.krate(db).cfg_options(db),
),
None => RawAttrs::EMPTY,
});
})
}
GenericParamId::TypeParamId(it) => {
let src = it.parent().child_source(db);
// FIXME: We should be never getting `None` here.
return Attrs(match src.value.get(it.local_id()) {
Attrs(match src.value.get(it.local_id()) {
Some(val) => RawAttrs::new_expanded(
db,
val,
db.span_map(src.file_id).as_ref(),
def.krate(db).cfg_options(db),
),
None => RawAttrs::EMPTY,
});
})
}
GenericParamId::LifetimeParamId(it) => {
let src = it.parent.child_source(db);
// FIXME: We should be never getting `None` here.
return Attrs(match src.value.get(it.local_id) {
Attrs(match src.value.get(it.local_id) {
Some(val) => RawAttrs::new_expanded(
db,
val,
db.span_map(src.file_id).as_ref(),
def.krate(db).cfg_options(db),
),
None => RawAttrs::EMPTY,
});
})
}
},
AttrDefId::ExternBlockId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::ExternCrateId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::UseId(it) => attrs_from_item_tree_loc(db, it),
};

let attrs = raw_attrs.expand_cfg_attr(db, def.krate(db));
Attrs(attrs)
AttrDefId::ExternBlockId(it) => attrs_from_ast_id_loc(db, it),
AttrDefId::ExternCrateId(it) => attrs_from_ast_id_loc(db, it),
AttrDefId::UseId(it) => attrs_from_ast_id_loc(db, it),
}
}

pub fn source_map(&self, db: &dyn DefDatabase) -> AttrSourceMap {
Expand Down Expand Up @@ -787,14 +812,15 @@ fn any_has_attrs<'db>(
id.lookup(db).source(db).map(ast::AnyHasAttrs::new)
}

fn attrs_from_item_tree_loc<'db, N: ItemTreeNode>(
fn attrs_from_ast_id_loc<'db, N: AstIdNode + HasAttrs>(
db: &(dyn DefDatabase + 'db),
lookup: impl Lookup<Database = dyn DefDatabase, Data = impl ItemTreeLoc<Id = N>>,
) -> RawAttrs {
let id = lookup.lookup(db).item_tree_id();
let tree = id.item_tree(db);
let attr_owner = N::attr_owner(id.value);
tree.raw_attrs(attr_owner).clone()
lookup: impl Lookup<Database = dyn DefDatabase, Data = impl AstIdLoc<Ast = N> + HasModule>,
) -> Attrs {
let loc = lookup.lookup(db);
let source = loc.source(db);
let span_map = db.span_map(source.file_id);
let cfg_options = loc.krate(db).cfg_options(db);
Attrs(RawAttrs::new_expanded(db, &source.value, span_map.as_ref(), cfg_options))
}

pub(crate) fn fields_attrs_source_map(
Expand Down
39 changes: 18 additions & 21 deletions crates/hir-def/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! Defines database & queries for name resolution.
use base_db::{Crate, RootQueryDb, SourceDatabase};
use either::Either;
use hir_expand::{EditionedFileId, HirFileId, MacroCallId, MacroDefId, db::ExpandDatabase};
use intern::sym;
use hir_expand::{
EditionedFileId, HirFileId, InFile, Lookup, MacroCallId, MacroDefId, MacroDefKind,
db::ExpandDatabase,
};
use intern::{Symbol, sym};
use la_arena::ArenaMap;
use syntax::{AstPtr, ast};
use thin_vec::ThinVec;
Expand All @@ -11,8 +14,8 @@ use triomphe::Arc;
use crate::{
AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, EnumVariantId,
EnumVariantLoc, ExternBlockId, ExternBlockLoc, ExternCrateId, ExternCrateLoc, FunctionId,
FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalFieldId, Macro2Id, Macro2Loc, MacroId,
MacroRulesId, MacroRulesLoc, MacroRulesLocFlags, ProcMacroId, ProcMacroLoc, StaticId,
FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalFieldId, Macro2Id, Macro2Loc, MacroExpander,
MacroId, MacroRulesId, MacroRulesLoc, MacroRulesLocFlags, ProcMacroId, ProcMacroLoc, StaticId,
StaticLoc, StructId, StructLoc, TraitAliasId, TraitAliasLoc, TraitId, TraitLoc, TypeAliasId,
TypeAliasLoc, UnionId, UnionLoc, UseId, UseLoc, VariantId,
attr::{Attrs, AttrsWithOwner},
Expand Down Expand Up @@ -123,6 +126,8 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + SourceDatabase {
id: VariantId,
) -> (Arc<VariantFields>, Arc<ExpressionStoreSourceMap>);

// FIXME: Should we make this transparent? The only unstable thing in `enum_variants_with_diagnostics()`
// is ast ids, and ast ids are pretty stable now.
#[salsa::tracked]
fn enum_variants(&self, id: EnumId) -> Arc<EnumVariants> {
self.enum_variants_with_diagnostics(id).0
Expand Down Expand Up @@ -263,6 +268,9 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + SourceDatabase {
e: TypeAliasId,
) -> (Arc<TypeAliasSignature>, Arc<ExpressionStoreSourceMap>);

#[salsa::invoke(crate::signatures::extern_block_abi_query)]
fn extern_block_abi(&self, extern_block: ExternBlockId) -> Option<Symbol>;

// endregion:data

#[salsa::invoke(Body::body_with_source_map_query)]
Expand Down Expand Up @@ -334,6 +342,9 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + SourceDatabase {
#[salsa::invoke(visibility::type_alias_visibility_query)]
fn type_alias_visibility(&self, def: TypeAliasId) -> Visibility;

#[salsa::invoke(visibility::trait_visibility_query)]
fn trait_visibility(&self, def: TraitId) -> Visibility;

// endregion:visibilities

#[salsa::invoke(crate::lang_item::notable_traits_in_deps)]
Expand Down Expand Up @@ -399,10 +410,6 @@ fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: Crate) -> bool {
}

fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
use hir_expand::InFile;

use crate::{Lookup, MacroDefKind, MacroExpander};

let kind = |expander, file_id, m| {
let in_file = InFile::new(file_id, m);
match expander {
Expand All @@ -418,11 +425,9 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
MacroId::Macro2Id(it) => {
let loc: Macro2Loc = it.lookup(db);

let item_tree = loc.id.item_tree(db);
let makro = &item_tree[loc.id.value];
MacroDefId {
krate: loc.container.krate,
kind: kind(loc.expander, loc.id.file_id(), makro.ast_id.upcast()),
kind: kind(loc.expander, loc.id.file_id, loc.id.value.upcast()),
local_inner: false,
allow_internal_unsafe: loc.allow_internal_unsafe,
edition: loc.edition,
Expand All @@ -431,11 +436,9 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
MacroId::MacroRulesId(it) => {
let loc: MacroRulesLoc = it.lookup(db);

let item_tree = loc.id.item_tree(db);
let makro = &item_tree[loc.id.value];
MacroDefId {
krate: loc.container.krate,
kind: kind(loc.expander, loc.id.file_id(), makro.ast_id.upcast()),
kind: kind(loc.expander, loc.id.file_id, loc.id.value.upcast()),
local_inner: loc.flags.contains(MacroRulesLocFlags::LOCAL_INNER),
allow_internal_unsafe: loc
.flags
Expand All @@ -446,15 +449,9 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
MacroId::ProcMacroId(it) => {
let loc = it.lookup(db);

let item_tree = loc.id.item_tree(db);
let makro = &item_tree[loc.id.value];
MacroDefId {
krate: loc.container.krate,
kind: MacroDefKind::ProcMacro(
InFile::new(loc.id.file_id(), makro.ast_id),
loc.expander,
loc.kind,
),
kind: MacroDefKind::ProcMacro(loc.id, loc.expander, loc.kind),
local_inner: false,
allow_internal_unsafe: false,
edition: loc.edition,
Expand Down
Loading