Skip to content

Commit 57353c7

Browse files
committed
internal: flatten/deduplicate base_db::EditionedFileId
1 parent 926d757 commit 57353c7

File tree

35 files changed

+54
-86
lines changed

35 files changed

+54
-86
lines changed

crates/base-db/src/lib.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,21 @@ impl Files {
157157

158158
#[salsa::interned(no_lifetime)]
159159
pub struct EditionedFileId {
160-
pub file_id: vfs::FileId,
161160
pub editioned_file_id: span::EditionedFileId,
162161
}
163162

163+
impl EditionedFileId {
164+
pub fn file_id(&self, db: &dyn salsa::Database) -> vfs::FileId {
165+
let id = self.editioned_file_id(db);
166+
id.file_id()
167+
}
168+
169+
fn unpack(&self, db: &dyn salsa::Database) -> (vfs::FileId, span::Edition) {
170+
let id = self.editioned_file_id(db);
171+
(id.file_id(), id.edition())
172+
}
173+
}
174+
164175
#[salsa::input]
165176
pub struct FileText {
166177
pub text: Arc<str>,
@@ -267,9 +278,8 @@ fn toolchain_channel(db: &dyn RootQueryDb, krate: CrateId) -> Option<ReleaseChan
267278

268279
fn parse(db: &dyn RootQueryDb, file_id: EditionedFileId) -> Parse<ast::SourceFile> {
269280
let _p = tracing::info_span!("parse", ?file_id).entered();
270-
let (text, editioned_file_id) =
271-
(db.file_text(file_id.file_id(db)).text(db), file_id.editioned_file_id(db));
272-
let (_, edition) = editioned_file_id.unpack();
281+
let (file_id, edition) = file_id.unpack(db.as_dyn_database());
282+
let text = db.file_text(file_id).text(db);
273283
ast::SourceFile::parse(&text, edition)
274284
}
275285

crates/hir-def/src/expr_store/scope.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ mod tests {
363363

364364
let (file_id, _) = editioned_file_id.unpack();
365365
let editioned_file_id_wrapper =
366-
base_db::EditionedFileId::new(db.as_dyn_database(), file_id, editioned_file_id);
366+
base_db::EditionedFileId::new(db.as_dyn_database(), editioned_file_id);
367367

368368
let file_syntax = db.parse(editioned_file_id_wrapper).syntax_node();
369369
let marker: ast::PathExpr = find_node_at_offset(&file_syntax, offset).unwrap();
@@ -521,7 +521,7 @@ fn foo() {
521521

522522
let (file_id, _) = editioned_file_id.unpack();
523523
let file_id_wrapper =
524-
base_db::EditionedFileId::new(db.as_dyn_database(), file_id, editioned_file_id);
524+
base_db::EditionedFileId::new(db.as_dyn_database(), editioned_file_id);
525525

526526
let file = db.parse(file_id_wrapper).ok().unwrap();
527527
let expected_name = find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into())

crates/hir-def/src/macro_expansion_tests/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ fn check_errors(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect)
6767

6868
let editioned_file_id =
6969
ast_id.file_id.file_id().expect("unable to get FileId; this is a bug");
70-
let (file_id, _) = editioned_file_id.unpack();
71-
let editioned_file_id_wrapper =
72-
base_db::EditionedFileId::new(db.as_dyn_database(), file_id, editioned_file_id);
70+
let editioned_file_id =
71+
base_db::EditionedFileId::new(db.as_dyn_database(), editioned_file_id);
7372

74-
let ast = db.parse(editioned_file_id_wrapper).syntax_node();
73+
let ast = db.parse(editioned_file_id).syntax_node();
7574
let ast_id_map = db.ast_id_map(ast_id.file_id);
7675
let node = ast_id_map.get_erased(ast_id.value).to_node(&ast);
7776
Some((node.text_range(), errors))

crates/hir-def/src/nameres.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,7 @@ impl ModuleOrigin {
297297
match self {
298298
&ModuleOrigin::File { definition: editioned_file_id, .. }
299299
| &ModuleOrigin::CrateRoot { definition: editioned_file_id } => {
300-
let (file_id, _) = editioned_file_id.unpack();
301-
let definition = base_db::EditionedFileId::new(db, file_id, editioned_file_id);
300+
let definition = base_db::EditionedFileId::new(db, editioned_file_id);
302301

303302
let sf = db.parse(definition).tree();
304303
InFile::new(editioned_file_id.into(), ModuleSource::SourceFile(sf))

crates/hir-def/src/test_db.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,8 @@ impl TestDB {
270270
let source_map = db.body_with_source_map(def_with_body).1;
271271
let scopes = db.expr_scopes(def_with_body);
272272

273-
let (file_id, _) = position.file_id.unpack();
274273
let editioned_file_id_wrapper =
275-
base_db::EditionedFileId::new(db.as_dyn_database(), file_id, position.file_id);
274+
base_db::EditionedFileId::new(db.as_dyn_database(), position.file_id);
276275

277276
let root = db.parse(editioned_file_id_wrapper);
278277
let scope_iter = algo::ancestors_at_offset(&root.syntax_node(), position.offset)

crates/hir-expand/src/builtin/fn_macro.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -728,11 +728,9 @@ fn include_expand(
728728
tt: &tt::TopSubtree,
729729
span: Span,
730730
) -> ExpandResult<tt::TopSubtree> {
731-
let (file_id, editioned_file_id) = match include_input_to_file_id(db, arg_id, tt) {
731+
let (file_id_wrapper, editioned_file_id) = match include_input_to_file_id(db, arg_id, tt) {
732732
Ok(editioned_file_id) => {
733-
let (file_id, _) = editioned_file_id.unpack();
734-
let file_id = base_db::EditionedFileId::new(db, file_id, editioned_file_id);
735-
(file_id, editioned_file_id)
733+
(base_db::EditionedFileId::new(db, editioned_file_id), editioned_file_id)
736734
}
737735
Err(e) => {
738736
return ExpandResult::new(
@@ -744,7 +742,7 @@ fn include_expand(
744742
let span_map = db.real_span_map(editioned_file_id);
745743
// FIXME: Parse errors
746744
ExpandResult::ok(syntax_node_to_token_tree(
747-
&db.parse(file_id).syntax_node(),
745+
&db.parse(file_id_wrapper).syntax_node(),
748746
SpanMap::RealSpanMap(span_map),
749747
span,
750748
syntax_bridge::DocCommentDesugarMode::ProcMacro,

crates/hir-expand/src/db.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,7 @@ fn ast_id_map(db: &dyn ExpandDatabase, file_id: span::HirFileId) -> triomphe::Ar
369369
fn parse_or_expand(db: &dyn ExpandDatabase, file_id: HirFileId) -> SyntaxNode {
370370
match file_id.repr() {
371371
HirFileIdRepr::FileId(editioned_file_id) => {
372-
let (file_id, _) = editioned_file_id.unpack();
373-
let file_id = base_db::EditionedFileId::new(db, file_id, editioned_file_id);
372+
let file_id = base_db::EditionedFileId::new(db, editioned_file_id);
374373
db.parse(file_id).syntax_node()
375374
}
376375

@@ -432,8 +431,7 @@ pub(crate) fn parse_with_map(
432431
) -> (Parse<SyntaxNode>, SpanMap) {
433432
match file_id.repr() {
434433
HirFileIdRepr::FileId(editioned_file_id) => {
435-
let (file_id, _) = editioned_file_id.unpack();
436-
let file_id = base_db::EditionedFileId::new(db, file_id, editioned_file_id);
434+
let file_id = base_db::EditionedFileId::new(db, editioned_file_id);
437435

438436
(
439437
db.parse(file_id).to_syntax(),

crates/hir-expand/src/files.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ trait FileIdToSyntax: Copy {
159159

160160
impl FileIdToSyntax for EditionedFileId {
161161
fn file_syntax(self, db: &dyn db::ExpandDatabase) -> SyntaxNode {
162-
let (file_id, _) = self.unpack();
163-
let file_id = base_db::EditionedFileId::new(db, file_id, self);
162+
let file_id = base_db::EditionedFileId::new(db, self);
164163

165164
db.parse(file_id).syntax_node()
166165
}
@@ -293,8 +292,7 @@ impl<SN: Borrow<SyntaxNode>> InFile<SN> {
293292
self.value.borrow().text_range(),
294293
)?;
295294

296-
let (file_id, _) = editioned_file_id.unpack();
297-
let file_id = base_db::EditionedFileId::new(db, file_id, editioned_file_id);
295+
let file_id = base_db::EditionedFileId::new(db, editioned_file_id);
298296

299297
let kind = self.kind();
300298
let value = db
@@ -473,8 +471,7 @@ impl<N: AstNode> InFile<N> {
473471
self.value.syntax().text_range(),
474472
)?;
475473

476-
let (file_id, _) = editioned_file_id.unpack();
477-
let file_id = base_db::EditionedFileId::new(db, file_id, editioned_file_id);
474+
let file_id = base_db::EditionedFileId::new(db, editioned_file_id);
478475

479476
// FIXME: This heuristic is brittle and with the right macro may select completely unrelated nodes?
480477
let anc = db.parse(file_id).syntax_node().covering_element(range);

crates/hir-expand/src/span_map.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ pub(crate) fn real_span_map(
8787
let mut pairs = vec![(syntax::TextSize::new(0), span::ROOT_ERASED_FILE_AST_ID)];
8888
let ast_id_map = db.ast_id_map(editioned_file_id.into());
8989

90-
let (file_id, _) = editioned_file_id.unpack();
91-
let file_id = base_db::EditionedFileId::new(db, file_id, editioned_file_id);
90+
let file_id = base_db::EditionedFileId::new(db, editioned_file_id);
9291

9392
let tree = db.parse(file_id).tree();
9493
// This is an incrementality layer. Basically we can't use absolute ranges for our spans as that

crates/hir/src/semantics.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,7 @@ impl<'db> SemanticsImpl<'db> {
326326
.attach_first_edition(file_id)
327327
.unwrap_or_else(|| EditionedFileId::current_edition(file_id));
328328

329-
let (file_id, _) = editioned_file_id.unpack();
330-
let file_id = base_db::EditionedFileId::new(self.db, file_id, editioned_file_id);
329+
let file_id = base_db::EditionedFileId::new(self.db, editioned_file_id);
331330

332331
let tree = self.db.parse(file_id).tree();
333332
self.cache(tree.syntax().clone(), editioned_file_id.into());
@@ -1876,8 +1875,7 @@ fn macro_call_to_macro_id(
18761875
Either::Left(it) => {
18771876
let node = match it.file_id.repr() {
18781877
HirFileIdRepr::FileId(editioned_file_id) => {
1879-
let (file_id, _) = editioned_file_id.unpack();
1880-
let file_id = base_db::EditionedFileId::new(db, file_id, editioned_file_id);
1878+
let file_id = base_db::EditionedFileId::new(db, editioned_file_id);
18811879

18821880
it.to_ptr(db).to_node(&db.parse(file_id).syntax_node())
18831881
}
@@ -1891,8 +1889,7 @@ fn macro_call_to_macro_id(
18911889
Either::Right(it) => {
18921890
let node = match it.file_id.repr() {
18931891
HirFileIdRepr::FileId(editioned_file_id) => {
1894-
let (file_id, _) = editioned_file_id.unpack();
1895-
let file_id = base_db::EditionedFileId::new(db, file_id, editioned_file_id);
1892+
let file_id = base_db::EditionedFileId::new(db, editioned_file_id);
18961893

18971894
it.to_ptr(db).to_node(&db.parse(file_id).syntax_node())
18981895
}

crates/ide-assists/src/assist_context.rs

-2
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ impl<'a> AssistContext<'a> {
6565
config: &'a AssistConfig,
6666
frange: FileRange,
6767
) -> AssistContext<'a> {
68-
let (file_id, _) = frange.file_id.unpack();
6968
let editioned_file_id = ide_db::base_db::EditionedFileId::new(
7069
sema.db.as_dyn_database(),
71-
file_id,
7270
frange.file_id,
7371
);
7472

crates/ide-assists/src/handlers/add_missing_match_arms.rs

-2
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,8 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
257257
// Just replace the element that the original range came from
258258
let old_place = {
259259
// Find the original element
260-
let file_id = arm_list_range.file_id.file_id();
261260
let editioned_file_id = ide_db::base_db::EditionedFileId::new(
262261
ctx.sema.db.as_dyn_database(),
263-
file_id,
264262
arm_list_range.file_id,
265263
);
266264

crates/ide-assists/src/handlers/extract_module.rs

-2
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ impl Module {
334334
for (file_id, refs) in node_def.usages(&ctx.sema).all() {
335335
let editioned_file_id = ide_db::base_db::EditionedFileId::new(
336336
ctx.sema.db.as_dyn_database(),
337-
file_id.file_id(),
338337
file_id,
339338
);
340339

@@ -467,7 +466,6 @@ impl Module {
467466

468467
let editioned_file_id = ide_db::base_db::EditionedFileId::new(
469468
ctx.sema.db.as_dyn_database(),
470-
file_id.file_id(),
471469
file_id,
472470
);
473471

crates/ide-assists/src/handlers/generate_function.rs

-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ fn get_adt_source(
209209

210210
let editioned_file_id = ide_db::base_db::EditionedFileId::new(
211211
ctx.sema.db.as_dyn_database(),
212-
range.file_id.file_id(),
213212
range.file_id,
214213
);
215214

crates/ide-assists/src/handlers/remove_unused_param.rs

-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ fn process_usages(
106106
) {
107107
let editioned_file_id_wrapper = ide_db::base_db::EditionedFileId::new(
108108
ctx.sema.db.as_dyn_database(),
109-
file_id.file_id(),
110109
file_id,
111110
);
112111

crates/ide-completion/src/context.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,6 @@ impl<'a> CompletionContext<'a> {
709709
let editioned_file_id = sema.attach_first_edition(file_id)?;
710710
let editioned_file_id_wrapper = ide_db::base_db::EditionedFileId::new(
711711
sema.db.as_dyn_database(),
712-
file_id,
713712
editioned_file_id,
714713
);
715714

@@ -719,9 +718,9 @@ impl<'a> CompletionContext<'a> {
719718
// to determine context, though the original_file will be used for
720719
// actual completion.
721720
let file_with_fake_ident = {
722-
let (file_id, edition) = editioned_file_id.unpack();
721+
let (_, edition) = editioned_file_id.unpack();
723722
let file_id_wrapper =
724-
base_db::EditionedFileId::new(db.as_dyn_database(), file_id, editioned_file_id);
723+
base_db::EditionedFileId::new(db.as_dyn_database(), editioned_file_id);
725724

726725
let parse = db.parse(file_id_wrapper);
727726
parse.reparse(TextRange::empty(offset), COMPLETION_MARKER, edition).tree()

crates/ide-completion/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,12 @@ pub fn resolve_completion_edits(
277277
let sema = hir::Semantics::new(db);
278278

279279
let editioned_file_id = sema.attach_first_edition(file_id)?;
280-
let editioned_file_id_wrapper = ide_db::base_db::EditionedFileId::new(
280+
let editioned_file_id = ide_db::base_db::EditionedFileId::new(
281281
db.as_dyn_database(),
282-
editioned_file_id.file_id(),
283282
editioned_file_id,
284283
);
285284

286-
let original_file = sema.parse(editioned_file_id_wrapper);
285+
let original_file = sema.parse(editioned_file_id);
287286
let original_token =
288287
syntax::AstNode::syntax(&original_file).token_at_offset(offset).left_biased()?;
289288
let position_for_import = &original_token.parent()?;

crates/ide-db/src/imports/insert_use/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1253,14 +1253,14 @@ fn check_with_config(
12531253
let (db, file_id, range_or_offset) = RootDatabase::with_range_or_offset(ra_fixture_before);
12541254

12551255
let file_id =
1256-
crate::base_db::EditionedFileId::new(db.as_dyn_database(), file_id.file_id(), file_id);
1256+
crate::base_db::EditionedFileId::new(db.as_dyn_database(), file_id);
12571257

12581258
(db, file_id, Some(range_or_offset))
12591259
} else {
12601260
let (db, file_id) = RootDatabase::with_single_file(ra_fixture_before);
12611261

12621262
let file_id =
1263-
crate::base_db::EditionedFileId::new(db.as_dyn_database(), file_id.file_id(), file_id);
1263+
crate::base_db::EditionedFileId::new(db.as_dyn_database(), file_id);
12641264

12651265
(db, file_id, None)
12661266
};

crates/ide-db/src/search.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ impl<'a> FindUsages<'a> {
653653
FindUsages::scope_files(db, &current_to_process_search_scope)
654654
{
655655
let file_id =
656-
crate::base_db::EditionedFileId::new(db, file_id.file_id(), file_id);
656+
crate::base_db::EditionedFileId::new(db, file_id);
657657

658658
let tree = LazyCell::new(move || sema.parse(file_id).syntax().clone());
659659

@@ -815,7 +815,7 @@ impl<'a> FindUsages<'a> {
815815
) {
816816
for (file_text, file_id, search_range) in files {
817817
let file_id_wrapper =
818-
crate::base_db::EditionedFileId::new(this.sema.db, file_id.file_id(), file_id);
818+
crate::base_db::EditionedFileId::new(this.sema.db, file_id);
819819

820820
let tree = LazyCell::new(move || this.sema.parse(file_id_wrapper).syntax().clone());
821821

@@ -957,7 +957,7 @@ impl<'a> FindUsages<'a> {
957957
self.include_self_kw_refs.as_ref().map(|ty| (ty, Finder::new("Self")));
958958
for (text, file_id, search_range) in Self::scope_files(sema.db, &search_scope) {
959959
let file_id_wrapper =
960-
crate::base_db::EditionedFileId::new(sema.db, file_id.file_id(), file_id);
960+
crate::base_db::EditionedFileId::new(sema.db,file_id);
961961

962962
let tree = LazyCell::new(move || sema.parse(file_id_wrapper).syntax().clone());
963963

@@ -1015,7 +1015,7 @@ impl<'a> FindUsages<'a> {
10151015
self.sema.db.unwind_if_revision_cancelled();
10161016

10171017
let file_id_wrapper =
1018-
crate::base_db::EditionedFileId::new(sema.db, file_id.file_id(), file_id);
1018+
crate::base_db::EditionedFileId::new(sema.db, file_id);
10191019
let tree = LazyCell::new(move || sema.parse(file_id_wrapper).syntax().clone());
10201020

10211021
for offset in Self::match_indices(&text, finder, search_range) {
@@ -1070,7 +1070,7 @@ impl<'a> FindUsages<'a> {
10701070
search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(&*text)));
10711071

10721072
let file_id =
1073-
crate::base_db::EditionedFileId::new(sema.db, file_id.file_id(), file_id);
1073+
crate::base_db::EditionedFileId::new(sema.db, file_id);
10741074

10751075
let tree = LazyCell::new(|| sema.parse(file_id).syntax().clone());
10761076
let finder = &Finder::new("self");

crates/ide-db/src/syntax_helpers/suggest_name.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ mod tests {
460460
let sema = Semantics::new(&db);
461461

462462
let file_id =
463-
crate::base_db::EditionedFileId::new(sema.db, frange.file_id.file_id(), frange.file_id);
463+
crate::base_db::EditionedFileId::new(sema.db, frange.file_id);
464464
let source_file = sema.parse(file_id);
465465

466466
let element = source_file.syntax().covering_element(frange.range);

crates/ide-db/src/traits.rs

-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ mod tests {
142142

143143
let editioned_file_id = crate::base_db::EditionedFileId::new(
144144
sema.db.as_dyn_database(),
145-
position.file_id.file_id(),
146145
position.file_id,
147146
);
148147

@@ -163,7 +162,6 @@ mod tests {
163162

164163
let editioned_file_id = crate::base_db::EditionedFileId::new(
165164
sema.db.as_dyn_database(),
166-
position.file_id.file_id(),
167165
position.file_id,
168166
);
169167

crates/ide-diagnostics/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ pub fn syntax_diagnostics(
323323
let (file_id, _) = editioned_file_id.unpack();
324324

325325
let editioned_file_id_wrapper =
326-
ide_db::base_db::EditionedFileId::new(db.as_dyn_database(), file_id, editioned_file_id);
326+
ide_db::base_db::EditionedFileId::new(db.as_dyn_database(), editioned_file_id);
327327

328328
// [#3434] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily.
329329
db.parse_errors(editioned_file_id_wrapper)
@@ -357,7 +357,7 @@ pub fn semantic_diagnostics(
357357

358358
let (file_id, edition) = editioned_file_id.unpack();
359359
let editioned_file_id_wrapper =
360-
ide_db::base_db::EditionedFileId::new(db.as_dyn_database(), file_id, editioned_file_id);
360+
ide_db::base_db::EditionedFileId::new(db.as_dyn_database(), editioned_file_id);
361361

362362
let mut res = Vec::new();
363363

0 commit comments

Comments
 (0)