Skip to content

Commit 5efb4e5

Browse files
Introduce and use CmRefCell
1 parent 514467b commit 5efb4e5

File tree

4 files changed

+67
-16
lines changed

4 files changed

+67
-16
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
505505
});
506506
}
507507
}
508-
ImportKind::Glob { .. } => current_module.globs.borrow_mut().push(import),
508+
ImportKind::Glob { .. } => current_module.globs.borrow_mut_unchecked().push(import),
509509
_ => unreachable!(),
510510
}
511511
}
@@ -1196,7 +1196,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11961196
/// directly into its parent scope's module.
11971197
fn visit_invoc_in_module(&mut self, id: NodeId) -> MacroRulesScopeRef<'ra> {
11981198
let invoc_id = self.visit_invoc(id);
1199-
self.parent_scope.module.unexpanded_invocations.borrow_mut().insert(invoc_id);
1199+
self.parent_scope.module.unexpanded_invocations.borrow_mut_unchecked().insert(invoc_id);
12001200
self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Invocation(invoc_id))
12011201
}
12021202

compiler/rustc_resolve/src/imports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
483483
}
484484
};
485485

486-
let Ok(glob_importers) = module.glob_importers.try_borrow_mut() else {
486+
let Ok(glob_importers) = module.glob_importers.try_borrow_mut_unchecked() else {
487487
return t;
488488
};
489489

@@ -1511,7 +1511,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15111511
}
15121512

15131513
// Add to module's glob_importers
1514-
module.glob_importers.borrow_mut().push(import);
1514+
module.glob_importers.borrow_mut_unchecked().push(import);
15151515

15161516
// Ensure that `resolutions` isn't borrowed during `try_define`,
15171517
// since it might get updated via a glob cycle.
@@ -1553,7 +1553,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15531553
// reporting conflicts, and reporting unresolved imports.
15541554
fn finalize_resolutions_in(&mut self, module: Module<'ra>) {
15551555
// Since import resolution is finished, globs will not define any more names.
1556-
*module.globs.borrow_mut() = Vec::new();
1556+
*module.globs.borrow_mut_unchecked() = Vec::new();
15571557

15581558
let Some(def_id) = module.opt_def_id() else { return };
15591559

compiler/rustc_resolve/src/lib.rs

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#![recursion_limit = "256"]
2626
// tidy-alphabetical-end
2727

28-
use std::cell::{Cell, Ref, RefCell};
28+
use std::cell::{BorrowMutError, Cell, Ref, RefCell, RefMut};
2929
use std::collections::BTreeSet;
3030
use std::fmt::{self, Formatter};
3131
use std::sync::Arc;
@@ -598,17 +598,17 @@ struct ModuleData<'ra> {
598598
underscore_disambiguator: CmCell<u32>, // FIXME(batched): Use an atomic in batched import resolution?
599599

600600
/// Macro invocations that can expand into items in this module.
601-
unexpanded_invocations: RefCell<FxHashSet<LocalExpnId>>,
601+
unexpanded_invocations: CmRefCell<FxHashSet<LocalExpnId>>,
602602

603603
/// Whether `#[no_implicit_prelude]` is active.
604604
no_implicit_prelude: bool,
605605

606-
glob_importers: RefCell<Vec<Import<'ra>>>,
607-
globs: RefCell<Vec<Import<'ra>>>,
606+
glob_importers: CmRefCell<Vec<Import<'ra>>>,
607+
globs: CmRefCell<Vec<Import<'ra>>>,
608608

609609
/// Used to memoize the traits in this module for faster searches through all traits in scope.
610610
traits:
611-
RefCell<Option<Box<[(Macros20NormalizedIdent, NameBinding<'ra>, Option<Module<'ra>>)]>>>,
611+
CmRefCell<Option<Box<[(Macros20NormalizedIdent, NameBinding<'ra>, Option<Module<'ra>>)]>>>,
612612

613613
/// Span of the module itself. Used for error reporting.
614614
span: Span,
@@ -660,9 +660,9 @@ impl<'ra> ModuleData<'ra> {
660660
underscore_disambiguator: CmCell::new(0),
661661
unexpanded_invocations: Default::default(),
662662
no_implicit_prelude,
663-
glob_importers: RefCell::new(Vec::new()),
664-
globs: RefCell::new(Vec::new()),
665-
traits: RefCell::new(None),
663+
glob_importers: CmRefCell::new(Vec::new()),
664+
globs: CmRefCell::new(Vec::new()),
665+
traits: CmRefCell::new(None),
666666
span,
667667
expansion,
668668
self_binding,
@@ -697,7 +697,7 @@ impl<'ra> Module<'ra> {
697697

698698
/// This modifies `self` in place. The traits will be stored in `self.traits`.
699699
fn ensure_traits<'tcx>(self, resolver: &impl AsRef<Resolver<'ra, 'tcx>>) {
700-
let mut traits = self.traits.borrow_mut();
700+
let mut traits = self.traits.borrow_mut_unchecked();
701701
if traits.is_none() {
702702
let mut collected_traits = Vec::new();
703703
self.for_each_child(resolver, |r, name, ns, binding| {
@@ -2587,7 +2587,7 @@ impl<T: Copy> CmCell<T> {
25872587
self.value.get()
25882588
}
25892589

2590-
pub fn update_unchecked(&self, f: impl FnOnce(T) -> T)
2590+
fn update_unchecked(&self, f: impl FnOnce(T) -> T)
25912591
where
25922592
T: Copy,
25932593
{
@@ -2618,3 +2618,54 @@ impl<T> CmCell<T> {
26182618
self.value.into_inner()
26192619
}
26202620
}
2621+
#[derive(Default)]
2622+
struct CmRefCell<T> {
2623+
ref_cell: RefCell<T>,
2624+
}
2625+
2626+
impl<T> CmRefCell<T> {
2627+
const fn new(value: T) -> CmRefCell<T> {
2628+
CmRefCell { ref_cell: RefCell::new(value) }
2629+
}
2630+
2631+
#[inline]
2632+
#[track_caller]
2633+
#[allow(dead_code)]
2634+
fn borrow_mut<'ra, 'tcx>(&self, r: &Resolver<'ra, 'tcx>) -> RefMut<'_, T> {
2635+
if r.assert_speculative {
2636+
panic!("Not allowed to mutably borrow a CmRefCell during speculative resolution");
2637+
}
2638+
self.ref_cell.borrow_mut()
2639+
}
2640+
2641+
#[inline]
2642+
#[track_caller]
2643+
fn borrow_mut_unchecked(&self) -> RefMut<'_, T> {
2644+
self.ref_cell.borrow_mut()
2645+
}
2646+
2647+
#[inline]
2648+
#[track_caller]
2649+
fn try_borrow_mut_unchecked(&self) -> Result<RefMut<'_, T>, BorrowMutError> {
2650+
self.ref_cell.try_borrow_mut()
2651+
}
2652+
2653+
#[inline]
2654+
#[track_caller]
2655+
#[allow(dead_code)]
2656+
fn try_borrow_mut<'ra, 'tcx>(
2657+
&self,
2658+
r: Resolver<'ra, 'tcx>,
2659+
) -> Result<RefMut<'_, T>, BorrowMutError> {
2660+
if r.assert_speculative {
2661+
panic!("Not allowed to mutably borrow a CmRefCell during speculative resolution");
2662+
}
2663+
self.ref_cell.try_borrow_mut()
2664+
}
2665+
2666+
#[inline]
2667+
#[track_caller]
2668+
fn borrow(&self) -> Ref<'_, T> {
2669+
self.ref_cell.borrow()
2670+
}
2671+
}

compiler/rustc_resolve/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
189189
let output_macro_rules_scope = self.build_reduced_graph(fragment, parent_scope);
190190
self.output_macro_rules_scopes.insert(expansion, output_macro_rules_scope);
191191

192-
parent_scope.module.unexpanded_invocations.borrow_mut().remove(&expansion);
192+
parent_scope.module.unexpanded_invocations.borrow_mut_unchecked().remove(&expansion);
193193
if let Some(unexpanded_invocations) =
194194
self.impl_unexpanded_invocations.get_mut(&self.invocation_parent(expansion))
195195
{

0 commit comments

Comments
 (0)