Skip to content

Commit 99fa130

Browse files
committed
fix: Fix invalid PartialEq impl for EditionedFileIdData
This broke the contract of `HashEqLike`, causing interning to return different IDs
1 parent 23cc64a commit 99fa130

6 files changed

Lines changed: 39 additions & 22 deletions

File tree

crates/base-db/src/editioned_file_id.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const _: () = {
2020
use zalsa_::interned as zalsa_struct_;
2121
type Configuration_ = EditionedFileId;
2222

23-
#[derive(Debug, Clone, PartialEq, Eq)]
23+
#[derive(Debug, Clone, Eq)]
2424
pub struct EditionedFileIdData {
2525
editioned_file_id: span::EditionedFileId,
2626
krate: Crate,
@@ -52,6 +52,14 @@ const _: () = {
5252
editioned_file_id: span::EditionedFileId,
5353
}
5454

55+
impl PartialEq for EditionedFileIdData {
56+
fn eq(&self, other: &Self) -> bool {
57+
let Self { editioned_file_id, krate: _ } = self;
58+
let Self { editioned_file_id: other_editioned_file_id, krate: _ } = other;
59+
editioned_file_id == other_editioned_file_id
60+
}
61+
}
62+
5563
impl Hash for EditionedFileIdData {
5664
#[inline]
5765
fn hash<H: Hasher>(&self, state: &mut H) {
@@ -210,6 +218,8 @@ const _: () = {
210218
/// 1. The file is not in the module tree.
211219
/// 2. You are latency sensitive and cannot afford calling the def map to precisely compute the origin
212220
/// (e.g. on enter feature, folding, etc.).
221+
// FIXME: Remove this and all the weird crate ignoring plumbing around this
222+
// This can cause a variety of weird bugs https://rust-lang.zulipchat.com/#narrow/channel/185405-t-compiler.2Frust-analyzer/topic/Broken.20token.20mapping/with/577739887
213223
pub fn from_span_guess_origin(
214224
db: &dyn RootQueryDb,
215225
editioned_file_id: span::EditionedFileId,
@@ -281,6 +291,8 @@ impl EditionedFileId {
281291
/// 1. The file is not in the module tree.
282292
/// 2. You are latency sensitive and cannot afford calling the def map to precisely compute the origin
283293
/// (e.g. on enter feature, folding, etc.).
294+
// FIXME: Remove this and all the weird crate ignoring plumbing around this
295+
// This can cause a variety of weird bugs https://rust-lang.zulipchat.com/#narrow/channel/185405-t-compiler.2Frust-analyzer/topic/Broken.20token.20mapping/with/577739887
284296
#[inline]
285297
pub fn current_edition_guess_origin(db: &dyn RootQueryDb, file_id: FileId) -> Self {
286298
Self::from_span_guess_origin(db, span::EditionedFileId::current_edition(file_id))

crates/hir-def/src/nameres/tests/incremental.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ pub const BAZ: u32 = 0;
118118
expect![[r#"
119119
[
120120
"crate_local_def_map",
121-
"file_item_tree_query",
122121
"crate_local_def_map",
123122
]
124123
"#]],

crates/hir-ty/src/tests/macros.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ use super::{check_infer, check_types};
99
fn cfg_impl_def() {
1010
check_types(
1111
r#"
12-
//- /main.rs crate:main deps:foo cfg:test
12+
//- /main.rs crate:main deps:foo cfg:some_cfg
1313
use foo::S as T;
1414
struct S;
1515
16-
#[cfg(test)]
16+
#[cfg(some_cfg)]
1717
impl S {
1818
fn foo1(&self) -> i32 { 0 }
1919
}
2020
21-
#[cfg(not(test))]
21+
#[cfg(not(some_cfg))]
2222
impl S {
2323
fn foo2(&self) -> i32 { 0 }
2424
}
@@ -31,12 +31,12 @@ fn test() {
3131
//- /foo.rs crate:foo
3232
pub struct S;
3333
34-
#[cfg(not(test))]
34+
#[cfg(not(some_cfg))]
3535
impl S {
3636
pub fn foo3(&self) -> i32 { 0 }
3737
}
3838
39-
#[cfg(test)]
39+
#[cfg(some_cfg)]
4040
impl S {
4141
pub fn foo4(&self) -> i32 { 0 }
4242
}

crates/ide-db/src/traits.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ fn assoc_item_of_trait(
113113

114114
#[cfg(test)]
115115
mod tests {
116+
use base_db::RootQueryDb;
116117
use expect_test::{Expect, expect};
117118
use hir::{EditionedFileId, FilePosition, Semantics};
118119
use span::Edition;
@@ -130,7 +131,9 @@ mod tests {
130131
database.apply_change(change_fixture.change);
131132
let (file_id, range_or_offset) =
132133
change_fixture.file_position.expect("expected a marker ($0)");
133-
let file_id = EditionedFileId::from_span_guess_origin(&database, file_id);
134+
135+
let &krate = database.relevant_crates(file_id.file_id()).first().unwrap();
136+
let file_id = EditionedFileId::from_span(&database, file_id, krate);
134137
let offset = range_or_offset.expect_offset();
135138
(database, FilePosition { file_id, offset })
136139
}

crates/ide/src/typing.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ mod on_enter;
1717

1818
use either::Either;
1919
use hir::EditionedFileId;
20-
use ide_db::{
21-
FilePosition, RootDatabase,
22-
base_db::{RootQueryDb, SourceDatabase},
23-
};
20+
use ide_db::{FilePosition, RootDatabase, base_db::RootQueryDb};
2421
use span::Edition;
2522
use std::iter;
2623

@@ -73,15 +70,16 @@ pub(crate) fn on_char_typed(
7370
if !TRIGGER_CHARS.contains(&char_typed) {
7471
return None;
7572
}
76-
let edition = db
77-
.source_root_crates(db.file_source_root(position.file_id).source_root_id(db))
73+
let krate = db
74+
.relevant_crates(position.file_id)
7875
.first()
79-
.map_or(Edition::CURRENT, |crates| crates.data(db).edition);
80-
// FIXME: We are hitting the database here, if we are unlucky this call might block momentarily
81-
// causing the editor to feel sluggish! We need to make this bail if it would block too long?
82-
let editioned_file_id_wrapper = EditionedFileId::from_span_guess_origin(
76+
.copied()
77+
.unwrap_or_else(|| *db.all_crates().first().unwrap());
78+
let edition = krate.data(db).edition;
79+
let editioned_file_id_wrapper = EditionedFileId::from_span(
8380
db,
8481
span::EditionedFileId::new(position.file_id, edition),
82+
krate,
8583
);
8684
let file = &db.parse(editioned_file_id_wrapper);
8785
let char_matches_position =

crates/test-fixture/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ pub trait WithFixture: Default + ExpandDatabase + SourceDatabase + 'static {
149149
let fixture = ChangeFixture::parse(ra_fixture);
150150
fixture.change.apply(&mut db);
151151
assert_eq!(fixture.files.len(), 1, "Multiple file found in the fixture");
152-
let file = EditionedFileId::from_span_guess_origin(&db, fixture.files[0]);
153-
(db, file)
152+
let &krate = db.relevant_crates(fixture.files[0].file_id()).first().unwrap();
153+
let file_id = EditionedFileId::from_span(&db, fixture.files[0], krate);
154+
(db, file_id)
154155
}
155156

156157
/// See the trait documentation for more information on fixtures.
@@ -165,7 +166,10 @@ pub trait WithFixture: Default + ExpandDatabase + SourceDatabase + 'static {
165166
let files = fixture
166167
.files
167168
.into_iter()
168-
.map(|file| EditionedFileId::from_span_guess_origin(&db, file))
169+
.map(|file| {
170+
let &krate = db.relevant_crates(file.file_id()).first().unwrap();
171+
EditionedFileId::from_span(&db, file, krate)
172+
})
169173
.collect();
170174
(db, files)
171175
}
@@ -222,7 +226,8 @@ pub trait WithFixture: Default + ExpandDatabase + SourceDatabase + 'static {
222226
let (file_id, range_or_offset) = fixture
223227
.file_position
224228
.expect("Could not find file position in fixture. Did you forget to add an `$0`?");
225-
let file_id = EditionedFileId::from_span_guess_origin(&db, file_id);
229+
let &krate = db.relevant_crates(file_id.file_id()).first().unwrap();
230+
let file_id = EditionedFileId::from_span(&db, file_id, krate);
226231
(db, file_id, range_or_offset)
227232
}
228233

0 commit comments

Comments
 (0)