Skip to content

Commit 0647098

Browse files
committed
Auto merge of rust-lang#134266 - matthiaskrgr:rollup-cpiuez5, r=<try>
Rollup of 4 pull requests Successful merges: - rust-lang#132038 (Add lint rule for `#[deprecated]` on re-exports) - rust-lang#132150 (Fix powerpc64 big-endian FreeBSD ABI) - rust-lang#133633 (don't show the full linker args unless `--verbose` is passed) - rust-lang#133942 (Clarify how to use `black_box()`) r? `@ghost` `@rustbot` modify labels: rollup try-job: dist-x86_64-linux
2 parents 4847d6a + 33c9357 commit 0647098

File tree

20 files changed

+239
-29
lines changed

20 files changed

+239
-29
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -992,12 +992,12 @@ fn link_natively(
992992
let mut output = prog.stderr.clone();
993993
output.extend_from_slice(&prog.stdout);
994994
let escaped_output = escape_linker_output(&output, flavor);
995-
// FIXME: Add UI tests for this error.
996995
let err = errors::LinkingFailed {
997996
linker_path: &linker_path,
998997
exit_status: prog.status,
999-
command: &cmd,
998+
command: cmd,
1000999
escaped_output,
1000+
verbose: sess.opts.verbose,
10011001
};
10021002
sess.dcx().emit_err(err);
10031003
// If MSVC's `link.exe` was expected but the return code

compiler/rustc_codegen_ssa/src/errors.rs

+66-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Errors emitted by codegen_ssa
22
33
use std::borrow::Cow;
4+
use std::ffi::OsString;
45
use std::io::Error;
56
use std::num::ParseIntError;
67
use std::path::{Path, PathBuf};
@@ -345,21 +346,82 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for ThorinErrorWrapper {
345346
}
346347

347348
pub(crate) struct LinkingFailed<'a> {
348-
pub linker_path: &'a PathBuf,
349+
pub linker_path: &'a Path,
349350
pub exit_status: ExitStatus,
350-
pub command: &'a Command,
351+
pub command: Command,
351352
pub escaped_output: String,
353+
pub verbose: bool,
352354
}
353355

354356
impl<G: EmissionGuarantee> Diagnostic<'_, G> for LinkingFailed<'_> {
355-
fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
357+
fn into_diag(mut self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> {
356358
let mut diag = Diag::new(dcx, level, fluent::codegen_ssa_linking_failed);
357359
diag.arg("linker_path", format!("{}", self.linker_path.display()));
358360
diag.arg("exit_status", format!("{}", self.exit_status));
359361

360362
let contains_undefined_ref = self.escaped_output.contains("undefined reference to");
361363

362-
diag.note(format!("{:?}", self.command)).note(self.escaped_output);
364+
if self.verbose {
365+
diag.note(format!("{:?}", self.command));
366+
} else {
367+
enum ArgGroup {
368+
Regular(OsString),
369+
Objects(usize),
370+
Rlibs(PathBuf, Vec<OsString>),
371+
}
372+
373+
// Omit rust object files and fold rlibs in the error by default to make linker errors a
374+
// bit less verbose.
375+
let orig_args = self.command.take_args();
376+
let mut args: Vec<ArgGroup> = vec![];
377+
for arg in orig_args {
378+
if arg.as_encoded_bytes().ends_with(b".rcgu.o") {
379+
if let Some(ArgGroup::Objects(n)) = args.last_mut() {
380+
*n += 1;
381+
} else {
382+
args.push(ArgGroup::Objects(1));
383+
}
384+
} else if arg.as_encoded_bytes().ends_with(b".rlib") {
385+
let rlib_path = Path::new(&arg);
386+
let dir = rlib_path.parent().unwrap();
387+
let filename = rlib_path.file_name().unwrap().to_owned();
388+
if let Some(ArgGroup::Rlibs(parent, rlibs)) = args.last_mut() {
389+
if parent == dir {
390+
rlibs.push(filename);
391+
} else {
392+
args.push(ArgGroup::Rlibs(dir.to_owned(), vec![filename]));
393+
}
394+
} else {
395+
args.push(ArgGroup::Rlibs(dir.to_owned(), vec![filename]));
396+
}
397+
} else {
398+
args.push(ArgGroup::Regular(arg));
399+
}
400+
}
401+
self.command.args(args.into_iter().map(|arg_group| match arg_group {
402+
ArgGroup::Regular(arg) => arg,
403+
ArgGroup::Objects(n) => OsString::from(format!("<{n} object files omitted>")),
404+
ArgGroup::Rlibs(dir, rlibs) => {
405+
let mut arg = dir.into_os_string();
406+
arg.push("/{");
407+
let mut first = true;
408+
for rlib in rlibs {
409+
if !first {
410+
arg.push(",");
411+
}
412+
first = false;
413+
arg.push(rlib);
414+
}
415+
arg.push("}");
416+
arg
417+
}
418+
}));
419+
420+
diag.note(format!("{:?}", self.command));
421+
diag.note("some arguments are omitted. use `--verbose` to show all linker arguments");
422+
}
423+
424+
diag.note(self.escaped_output);
363425

364426
// Trying to match an error from OS linkers
365427
// which by now we have no way to translate.

compiler/rustc_passes/src/stability.rs

+3
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
411411
kind = AnnotationKind::DeprecationProhibited;
412412
const_stab_inherit = InheritConstStability::Yes;
413413
}
414+
hir::ItemKind::Use(_, _) => {
415+
kind = AnnotationKind::DeprecationProhibited;
416+
}
414417
hir::ItemKind::Struct(ref sd, _) => {
415418
if let Some(ctor_def_id) = sd.ctor_def_id() {
416419
self.annotate(

compiler/rustc_target/src/callconv/powerpc64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ where
9999
Ty: TyAbiInterface<'a, C> + Copy,
100100
C: HasDataLayout + HasTargetSpec,
101101
{
102-
let abi = if cx.target_spec().env == "musl" {
102+
let abi = if cx.target_spec().env == "musl" || cx.target_spec().os == "freebsd" {
103103
ELFv2
104104
} else if cx.target_spec().os == "aix" {
105105
AIX

compiler/rustc_target/src/spec/targets/powerpc64_unknown_freebsd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub(crate) fn target() -> Target {
1111
Target {
1212
llvm_target: "powerpc64-unknown-freebsd".into(),
1313
metadata: crate::spec::TargetMetadata {
14-
description: Some("PPC64 FreeBSD (ELFv1 and ELFv2)".into()),
14+
description: Some("PPC64 FreeBSD (ELFv2)".into()),
1515
tier: Some(3),
1616
host_tools: Some(true),
1717
std: Some(true),

library/core/src/alloc/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ pub use self::global::GlobalAlloc;
1010
#[stable(feature = "alloc_layout", since = "1.28.0")]
1111
pub use self::layout::Layout;
1212
#[stable(feature = "alloc_layout", since = "1.28.0")]
13-
#[deprecated(
14-
since = "1.52.0",
15-
note = "Name does not follow std convention, use LayoutError",
16-
suggestion = "LayoutError"
17-
)]
1813
#[allow(deprecated, deprecated_in_future)]
1914
pub use self::layout::LayoutErr;
2015
#[stable(feature = "alloc_layout_error", since = "1.50.0")]

library/core/src/hint.rs

+89-3
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,17 @@ pub fn spin_loop() {
310310
/// behavior in the calling code. This property makes `black_box` useful for writing code in which
311311
/// certain optimizations are not desired, such as benchmarks.
312312
///
313+
/// <div class="warning">
314+
///
313315
/// Note however, that `black_box` is only (and can only be) provided on a "best-effort" basis. The
314316
/// extent to which it can block optimisations may vary depending upon the platform and code-gen
315317
/// backend used. Programs cannot rely on `black_box` for *correctness*, beyond it behaving as the
316318
/// identity function. As such, it **must not be relied upon to control critical program behavior.**
317319
/// This also means that this function does not offer any guarantees for cryptographic or security
318320
/// purposes.
319321
///
322+
/// </div>
323+
///
320324
/// [`std::convert::identity`]: crate::convert::identity
321325
///
322326
/// # When is this useful?
@@ -357,7 +361,7 @@ pub fn spin_loop() {
357361
/// ```
358362
/// use std::hint::black_box;
359363
///
360-
/// // Same `contains` function
364+
/// // Same `contains` function.
361365
/// fn contains(haystack: &[&str], needle: &str) -> bool {
362366
/// haystack.iter().any(|x| x == &needle)
363367
/// }
@@ -366,8 +370,13 @@ pub fn spin_loop() {
366370
/// let haystack = vec!["abc", "def", "ghi", "jkl", "mno"];
367371
/// let needle = "ghi";
368372
/// for _ in 0..10 {
369-
/// // Adjust our benchmark loop contents
370-
/// black_box(contains(black_box(&haystack), black_box(needle)));
373+
/// // Force the compiler to run `contains`, even though it is a pure function whose
374+
/// // results are unused.
375+
/// black_box(contains(
376+
/// // Prevent the compiler from making assumptions about the input.
377+
/// black_box(&haystack),
378+
/// black_box(needle),
379+
/// ));
371380
/// }
372381
/// }
373382
/// ```
@@ -382,6 +391,83 @@ pub fn spin_loop() {
382391
///
383392
/// This makes our benchmark much more realistic to how the function would actually be used, where
384393
/// arguments are usually not known at compile time and the result is used in some way.
394+
///
395+
/// # How to use this
396+
///
397+
/// In practice, `black_box` serves two purposes:
398+
///
399+
/// 1. It prevents the compiler from making optimizations related to the value returned by `black_box`
400+
/// 2. It forces the value passed to `black_box` to be calculated, even if the return value of `black_box` is unused
401+
///
402+
/// ```
403+
/// use std::hint::black_box;
404+
///
405+
/// let zero = 0;
406+
/// let five = 5;
407+
///
408+
/// // The compiler will see this and remove the `* five` call, because it knows that multiplying
409+
/// // any integer by 0 will result in 0.
410+
/// let c = zero * five;
411+
///
412+
/// // Adding `black_box` here disables the compiler's ability to reason about the first operand in the multiplication.
413+
/// // It is forced to assume that it can be any possible number, so it cannot remove the `* five`
414+
/// // operation.
415+
/// let c = black_box(zero) * five;
416+
/// ```
417+
///
418+
/// While most cases will not be as clear-cut as the above example, it still illustrates how
419+
/// `black_box` can be used. When benchmarking a function, you usually want to wrap its inputs in
420+
/// `black_box` so the compiler cannot make optimizations that would be unrealistic in real-life
421+
/// use.
422+
///
423+
/// ```
424+
/// use std::hint::black_box;
425+
///
426+
/// // This is a simple function that increments its input by 1. Note that it is pure, meaning it
427+
/// // has no side-effects. This function has no effect if its result is unused. (An example of a
428+
/// // function *with* side-effects is `println!()`.)
429+
/// fn increment(x: u8) -> u8 {
430+
/// x + 1
431+
/// }
432+
///
433+
/// // Here, we call `increment` but discard its result. The compiler, seeing this and knowing that
434+
/// // `increment` is pure, will eliminate this function call entirely. This may not be desired,
435+
/// // though, especially if we're trying to track how much time `increment` takes to execute.
436+
/// let _ = increment(black_box(5));
437+
///
438+
/// // Here, we force `increment` to be executed. This is because the compiler treats `black_box`
439+
/// // as if it has side-effects, and thus must compute its input.
440+
/// let _ = black_box(increment(black_box(5)));
441+
/// ```
442+
///
443+
/// There may be additional situations where you want to wrap the result of a function in
444+
/// `black_box` to force its execution. This is situational though, and may not have any effect
445+
/// (such as when the function returns a zero-sized type such as [`()` unit][unit]).
446+
///
447+
/// Note that `black_box` has no effect on how its input is treated, only its output. As such,
448+
/// expressions passed to `black_box` may still be optimized:
449+
///
450+
/// ```
451+
/// use std::hint::black_box;
452+
///
453+
/// // The compiler sees this...
454+
/// let y = black_box(5 * 10);
455+
///
456+
/// // ...as this. As such, it will likely simplify `5 * 10` to just `50`.
457+
/// let _0 = 5 * 10;
458+
/// let y = black_box(_0);
459+
/// ```
460+
///
461+
/// In the above example, the `5 * 10` expression is considered distinct from the `black_box` call,
462+
/// and thus is still optimized by the compiler. You can prevent this by moving the multiplication
463+
/// operation outside of `black_box`:
464+
///
465+
/// ```
466+
/// use std::hint::black_box;
467+
///
468+
/// // No assumptions can be made about either operand, so the multiplication is not optimized out.
469+
/// let y = black_box(5) * black_box(10);
470+
/// ```
385471
#[inline]
386472
#[stable(feature = "bench_black_box", since = "1.66.0")]
387473
#[rustc_const_unstable(feature = "const_black_box", issue = "none")]

library/std/src/collections/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,7 @@ pub use self::hash_map::HashMap;
433433
#[doc(inline)]
434434
pub use self::hash_set::HashSet;
435435
#[stable(feature = "rust1", since = "1.0.0")]
436-
// FIXME(#82080) The deprecation here is only theoretical, and does not actually produce a warning.
437-
#[deprecated(note = "moved to `std::ops::Bound`", since = "1.26.0")]
436+
// FIXME(#82080) This has moved but #[deprecated] on `use` is unsupported.
438437
#[doc(hidden)]
439438
pub use crate::ops::Bound;
440439

src/doc/rustc/src/platform-support.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ target | std | host | notes
343343
[`powerpc-unknown-openbsd`](platform-support/powerpc-unknown-openbsd.md) | * | |
344344
[`powerpc-wrs-vxworks-spe`](platform-support/vxworks.md) | ✓ | |
345345
[`powerpc-wrs-vxworks`](platform-support/vxworks.md) | ✓ | |
346-
`powerpc64-unknown-freebsd` | ✓ | ✓ | PPC64 FreeBSD (ELFv1 and ELFv2)
346+
`powerpc64-unknown-freebsd` | ✓ | ✓ | PPC64 FreeBSD (ELFv2)
347347
`powerpc64le-unknown-freebsd` | ✓ | ✓ | PPC64LE FreeBSD
348348
`powerpc-unknown-freebsd` | ? | | PowerPC FreeBSD
349349
`powerpc64-unknown-linux-musl` | ? | | 64-bit PowerPC Linux with musl 1.2.3

src/tools/run-make-support/src/external_deps/rustc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ impl Rustc {
325325
self
326326
}
327327

328+
/// Pass the `--verbose` flag.
329+
pub fn verbose(&mut self) -> &mut Self {
330+
self.cmd.arg("--verbose");
331+
self
332+
}
333+
328334
/// `EXTRARSCXXFLAGS`
329335
pub fn extra_rs_cxx_flags(&mut self) -> &mut Self {
330336
// Adapted from tools.mk (trimmed):

tests/run-make/link-args-order/rmake.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ fn main() {
1515
.link_args("b c")
1616
.link_args("d e")
1717
.link_arg("f")
18+
.arg("--print=link-args")
1819
.run_fail()
19-
.assert_stderr_contains(r#""a" "b" "c" "d" "e" "f""#);
20+
.assert_stdout_contains(r#""a" "b" "c" "d" "e" "f""#);
2021
rustc()
2122
.input("empty.rs")
2223
.linker_flavor(linker)
2324
.arg("-Zpre-link-arg=a")
2425
.arg("-Zpre-link-args=b c")
2526
.arg("-Zpre-link-args=d e")
2627
.arg("-Zpre-link-arg=f")
28+
.arg("--print=link-args")
2729
.run_fail()
28-
.assert_stderr_contains(r#""a" "b" "c" "d" "e" "f""#);
30+
.assert_stdout_contains(r#""a" "b" "c" "d" "e" "f""#);
2931
}

tests/run-make/link-dedup/rmake.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ fn main() {
1414
rustc().input("depb.rs").run();
1515
rustc().input("depc.rs").run();
1616

17-
let output = rustc().input("empty.rs").cfg("bar").run_fail();
18-
output.assert_stderr_contains(needle_from_libs(&["testa", "testb", "testa"]));
17+
let output = rustc().input("empty.rs").cfg("bar").arg("--print=link-args").run_fail();
18+
output.assert_stdout_contains(needle_from_libs(&["testa", "testb", "testa"]));
1919

20-
let output = rustc().input("empty.rs").run_fail();
21-
output.assert_stderr_contains(needle_from_libs(&["testa"]));
22-
output.assert_stderr_not_contains(needle_from_libs(&["testb"]));
23-
output.assert_stderr_not_contains(needle_from_libs(&["testa", "testa", "testa"]));
20+
let output = rustc().input("empty.rs").arg("--print=link-args").run_fail();
21+
output.assert_stdout_contains(needle_from_libs(&["testa"]));
22+
output.assert_stdout_not_contains(needle_from_libs(&["testb"]));
23+
output.assert_stdout_not_contains(needle_from_libs(&["testa", "testa", "testa"]));
2424
// Adjacent identical native libraries are no longer deduplicated if
2525
// they come from different crates (https://github.com/rust-lang/rust/pull/103311)
2626
// so the following will fail:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
for arg in std::env::args() {
3+
match &*arg {
4+
"run_make_info" => println!("foo"),
5+
"run_make_warn" => eprintln!("warning: bar"),
6+
"run_make_error" => {
7+
eprintln!("error: baz");
8+
std::process::exit(1);
9+
}
10+
_ => (),
11+
}
12+
}
13+
}

tests/run-make/linker-warning/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use run_make_support::{Rustc, rustc};
2+
3+
fn run_rustc() -> Rustc {
4+
let mut rustc = rustc();
5+
rustc.arg("main.rs").output("main").linker("./fake-linker");
6+
rustc
7+
}
8+
9+
fn main() {
10+
// first, compile our linker
11+
rustc().arg("fake-linker.rs").output("fake-linker").run();
12+
13+
// Make sure we don't show the linker args unless `--verbose` is passed
14+
run_rustc()
15+
.link_arg("run_make_error")
16+
.verbose()
17+
.run_fail()
18+
.assert_stderr_contains_regex("fake-linker.*run_make_error")
19+
.assert_stderr_not_contains("object files omitted")
20+
.assert_stderr_contains_regex(r"lib[/\\]libstd");
21+
run_rustc()
22+
.link_arg("run_make_error")
23+
.run_fail()
24+
.assert_stderr_contains("fake-linker")
25+
.assert_stderr_contains("object files omitted")
26+
.assert_stderr_contains_regex(r"\{")
27+
.assert_stderr_not_contains_regex(r"lib[/\\]libstd");
28+
}

0 commit comments

Comments
 (0)