From c8390cdbfaccc55aa374222e39cdf9092a1e5ff6 Mon Sep 17 00:00:00 2001 From: Michael Baikov Date: Sat, 6 Apr 2024 11:22:21 -0400 Subject: [PATCH 01/12] Show files produced by --emit foo in json artifact notifications --- .../rustc_codegen_cranelift/src/driver/aot.rs | 23 ++++++++++ compiler/rustc_codegen_ssa/src/back/write.rs | 23 ++++++++++ compiler/rustc_codegen_ssa/src/lib.rs | 18 ++++++++ compiler/rustc_mir_transform/src/dump_mir.rs | 3 ++ src/doc/rustc/src/json.md | 8 +++- .../run-make/notify-all-emit-artifacts/lib.rs | 21 +++++++++ .../notify-all-emit-artifacts/rmake.rs | 45 +++++++++++++++++++ 7 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 tests/run-make/notify-all-emit-artifacts/lib.rs create mode 100644 tests/run-make/notify-all-emit-artifacts/rmake.rs diff --git a/compiler/rustc_codegen_cranelift/src/driver/aot.rs b/compiler/rustc_codegen_cranelift/src/driver/aot.rs index e8c96486041b1..aff9448a89c30 100644 --- a/compiler/rustc_codegen_cranelift/src/driver/aot.rs +++ b/compiler/rustc_codegen_cranelift/src/driver/aot.rs @@ -287,6 +287,29 @@ fn produce_final_output_artifacts( } } + if sess.opts.json_artifact_notifications { + if codegen_results.modules.len() == 1 { + codegen_results.modules[0].for_each_output(|_path, ty| { + if sess.opts.output_types.contains_key(&ty) { + let descr = ty.shorthand(); + // for single cgu file is renamed to drop cgu specific suffix + // so we regenerate it the same way + let path = crate_output.path(ty); + sess.dcx().emit_artifact_notification(path.as_path(), descr); + } + }); + } else { + for module in &codegen_results.modules { + module.for_each_output(|path, ty| { + if sess.opts.output_types.contains_key(&ty) { + let descr = ty.shorthand(); + sess.dcx().emit_artifact_notification(&path, descr); + } + }); + } + } + } + // We leave the following files around by default: // - #crate#.o // - #crate#.crate.metadata.o diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index c4f062405bb5c..72030dcbac54f 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -706,6 +706,29 @@ fn produce_final_output_artifacts( } } + if sess.opts.json_artifact_notifications { + if compiled_modules.modules.len() == 1 { + compiled_modules.modules[0].for_each_output(|_path, ty| { + if sess.opts.output_types.contains_key(&ty) { + let descr = ty.shorthand(); + // for single cgu file is renamed to drop cgu specific suffix + // so we regenerate it the same way + let path = crate_output.path(ty); + sess.dcx().emit_artifact_notification(path.as_path(), descr); + } + }); + } else { + for module in &compiled_modules.modules { + module.for_each_output(|path, ty| { + if sess.opts.output_types.contains_key(&ty) { + let descr = ty.shorthand(); + sess.dcx().emit_artifact_notification(&path, descr); + } + }); + } + } + } + // We leave the following files around by default: // - #crate#.o // - #crate#.crate.metadata.o diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index 80fe7e0bb7865..7e0e5a9e632e7 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -113,6 +113,24 @@ pub struct CompiledModule { pub llvm_ir: Option, // --emit=llvm-ir, llvm-bc is in bytecode } +impl CompiledModule { + /// Call `emit` function with every artifact type currently compiled + pub fn for_each_output(&self, mut emit: impl FnMut(&Path, OutputType)) { + if let Some(path) = self.object.as_deref() { + emit(path, OutputType::Object); + } + if let Some(path) = self.bytecode.as_deref() { + emit(path, OutputType::Bitcode); + } + if let Some(path) = self.llvm_ir.as_deref() { + emit(path, OutputType::LlvmAssembly); + } + if let Some(path) = self.assembly.as_deref() { + emit(path, OutputType::Assembly); + } + } +} + pub struct CachedModuleCodegen { pub name: String, pub source: WorkProduct, diff --git a/compiler/rustc_mir_transform/src/dump_mir.rs b/compiler/rustc_mir_transform/src/dump_mir.rs index 13841be494cf0..3b71cf02c1a0a 100644 --- a/compiler/rustc_mir_transform/src/dump_mir.rs +++ b/compiler/rustc_mir_transform/src/dump_mir.rs @@ -28,6 +28,9 @@ pub fn emit_mir(tcx: TyCtxt<'_>) -> io::Result<()> { OutFileName::Real(path) => { let mut f = io::BufWriter::new(File::create(&path)?); write_mir_pretty(tcx, None, &mut f)?; + if tcx.sess.opts.json_artifact_notifications { + tcx.dcx().emit_artifact_notification(&path, "mir"); + } } } Ok(()) diff --git a/src/doc/rustc/src/json.md b/src/doc/rustc/src/json.md index 32083b2f731d8..c853f34ee036c 100644 --- a/src/doc/rustc/src/json.md +++ b/src/doc/rustc/src/json.md @@ -217,7 +217,8 @@ Diagnostics have the following format: Artifact notifications are emitted when the [`--json=artifacts` flag][option-json] is used. They indicate that a file artifact has been saved to disk. More information about emit kinds may be found in the [`--emit` -flag][option-emit] documentation. +flag][option-emit] documentation. Notifications can contain more than one file +for each type, for example when using multiple codegen units. ```javascript { @@ -229,6 +230,11 @@ flag][option-emit] documentation. - "link": The generated crate as specified by the crate-type. - "dep-info": The `.d` file with dependency information in a Makefile-like syntax. - "metadata": The Rust `.rmeta` file containing metadata about the crate. + - "asm": The `.s` file with generated assembly + - "llvm-ir": The `.ll` file with generated textual LLVM IR + - "llvm-bc": The `.bc` file with generated LLVM bitcode + - "mir": The `.mir` file with rustc's mid-level intermediate representation. + - "obj": The `.o` file with generated native object code */ "emit": "link" } diff --git a/tests/run-make/notify-all-emit-artifacts/lib.rs b/tests/run-make/notify-all-emit-artifacts/lib.rs new file mode 100644 index 0000000000000..6ed194204b454 --- /dev/null +++ b/tests/run-make/notify-all-emit-artifacts/lib.rs @@ -0,0 +1,21 @@ +fn one() -> usize { + 1 +} + +pub mod a { + pub fn two() -> usize { + ::one() + ::one() + } +} + +pub mod b { + pub fn three() -> usize { + ::one() + ::a::two() + } +} + +#[inline(never)] +pub fn main() { + a::two(); + b::three(); +} diff --git a/tests/run-make/notify-all-emit-artifacts/rmake.rs b/tests/run-make/notify-all-emit-artifacts/rmake.rs new file mode 100644 index 0000000000000..c866d9179f94b --- /dev/null +++ b/tests/run-make/notify-all-emit-artifacts/rmake.rs @@ -0,0 +1,45 @@ +// rust should produce artifact notifications about files it was asked to --emit. +// +// It should work in incremental mode both on the first pass where files are generated as well +// as on subsequent passes where they are taken from the incremental cache +// +// See +extern crate run_make_support; + +use run_make_support::{rustc, tmp_dir}; + +fn main() { + let inc_dir = tmp_dir(); + + // With single codegen unit files are renamed to match the source file name + for _ in 0..=1 { + let output = rustc() + .input("lib.rs") + .emit("obj,asm,llvm-ir,llvm-bc,mir") + .codegen_units(1) + .json("artifacts") + .error_format("json") + .incremental(&inc_dir) + .run(); + let stderr = String::from_utf8_lossy(&output.stderr); + for file in &["lib.o", "lib.ll", "lib.bc", "lib.s"] { + assert!(stderr.contains(file), "No {:?} in {:?}", file, stderr); + } + } + + // with multiple codegen units files keep codegen unit id part. + for _ in 0..=1 { + let output = rustc() + .input("lib.rs") + .emit("obj,asm,llvm-ir,llvm-bc,mir") + .codegen_units(2) + .json("artifacts") + .error_format("json") + .incremental(&inc_dir) + .run(); + let stderr = String::from_utf8_lossy(&output.stderr); + for file in &["rcgu.o", "rcgu.ll", "rcgu.bc", "rcgu.s"] { + assert!(stderr.contains(file), "No {:?} in {:?}", file, stderr); + } + } +} From d00a59c4a665c3ffa05c1ee39a1ab99a91a8e14e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 2 Jun 2024 11:51:08 +0200 Subject: [PATCH 02/12] Add new `MSVC_LIB_PATH` runtest environment variable to know location of the `msvc_lib` binary --- src/tools/compiletest/src/runtest.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index 79e158992d477..a104240aa1691 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -3369,6 +3369,7 @@ impl<'test> TestCx<'test> { cmd.env("IS_MSVC", "1") .env("IS_WINDOWS", "1") .env("MSVC_LIB", format!("'{}' -nologo", lib.display())) + .env("MSVC_LIB_PATH", format!("{}", lib.display())) .env("CC", format!("'{}' {}", self.config.cc, cflags)) .env("CXX", format!("'{}' {}", &self.config.cxx, cxxflags)); } else { From 5a00105321461d7ba920ed37acdf7fa78fcc41ab Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 2 Jun 2024 11:51:30 +0200 Subject: [PATCH 03/12] Add `Rustc::extra_filename` method --- src/tools/run-make-support/src/rustc.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/tools/run-make-support/src/rustc.rs b/src/tools/run-make-support/src/rustc.rs index 8b0252b8f04c4..be2d272dc130e 100644 --- a/src/tools/run-make-support/src/rustc.rs +++ b/src/tools/run-make-support/src/rustc.rs @@ -190,6 +190,11 @@ impl Rustc { self } + pub fn extra_filename(&mut self, extra: &str) -> &mut Self { + self.cmd.arg(format!("-Cextra-filename={extra}")); + self + } + /// Specify a stdin input pub fn stdin>(&mut self, input: I) -> &mut Self { self.stdin = Some(input.as_ref().to_vec().into_boxed_slice()); From e3ded7ba655fd76d2a2575f37e9ebd9e63af44e5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 2 Jun 2024 11:52:24 +0200 Subject: [PATCH 04/12] Add `run_make_support::build_native_static_lib` function --- src/tools/run-make-support/src/lib.rs | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index 0cf64db6ac9c0..da043c0e2bd8d 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -149,6 +149,50 @@ pub fn bin_name(name: &str) -> String { if is_windows() { format!("{name}.exe") } else { name.to_string() } } +fn ar, P2: AsRef>(obj_path: P, lib_path: P2, caller_line_number: u32) { + let mut ar = Command::new(env::var("AR").unwrap()); + ar.current_dir(tmp_dir()).arg("crus").arg(lib_path.as_ref()).arg(obj_path.as_ref()); + let output = ar.output().unwrap(); + if !output.status.success() { + handle_failed_output(&ar, output, caller_line_number); + } +} + +/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name. +#[track_caller] +pub fn build_native_static_lib(lib_name: &str) -> PathBuf { + let caller_location = std::panic::Location::caller(); + let caller_line_number = caller_location.line(); + + let obj_file = format!("{lib_name}.o"); + let src = format!("{lib_name}.c"); + let lib_name = if is_msvc() { + let lib_path = format!("lib{lib_name}.lib"); + // First compiling `.c` to `.o`. + cc().arg("-c").out_exe(lib_name).input(src).run(); + // Generating `.lib` from `.o`. + let mut msvc_lib = Command::new(env::var("MSVC_LIB_PATH").unwrap()); + msvc_lib + .current_dir(tmp_dir()) + .arg("-nologo") + .arg(&format!("-out:{}", cygpath_windows(&lib_path))) + .arg(&obj_file); + let output = msvc_lib.output().unwrap(); + if !output.status.success() { + handle_failed_output(&msvc_lib, output, caller_line_number); + } + lib_path + } else { + let lib_path = format!("lib{lib_name}.a"); + // First compiling `.c` to `.o`. + cc().arg("-v").arg("-c").out_exe(&obj_file).input(src).run(); + // Generating `.a` from `.o`. + ar(obj_file, &lib_path, caller_line_number); + lib_path + }; + tmp_dir().join(lib_name) +} + /// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is /// available on the platform! #[track_caller] From 050ff9c34372a90900773ae4c33e3139654ce1e0 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 2 Jun 2024 11:53:00 +0200 Subject: [PATCH 05/12] Migrate `run-make/issue-15460` to `rmake.rs` --- src/tools/tidy/src/allowed_run_make_makefiles.txt | 1 - tests/run-make/issue-15460/Makefile | 7 ------- tests/run-make/issue-15460/rmake.rs | 12 ++++++++++++ 3 files changed, 12 insertions(+), 8 deletions(-) delete mode 100644 tests/run-make/issue-15460/Makefile create mode 100644 tests/run-make/issue-15460/rmake.rs diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index ebe9d2b7bb0b9..a4eb0da1c4db5 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -87,7 +87,6 @@ run-make/issue-107094/Makefile run-make/issue-10971-temps-dir/Makefile run-make/issue-109934-lto-debuginfo/Makefile run-make/issue-14698/Makefile -run-make/issue-15460/Makefile run-make/issue-18943/Makefile run-make/issue-20626/Makefile run-make/issue-22131/Makefile diff --git a/tests/run-make/issue-15460/Makefile b/tests/run-make/issue-15460/Makefile deleted file mode 100644 index a36a085fa6f18..0000000000000 --- a/tests/run-make/issue-15460/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -# ignore-cross-compile -include ../tools.mk - -all: $(call NATIVE_STATICLIB,foo) - $(RUSTC) foo.rs -C extra-filename=-383hf8 -C prefer-dynamic - $(RUSTC) bar.rs - $(call RUN,bar) diff --git a/tests/run-make/issue-15460/rmake.rs b/tests/run-make/issue-15460/rmake.rs new file mode 100644 index 0000000000000..6f2c0d4e59536 --- /dev/null +++ b/tests/run-make/issue-15460/rmake.rs @@ -0,0 +1,12 @@ +//@ ignore-cross-compile + +use run_make_support::{build_native_static_lib, run, rustc}; + +fn main() { + build_native_static_lib("foo"); + + rustc().input("foo.rs").extra_filename("-383hf8").arg("-Cprefer-dynamic").run(); + rustc().input("bar.rs").run(); + + run("bar"); +} From 36aba94f1b9c053833ada18ecae986516d7d385b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 2 Jun 2024 14:20:39 +0200 Subject: [PATCH 06/12] Rename `tests/run-make/issue-15460` into `tests/run-make/link-native-static-lib-to-dylib` --- .../{issue-15460 => link-native-static-lib-to-dylib}/bar.rs | 0 .../{issue-15460 => link-native-static-lib-to-dylib}/foo.c | 0 .../{issue-15460 => link-native-static-lib-to-dylib}/foo.rs | 0 .../{issue-15460 => link-native-static-lib-to-dylib}/rmake.rs | 2 ++ 4 files changed, 2 insertions(+) rename tests/run-make/{issue-15460 => link-native-static-lib-to-dylib}/bar.rs (100%) rename tests/run-make/{issue-15460 => link-native-static-lib-to-dylib}/foo.c (100%) rename tests/run-make/{issue-15460 => link-native-static-lib-to-dylib}/foo.rs (100%) rename tests/run-make/{issue-15460 => link-native-static-lib-to-dylib}/rmake.rs (78%) diff --git a/tests/run-make/issue-15460/bar.rs b/tests/run-make/link-native-static-lib-to-dylib/bar.rs similarity index 100% rename from tests/run-make/issue-15460/bar.rs rename to tests/run-make/link-native-static-lib-to-dylib/bar.rs diff --git a/tests/run-make/issue-15460/foo.c b/tests/run-make/link-native-static-lib-to-dylib/foo.c similarity index 100% rename from tests/run-make/issue-15460/foo.c rename to tests/run-make/link-native-static-lib-to-dylib/foo.c diff --git a/tests/run-make/issue-15460/foo.rs b/tests/run-make/link-native-static-lib-to-dylib/foo.rs similarity index 100% rename from tests/run-make/issue-15460/foo.rs rename to tests/run-make/link-native-static-lib-to-dylib/foo.rs diff --git a/tests/run-make/issue-15460/rmake.rs b/tests/run-make/link-native-static-lib-to-dylib/rmake.rs similarity index 78% rename from tests/run-make/issue-15460/rmake.rs rename to tests/run-make/link-native-static-lib-to-dylib/rmake.rs index 6f2c0d4e59536..0746c3963143b 100644 --- a/tests/run-make/issue-15460/rmake.rs +++ b/tests/run-make/link-native-static-lib-to-dylib/rmake.rs @@ -1,3 +1,5 @@ +// Regression test for . + //@ ignore-cross-compile use run_make_support::{build_native_static_lib, run, rustc}; From c3de4b3aad6a2be86d4711086267e21660bf2b23 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 2 Jun 2024 12:40:07 +0000 Subject: [PATCH 07/12] Handle all GVN binops in a single place. --- compiler/rustc_mir_transform/src/gvn.rs | 70 ++++++++++++++----------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index fadb5edefdfb1..548e4e309360e 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -223,7 +223,6 @@ enum Value<'tcx> { NullaryOp(NullOp<'tcx>, Ty<'tcx>), UnaryOp(UnOp, VnIndex), BinaryOp(BinOp, VnIndex, VnIndex), - CheckedBinaryOp(BinOp, VnIndex, VnIndex), // FIXME get rid of this, work like MIR instead Cast { kind: CastKind, value: VnIndex, @@ -508,17 +507,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { let val = self.ecx.binary_op(bin_op, &lhs, &rhs).ok()?; val.into() } - CheckedBinaryOp(bin_op, lhs, rhs) => { - let lhs = self.evaluated[lhs].as_ref()?; - let lhs = self.ecx.read_immediate(lhs).ok()?; - let rhs = self.evaluated[rhs].as_ref()?; - let rhs = self.ecx.read_immediate(rhs).ok()?; - let val = self - .ecx - .binary_op(bin_op.wrapping_to_overflowing().unwrap(), &lhs, &rhs) - .ok()?; - val.into() - } Cast { kind, value, from: _, to } => match kind { CastKind::IntToInt | CastKind::IntToFloat => { let value = self.evaluated[value].as_ref()?; @@ -829,17 +817,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { let lhs = lhs?; let rhs = rhs?; - if let Some(op) = op.overflowing_to_wrapping() { - if let Some(value) = self.simplify_binary(op, true, ty, lhs, rhs) { - return Some(value); - } - Value::CheckedBinaryOp(op, lhs, rhs) - } else { - if let Some(value) = self.simplify_binary(op, false, ty, lhs, rhs) { - return Some(value); - } - Value::BinaryOp(op, lhs, rhs) + if let Some(value) = self.simplify_binary(op, ty, lhs, rhs) { + return Some(value); } + Value::BinaryOp(op, lhs, rhs) } Rvalue::UnaryOp(op, ref mut arg) => { let arg = self.simplify_operand(arg, location)?; @@ -970,7 +951,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { fn simplify_binary( &mut self, op: BinOp, - checked: bool, lhs_ty: Ty<'tcx>, lhs: VnIndex, rhs: VnIndex, @@ -999,22 +979,39 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { use Either::{Left, Right}; let a = as_bits(lhs).map_or(Right(lhs), Left); let b = as_bits(rhs).map_or(Right(rhs), Left); + let result = match (op, a, b) { // Neutral elements. - (BinOp::Add | BinOp::BitOr | BinOp::BitXor, Left(0), Right(p)) + ( + BinOp::Add + | BinOp::AddWithOverflow + | BinOp::AddUnchecked + | BinOp::BitOr + | BinOp::BitXor, + Left(0), + Right(p), + ) | ( BinOp::Add + | BinOp::AddWithOverflow + | BinOp::AddUnchecked | BinOp::BitOr | BinOp::BitXor | BinOp::Sub + | BinOp::SubWithOverflow + | BinOp::SubUnchecked | BinOp::Offset | BinOp::Shl | BinOp::Shr, Right(p), Left(0), ) - | (BinOp::Mul, Left(1), Right(p)) - | (BinOp::Mul | BinOp::Div, Right(p), Left(1)) => p, + | (BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked, Left(1), Right(p)) + | ( + BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked | BinOp::Div, + Right(p), + Left(1), + ) => p, // Attempt to simplify `x & ALL_ONES` to `x`, with `ALL_ONES` depending on type size. (BinOp::BitAnd, Right(p), Left(ones)) | (BinOp::BitAnd, Left(ones), Right(p)) if ones == layout.size.truncate(u128::MAX) @@ -1023,10 +1020,21 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { p } // Absorbing elements. - (BinOp::Mul | BinOp::BitAnd, _, Left(0)) + ( + BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked | BinOp::BitAnd, + _, + Left(0), + ) | (BinOp::Rem, _, Left(1)) | ( - BinOp::Mul | BinOp::Div | BinOp::Rem | BinOp::BitAnd | BinOp::Shl | BinOp::Shr, + BinOp::Mul + | BinOp::MulWithOverflow + | BinOp::MulUnchecked + | BinOp::Div + | BinOp::Rem + | BinOp::BitAnd + | BinOp::Shl + | BinOp::Shr, Left(0), _, ) => self.insert_scalar(Scalar::from_uint(0u128, layout.size), lhs_ty), @@ -1038,7 +1046,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { self.insert_scalar(Scalar::from_uint(ones, layout.size), lhs_ty) } // Sub/Xor with itself. - (BinOp::Sub | BinOp::BitXor, a, b) if a == b => { + (BinOp::Sub | BinOp::SubWithOverflow | BinOp::SubUnchecked | BinOp::BitXor, a, b) + if a == b => + { self.insert_scalar(Scalar::from_uint(0u128, layout.size), lhs_ty) } // Comparison: @@ -1052,7 +1062,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { _ => return None, }; - if checked { + if op.is_overflowing() { let false_val = self.insert_bool(false); Some(self.insert_tuple(vec![result, false_val])) } else { From 8530285f4ef240bf748cb207fa7d1794dca2e68f Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 3 Jun 2024 01:01:18 +0300 Subject: [PATCH 08/12] rustc_span: Inline some hot functions --- compiler/rustc_span/src/span_encoding.rs | 1 + compiler/rustc_span/src/symbol.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/compiler/rustc_span/src/span_encoding.rs b/compiler/rustc_span/src/span_encoding.rs index 788a52faf5688..6a02822663169 100644 --- a/compiler/rustc_span/src/span_encoding.rs +++ b/compiler/rustc_span/src/span_encoding.rs @@ -216,6 +216,7 @@ impl Span { // Returns either syntactic context, if it can be retrieved without taking the interner lock, // or an index into the interner if it cannot. + #[inline] fn inline_ctxt(self) -> Result { Ok(if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER { if self.len_with_tag_or_marker & PARENT_TAG == 0 { diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 61ca0d54ca490..fe883e03c44cd 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -2213,6 +2213,7 @@ impl fmt::Display for IdentPrinter { pub struct MacroRulesNormalizedIdent(Ident); impl MacroRulesNormalizedIdent { + #[inline] pub fn new(ident: Ident) -> Self { Self(ident.normalize_to_macro_rules()) } From b320ac749193d87b8705c5baaa75ba7b9433448f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Mon, 3 Jun 2024 03:50:15 +0200 Subject: [PATCH 09/12] Add a regression test for a former blanket impl synthesis ICE --- tests/rustdoc-ui/ice-blanket-impl-119792.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/rustdoc-ui/ice-blanket-impl-119792.rs diff --git a/tests/rustdoc-ui/ice-blanket-impl-119792.rs b/tests/rustdoc-ui/ice-blanket-impl-119792.rs new file mode 100644 index 0000000000000..90f0ea8469b98 --- /dev/null +++ b/tests/rustdoc-ui/ice-blanket-impl-119792.rs @@ -0,0 +1,19 @@ +//@ check-pass +// https://github.com/rust-lang/rust/issues/119792 + +struct Wrapper(T); + +trait Div {} +trait Mul { + type Output; +} + +impl Mul for Wrapper { + type Output = (); +} + +impl Div for Wrapper {} + +pub trait NumOps {} + +impl NumOps for T where T: Mul + Div {} From 56699f9f19ba6b4b74b1e2e4dfb210e0f4908c9f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 3 Jun 2024 09:40:58 +0200 Subject: [PATCH 10/12] Add missing `MSVC_LIB_PATH` env var --- src/tools/compiletest/src/runtest.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index a104240aa1691..c4deaf29d57c4 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -3369,7 +3369,6 @@ impl<'test> TestCx<'test> { cmd.env("IS_MSVC", "1") .env("IS_WINDOWS", "1") .env("MSVC_LIB", format!("'{}' -nologo", lib.display())) - .env("MSVC_LIB_PATH", format!("{}", lib.display())) .env("CC", format!("'{}' {}", self.config.cc, cflags)) .env("CXX", format!("'{}' {}", &self.config.cxx, cxxflags)); } else { @@ -3633,6 +3632,7 @@ impl<'test> TestCx<'test> { cmd.env("IS_MSVC", "1") .env("IS_WINDOWS", "1") .env("MSVC_LIB", format!("'{}' -nologo", lib.display())) + .env("MSVC_LIB_PATH", format!("{}", lib.display())) // Note: we diverge from legacy run_make and don't lump `CC` the compiler and // default flags together. .env("CC_DEFAULT_FLAGS", &cflags) From 12588e97333c72c369cdefd80b43ef584fa70dd7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 3 Jun 2024 10:28:52 +0200 Subject: [PATCH 11/12] Create `run-make` `env_var` and `env_var_os` helpers --- src/tools/run-make-support/src/cc.rs | 9 +++--- src/tools/run-make-support/src/clang.rs | 6 ++-- src/tools/run-make-support/src/lib.rs | 31 ++++++++++++++----- .../run-make-support/src/llvm_readobj.rs | 6 ++-- src/tools/run-make-support/src/run.rs | 14 ++++----- src/tools/run-make-support/src/rustc.rs | 5 ++- src/tools/run-make-support/src/rustdoc.rs | 7 ++--- tests/run-make/compiler-builtins/rmake.rs | 10 +++--- .../windows-binary-no-external-deps/rmake.rs | 5 ++- 9 files changed, 51 insertions(+), 42 deletions(-) diff --git a/src/tools/run-make-support/src/cc.rs b/src/tools/run-make-support/src/cc.rs index 0012e7b66af35..1472a39305e3a 100644 --- a/src/tools/run-make-support/src/cc.rs +++ b/src/tools/run-make-support/src/cc.rs @@ -1,8 +1,9 @@ -use std::env; use std::path::Path; use std::process::Command; -use crate::{bin_name, cygpath_windows, handle_failed_output, is_msvc, is_windows, tmp_dir, uname}; +use crate::{ + bin_name, cygpath_windows, env_var, handle_failed_output, is_msvc, is_windows, tmp_dir, uname, +}; /// Construct a new platform-specific C compiler invocation. /// @@ -27,11 +28,11 @@ impl Cc { /// WARNING: This means that what flags are accepted by the underlying C compile is /// platform- AND compiler-specific. Consult the relevant docs for `gcc`, `clang` and `mvsc`. pub fn new() -> Self { - let compiler = env::var("CC").unwrap(); + let compiler = env_var("CC"); let mut cmd = Command::new(compiler); - let default_cflags = env::var("CC_DEFAULT_FLAGS").unwrap(); + let default_cflags = env_var("CC_DEFAULT_FLAGS"); for flag in default_cflags.split(char::is_whitespace) { cmd.arg(flag); } diff --git a/src/tools/run-make-support/src/clang.rs b/src/tools/run-make-support/src/clang.rs index 6ccce67b250db..63c5af17c1dbe 100644 --- a/src/tools/run-make-support/src/clang.rs +++ b/src/tools/run-make-support/src/clang.rs @@ -1,8 +1,7 @@ -use std::env; use std::path::Path; use std::process::Command; -use crate::{bin_name, handle_failed_output, tmp_dir}; +use crate::{bin_name, env_var, handle_failed_output, tmp_dir}; /// Construct a new `clang` invocation. `clang` is not always available for all targets. pub fn clang() -> Clang { @@ -20,8 +19,7 @@ crate::impl_common_helpers!(Clang); impl Clang { /// Construct a new `clang` invocation. `clang` is not always available for all targets. pub fn new() -> Self { - let clang = - env::var("CLANG").expect("`CLANG` not specified, but this is required to find `clang`"); + let clang = env_var("CLANG"); let cmd = Command::new(clang); Self { cmd } } diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index 0cf64db6ac9c0..dccc21a7fa536 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -12,6 +12,7 @@ pub mod rustc; pub mod rustdoc; use std::env; +use std::ffi::OsString; use std::fs; use std::io; use std::path::{Path, PathBuf}; @@ -30,14 +31,28 @@ pub use run::{run, run_fail}; pub use rustc::{aux_build, rustc, Rustc}; pub use rustdoc::{bare_rustdoc, rustdoc, Rustdoc}; +pub fn env_var(name: &str) -> String { + match env::var(name) { + Ok(v) => v, + Err(err) => panic!("failed to retrieve environment variable {name:?}: {err:?}"), + } +} + +pub fn env_var_os(name: &str) -> OsString { + match env::var_os(name) { + Some(v) => v, + None => panic!("failed to retrieve environment variable {name:?}"), + } +} + /// Path of `TMPDIR` (a temporary build directory, not under `/tmp`). pub fn tmp_dir() -> PathBuf { - env::var_os("TMPDIR").unwrap().into() + env_var_os("TMPDIR").into() } /// `TARGET` pub fn target() -> String { - env::var("TARGET").unwrap() + env_var("TARGET") } /// Check if target is windows-like. @@ -62,7 +77,7 @@ pub fn static_lib(name: &str) -> PathBuf { } pub fn python_command() -> Command { - let python_path = std::env::var("PYTHON").expect("PYTHON environment variable does not exist"); + let python_path = env_var("PYTHON"); Command::new(python_path) } @@ -73,7 +88,7 @@ pub fn htmldocck() -> Command { } pub fn source_path() -> PathBuf { - std::env::var("S").expect("S variable does not exist").into() + env_var("S").into() } /// Construct the static library name based on the platform. @@ -197,12 +212,12 @@ fn handle_failed_output(cmd: &Command, output: Output, caller_line_number: u32) /// Set the runtime library path as needed for running the host rustc/rustdoc/etc. pub fn set_host_rpath(cmd: &mut Command) { - let ld_lib_path_envvar = env::var("LD_LIB_PATH_ENVVAR").unwrap(); + let ld_lib_path_envvar = env_var("LD_LIB_PATH_ENVVAR"); cmd.env(&ld_lib_path_envvar, { let mut paths = vec![]; - paths.push(PathBuf::from(env::var("TMPDIR").unwrap())); - paths.push(PathBuf::from(env::var("HOST_RPATH_DIR").unwrap())); - for p in env::split_paths(&env::var(&ld_lib_path_envvar).unwrap()) { + paths.push(PathBuf::from(env_var("TMPDIR"))); + paths.push(PathBuf::from(env_var("HOST_RPATH_DIR"))); + for p in env::split_paths(&env_var(&ld_lib_path_envvar)) { paths.push(p.to_path_buf()); } env::join_paths(paths.iter()).unwrap() diff --git a/src/tools/run-make-support/src/llvm_readobj.rs b/src/tools/run-make-support/src/llvm_readobj.rs index f114aacfa3fc7..77aaadfe18c47 100644 --- a/src/tools/run-make-support/src/llvm_readobj.rs +++ b/src/tools/run-make-support/src/llvm_readobj.rs @@ -1,8 +1,7 @@ -use std::env; use std::path::{Path, PathBuf}; use std::process::Command; -use crate::handle_failed_output; +use crate::{env_var, handle_failed_output}; /// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available /// at `$LLVM_BIN_DIR/llvm-readobj`. @@ -22,8 +21,7 @@ impl LlvmReadobj { /// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available /// at `$LLVM_BIN_DIR/llvm-readobj`. pub fn new() -> Self { - let llvm_bin_dir = env::var("LLVM_BIN_DIR") - .expect("`LLVM_BIN_DIR` not specified, but this is required to find `llvm-readobj`"); + let llvm_bin_dir = env_var("LLVM_BIN_DIR"); let llvm_bin_dir = PathBuf::from(llvm_bin_dir); let llvm_readobj = llvm_bin_dir.join("llvm-readobj"); let cmd = Command::new(llvm_readobj); diff --git a/src/tools/run-make-support/src/run.rs b/src/tools/run-make-support/src/run.rs index 9aad91f1b4630..bda53cfb1d6be 100644 --- a/src/tools/run-make-support/src/run.rs +++ b/src/tools/run-make-support/src/run.rs @@ -2,23 +2,23 @@ use std::env; use std::path::{Path, PathBuf}; use std::process::{Command, Output}; -use crate::is_windows; +use crate::{env_var, is_windows}; use super::{bin_name, handle_failed_output}; fn run_common(name: &str) -> (Command, Output) { let mut bin_path = PathBuf::new(); - bin_path.push(env::var("TMPDIR").unwrap()); + bin_path.push(env_var("TMPDIR")); bin_path.push(&bin_name(name)); - let ld_lib_path_envvar = env::var("LD_LIB_PATH_ENVVAR").unwrap(); + let ld_lib_path_envvar = env_var("LD_LIB_PATH_ENVVAR"); let mut cmd = Command::new(bin_path); cmd.env(&ld_lib_path_envvar, { let mut paths = vec![]; - paths.push(PathBuf::from(env::var("TMPDIR").unwrap())); - for p in env::split_paths(&env::var("TARGET_RPATH_ENV").unwrap()) { + paths.push(PathBuf::from(env_var("TMPDIR"))); + for p in env::split_paths(&env_var("TARGET_RPATH_ENV")) { paths.push(p.to_path_buf()); } - for p in env::split_paths(&env::var(&ld_lib_path_envvar).unwrap()) { + for p in env::split_paths(&env_var(&ld_lib_path_envvar)) { paths.push(p.to_path_buf()); } env::join_paths(paths.iter()).unwrap() @@ -29,7 +29,7 @@ fn run_common(name: &str) -> (Command, Output) { for p in env::split_paths(&std::env::var("PATH").unwrap_or(String::new())) { paths.push(p.to_path_buf()); } - paths.push(Path::new(&std::env::var("TARGET_RPATH_DIR").unwrap()).to_path_buf()); + paths.push(Path::new(&env_var("TARGET_RPATH_DIR")).to_path_buf()); cmd.env("PATH", env::join_paths(paths.iter()).unwrap()); } diff --git a/src/tools/run-make-support/src/rustc.rs b/src/tools/run-make-support/src/rustc.rs index 8b0252b8f04c4..607f68d12d007 100644 --- a/src/tools/run-make-support/src/rustc.rs +++ b/src/tools/run-make-support/src/rustc.rs @@ -1,10 +1,9 @@ -use std::env; use std::ffi::{OsStr, OsString}; use std::io::Write; use std::path::Path; use std::process::{Command, Output, Stdio}; -use crate::{handle_failed_output, set_host_rpath, tmp_dir}; +use crate::{env_var, handle_failed_output, set_host_rpath, tmp_dir}; /// Construct a new `rustc` invocation. pub fn rustc() -> Rustc { @@ -26,7 +25,7 @@ pub struct Rustc { crate::impl_common_helpers!(Rustc); fn setup_common() -> Command { - let rustc = env::var("RUSTC").unwrap(); + let rustc = env_var("RUSTC"); let mut cmd = Command::new(rustc); set_host_rpath(&mut cmd); cmd.arg("--out-dir").arg(tmp_dir()).arg("-L").arg(tmp_dir()); diff --git a/src/tools/run-make-support/src/rustdoc.rs b/src/tools/run-make-support/src/rustdoc.rs index 61d7448a6bfe7..34d32992e65e9 100644 --- a/src/tools/run-make-support/src/rustdoc.rs +++ b/src/tools/run-make-support/src/rustdoc.rs @@ -1,10 +1,9 @@ -use std::env; use std::ffi::OsStr; use std::io::Write; use std::path::Path; use std::process::{Command, Output, Stdio}; -use crate::{handle_failed_output, set_host_rpath}; +use crate::{env_var, env_var_os, handle_failed_output, set_host_rpath}; /// Construct a plain `rustdoc` invocation with no flags set. pub fn bare_rustdoc() -> Rustdoc { @@ -25,7 +24,7 @@ pub struct Rustdoc { crate::impl_common_helpers!(Rustdoc); fn setup_common() -> Command { - let rustdoc = env::var("RUSTDOC").unwrap(); + let rustdoc = env_var("RUSTDOC"); let mut cmd = Command::new(rustdoc); set_host_rpath(&mut cmd); cmd @@ -41,7 +40,7 @@ impl Rustdoc { /// Construct a `rustdoc` invocation with `-L $(TARGET_RPATH_DIR)` set. pub fn new() -> Self { let mut cmd = setup_common(); - let target_rpath_dir = env::var_os("TARGET_RPATH_DIR").unwrap(); + let target_rpath_dir = env_var_os("TARGET_RPATH_DIR"); cmd.arg(format!("-L{}", target_rpath_dir.to_string_lossy())); Self { cmd, stdin: None } } diff --git a/tests/run-make/compiler-builtins/rmake.rs b/tests/run-make/compiler-builtins/rmake.rs index f5da50ebb043b..f7e73e3506f5b 100644 --- a/tests/run-make/compiler-builtins/rmake.rs +++ b/tests/run-make/compiler-builtins/rmake.rs @@ -14,7 +14,6 @@ #![deny(warnings)] -use run_make_support::object; use run_make_support::object::read::archive::ArchiveFile; use run_make_support::object::read::Object; use run_make_support::object::ObjectSection; @@ -22,6 +21,7 @@ use run_make_support::object::ObjectSymbol; use run_make_support::object::RelocationTarget; use run_make_support::set_host_rpath; use run_make_support::tmp_dir; +use run_make_support::{env_var, object}; use std::collections::HashSet; const MANIFEST: &str = r#" @@ -35,7 +35,7 @@ path = "lib.rs""#; fn main() { let target_dir = tmp_dir().join("target"); - let target = std::env::var("TARGET").unwrap(); + let target = env_var("TARGET"); println!("Testing compiler_builtins for {}", target); @@ -44,9 +44,9 @@ fn main() { std::fs::write(&manifest_path, MANIFEST.as_bytes()).unwrap(); std::fs::write(tmp_dir().join("lib.rs"), b"#![no_std]").unwrap(); - let path = std::env::var("PATH").unwrap(); - let rustc = std::env::var("RUSTC").unwrap(); - let bootstrap_cargo = std::env::var("BOOTSTRAP_CARGO").unwrap(); + let path = env_var("PATH"); + let rustc = env_var("RUSTC"); + let bootstrap_cargo = env_var("BOOTSTRAP_CARGO"); let mut cmd = std::process::Command::new(bootstrap_cargo); cmd.args([ "build", diff --git a/tests/run-make/windows-binary-no-external-deps/rmake.rs b/tests/run-make/windows-binary-no-external-deps/rmake.rs index 6704fe0414834..ccf2d64c8539f 100644 --- a/tests/run-make/windows-binary-no-external-deps/rmake.rs +++ b/tests/run-make/windows-binary-no-external-deps/rmake.rs @@ -2,15 +2,14 @@ //! a "hello world" application by setting `PATH` to `C:\Windows\System32`. //@ only-windows -use run_make_support::{rustc, tmp_dir}; -use std::env; +use run_make_support::{env_var, rustc, tmp_dir}; use std::path::PathBuf; use std::process::Command; fn main() { rustc().input("hello.rs").run(); - let windows_dir = env::var("SystemRoot").unwrap(); + let windows_dir = env_var("SystemRoot"); let system32: PathBuf = [&windows_dir, "System32"].iter().collect(); // Note: This does not use the support wrappers so that we can precisely control the PATH let exe = tmp_dir().join("hello.exe"); From 45760276fd27086995d07abeae9f7e86660c2e3c Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Mon, 3 Jun 2024 12:20:19 +0200 Subject: [PATCH 12/12] Remove stray "this" --- library/std/src/sys/pal/unix/fs.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/library/std/src/sys/pal/unix/fs.rs b/library/std/src/sys/pal/unix/fs.rs index fbbd40bfb796a..a79a232e3d5b8 100644 --- a/library/std/src/sys/pal/unix/fs.rs +++ b/library/std/src/sys/pal/unix/fs.rs @@ -1910,8 +1910,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result { // The code below ensures that `FreeOnDrop` is never a null pointer unsafe { // `copyfile_state_free` returns -1 if the `to` or `from` files - // cannot be closed. However, this is not considered this an - // error. + // cannot be closed. However, this is not considered an error. libc::copyfile_state_free(self.0); } }