From 2661a4edb90773b22bab3af31fa12a25963c1b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Thu, 29 Oct 2020 00:00:00 +0000 Subject: [PATCH 1/4] Avoid BorrowMutError with RUSTC_LOG=debug $ 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. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 7e33a479228..2a81737e168 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -2042,6 +2042,10 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata { encoder.emit_raw_bytes(&[0, 0, 0, 0]); let source_map_files = tcx.sess.source_map().files(); + let source_file_cache = (source_map_files[0].clone(), 0); + let required_source_files = Some(GrowableBitSet::with_capacity(source_map_files.len())); + drop(source_map_files); + let hygiene_ctxt = HygieneEncodeContext::default(); let mut ecx = EncodeContext { @@ -2052,13 +2056,12 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata { lazy_state: LazyState::NoNode, type_shorthands: Default::default(), predicate_shorthands: Default::default(), - source_file_cache: (source_map_files[0].clone(), 0), + source_file_cache, interpret_allocs: Default::default(), - required_source_files: Some(GrowableBitSet::with_capacity(source_map_files.len())), + required_source_files, is_proc_macro: tcx.sess.crate_types().contains(&CrateType::ProcMacro), hygiene_ctxt: &hygiene_ctxt, }; - drop(source_map_files); // Encode the rustc version string in a predictable location. rustc_version().encode(&mut ecx).unwrap(); From 79cc5099b1825dae5507e74a09b6f6eb913667c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Thu, 29 Oct 2020 00:00:00 +0000 Subject: [PATCH 2/4] Use RwLock instead of Lock for SourceMap::files --- compiler/rustc_data_structures/src/sync.rs | 2 +- compiler/rustc_span/src/source_map.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_data_structures/src/sync.rs b/compiler/rustc_data_structures/src/sync.rs index d22f3adfb01..26706cd2b1b 100644 --- a/compiler/rustc_data_structures/src/sync.rs +++ b/compiler/rustc_data_structures/src/sync.rs @@ -512,7 +512,7 @@ fn clone(&self) -> Self { } } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct RwLock(InnerRwLock); impl RwLock { diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 3b929c4acb9..f067cdb7308 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -12,7 +12,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::StableHasher; -use rustc_data_structures::sync::{AtomicU32, Lock, LockGuard, Lrc, MappedLockGuard}; +use rustc_data_structures::sync::{AtomicU32, Lrc, MappedReadGuard, ReadGuard, RwLock}; use std::cmp; use std::convert::TryFrom; use std::hash::Hash; @@ -168,7 +168,7 @@ pub struct SourceMap { /// The address space below this value is currently used by the files in the source map. used_address_space: AtomicU32, - files: Lock, + files: RwLock, file_loader: Box, // This is used to apply the file path remapping as specified via // `--remap-path-prefix` to all `SourceFile`s allocated within this `SourceMap`. @@ -236,8 +236,8 @@ pub fn load_binary_file(&self, path: &Path) -> io::Result> { // By returning a `MonotonicVec`, we ensure that consumers cannot invalidate // any existing indices pointing into `files`. - pub fn files(&self) -> MappedLockGuard<'_, monotonic::MonotonicVec>> { - LockGuard::map(self.files.borrow(), |files| &mut files.source_files) + pub fn files(&self) -> MappedReadGuard<'_, monotonic::MonotonicVec>> { + ReadGuard::map(self.files.borrow(), |files| &files.source_files) } pub fn source_file_by_stable_id( From 4fc21689d8cfd43a9c5d8a682f54906d326eb5cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Thu, 29 Oct 2020 00:00:00 +0000 Subject: [PATCH 3/4] Add support for rustc-env and unset-rustc-env for aux-builds --- src/tools/compiletest/src/runtest.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index d85558ea2f5..666e5d402ef 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -1775,6 +1775,11 @@ fn build_auxiliary(&self, source_path: &str, aux_dir: &Path) -> bool { let mut aux_rustc = aux_cx.make_compile_args(input_file, aux_output, EmitMetadata::No, AllowUnused::No); + for key in &aux_props.unset_rustc_env { + aux_rustc.env_remove(key); + } + aux_rustc.envs(aux_props.rustc_env.clone()); + let (dylib, crate_type) = if aux_props.no_prefer_dynamic { (true, None) } else if self.config.target.contains("cloudabi") From a15e0dc4991a6c06dd5108d72163fb9809db2b07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Thu, 29 Oct 2020 00:00:00 +0000 Subject: [PATCH 4/4] Test building of libraries with rustc logging enabled --- src/test/ui/auxiliary/rustc-rust-log-aux.rs | 1 + src/test/ui/rustc-rust-log.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/auxiliary/rustc-rust-log-aux.rs diff --git a/src/test/ui/auxiliary/rustc-rust-log-aux.rs b/src/test/ui/auxiliary/rustc-rust-log-aux.rs new file mode 100644 index 00000000000..daa8e9f495e --- /dev/null +++ b/src/test/ui/auxiliary/rustc-rust-log-aux.rs @@ -0,0 +1 @@ +// rustc-env:RUSTC_LOG=debug diff --git a/src/test/ui/rustc-rust-log.rs b/src/test/ui/rustc-rust-log.rs index 1c4252b23ea..8ceb24dd2af 100644 --- a/src/test/ui/rustc-rust-log.rs +++ b/src/test/ui/rustc-rust-log.rs @@ -8,7 +8,7 @@ // dont-check-compiler-stdout // dont-check-compiler-stderr // compile-flags: --error-format human - +// aux-build: rustc-rust-log-aux.rs // rustc-env:RUSTC_LOG=debug fn main() {}