Skip to content

Commit 99118ee

Browse files
bors[bot]matklad
andauthored
Merge #6784
6784: Introduce anchored_path r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 42be522 + 6e24321 commit 99118ee

File tree

16 files changed

+130
-72
lines changed

16 files changed

+130
-72
lines changed

crates/base_db/src/lib.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub use crate::{
1818
},
1919
};
2020
pub use salsa;
21-
pub use vfs::{file_set::FileSet, FileId, VfsPath};
21+
pub use vfs::{file_set::FileSet, AnchoredPath, AnchoredPathBuf, FileId, VfsPath};
2222

2323
#[macro_export]
2424
macro_rules! impl_intern_key {
@@ -91,12 +91,7 @@ pub const DEFAULT_LRU_CAP: usize = 128;
9191
pub trait FileLoader {
9292
/// Text of the file.
9393
fn file_text(&self, file_id: FileId) -> Arc<String>;
94-
/// Note that we intentionally accept a `&str` and not a `&Path` here. This
95-
/// method exists to handle `#[path = "/some/path.rs"] mod foo;` and such,
96-
/// so the input is guaranteed to be utf-8 string. One might be tempted to
97-
/// introduce some kind of "utf-8 path with / separators", but that's a bad idea. Behold
98-
/// `#[path = "C://no/way"]`
99-
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>;
94+
fn resolve_path(&self, path: AnchoredPath) -> Option<FileId>;
10095
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
10196
}
10297

@@ -155,11 +150,11 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
155150
fn file_text(&self, file_id: FileId) -> Arc<String> {
156151
SourceDatabaseExt::file_text(self.0, file_id)
157152
}
158-
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
153+
fn resolve_path(&self, path: AnchoredPath) -> Option<FileId> {
159154
// FIXME: this *somehow* should be platform agnostic...
160-
let source_root = self.0.file_source_root(anchor);
155+
let source_root = self.0.file_source_root(path.anchor);
161156
let source_root = self.0.source_root(source_root);
162-
source_root.file_set.resolve_path(anchor, path)
157+
source_root.file_set.resolve_path(path)
163158
}
164159

165160
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {

crates/hir_def/src/nameres/mod_resolution.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! This module resolves `mod foo;` declaration to file.
2-
use base_db::FileId;
2+
use base_db::{AnchoredPath, FileId};
33
use hir_expand::name::Name;
44
use syntax::SmolStr;
55
use test_utils::mark;
@@ -77,7 +77,8 @@ impl ModDir {
7777
};
7878

7979
for candidate in candidate_files.iter() {
80-
if let Some(file_id) = db.resolve_path(file_id, candidate.as_str()) {
80+
let path = AnchoredPath { anchor: file_id, path: candidate.as_str() };
81+
if let Some(file_id) = db.resolve_path(path) {
8182
let is_mod_rs = candidate.ends_with("mod.rs");
8283

8384
let (dir_path, root_non_dir_owner) = if is_mod_rs || attr_path.is_some() {

crates/hir_def/src/test_db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::{
55
sync::{Arc, Mutex},
66
};
77

8-
use base_db::SourceDatabase;
98
use base_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, Upcast};
9+
use base_db::{AnchoredPath, SourceDatabase};
1010
use hir_expand::db::AstDatabase;
1111
use hir_expand::diagnostics::Diagnostic;
1212
use hir_expand::diagnostics::DiagnosticSinkBuilder;
@@ -63,8 +63,8 @@ impl FileLoader for TestDB {
6363
fn file_text(&self, file_id: FileId) -> Arc<String> {
6464
FileLoaderDelegate(self).file_text(file_id)
6565
}
66-
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
67-
FileLoaderDelegate(self).resolve_path(anchor, path)
66+
fn resolve_path(&self, path: AnchoredPath) -> Option<FileId> {
67+
FileLoaderDelegate(self).resolve_path(path)
6868
}
6969
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
7070
FileLoaderDelegate(self).relevant_crates(file_id)

crates/hir_expand/src/builtin_macro.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
MacroDefId, MacroDefKind, TextSize,
55
};
66

7-
use base_db::FileId;
7+
use base_db::{AnchoredPath, FileId};
88
use either::Either;
99
use mbe::{parse_to_token_tree, ExpandResult};
1010
use parser::FragmentKind;
@@ -324,7 +324,8 @@ fn relative_file(
324324
allow_recursion: bool,
325325
) -> Option<FileId> {
326326
let call_site = call_id.as_file().original_file(db);
327-
let res = db.resolve_path(call_site, path)?;
327+
let path = AnchoredPath { anchor: call_site, path };
328+
let res = db.resolve_path(path)?;
328329
// Prevent include itself
329330
if res == call_site && !allow_recursion {
330331
None

crates/hir_expand/src/test_db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
sync::{Arc, Mutex},
66
};
77

8-
use base_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate};
8+
use base_db::{salsa, AnchoredPath, CrateId, FileId, FileLoader, FileLoaderDelegate};
99
use rustc_hash::FxHashSet;
1010

1111
#[salsa::database(
@@ -40,8 +40,8 @@ impl FileLoader for TestDB {
4040
fn file_text(&self, file_id: FileId) -> Arc<String> {
4141
FileLoaderDelegate(self).file_text(file_id)
4242
}
43-
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
44-
FileLoaderDelegate(self).resolve_path(anchor, path)
43+
fn resolve_path(&self, path: AnchoredPath) -> Option<FileId> {
44+
FileLoaderDelegate(self).resolve_path(path)
4545
}
4646
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
4747
FileLoaderDelegate(self).relevant_crates(file_id)

crates/hir_ty/src/test_db.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use std::{
55
sync::{Arc, Mutex},
66
};
77

8-
use base_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast};
8+
use base_db::{
9+
salsa, AnchoredPath, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast,
10+
};
911
use hir_def::{db::DefDatabase, ModuleId};
1012
use hir_expand::db::AstDatabase;
1113
use rustc_hash::{FxHashMap, FxHashSet};
@@ -67,8 +69,8 @@ impl FileLoader for TestDB {
6769
fn file_text(&self, file_id: FileId) -> Arc<String> {
6870
FileLoaderDelegate(self).file_text(file_id)
6971
}
70-
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
71-
FileLoaderDelegate(self).resolve_path(anchor, path)
72+
fn resolve_path(&self, path: AnchoredPath) -> Option<FileId> {
73+
FileLoaderDelegate(self).resolve_path(path)
7274
}
7375
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
7476
FileLoaderDelegate(self).relevant_crates(file_id)

crates/ide/src/diagnostics.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,10 +610,12 @@ fn test_fn() {
610610
source_file_edits: [],
611611
file_system_edits: [
612612
CreateFile {
613-
anchor: FileId(
614-
0,
615-
),
616-
dst: "foo.rs",
613+
dst: AnchoredPathBuf {
614+
anchor: FileId(
615+
0,
616+
),
617+
path: "foo.rs",
618+
},
617619
},
618620
],
619621
is_snippet: false,

crates/ide/src/diagnostics/fixes.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use hir::{
88
},
99
HasSource, HirDisplay, Semantics, VariantDef,
1010
};
11-
use ide_db::base_db::FileId;
11+
use ide_db::base_db::{AnchoredPathBuf, FileId};
1212
use ide_db::{
1313
source_change::{FileSystemEdit, SourceFileEdit},
1414
RootDatabase,
@@ -36,8 +36,10 @@ impl DiagnosticWithFix for UnresolvedModule {
3636
Some(Fix::new(
3737
"Create module",
3838
FileSystemEdit::CreateFile {
39-
anchor: self.file.original_file(sema.db),
40-
dst: self.candidate.clone(),
39+
dst: AnchoredPathBuf {
40+
anchor: self.file.original_file(sema.db),
41+
path: self.candidate.clone(),
42+
},
4143
}
4244
.into(),
4345
unresolved_module.syntax().text_range(),

crates/ide/src/references/rename.rs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
};
77

88
use hir::{Module, ModuleDef, ModuleSource, Semantics};
9-
use ide_db::base_db::{FileRange, SourceDatabaseExt};
9+
use ide_db::base_db::{AnchoredPathBuf, FileRange, SourceDatabaseExt};
1010
use ide_db::{
1111
defs::{Definition, NameClass, NameRefClass},
1212
RootDatabase,
@@ -182,12 +182,13 @@ fn rename_mod(
182182
match src.value {
183183
ModuleSource::SourceFile(..) => {
184184
// mod is defined in path/to/dir/mod.rs
185-
let dst = if module.is_mod_rs(sema.db) {
185+
let path = if module.is_mod_rs(sema.db) {
186186
format!("../{}/mod.rs", new_name)
187187
} else {
188188
format!("{}.rs", new_name)
189189
};
190-
let move_file = FileSystemEdit::MoveFile { src: file_id, anchor: file_id, dst };
190+
let dst = AnchoredPathBuf { anchor: file_id, path };
191+
let move_file = FileSystemEdit::MoveFile { src: file_id, dst };
191192
file_system_edits.push(move_file);
192193
}
193194
ModuleSource::Module(..) => {}
@@ -771,10 +772,12 @@ mod foo<|>;
771772
src: FileId(
772773
2,
773774
),
774-
anchor: FileId(
775-
2,
776-
),
777-
dst: "foo2.rs",
775+
dst: AnchoredPathBuf {
776+
anchor: FileId(
777+
2,
778+
),
779+
path: "foo2.rs",
780+
},
778781
},
779782
],
780783
is_snippet: false,
@@ -837,10 +840,12 @@ use crate::foo<|>::FooContent;
837840
src: FileId(
838841
1,
839842
),
840-
anchor: FileId(
841-
1,
842-
),
843-
dst: "quux.rs",
843+
dst: AnchoredPathBuf {
844+
anchor: FileId(
845+
1,
846+
),
847+
path: "quux.rs",
848+
},
844849
},
845850
],
846851
is_snippet: false,
@@ -884,10 +889,12 @@ mod fo<|>o;
884889
src: FileId(
885890
1,
886891
),
887-
anchor: FileId(
888-
1,
889-
),
890-
dst: "../foo2/mod.rs",
892+
dst: AnchoredPathBuf {
893+
anchor: FileId(
894+
1,
895+
),
896+
path: "../foo2/mod.rs",
897+
},
891898
},
892899
],
893900
is_snippet: false,
@@ -932,10 +939,12 @@ mod outer { mod fo<|>o; }
932939
src: FileId(
933940
1,
934941
),
935-
anchor: FileId(
936-
1,
937-
),
938-
dst: "bar.rs",
942+
dst: AnchoredPathBuf {
943+
anchor: FileId(
944+
1,
945+
),
946+
path: "bar.rs",
947+
},
939948
},
940949
],
941950
is_snippet: false,
@@ -1016,10 +1025,12 @@ pub mod foo<|>;
10161025
src: FileId(
10171026
2,
10181027
),
1019-
anchor: FileId(
1020-
2,
1021-
),
1022-
dst: "foo2.rs",
1028+
dst: AnchoredPathBuf {
1029+
anchor: FileId(
1030+
2,
1031+
),
1032+
path: "foo2.rs",
1033+
},
10231034
},
10241035
],
10251036
is_snippet: false,

crates/ide_db/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use std::{fmt, sync::Arc};
1919

2020
use base_db::{
2121
salsa::{self, Durability},
22-
Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase,
23-
Upcast,
22+
AnchoredPath, Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate,
23+
SourceDatabase, Upcast,
2424
};
2525
use hir::db::{AstDatabase, DefDatabase, HirDatabase};
2626
use rustc_hash::FxHashSet;
@@ -72,8 +72,8 @@ impl FileLoader for RootDatabase {
7272
fn file_text(&self, file_id: FileId) -> Arc<String> {
7373
FileLoaderDelegate(self).file_text(file_id)
7474
}
75-
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
76-
FileLoaderDelegate(self).resolve_path(anchor, path)
75+
fn resolve_path(&self, path: AnchoredPath) -> Option<FileId> {
76+
FileLoaderDelegate(self).resolve_path(path)
7777
}
7878
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
7979
FileLoaderDelegate(self).relevant_crates(file_id)

0 commit comments

Comments
 (0)