Skip to content

Commit 04a49fb

Browse files
Merge pull request rust-lang#22528 from Wilfred/missing_source_root_message
internal: Improve panic message when FileSourceRootInput is missing
2 parents aadee11 + 273abbb commit 04a49fb

4 files changed

Lines changed: 21 additions & 5 deletions

File tree

crates/base-db/src/lib.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,32 @@ impl Files {
170170
};
171171
}
172172

173-
pub fn file_source_root(&self, id: vfs::FileId) -> FileSourceRootInput {
173+
pub fn file_source_root(
174+
&self,
175+
db: &dyn SourceDatabase,
176+
id: vfs::FileId,
177+
) -> FileSourceRootInput {
174178
let file_source_root = match self.file_source_roots.get(&id) {
175179
Some(file_source_root) => file_source_root,
176180
None => panic!(
177-
"Unable to get `FileSourceRootInput` with `vfs::FileId` ({id:?}); this is a bug",
181+
"Unable to get `FileSourceRootInput` with `vfs::FileId` ({id:?}, path: {}); this is a bug",
182+
self.path_for_file(db, id)
183+
.map_or_else(|| "<unknown>".to_owned(), |path| path.to_string()),
178184
),
179185
};
180186
*file_source_root
181187
}
182188

189+
fn path_for_file(&self, db: &dyn SourceDatabase, id: vfs::FileId) -> Option<vfs::VfsPath> {
190+
for source_root in &*self.source_roots {
191+
let source_root = *source_root.value();
192+
if let Some(path) = source_root.source_root(db).path_for_file(&id) {
193+
return Some(path.clone());
194+
}
195+
}
196+
None
197+
}
198+
183199
pub fn set_file_source_root_with_durability(
184200
&self,
185201
db: &mut dyn SourceDatabase,

crates/hir-def/src/test_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl SourceDatabase for TestDB {
122122
}
123123

124124
fn file_source_root(&self, id: base_db::FileId) -> FileSourceRootInput {
125-
self.files.file_source_root(id)
125+
self.files.file_source_root(self, id)
126126
}
127127

128128
fn set_file_source_root_with_durability(

crates/hir-ty/src/test_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl SourceDatabase for TestDB {
112112
}
113113

114114
fn file_source_root(&self, id: base_db::FileId) -> FileSourceRootInput {
115-
self.files.file_source_root(id)
115+
self.files.file_source_root(self, id)
116116
}
117117

118118
fn set_file_source_root_with_durability(

crates/ide-db/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl SourceDatabase for RootDatabase {
160160
}
161161

162162
fn file_source_root(&self, id: vfs::FileId) -> FileSourceRootInput {
163-
self.files.file_source_root(id)
163+
self.files.file_source_root(self, id)
164164
}
165165

166166
fn set_file_source_root_with_durability(

0 commit comments

Comments
 (0)