Skip to content

Commit 2ffa022

Browse files
authored
Merge pull request rust-lang#20470 from rust-lang/veykril/push-wppxsntzqtou
Auto-attach database in `Analysis` calls
2 parents af8988c + b9d225b commit 2ffa022

File tree

12 files changed

+137
-101
lines changed

12 files changed

+137
-101
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/next_solver/generic_arg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'db> GenericArg<'db> {
4646
pub fn expect_ty(self) -> Ty<'db> {
4747
match self.kind() {
4848
GenericArgKind::Type(ty) => ty,
49-
_ => panic!("Expected ty, got {:?}", self),
49+
_ => panic!("Expected ty, got {self:?}"),
5050
}
5151
}
5252

src/tools/rust-analyzer/crates/ide-assists/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ mod tests;
6767
pub mod utils;
6868

6969
use hir::Semantics;
70-
use ide_db::{EditionedFileId, RootDatabase, base_db::salsa};
70+
use ide_db::{EditionedFileId, RootDatabase};
7171
use syntax::{Edition, TextRange};
7272

7373
pub(crate) use crate::assist_context::{AssistContext, Assists};
@@ -93,11 +93,8 @@ pub fn assists(
9393
.unwrap_or_else(|| EditionedFileId::new(db, range.file_id, Edition::CURRENT));
9494
let ctx = AssistContext::new(sema, config, hir::FileRange { file_id, range: range.range });
9595
let mut acc = Assists::new(&ctx, resolve);
96-
// the handlers may invoke trait solving related things which accesses salsa structs outside queries
97-
salsa::attach(db, || {
98-
handlers::all().iter().for_each(|handler| {
99-
handler(&mut acc, &ctx);
100-
});
96+
handlers::all().iter().for_each(|handler| {
97+
handler(&mut acc, &ctx);
10198
});
10299
acc.finish()
103100
}

src/tools/rust-analyzer/crates/ide-assists/src/tests.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod generated;
22

33
use expect_test::expect;
4-
use hir::{Semantics, setup_tracing};
4+
use hir::{Semantics, db::HirDatabase, setup_tracing};
55
use ide_db::{
66
EditionedFileId, FileRange, RootDatabase, SnippetCap,
77
assists::ExprFillDefaultMode,
@@ -16,7 +16,7 @@ use test_utils::{assert_eq_text, extract_offset};
1616

1717
use crate::{
1818
Assist, AssistConfig, AssistContext, AssistKind, AssistResolveStrategy, Assists, SingleResolve,
19-
assists, handlers::Handler,
19+
handlers::Handler,
2020
};
2121

2222
pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
@@ -103,6 +103,18 @@ pub(crate) const TEST_CONFIG_IMPORT_ONE: AssistConfig = AssistConfig {
103103
prefer_self_ty: false,
104104
};
105105

106+
fn assists(
107+
db: &RootDatabase,
108+
config: &AssistConfig,
109+
resolve: AssistResolveStrategy,
110+
range: ide_db::FileRange,
111+
) -> Vec<Assist> {
112+
salsa::attach(db, || {
113+
HirDatabase::zalsa_register_downcaster(db);
114+
crate::assists(db, config, resolve, range)
115+
})
116+
}
117+
106118
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, EditionedFileId) {
107119
RootDatabase::with_single_file(text)
108120
}
@@ -320,6 +332,7 @@ fn check_with_config(
320332
};
321333
let mut acc = Assists::new(&ctx, resolve);
322334
salsa::attach(&db, || {
335+
HirDatabase::zalsa_register_downcaster(&db);
323336
handler(&mut acc, &ctx);
324337
});
325338
let mut res = acc.finish();

src/tools/rust-analyzer/crates/ide-completion/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ mod snippet;
1010
#[cfg(test)]
1111
mod tests;
1212

13-
use base_db::salsa;
1413
use ide_db::{
1514
FilePosition, FxHashSet, RootDatabase,
1615
imports::insert_use::{self, ImportScope},
@@ -229,7 +228,7 @@ pub fn completions(
229228
{
230229
let acc = &mut completions;
231230

232-
salsa::attach(db, || match analysis {
231+
match analysis {
233232
CompletionAnalysis::Name(name_ctx) => completions::complete_name(acc, ctx, name_ctx),
234233
CompletionAnalysis::NameRef(name_ref_ctx) => {
235234
completions::complete_name_ref(acc, ctx, name_ref_ctx)
@@ -257,7 +256,7 @@ pub fn completions(
257256
);
258257
}
259258
CompletionAnalysis::UnexpandedAttrTT { .. } | CompletionAnalysis::String { .. } => (),
260-
})
259+
}
261260
}
262261

263262
Some(completions.into())

src/tools/rust-analyzer/crates/ide-completion/src/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mod type_pos;
2424
mod use_tree;
2525
mod visibility;
2626

27-
use base_db::SourceDatabase;
27+
use base_db::{SourceDatabase, salsa};
2828
use expect_test::Expect;
2929
use hir::{PrefixKind, setup_tracing};
3030
use ide_db::{
@@ -243,7 +243,7 @@ pub(crate) fn check_edit_with_config(
243243
let ra_fixture_after = trim_indent(ra_fixture_after);
244244
let (db, position) = position(ra_fixture_before);
245245
let completions: Vec<CompletionItem> =
246-
crate::completions(&db, &config, position, None).unwrap();
246+
salsa::attach(&db, || crate::completions(&db, &config, position, None).unwrap());
247247
let (completion,) = completions
248248
.iter()
249249
.filter(|it| it.lookup() == what)
@@ -306,7 +306,7 @@ pub(crate) fn get_all_items(
306306
trigger_character: Option<char>,
307307
) -> Vec<CompletionItem> {
308308
let (db, position) = position(code);
309-
let res = crate::completions(&db, &config, position, trigger_character)
309+
let res = salsa::attach(&db, || crate::completions(&db, &config, position, trigger_character))
310310
.map_or_else(Vec::default, Into::into);
311311
// validate
312312
res.iter().for_each(|it| {

src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ use hir::{
9292
use ide_db::{
9393
EditionedFileId, FileId, FileRange, FxHashMap, FxHashSet, RootDatabase, Severity, SnippetCap,
9494
assists::{Assist, AssistId, AssistResolveStrategy, ExprFillDefaultMode},
95-
base_db::{ReleaseChannel, RootQueryDb as _, salsa},
95+
base_db::{ReleaseChannel, RootQueryDb as _},
9696
generated::lints::{CLIPPY_LINT_GROUPS, DEFAULT_LINT_GROUPS, DEFAULT_LINTS, Lint, LintGroup},
9797
imports::insert_use::InsertUseConfig,
9898
label::Label,
@@ -537,12 +537,10 @@ pub fn full_diagnostics(
537537
resolve: &AssistResolveStrategy,
538538
file_id: FileId,
539539
) -> Vec<Diagnostic> {
540-
salsa::attach(db, || {
541-
let mut res = syntax_diagnostics(db, config, file_id);
542-
let sema = semantic_diagnostics(db, config, resolve, file_id);
543-
res.extend(sema);
544-
res
545-
})
540+
let mut res = syntax_diagnostics(db, config, file_id);
541+
let sema = semantic_diagnostics(db, config, resolve, file_id);
542+
res.extend(sema);
543+
res
546544
}
547545

548546
/// Returns whether to keep this diagnostic (or remove it).

src/tools/rust-analyzer/crates/ide-diagnostics/src/tests.rs

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hir::setup_tracing;
66
use ide_db::{
77
LineIndexDatabase, RootDatabase,
88
assists::{AssistResolveStrategy, ExprFillDefaultMode},
9-
base_db::SourceDatabase,
9+
base_db::{SourceDatabase, salsa},
1010
};
1111
use itertools::Itertools;
1212
use stdx::trim_indent;
@@ -74,14 +74,16 @@ fn check_nth_fix_with_config(
7474
let after = trim_indent(ra_fixture_after);
7575

7676
let (db, file_position) = RootDatabase::with_position(ra_fixture_before);
77-
let diagnostic = super::full_diagnostics(
78-
&db,
79-
&config,
80-
&AssistResolveStrategy::All,
81-
file_position.file_id.file_id(&db),
82-
)
83-
.pop()
84-
.expect("no diagnostics");
77+
let diagnostic = salsa::attach(&db, || {
78+
super::full_diagnostics(
79+
&db,
80+
&config,
81+
&AssistResolveStrategy::All,
82+
file_position.file_id.file_id(&db),
83+
)
84+
.pop()
85+
.expect("no diagnostics")
86+
});
8587
let fix = &diagnostic
8688
.fixes
8789
.unwrap_or_else(|| panic!("{:?} diagnostic misses fixes", diagnostic.code))[nth];
@@ -127,12 +129,14 @@ pub(crate) fn check_has_fix(
127129
let (db, file_position) = RootDatabase::with_position(ra_fixture_before);
128130
let mut conf = DiagnosticsConfig::test_sample();
129131
conf.expr_fill_default = ExprFillDefaultMode::Default;
130-
let fix = super::full_diagnostics(
131-
&db,
132-
&conf,
133-
&AssistResolveStrategy::All,
134-
file_position.file_id.file_id(&db),
135-
)
132+
let fix = salsa::attach(&db, || {
133+
super::full_diagnostics(
134+
&db,
135+
&conf,
136+
&AssistResolveStrategy::All,
137+
file_position.file_id.file_id(&db),
138+
)
139+
})
136140
.into_iter()
137141
.find(|d| {
138142
d.fixes
@@ -166,12 +170,14 @@ pub(crate) fn check_has_fix(
166170
/// Checks that there's a diagnostic *without* fix at `$0`.
167171
pub(crate) fn check_no_fix(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
168172
let (db, file_position) = RootDatabase::with_position(ra_fixture);
169-
let diagnostic = super::full_diagnostics(
170-
&db,
171-
&DiagnosticsConfig::test_sample(),
172-
&AssistResolveStrategy::All,
173-
file_position.file_id.file_id(&db),
174-
)
173+
let diagnostic = salsa::attach(&db, || {
174+
super::full_diagnostics(
175+
&db,
176+
&DiagnosticsConfig::test_sample(),
177+
&AssistResolveStrategy::All,
178+
file_position.file_id.file_id(&db),
179+
)
180+
})
175181
.pop()
176182
.unwrap();
177183
assert!(diagnostic.fixes.is_none(), "got a fix when none was expected: {diagnostic:?}");
@@ -206,7 +212,13 @@ pub(crate) fn check_diagnostics_with_config(
206212
.iter()
207213
.copied()
208214
.flat_map(|file_id| {
209-
super::full_diagnostics(&db, &config, &AssistResolveStrategy::All, file_id.file_id(&db))
215+
salsa::attach(&db, || {
216+
super::full_diagnostics(
217+
&db,
218+
&config,
219+
&AssistResolveStrategy::All,
220+
file_id.file_id(&db),
221+
)
210222
.into_iter()
211223
.map(|d| {
212224
let mut annotation = String::new();
@@ -224,6 +236,7 @@ pub(crate) fn check_diagnostics_with_config(
224236
annotation.push_str(&d.message);
225237
(d.range, annotation)
226238
})
239+
})
227240
})
228241
.map(|(diagnostic, annotation)| (diagnostic.file_id, (diagnostic.range, annotation)))
229242
.into_group_map();
@@ -275,15 +288,19 @@ fn test_disabled_diagnostics() {
275288
let (db, file_id) = RootDatabase::with_single_file(r#"mod foo;"#);
276289
let file_id = file_id.file_id(&db);
277290

278-
let diagnostics = super::full_diagnostics(&db, &config, &AssistResolveStrategy::All, file_id);
291+
let diagnostics = salsa::attach(&db, || {
292+
super::full_diagnostics(&db, &config, &AssistResolveStrategy::All, file_id)
293+
});
279294
assert!(diagnostics.is_empty());
280295

281-
let diagnostics = super::full_diagnostics(
282-
&db,
283-
&DiagnosticsConfig::test_sample(),
284-
&AssistResolveStrategy::All,
285-
file_id,
286-
);
296+
let diagnostics = salsa::attach(&db, || {
297+
super::full_diagnostics(
298+
&db,
299+
&DiagnosticsConfig::test_sample(),
300+
&AssistResolveStrategy::All,
301+
file_id,
302+
)
303+
});
287304
assert!(!diagnostics.is_empty());
288305
}
289306

src/tools/rust-analyzer/crates/ide/src/goto_definition.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use hir::{
1010
};
1111
use ide_db::{
1212
RootDatabase, SymbolKind,
13-
base_db::{AnchoredPath, SourceDatabase, salsa},
13+
base_db::{AnchoredPath, SourceDatabase},
1414
defs::{Definition, IdentClass},
1515
famous_defs::FamousDefs,
1616
helpers::pick_best_token,
@@ -108,7 +108,7 @@ pub(crate) fn goto_definition(
108108
}
109109

110110
Some(
111-
salsa::attach(sema.db, || IdentClass::classify_node(sema, &parent))?
111+
IdentClass::classify_node(sema, &parent)?
112112
.definitions()
113113
.into_iter()
114114
.flat_map(|(def, _)| {

src/tools/rust-analyzer/crates/ide/src/hover.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use hir::{
1212
};
1313
use ide_db::{
1414
FileRange, FxIndexSet, Ranker, RootDatabase,
15-
base_db::salsa,
1615
defs::{Definition, IdentClass, NameRefClass, OperatorClass},
1716
famous_defs::FamousDefs,
1817
helpers::pick_best_token,
@@ -137,20 +136,18 @@ pub(crate) fn hover(
137136
let edition =
138137
sema.attach_first_edition(file_id).map(|it| it.edition(db)).unwrap_or(Edition::CURRENT);
139138
let display_target = sema.first_crate(file_id)?.to_display_target(db);
140-
let mut res = salsa::attach(sema.db, || {
141-
if range.is_empty() {
142-
hover_offset(
143-
sema,
144-
FilePosition { file_id, offset: range.start() },
145-
file,
146-
config,
147-
edition,
148-
display_target,
149-
)
150-
} else {
151-
hover_ranged(sema, frange, file, config, edition, display_target)
152-
}
153-
})?;
139+
let mut res = if range.is_empty() {
140+
hover_offset(
141+
sema,
142+
FilePosition { file_id, offset: range.start() },
143+
file,
144+
config,
145+
edition,
146+
display_target,
147+
)
148+
} else {
149+
hover_ranged(sema, frange, file, config, edition, display_target)
150+
}?;
154151

155152
if let HoverDocFormat::PlainText = config.format {
156153
res.info.markup = remove_markdown(res.info.markup.as_str()).into();
@@ -293,7 +290,7 @@ fn hover_offset(
293290
.into_iter()
294291
.unique_by(|&((def, _), _, _, _)| def)
295292
.map(|((def, subst), macro_arm, hovered_definition, node)| {
296-
salsa::attach(sema.db, || hover_for_definition(
293+
hover_for_definition(
297294
sema,
298295
file_id,
299296
def,
@@ -304,7 +301,7 @@ fn hover_offset(
304301
config,
305302
edition,
306303
display_target,
307-
))
304+
)
308305
})
309306
.collect::<Vec<_>>(),
310307
)
@@ -583,13 +580,11 @@ fn goto_type_action_for_def(
583580
});
584581
}
585582

586-
salsa::attach(db, || {
587-
if let Ok(generic_def) = GenericDef::try_from(def) {
588-
generic_def.type_or_const_params(db).into_iter().for_each(|it| {
589-
walk_and_push_ty(db, &it.ty(db), &mut push_new_def);
590-
});
591-
}
592-
});
583+
if let Ok(generic_def) = GenericDef::try_from(def) {
584+
generic_def.type_or_const_params(db).into_iter().for_each(|it| {
585+
walk_and_push_ty(db, &it.ty(db), &mut push_new_def);
586+
});
587+
}
593588

594589
let ty = match def {
595590
Definition::Local(it) => Some(it.ty(db)),

0 commit comments

Comments
 (0)