Skip to content

Commit 0dcae95

Browse files
committed
Tidy up on_query_feed_collisions
1 parent 1af8383 commit 0dcae95

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

compiler/rustc_middle/src/query/inner.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -143,35 +143,35 @@ fn on_query_feed_collision<'tcx, C: QueryCache>(
143143
old: C::Value,
144144
value: C::Value,
145145
) {
146-
let format_value = query.format_value;
146+
// Shared message for both failure paths, as a closure since we might not need it.
147+
let bug_message = || {
148+
format!(
149+
"Trying to feed an already recorded value for query {query:?} key={key:?}:\n\
150+
old value: {old}\n\
151+
new value: {value}",
152+
old = (query.format_value)(&old),
153+
value = (query.format_value)(&value),
154+
)
155+
};
147156

148-
// The query already has a cached value for this key.
149-
// That's OK if both values are the same, i.e. they have the same hash,
150-
// so now we check their hashes.
157+
// The query already has a cached value for this key. That's OK as long as
158+
// the values are the same, or the difference could be explained by errors.
159+
// So now we compare hashes to see if the values are the same.
151160
if let Some(hash_value_fn) = query.hash_value_fn {
152161
let (old_hash, value_hash) = tcx.with_stable_hashing_context(|ref mut hcx| {
153162
(hash_value_fn(hcx, &old), hash_value_fn(hcx, &value))
154163
});
155164
if old_hash != value_hash {
156-
// We have an inconsistency. This can happen if one of the two
157-
// results is tainted by errors. In this case, delay a bug to
158-
// ensure compilation is doomed, and keep the `old` value.
159-
tcx.dcx().delayed_bug(format!(
160-
"Trying to feed an already recorded value for query {query:?} key={key:?}:\n\
161-
old value: {old}\nnew value: {value}",
162-
old = format_value(&old),
163-
value = format_value(&value),
164-
));
165+
// We have an inconsistency. This could be a bug, but it can also
166+
// occur if one of the two values is tainted by errors.
167+
// Therefore, issue a delayed bug to ensure that compilation is
168+
// doomed, keep the old value, and discard the new value.
169+
tcx.dcx().delayed_bug(bug_message());
165170
}
166171
} else {
167-
// The query is `no_hash`, so we have no way to perform a sanity check.
172+
// The query is `no_hash`, so we have no way to check consistency.
168173
// If feeding the same value multiple times needs to be supported,
169174
// the query should not be marked `no_hash`.
170-
bug!(
171-
"Trying to feed an already recorded value for query {query:?} key={key:?}:\n\
172-
old value: {old}\nnew value: {value}",
173-
old = format_value(&old),
174-
value = format_value(&value),
175-
)
175+
bug!("{}", bug_message());
176176
}
177177
}

0 commit comments

Comments
 (0)