Skip to content

Commit ad3d04c

Browse files
committed
A drive-by rewrite of give_region_a_name()
This rewrite makes the cache-updating nature of the function slightly clearer, using the Entry API into the hash table for region names to capture the update-insert nature of the method. May be marginally more efficient since it only runtime-borrows the map once, but in this context the performance impact is almost certainly completely negligible.
1 parent 94df917 commit ad3d04c

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt::{self, Display};
22
use std::iter;
33

4+
use rustc_data_structures::fx::IndexEntry;
45
use rustc_errors::Diagnostic;
56
use rustc_hir as hir;
67
use rustc_hir::def::{DefKind, Res};
@@ -247,25 +248,28 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
247248

248249
assert!(self.regioncx.universal_regions().is_universal_region(fr));
249250

250-
if let Some(value) = self.region_names.try_borrow_mut().unwrap().get(&fr) {
251-
return Some(value.clone());
252-
}
251+
match self.region_names.borrow_mut().entry(fr) {
252+
IndexEntry::Occupied(precomputed_name) => Some(precomputed_name.get().clone()),
253+
IndexEntry::Vacant(slot) => {
254+
let new_name = self
255+
.give_name_from_error_region(fr)
256+
.or_else(|| self.give_name_if_anonymous_region_appears_in_arguments(fr))
257+
.or_else(|| self.give_name_if_anonymous_region_appears_in_upvars(fr))
258+
.or_else(|| self.give_name_if_anonymous_region_appears_in_output(fr))
259+
.or_else(|| self.give_name_if_anonymous_region_appears_in_yield_ty(fr))
260+
.or_else(|| self.give_name_if_anonymous_region_appears_in_impl_signature(fr))
261+
.or_else(|| {
262+
self.give_name_if_anonymous_region_appears_in_arg_position_impl_trait(fr)
263+
});
264+
265+
if let Some(new_name) = &new_name {
266+
slot.insert(new_name.clone());
267+
}
268+
debug!("give_region_a_name: gave name {:?}", new_name);
253269

254-
let value = self
255-
.give_name_from_error_region(fr)
256-
.or_else(|| self.give_name_if_anonymous_region_appears_in_arguments(fr))
257-
.or_else(|| self.give_name_if_anonymous_region_appears_in_upvars(fr))
258-
.or_else(|| self.give_name_if_anonymous_region_appears_in_output(fr))
259-
.or_else(|| self.give_name_if_anonymous_region_appears_in_yield_ty(fr))
260-
.or_else(|| self.give_name_if_anonymous_region_appears_in_impl_signature(fr))
261-
.or_else(|| self.give_name_if_anonymous_region_appears_in_arg_position_impl_trait(fr));
262-
263-
if let Some(value) = &value {
264-
self.region_names.try_borrow_mut().unwrap().insert(fr, value.clone());
270+
new_name
271+
}
265272
}
266-
267-
debug!("give_region_a_name: gave name {:?}", value);
268-
value
269273
}
270274

271275
/// Checks for the case where `fr` maps to something that the

0 commit comments

Comments
 (0)