Skip to content

Commit f853922

Browse files
authored
Rollup merge of rust-lang#78524 - tmiasko:source-files-borrow, r=Aaron1011
Avoid BorrowMutError with RUSTC_LOG=debug ```console $ touch empty.rs $ env RUSTC_LOG=debug rustc +stage1 --crate-type=lib empty.rs ``` Fails with a `BorrowMutError` because source map files are already borrowed while `features_query` attempts to format a log message containing a span. Release the borrow before the query to avoid the issue.
2 parents 8111706 + a15e0dc commit f853922

File tree

6 files changed

+18
-9
lines changed

6 files changed

+18
-9
lines changed

compiler/rustc_data_structures/src/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl<T: Clone> Clone for Lock<T> {
512512
}
513513
}
514514

515-
#[derive(Debug)]
515+
#[derive(Debug, Default)]
516516
pub struct RwLock<T>(InnerRwLock<T>);
517517

518518
impl<T> RwLock<T> {

compiler/rustc_metadata/src/rmeta/encoder.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,10 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
20422042
encoder.emit_raw_bytes(&[0, 0, 0, 0]);
20432043

20442044
let source_map_files = tcx.sess.source_map().files();
2045+
let source_file_cache = (source_map_files[0].clone(), 0);
2046+
let required_source_files = Some(GrowableBitSet::with_capacity(source_map_files.len()));
2047+
drop(source_map_files);
2048+
20452049
let hygiene_ctxt = HygieneEncodeContext::default();
20462050

20472051
let mut ecx = EncodeContext {
@@ -2052,13 +2056,12 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
20522056
lazy_state: LazyState::NoNode,
20532057
type_shorthands: Default::default(),
20542058
predicate_shorthands: Default::default(),
2055-
source_file_cache: (source_map_files[0].clone(), 0),
2059+
source_file_cache,
20562060
interpret_allocs: Default::default(),
2057-
required_source_files: Some(GrowableBitSet::with_capacity(source_map_files.len())),
2061+
required_source_files,
20582062
is_proc_macro: tcx.sess.crate_types().contains(&CrateType::ProcMacro),
20592063
hygiene_ctxt: &hygiene_ctxt,
20602064
};
2061-
drop(source_map_files);
20622065

20632066
// Encode the rustc version string in a predictable location.
20642067
rustc_version().encode(&mut ecx).unwrap();

compiler/rustc_span/src/source_map.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub use crate::*;
1212

1313
use rustc_data_structures::fx::FxHashMap;
1414
use rustc_data_structures::stable_hasher::StableHasher;
15-
use rustc_data_structures::sync::{AtomicU32, Lock, LockGuard, Lrc, MappedLockGuard};
15+
use rustc_data_structures::sync::{AtomicU32, Lrc, MappedReadGuard, ReadGuard, RwLock};
1616
use std::cmp;
1717
use std::convert::TryFrom;
1818
use std::hash::Hash;
@@ -168,7 +168,7 @@ pub struct SourceMap {
168168
/// The address space below this value is currently used by the files in the source map.
169169
used_address_space: AtomicU32,
170170

171-
files: Lock<SourceMapFiles>,
171+
files: RwLock<SourceMapFiles>,
172172
file_loader: Box<dyn FileLoader + Sync + Send>,
173173
// This is used to apply the file path remapping as specified via
174174
// `--remap-path-prefix` to all `SourceFile`s allocated within this `SourceMap`.
@@ -236,8 +236,8 @@ impl SourceMap {
236236

237237
// By returning a `MonotonicVec`, we ensure that consumers cannot invalidate
238238
// any existing indices pointing into `files`.
239-
pub fn files(&self) -> MappedLockGuard<'_, monotonic::MonotonicVec<Lrc<SourceFile>>> {
240-
LockGuard::map(self.files.borrow(), |files| &mut files.source_files)
239+
pub fn files(&self) -> MappedReadGuard<'_, monotonic::MonotonicVec<Lrc<SourceFile>>> {
240+
ReadGuard::map(self.files.borrow(), |files| &files.source_files)
241241
}
242242

243243
pub fn source_file_by_stable_id(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// rustc-env:RUSTC_LOG=debug

src/test/ui/rustc-rust-log.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// dont-check-compiler-stdout
99
// dont-check-compiler-stderr
1010
// compile-flags: --error-format human
11-
11+
// aux-build: rustc-rust-log-aux.rs
1212
// rustc-env:RUSTC_LOG=debug
1313

1414
fn main() {}

src/tools/compiletest/src/runtest.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,11 @@ impl<'test> TestCx<'test> {
17751775
let mut aux_rustc =
17761776
aux_cx.make_compile_args(input_file, aux_output, EmitMetadata::No, AllowUnused::No);
17771777

1778+
for key in &aux_props.unset_rustc_env {
1779+
aux_rustc.env_remove(key);
1780+
}
1781+
aux_rustc.envs(aux_props.rustc_env.clone());
1782+
17781783
let (dylib, crate_type) = if aux_props.no_prefer_dynamic {
17791784
(true, None)
17801785
} else if self.config.target.contains("cloudabi")

0 commit comments

Comments
 (0)