Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
// Don't add underscore imports to `single_imports`
// because they cannot define any usable names.
if target.name != kw::Underscore {
self.r.per_ns(|this, ns| {
self.r.per_ns_mut(|this, ns| {
let key = BindingKey::new(IdentKey::new(target), ns);
this.resolution_or_default(current_module.to_module(), key, target.span)
.borrow_mut(this)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ impl Resolver<'_, '_> {
let mut check_redundant_imports = FxIndexSet::default();
for module in &self.local_modules {
for (_key, resolution) in self.resolutions(module.to_module()).iter() {
if let Some(decl) = resolution.borrow(self).best_decl()
if let Some(decl) = resolution.borrow_checked(self).best_decl()
&& let DeclKind::Import { import, .. } = decl.kind
&& let ImportKind::Single { id, .. } = import.kind
{
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_resolve/src/diagnostics/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1873,7 +1873,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
self.resolutions(parent_scope.module).iter().any(|(key, name_resolution)| {
if key.ns == TypeNS
&& key.ident == *ident
&& let Some(decl) = name_resolution.borrow(self).best_decl()
&& let Some(decl) = name_resolution.borrow_checked(self).best_decl()
{
match decl.res() {
// No disambiguation needed if the identically named item we
Expand Down Expand Up @@ -3634,7 +3634,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let mut res = false;
let m = r.expect_module(parent_module);
if m.is_local() {
for importer in m.glob_importers.borrow(r).iter() {
for importer in m.glob_importers.borrow_checked(r).iter() {
if let Some(next_parent_module) = importer.parent_scope.module.opt_def_id()
{
if next_parent_module == module
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {

// Check if one of glob imports can still define the name,
// if it can then our "no resolution" result is not determined and can be invalidated.
for glob_import in module.globs.borrow(&self).iter() {
for glob_import in module.globs.borrow_checked(&self).iter() {
if ignore_import == Some(*glob_import) {
continue;
}
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
|| max_vis.get().is_none_or(|max_vis| vis.greater_than(max_vis, self.tcx)))
{
// `set` can't fail because this can only happen during "write_import_resolutions"
max_vis.set(Some(vis), self)
max_vis.set_checked(Some(vis), self)
}

self.arenas.alloc_decl(DeclData {
Expand Down Expand Up @@ -585,15 +585,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
&& glob_decl.ambiguity.get().is_none()
{
// Do not lose glob ambiguities when re-fetching the glob.
glob_decl.ambiguity.set(Some((old_ambig, true)), self);
glob_decl.ambiguity.set_checked(Some((old_ambig, true)), self);
}
glob_decl
} else if glob_decl.res() != old_glob_decl.res() {
let warning = self.is_noise_0_7_0(old_glob_decl, glob_decl)
|| self.is_rustybuzz_0_4_0(old_glob_decl, glob_decl)
|| self.is_pdf_0_9_0(old_glob_decl, glob_decl)
|| self.is_net2_0_2_39(old_glob_decl, glob_decl);
old_glob_decl.ambiguity.set(Some((glob_decl, warning)), self);
old_glob_decl.ambiguity.set_checked(Some((glob_decl, warning)), self);
old_glob_decl
} else if let old_vis = old_glob_decl.vis()
&& let vis = glob_decl.vis()
Expand All @@ -602,17 +602,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// We are glob-importing the same item but with a different visibility.
// All visibilities here are ordered because all of them are ancestors of `module`.
if vis.greater_than(old_vis, self.tcx) {
old_glob_decl.ambiguity_vis_max.set(Some(glob_decl), self);
old_glob_decl.ambiguity_vis_max.set_checked(Some(glob_decl), self);
} else if let old_min_vis = old_glob_decl.min_vis()
&& old_min_vis != vis
&& old_min_vis.greater_than(vis, self.tcx)
{
old_glob_decl.ambiguity_vis_min.set(Some(glob_decl), self);
old_glob_decl.ambiguity_vis_min.set_checked(Some(glob_decl), self);
}
old_glob_decl
} else if glob_decl.is_ambiguity_recursive() && !old_glob_decl.is_ambiguity_recursive() {
// Overwriting a non-ambiguous glob import with an ambiguous glob import.
old_glob_decl.ambiguity.set(Some((glob_decl, true)), self);
old_glob_decl.ambiguity.set_checked(Some((glob_decl, true)), self);
old_glob_decl
} else {
old_glob_decl
Expand Down Expand Up @@ -1011,7 +1011,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
pub(crate) fn lint_reexports(&mut self, exported_ambiguities: FxHashSet<Decl<'ra>>) {
for module in &self.local_modules {
for (key, resolution) in self.resolutions(module.to_module()).iter() {
let resolution = resolution.borrow(self);
let resolution = resolution.borrow_checked(self);
let Some(binding) = resolution.best_decl() else { continue };

// Report "cannot reexport" errors for exotic cases involving macros 2.0
Expand Down Expand Up @@ -1808,7 +1808,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
.resolutions(module)
.iter()
.filter_map(|(key, resolution)| {
let res = resolution.borrow(self);
let res = resolution.borrow_checked(self);
let decl = res.determined_decl()?;
let mut key = *key;
let scope = match key.ident.ctxt.update_unchecked(|ctxt| {
Expand Down Expand Up @@ -1874,7 +1874,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
ambig_module_children: &mut LocalDefIdMap<Vec<AmbigModChild>>,
) {
// Since import resolution is finished, globs will not define any more names.
*module.globs.borrow_mut(self) = Vec::new();
*module.globs.borrow_mut_checked(self) = Vec::new();

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

Expand Down
68 changes: 47 additions & 21 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ impl<'ra> ModuleData<'ra> {
}

fn has_unexpanded_invocations<'tcx>(&self, r: &Resolver<'ra, 'tcx>) -> bool {
!self.unexpanded_invocations.borrow(r).is_empty()
!self.unexpanded_invocations.borrow_checked(r).is_empty()
}

fn res(&self) -> Option<Res> {
Expand All @@ -794,7 +794,7 @@ impl<'ra> Module<'ra> {
mut f: impl FnMut(&R, IdentKey, Span, Namespace, Decl<'ra>),
) {
for (key, name_resolution) in resolver.as_ref().resolutions(self).iter() {
let name_resolution = name_resolution.borrow(resolver.as_ref());
let name_resolution = name_resolution.borrow_checked(resolver.as_ref());
if let Some(decl) = name_resolution.best_decl() {
f(resolver, key.ident, name_resolution.orig_ident_span, key.ns, decl);
}
Expand All @@ -816,7 +816,7 @@ impl<'ra> Module<'ra> {

/// This modifies `self` in place. The traits will be stored in `self.traits`.
fn ensure_traits<'tcx>(self, resolver: &Resolver<'ra, 'tcx>) {
let mut traits = self.traits.borrow_mut(resolver.as_ref());
let mut traits = self.traits.borrow_mut_checked(resolver);
if traits.is_none() {
let mut collected_traits = Vec::new();
self.for_each_child(resolver, |r, ident, _, ns, mut decl| {
Expand Down Expand Up @@ -2184,7 +2184,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {

fn resolutions(&self, module: Module<'ra>) -> CmRef<'ra, ResolutionTable<'ra>> {
match &module.0.0.lazy_resolutions {
Resolutions::Local(local_res) => local_res.borrow(self),
Resolutions::Local(local_res) => local_res.borrow_checked(self),
Resolutions::Extern(extern_res) => {
// It is fine to return a `CmRef::Untracked`, we never give out a `&mut`
// to an external table.
Expand All @@ -2197,7 +2197,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}

fn resolutions_mut(&self, module: Module<'ra>) -> RefMut<'ra, ResolutionTable<'ra>> {
fn resolutions_mut(&mut self, module: Module<'ra>) -> RefMut<'ra, ResolutionTable<'ra>> {
match &module.0.0.lazy_resolutions {
Resolutions::Local(local_res) => local_res.borrow_mut(self),
Resolutions::Extern(_) => {
Expand All @@ -2213,12 +2213,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
module: Module<'ra>,
key: BindingKey,
) -> Option<CmRef<'ra, NameResolution<'ra>>> {
self.resolutions(module).get(&key).map(|resolution| resolution.0.borrow(self))
self.resolutions(module).get(&key).map(|resolution| resolution.0.borrow_checked(self))
}

#[track_caller]
fn resolution_or_default(
&self,
&mut self,
module: Module<'ra>,
key: BindingKey,
orig_ident_span: Span,
Expand Down Expand Up @@ -2908,10 +2908,11 @@ mod ref_mut {
self.0.get()
}

pub(crate) fn update<'ra, 'tcx>(&self, r: &Resolver<'ra, 'tcx>, f: impl FnOnce(T) -> T)
where
T: Copy,
{
pub(crate) fn update<'ra, 'tcx>(
&self,
r: &mut Resolver<'ra, 'tcx>,
f: impl FnOnce(T) -> T,
) {
let old = self.get();
self.set(f(old), r);
}
Expand All @@ -2922,10 +2923,15 @@ mod ref_mut {
CmCell(Cell::new(value))
}

pub(crate) fn set<'ra, 'tcx>(&self, val: T, r: &Resolver<'ra, 'tcx>) {
if r.speculative_flag.is_speculative() {
panic!("not allowed to mutate a `CmCell` during speculative resolution")
}
pub(crate) fn set<'ra, 'tcx>(&self, val: T, _: &mut Resolver<'ra, 'tcx>) {
self.0.set(val);
}

pub(crate) fn set_checked<'ra, 'tcx>(&self, val: T, r: &Resolver<'ra, 'tcx>) {
assert!(
!r.speculative_flag.is_speculative(),
"Cannot mutate `CmCell` during speculative resolution"
);
self.0.set(val);
}

Expand Down Expand Up @@ -2983,23 +2989,43 @@ mod ref_mut {
}

#[track_caller]
pub(crate) fn borrow_mut<'ra, 'tcx>(&self, r: &Resolver<'ra, 'tcx>) -> RefMut<'_, T> {
pub(crate) fn borrow_mut<'ra, 'tcx>(&self, r: &mut Resolver<'ra, 'tcx>) -> RefMut<'_, T> {
self.try_borrow_mut(r).unwrap()
}

#[track_caller]
pub(crate) fn try_borrow_mut<'ra, 'tcx>(
pub(crate) fn borrow_mut_checked<'ra, 'tcx>(
&self,
r: &Resolver<'ra, 'tcx>,
) -> RefMut<'_, T> {
self.try_borrow_mut_checked(r).unwrap()
}

#[track_caller]
pub(crate) fn try_borrow_mut_checked<'ra, 'tcx>(
&self,
r: &Resolver<'ra, 'tcx>,
) -> Result<RefMut<'_, T>, BorrowMutError> {
if r.speculative_flag.is_speculative() {
panic!("not allowed to mutably borrow a `CmRefCell` during speculative resolution");
}
assert!(
!r.speculative_flag.is_speculative(),
"Cannot mutate `CmRefCell` state/value during speculative resolution"
);
self.0.try_borrow_mut()
}

#[track_caller]
pub(crate) fn borrow<'ra, 'tcx>(&self, r: &Resolver<'ra, 'tcx>) -> CmRef<'_, T> {
pub(crate) fn try_borrow_mut<'ra, 'tcx>(
&self,
_: &mut Resolver<'ra, 'tcx>,
) -> Result<RefMut<'_, T>, BorrowMutError> {
self.0.try_borrow_mut()
}

pub(crate) fn borrow<'ra, 'tcx>(&self, _: &mut Resolver<'ra, 'tcx>) -> Ref<'_, T> {
self.0.borrow()
}

pub(crate) fn borrow_checked<'ra, 'tcx>(&self, r: &Resolver<'ra, 'tcx>) -> CmRef<'_, T> {
if r.speculative_flag.is_speculative() {
// `try_borrow_unguarded` is unsafe because it returns a `&T` instead
// of `Ref<'_, T>`. It does provides an extra check to make sure no live
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
PathResult::Module(..) => unreachable!(),
};

self.multi_segment_macro_resolutions.borrow_mut(&self).push((
self.multi_segment_macro_resolutions.borrow_mut_checked(&self).push((
path,
path_span,
kind,
Expand All @@ -888,7 +888,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
return Err(Determinacy::Undetermined);
}

self.single_segment_macro_resolutions.borrow_mut(&self).push((
self.single_segment_macro_resolutions.borrow_mut_checked(&self).push((
path[0].ident,
kind,
*parent_scope,
Expand Down
Loading