Skip to content

Commit 52d048a

Browse files
Fix a panic where all_crates() is empty
1 parent 1671349 commit 52d048a

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

crates/base-db/src/editioned_file_id.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,18 @@ const _: () = {
222222
.relevant_crates(editioned_file_id.file_id())
223223
.first()
224224
.copied()
225-
.unwrap_or_else(|| db.all_crates()[0]);
225+
.or_else(|| db.all_crates().get(0).copied())
226+
.unwrap_or_else(|| {
227+
// What we're doing here is a bit fishy. We rely on the fact that we only need
228+
// the crate in the item tree, and we should not create an `EditionedFileId`
229+
// without a crate except in cases where it does not matter. The chances that
230+
// `all_crates()` will be empty are also very slim, but it can occur during startup.
231+
// In the very unlikely case that there is a bug and we'll use this crate, Salsa
232+
// will panic.
233+
234+
// SAFETY: 0 is less than `Id::MAX_U32`.
235+
salsa::plumbing::FromId::from_id(unsafe { salsa::Id::from_index(0) })
236+
});
226237
EditionedFileIdData { editioned_file_id, krate }
227238
},
228239
)

0 commit comments

Comments
 (0)