Auto-attach database in Analysis calls

This commit is contained in:
Lukas Wirth 2025-08-16 16:26:30 +02:00
parent becf04b67a
commit aed0fec1a9
12 changed files with 137 additions and 101 deletions

View file

@ -10,7 +10,7 @@ use hir::{
};
use ide_db::{
RootDatabase, SymbolKind,
base_db::{AnchoredPath, SourceDatabase, salsa},
base_db::{AnchoredPath, SourceDatabase},
defs::{Definition, IdentClass},
famous_defs::FamousDefs,
helpers::pick_best_token,
@ -108,7 +108,7 @@ pub(crate) fn goto_definition(
}
Some(
salsa::attach(sema.db, || IdentClass::classify_node(sema, &parent))?
IdentClass::classify_node(sema, &parent)?
.definitions()
.into_iter()
.flat_map(|(def, _)| {

View file

@ -12,7 +12,6 @@ use hir::{
};
use ide_db::{
FileRange, FxIndexSet, Ranker, RootDatabase,
base_db::salsa,
defs::{Definition, IdentClass, NameRefClass, OperatorClass},
famous_defs::FamousDefs,
helpers::pick_best_token,
@ -137,20 +136,18 @@ pub(crate) fn hover(
let edition =
sema.attach_first_edition(file_id).map(|it| it.edition(db)).unwrap_or(Edition::CURRENT);
let display_target = sema.first_crate(file_id)?.to_display_target(db);
let mut res = salsa::attach(sema.db, || {
if range.is_empty() {
hover_offset(
sema,
FilePosition { file_id, offset: range.start() },
file,
config,
edition,
display_target,
)
} else {
hover_ranged(sema, frange, file, config, edition, display_target)
}
})?;
let mut res = if range.is_empty() {
hover_offset(
sema,
FilePosition { file_id, offset: range.start() },
file,
config,
edition,
display_target,
)
} else {
hover_ranged(sema, frange, file, config, edition, display_target)
}?;
if let HoverDocFormat::PlainText = config.format {
res.info.markup = remove_markdown(res.info.markup.as_str()).into();
@ -293,7 +290,7 @@ fn hover_offset(
.into_iter()
.unique_by(|&((def, _), _, _, _)| def)
.map(|((def, subst), macro_arm, hovered_definition, node)| {
salsa::attach(sema.db, || hover_for_definition(
hover_for_definition(
sema,
file_id,
def,
@ -304,7 +301,7 @@ fn hover_offset(
config,
edition,
display_target,
))
)
})
.collect::<Vec<_>>(),
)
@ -583,13 +580,11 @@ fn goto_type_action_for_def(
});
}
salsa::attach(db, || {
if let Ok(generic_def) = GenericDef::try_from(def) {
generic_def.type_or_const_params(db).into_iter().for_each(|it| {
walk_and_push_ty(db, &it.ty(db), &mut push_new_def);
});
}
});
if let Ok(generic_def) = GenericDef::try_from(def) {
generic_def.type_or_const_params(db).into_iter().for_each(|it| {
walk_and_push_ty(db, &it.ty(db), &mut push_new_def);
});
}
let ty = match def {
Definition::Local(it) => Some(it.ty(db)),

View file

@ -10,7 +10,6 @@ use hir::{
};
use ide_db::{
RootDatabase,
base_db::salsa,
defs::Definition,
documentation::{DocsRangeMap, HasDocs},
famous_defs::FamousDefs,
@ -45,7 +44,7 @@ pub(super) fn type_info_of(
Either::Left(expr) => sema.type_of_expr(expr)?,
Either::Right(pat) => sema.type_of_pat(pat)?,
};
salsa::attach(sema.db, || type_info(sema, _config, ty_info, edition, display_target))
type_info(sema, _config, ty_info, edition, display_target)
}
pub(super) fn closure_expr(
@ -912,7 +911,7 @@ pub(super) fn literal(
};
let ty = ty.display(sema.db, display_target);
let mut s = salsa::attach(sema.db, || format!("```rust\n{ty}\n```\n___\n\n"));
let mut s = format!("```rust\n{ty}\n```\n___\n\n");
match value {
Ok(value) => {
let backtick_len = value.chars().filter(|c| *c == '`').count();

View file

@ -62,7 +62,7 @@ use std::panic::{AssertUnwindSafe, UnwindSafe};
use cfg::CfgOptions;
use fetch_crates::CrateInfo;
use hir::{ChangeWithProcMacros, EditionedFileId, crate_def_map, sym};
use hir::{ChangeWithProcMacros, EditionedFileId, crate_def_map, db::HirDatabase, sym};
use ide_db::{
FxHashMap, FxIndexSet, LineIndexDatabase,
base_db::{
@ -478,10 +478,12 @@ impl Analysis {
/// Fuzzy searches for a symbol.
pub fn symbol_search(&self, query: Query, limit: usize) -> Cancellable<Vec<NavigationTarget>> {
self.with_db(|db| {
symbol_index::world_symbols(db, query)
.into_iter() // xx: should we make this a par iter?
.filter_map(|s| s.try_to_nav(db))
// `world_symbols` currently clones the database to run stuff in parallel, which will make any query panic
// if we were to attach it here.
Cancelled::catch(|| {
symbol_index::world_symbols(&self.db, query)
.into_iter()
.filter_map(|s| s.try_to_nav(&self.db))
.take(limit)
.map(UpmappingResult::call_site)
.collect::<Vec<_>>()
@ -660,15 +662,6 @@ impl Analysis {
})
}
/// Computes syntax highlighting for the given file
pub fn highlight(
&self,
highlight_config: HighlightConfig,
file_id: FileId,
) -> Cancellable<Vec<HlRange>> {
self.with_db(|db| syntax_highlighting::highlight(db, highlight_config, file_id, None))
}
/// Computes all ranges to highlight for a given item in a file.
pub fn highlight_related(
&self,
@ -682,20 +675,42 @@ impl Analysis {
})
}
/// Computes syntax highlighting for the given file
pub fn highlight(
&self,
highlight_config: HighlightConfig,
file_id: FileId,
) -> Cancellable<Vec<HlRange>> {
// highlighting may construct a new database for "speculative" execution, so we can't currently attach the database
// highlighting instead sets up the attach hook where neceesary for the trait solver
Cancelled::catch(|| {
syntax_highlighting::highlight(&self.db, highlight_config, file_id, None)
})
}
/// Computes syntax highlighting for the given file range.
pub fn highlight_range(
&self,
highlight_config: HighlightConfig,
frange: FileRange,
) -> Cancellable<Vec<HlRange>> {
self.with_db(|db| {
syntax_highlighting::highlight(db, highlight_config, frange.file_id, Some(frange.range))
// highlighting may construct a new database for "speculative" execution, so we can't currently attach the database
// highlighting instead sets up the attach hook where neceesary for the trait solver
Cancelled::catch(|| {
syntax_highlighting::highlight(
&self.db,
highlight_config,
frange.file_id,
Some(frange.range),
)
})
}
/// Computes syntax highlighting for the given file.
pub fn highlight_as_html(&self, file_id: FileId, rainbow: bool) -> Cancellable<String> {
self.with_db(|db| syntax_highlighting::highlight_as_html(db, file_id, rainbow))
// highlighting may construct a new database for "speculative" execution, so we can't currently attach the database
// highlighting instead sets up the attach hook where neceesary for the trait solver
Cancelled::catch(|| syntax_highlighting::highlight_as_html(&self.db, file_id, rainbow))
}
/// Computes completions at the given position.
@ -873,8 +888,12 @@ impl Analysis {
where
F: FnOnce(&RootDatabase) -> T + std::panic::UnwindSafe,
{
let snap = self.db.clone();
Cancelled::catch(|| f(&snap))
salsa::attach(&self.db, || {
// the trait solver code may invoke `as_view<HirDatabase>` outside of queries,
// so technically we might run into a panic in salsa if the downcaster has not yet been registered.
HirDatabase::zalsa_register_downcaster(&self.db);
Cancelled::catch(|| f(&self.db))
})
}
}