Skip to content

Commit 3969bc4

Browse files
committed
more with_lock
1 parent 985c87f commit 3969bc4

File tree

22 files changed

+362
-303
lines changed

22 files changed

+362
-303
lines changed

compiler/rustc_data_structures/src/sharded.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub type ShardedHashMap<K, V> = Sharded<FxHashMap<K, V>>;
122122

123123
impl<K: Eq, V> ShardedHashMap<K, V> {
124124
pub fn len(&self) -> usize {
125-
self.lock_shards().iter().map(|shard| shard.deref().len()).sum()
125+
self.lock_shards().iter().map(|shard| shard.len()).sum()
126126
}
127127
}
128128

compiler/rustc_data_structures/src/sync.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,12 +755,16 @@ pub struct LockGuard<'a, T> {
755755
marker: PhantomData<&'a mut T>,
756756
}
757757

758-
impl<T> LockGuard<'_, T> {
759-
pub const fn deref(&self) -> &T {
758+
impl<T> const Deref for LockGuard<'_, T> {
759+
type Target = T;
760+
761+
fn deref(&self) -> &T {
760762
unsafe { &*self.lock.data.get() }
761763
}
764+
}
762765

763-
pub const fn deref_mut(&mut self) -> &mut T {
766+
impl<T> const DerefMut for LockGuard<'_, T> {
767+
fn deref_mut(&mut self) -> &mut T {
764768
unsafe { &mut *self.lock.data.get() }
765769
}
766770
}

compiler/rustc_errors/src/lib.rs

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,12 @@ impl Handler {
639639
) -> String {
640640
let inner = self.inner.borrow();
641641
let args = crate::translation::to_fluent_args(args);
642-
inner.emitter.translate_message(&message, &args).map_err(Report::new).unwrap().to_string()
642+
inner
643+
.emitter
644+
.translate_message(&message, &args)
645+
.map_err(Report::new)
646+
.unwrap()
647+
.to_string()
643648
}
644649

645650
// This is here to not allow mutation of flags;
@@ -685,12 +690,14 @@ impl Handler {
685690
}
686691

687692
pub fn has_stashed_diagnostic(&self, span: Span, key: StashKey) -> bool {
688-
self.inner.borrow().stashed_diagnostics.get(&(span.with_parent(None), key)).is_some()
693+
self.inner.with_borrow(|inner| {
694+
inner.stashed_diagnostics.get(&(span.with_parent(None), key)).is_some()
695+
})
689696
}
690697

691698
/// Emit all stashed diagnostics.
692699
pub fn emit_stashed_diagnostics(&self) -> Option<ErrorGuaranteed> {
693-
self.inner.borrow_mut().emit_stashed_diagnostics()
700+
self.inner.lock().emit_stashed_diagnostics()
694701
}
695702

696703
/// Construct a builder with the `msg` at the level appropriate for the specific `EmissionGuarantee`.
@@ -992,7 +999,7 @@ impl Handler {
992999
}
9931000

9941001
pub fn span_bug(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) -> ! {
995-
self.inner.borrow_mut().span_bug(span, msg)
1002+
self.inner.lock().span_bug(span, msg)
9961003
}
9971004

9981005
/// For documentation on this, see `Session::delay_span_bug`.
@@ -1002,13 +1009,13 @@ impl Handler {
10021009
span: impl Into<MultiSpan>,
10031010
msg: impl Into<DiagnosticMessage>,
10041011
) -> ErrorGuaranteed {
1005-
self.inner.borrow_mut().delay_span_bug(span, msg)
1012+
self.inner.lock().delay_span_bug(span, msg)
10061013
}
10071014

10081015
// FIXME(eddyb) note the comment inside `impl Drop for HandlerInner`, that's
10091016
// where the explanation of what "good path" is (also, it should be renamed).
10101017
pub fn delay_good_path_bug(&self, msg: impl Into<DiagnosticMessage>) {
1011-
self.inner.borrow_mut().delay_good_path_bug(msg)
1018+
self.inner.lock().delay_good_path_bug(msg)
10121019
}
10131020

10141021
#[track_caller]
@@ -1041,12 +1048,12 @@ impl Handler {
10411048
// NOTE: intentionally doesn't raise an error so rustc_codegen_ssa only reports fatal errors in the main thread
10421049
#[rustc_lint_diagnostics]
10431050
pub fn fatal(&self, msg: impl Into<DiagnosticMessage>) -> FatalError {
1044-
self.inner.borrow_mut().fatal(msg)
1051+
self.inner.lock().fatal(msg)
10451052
}
10461053

10471054
#[rustc_lint_diagnostics]
10481055
pub fn err(&self, msg: impl Into<DiagnosticMessage>) -> ErrorGuaranteed {
1049-
self.inner.borrow_mut().err(msg)
1056+
self.inner.lock().err(msg)
10501057
}
10511058

10521059
#[rustc_lint_diagnostics]
@@ -1061,47 +1068,52 @@ impl Handler {
10611068
}
10621069

10631070
pub fn bug(&self, msg: impl Into<DiagnosticMessage>) -> ! {
1064-
self.inner.borrow_mut().bug(msg)
1071+
self.inner.lock().bug(msg)
10651072
}
10661073

10671074
#[inline]
10681075
pub fn err_count(&self) -> usize {
1069-
self.inner.borrow().err_count()
1076+
self.inner.with_borrow(|inner| inner.err_count())
10701077
}
10711078

10721079
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
1073-
self.inner.borrow().has_errors().then(ErrorGuaranteed::unchecked_claim_error_was_emitted)
1080+
self.inner.with_borrow(|inner| {
1081+
inner.has_errors().then(ErrorGuaranteed::unchecked_claim_error_was_emitted)
1082+
})
10741083
}
10751084

10761085
pub fn has_errors_or_lint_errors(&self) -> Option<ErrorGuaranteed> {
1077-
self.inner
1078-
.borrow()
1079-
.has_errors_or_lint_errors()
1080-
.then(ErrorGuaranteed::unchecked_claim_error_was_emitted)
1086+
self.inner.with_borrow(|inner| {
1087+
inner
1088+
.has_errors_or_lint_errors()
1089+
.then(ErrorGuaranteed::unchecked_claim_error_was_emitted)
1090+
})
10811091
}
10821092
pub fn has_errors_or_delayed_span_bugs(&self) -> Option<ErrorGuaranteed> {
1083-
self.inner
1084-
.borrow()
1085-
.has_errors_or_delayed_span_bugs()
1086-
.then(ErrorGuaranteed::unchecked_claim_error_was_emitted)
1093+
self.inner.with_borrow(|inner| {
1094+
inner
1095+
.has_errors_or_delayed_span_bugs()
1096+
.then(ErrorGuaranteed::unchecked_claim_error_was_emitted)
1097+
})
10871098
}
10881099
pub fn is_compilation_going_to_fail(&self) -> Option<ErrorGuaranteed> {
1089-
self.inner
1090-
.borrow()
1091-
.is_compilation_going_to_fail()
1092-
.then(ErrorGuaranteed::unchecked_claim_error_was_emitted)
1100+
self.inner.with_borrow(|inner| {
1101+
inner
1102+
.is_compilation_going_to_fail()
1103+
.then(ErrorGuaranteed::unchecked_claim_error_was_emitted)
1104+
})
10931105
}
10941106

10951107
pub fn print_error_count(&self, registry: &Registry) {
1096-
self.inner.borrow_mut().print_error_count(registry)
1108+
self.inner.lock().print_error_count(registry)
10971109
}
10981110

10991111
pub fn take_future_breakage_diagnostics(&self) -> Vec<Diagnostic> {
1100-
std::mem::take(&mut self.inner.borrow_mut().future_breakage_diagnostics)
1112+
std::mem::take(&mut self.inner.lock().future_breakage_diagnostics)
11011113
}
11021114

11031115
pub fn abort_if_errors(&self) {
1104-
self.inner.borrow_mut().abort_if_errors()
1116+
self.inner.lock().abort_if_errors()
11051117
}
11061118

11071119
/// `true` if we haven't taught a diagnostic with this code already.
@@ -1110,15 +1122,15 @@ impl Handler {
11101122
/// Used to suppress emitting the same error multiple times with extended explanation when
11111123
/// calling `-Zteach`.
11121124
pub fn must_teach(&self, code: &DiagnosticId) -> bool {
1113-
self.inner.borrow_mut().must_teach(code)
1125+
self.inner.lock().must_teach(code)
11141126
}
11151127

11161128
pub fn force_print_diagnostic(&self, db: Diagnostic) {
1117-
self.inner.borrow_mut().force_print_diagnostic(db)
1129+
self.inner.lock().force_print_diagnostic(db)
11181130
}
11191131

11201132
pub fn emit_diagnostic(&self, diagnostic: &mut Diagnostic) -> Option<ErrorGuaranteed> {
1121-
self.inner.borrow_mut().emit_diagnostic(diagnostic)
1133+
self.inner.lock().emit_diagnostic(diagnostic)
11221134
}
11231135

11241136
pub fn emit_err<'a>(&'a self, err: impl IntoDiagnostic<'a>) -> ErrorGuaranteed {
@@ -1198,16 +1210,15 @@ impl Handler {
11981210
mut diag: Diagnostic,
11991211
sp: impl Into<MultiSpan>,
12001212
) -> Option<ErrorGuaranteed> {
1201-
let mut inner = self.inner.borrow_mut();
1202-
inner.emit_diagnostic(diag.set_span(sp))
1213+
self.inner.lock().emit_diagnostic(diag.set_span(sp))
12031214
}
12041215

12051216
pub fn emit_artifact_notification(&self, path: &Path, artifact_type: &str) {
1206-
self.inner.borrow_mut().emit_artifact_notification(path, artifact_type)
1217+
self.inner.lock().emit_artifact_notification(path, artifact_type)
12071218
}
12081219

12091220
pub fn emit_future_breakage_report(&self, diags: Vec<Diagnostic>) {
1210-
self.inner.borrow_mut().emitter.emit_future_breakage_report(diags)
1221+
self.inner.lock().emitter.emit_future_breakage_report(diags)
12111222
}
12121223

12131224
pub fn emit_unused_externs(

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ pub(super) fn try_match_macro<'matcher, T: Tracker<'matcher>>(
331331
// This is used so that if a matcher is not `Success(..)`ful,
332332
// then the spans which became gated when parsing the unsuccessful matcher
333333
// are not recorded. On the first `Success(..)`ful matcher, the spans are merged.
334-
let mut gated_spans_snapshot = mem::take(&mut *sess.gated_spans.spans.borrow_mut());
334+
let mut gated_spans_snapshot = sess.gated_spans.spans.with_lock(|spans| mem::take(spans));
335335

336336
let result = tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs, track);
337337

@@ -364,7 +364,7 @@ pub(super) fn try_match_macro<'matcher, T: Tracker<'matcher>>(
364364

365365
// The matcher was not `Success(..)`ful.
366366
// Restore to the state before snapshotting and maybe try again.
367-
mem::swap(&mut gated_spans_snapshot, &mut sess.gated_spans.spans.borrow_mut());
367+
sess.gated_spans.spans.with_lock(|spans| mem::swap(&mut gated_spans_snapshot, spans));
368368
}
369369

370370
Err(CanRetry::Yes)

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,11 @@ impl server::FreeFunctions for Rustc<'_, '_> {
394394
fn track_env_var(&mut self, var: &str, value: Option<&str>) {
395395
self.sess()
396396
.env_depinfo
397-
.borrow_mut()
398-
.insert((Symbol::intern(var), value.map(Symbol::intern)));
397+
.with_lock(|info| info.insert((Symbol::intern(var), value.map(Symbol::intern))));
399398
}
400399

401400
fn track_path(&mut self, path: &str) {
402-
self.sess().file_depinfo.borrow_mut().insert(Symbol::intern(path));
401+
self.sess().file_depinfo.with_lock(|info| info.insert(Symbol::intern(path)));
403402
}
404403

405404
fn literal_from_str(&mut self, s: &str) -> Result<Literal<Self::Span, Self::Symbol>, ()> {

compiler/rustc_parse/src/lexer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl<'a> StringReader<'a> {
175175
if !sym.can_be_raw() {
176176
self.sess.emit_err(errors::CannotBeRawIdent { span, ident: sym });
177177
}
178-
self.sess.raw_identifier_spans.borrow_mut().push(span);
178+
self.sess.raw_identifier_spans.lock().push(span);
179179
token::Ident(sym, true)
180180
}
181181
rustc_lexer::TokenKind::UnknownPrefix => {
@@ -197,7 +197,7 @@ impl<'a> StringReader<'a> {
197197
{
198198
let sym = nfc_normalize(self.str_from(start));
199199
let span = self.mk_sp(start, self.pos);
200-
self.sess.bad_unicode_identifiers.borrow_mut().entry(sym).or_default()
200+
self.sess.bad_unicode_identifiers.lock().entry(sym).or_default()
201201
.push(span);
202202
token::Ident(sym, false)
203203
}

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ impl<'a> Parser<'a> {
813813
Applicability::MachineApplicable
814814
},
815815
);
816-
self.sess.type_ascription_path_suggestions.borrow_mut().insert(sp);
816+
self.sess.type_ascription_path_suggestions.lock().deref_mut().insert(sp);
817817
} else if op_pos.line != next_pos.line && maybe_expected_semicolon {
818818
err.span_suggestion(
819819
sp,
@@ -2113,7 +2113,7 @@ impl<'a> Parser<'a> {
21132113
};
21142114
let mut err = self.struct_span_err(span, &msg);
21152115
let sp = self.sess.source_map().start_point(self.token.span);
2116-
if let Some(sp) = self.sess.ambiguous_block_expr_parse.borrow().get(&sp) {
2116+
if let Some(sp) = self.sess.ambiguous_block_expr_parse.borrow().deref().get(&sp) {
21172117
err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp));
21182118
}
21192119
err.span_label(span, "expected expression");

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ pub(crate) fn make_unclosed_delims_error(
15401540
}
15411541

15421542
pub fn emit_unclosed_delims(unclosed_delims: &mut Vec<UnmatchedDelim>, sess: &ParseSess) {
1543-
*sess.reached_eof.borrow_mut() |=
1543+
*sess.reached_eof.lock() |=
15441544
unclosed_delims.iter().any(|unmatched_delim| unmatched_delim.found_delim.is_none());
15451545
for unmatched in unclosed_delims.drain(..) {
15461546
if let Some(mut e) = make_unclosed_delims_error(unmatched, sess) {

compiler/rustc_query_system/src/cache.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ impl<Key, Value> Default for Cache<Key, Value> {
2121
impl<Key, Value> Cache<Key, Value> {
2222
/// Actually frees the underlying memory in contrast to what stdlib containers do on `clear`
2323
pub fn clear(&self) {
24-
*self.hashmap.borrow_mut() = Default::default();
24+
self.hashmap.with_lock(|map| *map = Default::default());
2525
}
2626
}
2727

2828
impl<Key: Eq + Hash, Value: Clone> Cache<Key, Value> {
2929
pub fn get<Tcx: DepContext>(&self, key: &Key, tcx: Tcx) -> Option<Value> {
30-
Some(self.hashmap.borrow().get(key)?.get(tcx))
30+
self.hashmap.with_borrow(|map| map.get(key).map(|node| node.get(tcx)))
3131
}
3232

3333
pub fn insert(&self, key: Key, dep_node: DepNodeIndex, value: Value) {
34-
self.hashmap.borrow_mut().insert(key, WithDepNode::new(dep_node, value));
34+
self.hashmap.with_lock(|map| map.insert(key, WithDepNode::new(dep_node, value)));
3535
}
3636
}
3737

0 commit comments

Comments
 (0)