Skip to content

Commit dbcb9dd

Browse files
address review
1 parent 5efb4e5 commit dbcb9dd

File tree

4 files changed

+91
-109
lines changed

4 files changed

+91
-109
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ use crate::Namespace::{MacroNS, TypeNS, ValueNS};
3434
use crate::def_collector::collect_definitions;
3535
use crate::imports::{ImportData, ImportKind};
3636
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
37+
use crate::ref_mut::CmCell;
3738
use crate::{
38-
BindingKey, CmCell, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind,
39-
ModuleOrUniformRoot, NameBinding, ParentScope, PathResult, ResolutionError, Resolver, Segment,
40-
Used, VisResolutionError, errors,
39+
BindingKey, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind, ModuleOrUniformRoot,
40+
NameBinding, ParentScope, PathResult, ResolutionError, Resolver, Segment, Used,
41+
VisResolutionError, errors,
4142
};
4243

4344
type Res = def::Res<NodeId>;
@@ -505,7 +506,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
505506
});
506507
}
507508
}
508-
ImportKind::Glob { .. } => current_module.globs.borrow_mut_unchecked().push(import),
509+
ImportKind::Glob { .. } => current_module.globs.borrow_mut(self.r).push(import),
509510
_ => unreachable!(),
510511
}
511512
}
@@ -1196,7 +1197,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11961197
/// directly into its parent scope's module.
11971198
fn visit_invoc_in_module(&mut self, id: NodeId) -> MacroRulesScopeRef<'ra> {
11981199
let invoc_id = self.visit_invoc(id);
1199-
self.parent_scope.module.unexpanded_invocations.borrow_mut_unchecked().insert(invoc_id);
1200+
self.parent_scope.module.unexpanded_invocations.borrow_mut(self.r).insert(invoc_id);
12001201
self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Invocation(invoc_id))
12011202
}
12021203

compiler/rustc_resolve/src/imports.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ use crate::errors::{
3131
CannotBeReexportedPrivateNS, CannotDetermineImportResolution, CannotGlobImportAllCrates,
3232
ConsiderAddingMacroExport, ConsiderMarkingAsPub, ConsiderMarkingAsPubCrate,
3333
};
34+
use crate::ref_mut::CmCell;
3435
use crate::{
35-
AmbiguityError, AmbiguityKind, BindingKey, CmCell, CmResolver, Determinacy, Finalize,
36-
ImportSuggestion, Module, ModuleOrUniformRoot, NameBinding, NameBindingData, NameBindingKind,
37-
ParentScope, PathResult, PerNS, ResolutionError, Resolver, ScopeSet, Segment, Used,
38-
module_to_string, names_to_string,
36+
AmbiguityError, AmbiguityKind, BindingKey, CmResolver, Determinacy, Finalize, ImportSuggestion,
37+
Module, ModuleOrUniformRoot, NameBinding, NameBindingData, NameBindingKind, ParentScope,
38+
PathResult, PerNS, ResolutionError, Resolver, ScopeSet, Segment, Used, module_to_string,
39+
names_to_string,
3940
};
4041

4142
type Res = def::Res<NodeId>;
@@ -1553,7 +1554,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15531554
// reporting conflicts, and reporting unresolved imports.
15541555
fn finalize_resolutions_in(&mut self, module: Module<'ra>) {
15551556
// Since import resolution is finished, globs will not define any more names.
1556-
*module.globs.borrow_mut_unchecked() = Vec::new();
1557+
*module.globs.borrow_mut(self) = Vec::new();
15571558

15581559
let Some(def_id) = module.opt_def_id() else { return };
15591560

compiler/rustc_resolve/src/lib.rs

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

28-
use std::cell::{BorrowMutError, Cell, Ref, RefCell, RefMut};
28+
use std::cell::{Cell, Ref, RefCell};
2929
use std::collections::BTreeSet;
30-
use std::fmt::{self, Formatter};
30+
use std::fmt::{self};
3131
use std::sync::Arc;
3232

3333
use diagnostics::{ImportSuggestion, LabelSuggestion, Suggestion};
@@ -96,6 +96,8 @@ pub mod rustdoc;
9696

9797
pub use macros::registered_tools_ast;
9898

99+
use crate::ref_mut::{CmCell, CmRefCell};
100+
99101
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
100102

101103
#[derive(Debug)]
@@ -593,9 +595,9 @@ struct ModuleData<'ra> {
593595
/// Resolutions in modules from other crates are not populated until accessed.
594596
lazy_resolutions: Resolutions<'ra>,
595597
/// True if this is a module from other crate that needs to be populated on access.
596-
populate_on_access: CmCell<bool>, // FIXME(batched): Use an atomic in batched import resolution?
598+
populate_on_access: Cell<bool>, // FIXME(parallel): Use an atomic in parallel import resolution
597599
/// Used to disambiguate underscore items (`const _: T = ...`) in the module.
598-
underscore_disambiguator: CmCell<u32>, // FIXME(batched): Use an atomic in batched import resolution?
600+
underscore_disambiguator: CmCell<u32>,
599601

600602
/// Macro invocations that can expand into items in this module.
601603
unexpanded_invocations: CmRefCell<FxHashSet<LocalExpnId>>,
@@ -656,7 +658,7 @@ impl<'ra> ModuleData<'ra> {
656658
parent,
657659
kind,
658660
lazy_resolutions: Default::default(),
659-
populate_on_access: CmCell::new(is_foreign),
661+
populate_on_access: Cell::new(is_foreign),
660662
underscore_disambiguator: CmCell::new(0),
661663
unexpanded_invocations: Default::default(),
662664
no_implicit_prelude,
@@ -697,7 +699,7 @@ impl<'ra> Module<'ra> {
697699

698700
/// This modifies `self` in place. The traits will be stored in `self.traits`.
699701
fn ensure_traits<'tcx>(self, resolver: &impl AsRef<Resolver<'ra, 'tcx>>) {
700-
let mut traits = self.traits.borrow_mut_unchecked();
702+
let mut traits = self.traits.borrow_mut(resolver.as_ref());
701703
if traits.is_none() {
702704
let mut collected_traits = Vec::new();
703705
self.for_each_child(resolver, |r, name, ns, binding| {
@@ -1976,7 +1978,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19761978
fn resolutions(&self, module: Module<'ra>) -> &'ra Resolutions<'ra> {
19771979
if module.populate_on_access.get() {
19781980
// FIXME(batched): Will be fixed in batched import resolution.
1979-
module.populate_on_access.set_unchecked(false);
1981+
module.populate_on_access.set(false);
19801982
self.build_reduced_graph_external(module);
19811983
}
19821984
&module.0.0.lazy_resolutions
@@ -2504,9 +2506,20 @@ pub fn provide(providers: &mut Providers) {
25042506
providers.registered_tools = macros::registered_tools;
25052507
}
25062508

2509+
/// A wrapper around `&mut Resolver` that may be mutable or immutable, depending on a conditions.
2510+
///
2511+
/// `Cm` stands for "conditionally mutable".
2512+
///
2513+
/// Prefer constructing it through [`Resolver::cm`] to ensure correctness.
2514+
type CmResolver<'r, 'ra, 'tcx> = ref_mut::RefOrMut<'r, Resolver<'ra, 'tcx>>;
2515+
25072516
mod ref_mut {
2517+
use std::cell::{BorrowMutError, Cell, Ref, RefCell, RefMut};
2518+
use std::fmt;
25082519
use std::ops::Deref;
25092520

2521+
use crate::Resolver;
2522+
25102523
/// A wrapper around a mutable reference that conditionally allows mutable access.
25112524
pub(crate) struct RefOrMut<'a, T> {
25122525
p: &'a mut T,
@@ -2555,117 +2568,84 @@ mod ref_mut {
25552568
self.p
25562569
}
25572570
}
2558-
}
25592571

2560-
/// A wrapper around `&mut Resolver` that may be mutable or immutable, depending on a conditions.
2561-
///
2562-
/// `Cm` stands for "conditionally mutable".
2563-
///
2564-
/// Prefer constructing it through [`Resolver::cm`] to ensure correctness.
2565-
type CmResolver<'r, 'ra, 'tcx> = ref_mut::RefOrMut<'r, Resolver<'ra, 'tcx>>;
2566-
2567-
#[derive(Default)]
2568-
struct CmCell<T> {
2569-
value: Cell<T>,
2570-
}
2571-
2572-
impl<T: Copy + fmt::Debug> fmt::Debug for CmCell<T> {
2573-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2574-
f.debug_struct("CmCell").field("value", &self.get()).finish()
2575-
}
2576-
}
2572+
#[derive(Default)]
2573+
pub(crate) struct CmCell<T>(Cell<T>);
25772574

2578-
impl<T: Copy> Clone for CmCell<T> {
2579-
#[inline]
2580-
fn clone(&self) -> CmCell<T> {
2581-
CmCell::new(self.get())
2575+
impl<T: Copy + fmt::Debug> fmt::Debug for CmCell<T> {
2576+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2577+
f.debug_struct("CmCell").field("value", &self.get()).finish()
2578+
}
25822579
}
2583-
}
25842580

2585-
impl<T: Copy> CmCell<T> {
2586-
const fn get(&self) -> T {
2587-
self.value.get()
2581+
impl<T: Copy> Clone for CmCell<T> {
2582+
#[inline]
2583+
fn clone(&self) -> CmCell<T> {
2584+
CmCell::new(self.get())
2585+
}
25882586
}
25892587

2590-
fn update_unchecked(&self, f: impl FnOnce(T) -> T)
2591-
where
2592-
T: Copy,
2593-
{
2594-
let old = self.get();
2595-
self.set_unchecked(f(old));
2596-
}
2597-
}
2588+
impl<T: Copy> CmCell<T> {
2589+
pub(crate) const fn get(&self) -> T {
2590+
self.0.get()
2591+
}
25982592

2599-
impl<T> CmCell<T> {
2600-
const fn new(value: T) -> CmCell<T> {
2601-
CmCell { value: Cell::new(value) }
2593+
pub(crate) fn update_unchecked(&self, f: impl FnOnce(T) -> T)
2594+
where
2595+
T: Copy,
2596+
{
2597+
let old = self.get();
2598+
self.set_unchecked(f(old));
2599+
}
26022600
}
26032601

2604-
#[track_caller]
2605-
#[allow(dead_code)]
2606-
fn set<'ra, 'tcx>(&self, val: T, r: &Resolver<'ra, 'tcx>) {
2607-
if r.assert_speculative {
2608-
panic!("Not allowed to mutate CmCell during speculative resolution");
2602+
impl<T> CmCell<T> {
2603+
pub(crate) const fn new(value: T) -> CmCell<T> {
2604+
CmCell(Cell::new(value))
26092605
}
2610-
self.value.set(val);
2611-
}
26122606

2613-
fn set_unchecked(&self, val: T) {
2614-
self.value.set(val);
2615-
}
2607+
pub(crate) fn set_unchecked(&self, val: T) {
2608+
self.0.set(val);
2609+
}
26162610

2617-
fn into_inner(self) -> T {
2618-
self.value.into_inner()
2611+
pub(crate) fn into_inner(self) -> T {
2612+
self.0.into_inner()
2613+
}
26192614
}
2620-
}
2621-
#[derive(Default)]
2622-
struct CmRefCell<T> {
2623-
ref_cell: RefCell<T>,
2624-
}
26252615

2626-
impl<T> CmRefCell<T> {
2627-
const fn new(value: T) -> CmRefCell<T> {
2628-
CmRefCell { ref_cell: RefCell::new(value) }
2629-
}
2616+
#[derive(Default)]
2617+
pub(crate) struct CmRefCell<T>(RefCell<T>);
26302618

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");
2619+
impl<T> CmRefCell<T> {
2620+
pub(crate) const fn new(value: T) -> CmRefCell<T> {
2621+
CmRefCell(RefCell::new(value))
26372622
}
2638-
self.ref_cell.borrow_mut()
2639-
}
26402623

2641-
#[inline]
2642-
#[track_caller]
2643-
fn borrow_mut_unchecked(&self) -> RefMut<'_, T> {
2644-
self.ref_cell.borrow_mut()
2645-
}
2624+
#[inline]
2625+
#[track_caller]
2626+
pub(crate) fn borrow_mut_unchecked(&self) -> RefMut<'_, T> {
2627+
self.0.borrow_mut()
2628+
}
26462629

2647-
#[inline]
2648-
#[track_caller]
2649-
fn try_borrow_mut_unchecked(&self) -> Result<RefMut<'_, T>, BorrowMutError> {
2650-
self.ref_cell.try_borrow_mut()
2651-
}
2630+
#[inline]
2631+
#[track_caller]
2632+
pub(crate) fn borrow_mut<'ra, 'tcx>(&self, r: &Resolver<'ra, 'tcx>) -> RefMut<'_, T> {
2633+
if r.assert_speculative {
2634+
panic!("Not allowed to mutably borrow a CmRefCell during speculative resolution");
2635+
}
2636+
self.borrow_mut_unchecked()
2637+
}
26522638

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");
2639+
#[inline]
2640+
#[track_caller]
2641+
pub(crate) fn try_borrow_mut_unchecked(&self) -> Result<RefMut<'_, T>, BorrowMutError> {
2642+
self.0.try_borrow_mut()
26622643
}
2663-
self.ref_cell.try_borrow_mut()
2664-
}
26652644

2666-
#[inline]
2667-
#[track_caller]
2668-
fn borrow(&self) -> Ref<'_, T> {
2669-
self.ref_cell.borrow()
2645+
#[inline]
2646+
#[track_caller]
2647+
pub(crate) fn borrow(&self) -> Ref<'_, T> {
2648+
self.0.borrow()
2649+
}
26702650
}
26712651
}

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_unchecked().remove(&expansion);
192+
parent_scope.module.unexpanded_invocations.borrow_mut(self).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)