Skip to content

Commit e0830fa

Browse files
implement unsafe speculative flag to be used by CmRefCell::borrow, which does tracked and untracked borrowing
1 parent 7c329d6 commit e0830fa

8 files changed

Lines changed: 107 additions & 44 deletions

File tree

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ impl Resolver<'_, '_> {
559559
let mut check_redundant_imports = FxIndexSet::default();
560560
for module in &self.local_modules {
561561
for (_key, resolution) in self.resolutions(module.to_module()).iter() {
562-
if let Some(decl) = resolution.borrow().best_decl()
562+
if let Some(decl) = resolution.borrow(self).best_decl()
563563
&& let DeclKind::Import { import, .. } = decl.kind
564564
&& let ImportKind::Single { id, .. } = import.kind
565565
{

compiler/rustc_resolve/src/diagnostics/impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18731873
self.resolutions(parent_scope.module).iter().any(|(key, name_resolution)| {
18741874
if key.ns == TypeNS
18751875
&& key.ident == *ident
1876-
&& let Some(decl) = name_resolution.borrow().best_decl()
1876+
&& let Some(decl) = name_resolution.borrow(self).best_decl()
18771877
{
18781878
match decl.res() {
18791879
// No disambiguation needed if the identically named item we
@@ -3603,7 +3603,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
36033603
let mut res = false;
36043604
let m = r.expect_module(parent_module);
36053605
if m.is_local() {
3606-
for importer in m.glob_importers.borrow().iter() {
3606+
for importer in m.glob_importers.borrow(r).iter() {
36073607
if let Some(next_parent_module) = importer.parent_scope.module.opt_def_id()
36083608
{
36093609
if next_parent_module == module

compiler/rustc_resolve/src/effective_visibilities.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
126126
fn set_bindings_effective_visibilities(&mut self, module_id: LocalDefId) {
127127
let module = self.r.expect_module(module_id.to_def_id());
128128
for (_, name_resolution) in self.r.resolutions(module).iter() {
129-
let Some(decl) = name_resolution.borrow().best_decl() else {
129+
let Some(decl) = name_resolution.borrow(self.r).best_decl() else {
130130
continue;
131131
};
132132
self.update_decl_chain(decl, ParentId::Def(module_id));
@@ -310,7 +310,7 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
310310
if self.macro_reachable.insert((module_def_id, defining_mod)) {
311311
let module = self.r.expect_module(module_def_id.to_def_id());
312312
for (_, name_resolution) in self.r.resolutions(module).iter() {
313-
let Some(decl) = name_resolution.borrow().best_decl() else {
313+
let Some(decl) = name_resolution.borrow(self.r).best_decl() else {
314314
continue;
315315
};
316316

compiler/rustc_resolve/src/ident.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
714714
}
715715
Scope::MacroUsePrelude => match self.macro_use_prelude.get(&ident.name).cloned() {
716716
Some(decl) => Ok(decl),
717-
None => Err(Determinacy::determined(!self.graph_root.has_unexpanded_invocations())),
717+
None => {
718+
Err(Determinacy::determined(!self.graph_root.has_unexpanded_invocations(&self)))
719+
}
718720
},
719721
Scope::BuiltinAttrs => match self.builtin_attr_decls.get(&ident.name) {
720722
Some(decl) => Ok(*decl),
@@ -727,9 +729,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
727729
finalize.is_some(),
728730
) {
729731
Some(decl) => Ok(decl),
730-
None => {
731-
Err(Determinacy::determined(!self.graph_root.has_unexpanded_invocations()))
732-
}
732+
None => Err(Determinacy::determined(
733+
!self.graph_root.has_unexpanded_invocations(&self),
734+
)),
733735
}
734736
}
735737
Scope::ExternPreludeFlags => {
@@ -1158,7 +1160,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11581160

11591161
if let Some(finalize) = finalize {
11601162
// finalize implies that the module is fully expanded
1161-
assert!(!module.has_unexpanded_invocations());
1163+
assert!(!module.has_unexpanded_invocations(&self));
11621164
return self.get_mut().finalize_module_binding(
11631165
ident,
11641166
orig_ident_span,
@@ -1195,7 +1197,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11951197
}
11961198

11971199
// Check if one of unexpanded macros can still define the name.
1198-
if module.has_unexpanded_invocations() {
1200+
if module.has_unexpanded_invocations(&self) {
11991201
return Err(ControlFlow::Continue(Undetermined));
12001202
}
12011203

@@ -1224,7 +1226,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12241226

12251227
if let Some(finalize) = finalize {
12261228
// finalize implies that the module is fully expanded
1227-
assert!(!module.has_unexpanded_invocations());
1229+
assert!(!module.has_unexpanded_invocations(&self));
12281230
return self.get_mut().finalize_module_binding(
12291231
ident,
12301232
orig_ident_span,
@@ -1268,7 +1270,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12681270
// and prohibit access to macro-expanded `macro_export` macros instead (unless restricted
12691271
// shadowing is enabled, see `macro_expanded_macro_export_errors`).
12701272
if let Some(binding) = binding {
1271-
return if binding.determined() || ns == MacroNS || shadowing == Shadowing::Restricted {
1273+
return if binding.determined(&self)
1274+
|| ns == MacroNS
1275+
|| shadowing == Shadowing::Restricted
1276+
{
12721277
let accessible = self.is_accessible_from(binding.vis(), parent_scope.module);
12731278
if accessible { Ok(binding) } else { Err(ControlFlow::Break(Determined)) }
12741279
} else {
@@ -1283,13 +1288,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12831288
// scopes we return `Undetermined` with `ControlFlow::Continue`.
12841289
// Check if one of unexpanded macros can still define the name,
12851290
// if it can then our "no resolution" result is not determined and can be invalidated.
1286-
if module.has_unexpanded_invocations() {
1291+
if module.has_unexpanded_invocations(&self) {
12871292
return Err(ControlFlow::Continue(Undetermined));
12881293
}
12891294

12901295
// Check if one of glob imports can still define the name,
12911296
// if it can then our "no resolution" result is not determined and can be invalidated.
1292-
for glob_import in module.globs.borrow().iter() {
1297+
for glob_import in module.globs.borrow(&self).iter() {
12931298
if ignore_import == Some(*glob_import) {
12941299
continue;
12951300
}

compiler/rustc_resolve/src/imports.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -781,14 +781,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
781781

782782
let mut imports_to_resolve = mem::take(&mut self.indeterminate_imports);
783783

784-
self.assert_speculative = true;
784+
// SAFETY: This is a "top-level" function used by the macro expansion code, unless some
785+
// weird thing is done, all `tracked` borrows done in the previous call of
786+
// `resolve_imports` are dropped when that call ended.
787+
unsafe { self.speculative_flag.set(true) };
785788
rustc_data_structures::sync::par_for_each_slice(
786789
&mut imports_to_resolve,
787790
|(import, resolution, indeterminate_count)| {
788791
(*resolution, *indeterminate_count) = self.resolve_import(*import);
789792
},
790793
);
791-
self.assert_speculative = false;
794+
// SAFETY: All `untracked` borrows are dropped after the `par_for_each_slice` call,
795+
// as they cannot escape since they are tied to the `CmRefCell` they borrowed from.
796+
//
797+
// Note: Some `CmRefCell`s are arena allocated and thus have the `'ra` lifetime,
798+
// allowing these borrows to escape, but that does not and should not happen.
799+
unsafe { self.speculative_flag.set(false) };
792800

793801
self.write_import_resolutions(&imports_to_resolve);
794802

@@ -1003,7 +1011,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10031011
pub(crate) fn lint_reexports(&mut self, exported_ambiguities: FxHashSet<Decl<'ra>>) {
10041012
for module in &self.local_modules {
10051013
for (key, resolution) in self.resolutions(module.to_module()).iter() {
1006-
let resolution = resolution.borrow();
1014+
let resolution = resolution.borrow(self);
10071015
let Some(binding) = resolution.best_decl() else { continue };
10081016

10091017
// Report "cannot reexport" errors for exotic cases involving macros 2.0
@@ -1490,7 +1498,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14901498
return None;
14911499
} // `use _` is never valid
14921500

1493-
let resolution = resolution.borrow();
1501+
let resolution = resolution.borrow(self);
14941502
if let Some(name_binding) = resolution.best_decl() {
14951503
match name_binding.kind {
14961504
DeclKind::Import { source_decl, .. } => {
@@ -1800,7 +1808,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18001808
.resolutions(module)
18011809
.iter()
18021810
.filter_map(|(key, resolution)| {
1803-
let res = resolution.borrow();
1811+
let res = resolution.borrow(self);
18041812
let decl = res.determined_decl()?;
18051813
let mut key = *key;
18061814
let scope = match key.ident.ctxt.update_unchecked(|ctxt| {

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
194194
if key.ident.name != assoc_name {
195195
return None;
196196
}
197-
let resolution = resolution.borrow();
197+
let resolution = resolution.borrow(self.r);
198198
let binding = resolution.best_decl()?;
199199
match binding.res() {
200200
Res::Def(DefKind::AssocTy, def_id) => Some(def_id),
@@ -1165,7 +1165,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11651165
let find_doc_alias_name = |r: &mut Resolver<'ra, '_>, m: Module<'ra>, item_name: Symbol| {
11661166
for resolution in r.resolutions(m).values() {
11671167
let Some(did) =
1168-
resolution.borrow().best_decl().and_then(|binding| binding.res().opt_def_id())
1168+
resolution.borrow(r).best_decl().and_then(|binding| binding.res().opt_def_id())
11691169
else {
11701170
continue;
11711171
};
@@ -1905,7 +1905,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
19051905
.resolutions(module)
19061906
.iter()
19071907
.filter_map(|(key, resolution)| {
1908-
let resolution = resolution.borrow();
1908+
let resolution = resolution.borrow(self.r);
19091909
resolution.best_decl().map(|binding| binding.res()).and_then(|res| {
19101910
if filter_fn(res) {
19111911
Some((key.ident.name, resolution.orig_ident_span, res))
@@ -2766,7 +2766,9 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
27662766
.r
27672767
.resolutions(*module)
27682768
.iter()
2769-
.filter_map(|(key, res)| res.borrow().best_decl().map(|binding| (key, binding.res())))
2769+
.filter_map(|(key, res)| {
2770+
res.borrow(self.r).best_decl().map(|binding| (key, binding.res()))
2771+
})
27702772
.filter(|(_, res)| match (kind, res) {
27712773
(AssocItemKind::Const(..), Res::Def(DefKind::AssocConst { .. }, _)) => true,
27722774
(AssocItemKind::Fn(_), Res::Def(DefKind::AssocFn, _)) => true,

0 commit comments

Comments
 (0)