From 12cde3091a5d258c2f3e140e47537818680cb58d Mon Sep 17 00:00:00 2001 From: yukang Date: Fri, 7 Nov 2025 08:27:23 +0800 Subject: [PATCH 1/9] Add note for identifier with attempted hygiene violation --- .../rustc_resolve/src/late/diagnostics.rs | 24 +++++++++++++++++++ .../macros/macro-hygiene-help-issue-148580.rs | 15 ++++++++++++ .../macro-hygiene-help-issue-148580.stderr | 19 +++++++++++++++ .../macros/macro-hygiene-scope-15167.stderr | 20 ++++++++++++++++ .../metavar-expressions/concat-hygiene.stderr | 5 ++++ .../proc-macro/gen-macro-rules-hygiene.stderr | 5 ++++ tests/ui/proc-macro/mixed-site-span.stderr | 5 ++++ tests/ui/proc-macro/weird-hygiene.stderr | 10 ++++++++ 8 files changed, 103 insertions(+) create mode 100644 tests/ui/macros/macro-hygiene-help-issue-148580.rs create mode 100644 tests/ui/macros/macro-hygiene-help-issue-148580.stderr diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 1d00a6b81fabc..ad3493d93e80a 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1125,6 +1125,8 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { } } } + + self.suggest_ident_hidden_by_hygiene(err, path, span); } else if err_code == E0412 { if let Some(correct) = Self::likely_rust_type(path) { err.span_suggestion( @@ -1138,6 +1140,28 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { } } + fn suggest_ident_hidden_by_hygiene(&self, err: &mut Diag<'_>, path: &[Segment], span: Span) { + let [segment] = path else { return }; + + let ident = segment.ident; + let callsite_span = span.source_callsite(); + for rib in self.ribs[ValueNS].iter().rev() { + for (binding_ident, _) in &rib.bindings { + if binding_ident.name == ident.name + && !binding_ident.span.eq_ctxt(span) + && !binding_ident.span.from_expansion() + && binding_ident.span.lo() < callsite_span.lo() + { + err.span_help( + binding_ident.span, + "an identifier with the same name exists, but is not accessible due to macro hygiene", + ); + return; + } + } + } + } + /// Emit special messages for unresolved `Self` and `self`. fn suggest_self_ty( &self, diff --git a/tests/ui/macros/macro-hygiene-help-issue-148580.rs b/tests/ui/macros/macro-hygiene-help-issue-148580.rs new file mode 100644 index 0000000000000..8441290b17228 --- /dev/null +++ b/tests/ui/macros/macro-hygiene-help-issue-148580.rs @@ -0,0 +1,15 @@ +macro_rules! print_it { {} => { println!("{:?}", it); } } +//~^ ERROR cannot find value `it` in this scope + +fn main() { + { + let it = "hello"; + } + { + let it = "world"; + { + let it = (); + print_it!(); + } + } +} diff --git a/tests/ui/macros/macro-hygiene-help-issue-148580.stderr b/tests/ui/macros/macro-hygiene-help-issue-148580.stderr new file mode 100644 index 0000000000000..f6a4ae7dd1c66 --- /dev/null +++ b/tests/ui/macros/macro-hygiene-help-issue-148580.stderr @@ -0,0 +1,19 @@ +error[E0425]: cannot find value `it` in this scope + --> $DIR/macro-hygiene-help-issue-148580.rs:1:50 + | +LL | macro_rules! print_it { {} => { println!("{:?}", it); } } + | ^^ not found in this scope +... +LL | print_it!(); + | ----------- in this macro invocation + | +help: an identifier with the same name exists, but is not accessible due to macro hygiene + --> $DIR/macro-hygiene-help-issue-148580.rs:11:17 + | +LL | let it = (); + | ^^ + = note: this error originates in the macro `print_it` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0425`. diff --git a/tests/ui/macros/macro-hygiene-scope-15167.stderr b/tests/ui/macros/macro-hygiene-scope-15167.stderr index 58112c52df159..332a58467cee1 100644 --- a/tests/ui/macros/macro-hygiene-scope-15167.stderr +++ b/tests/ui/macros/macro-hygiene-scope-15167.stderr @@ -7,6 +7,11 @@ LL | macro_rules! f { () => (n) } LL | println!("{}", f!()); | ---- in this macro invocation | +help: an identifier with the same name exists, but is not accessible due to macro hygiene + --> $DIR/macro-hygiene-scope-15167.rs:12:9 + | +LL | for n in 0..1 { + | ^ = note: this error originates in the macro `f` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find value `n` in this scope @@ -18,6 +23,11 @@ LL | macro_rules! f { () => (n) } LL | println!("{}", f!()); | ---- in this macro invocation | +help: an identifier with the same name exists, but is not accessible due to macro hygiene + --> $DIR/macro-hygiene-scope-15167.rs:16:17 + | +LL | if let Some(n) = None { + | ^ = note: this error originates in the macro `f` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find value `n` in this scope @@ -29,6 +39,11 @@ LL | macro_rules! f { () => (n) } LL | println!("{}", f!()); | ---- in this macro invocation | +help: an identifier with the same name exists, but is not accessible due to macro hygiene + --> $DIR/macro-hygiene-scope-15167.rs:21:24 + | +LL | } else if let Some(n) = None { + | ^ = note: this error originates in the macro `f` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find value `n` in this scope @@ -40,6 +55,11 @@ LL | macro_rules! f { () => (n) } LL | println!("{}", f!()); | ---- in this macro invocation | +help: an identifier with the same name exists, but is not accessible due to macro hygiene + --> $DIR/macro-hygiene-scope-15167.rs:25:20 + | +LL | while let Some(n) = None { + | ^ = note: this error originates in the macro `f` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 4 previous errors diff --git a/tests/ui/macros/metavar-expressions/concat-hygiene.stderr b/tests/ui/macros/metavar-expressions/concat-hygiene.stderr index f3150d385ee70..9520f5182f4a7 100644 --- a/tests/ui/macros/metavar-expressions/concat-hygiene.stderr +++ b/tests/ui/macros/metavar-expressions/concat-hygiene.stderr @@ -7,6 +7,11 @@ LL | ${concat($lhs, $rhs)} LL | let _another = join!(abc, def); | --------------- in this macro invocation | +help: an identifier with the same name exists, but is not accessible due to macro hygiene + --> $DIR/concat-hygiene.rs:11:9 + | +LL | let abcdef = 1; + | ^^^^^^ = note: this error originates in the macro `join` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 1 previous error diff --git a/tests/ui/proc-macro/gen-macro-rules-hygiene.stderr b/tests/ui/proc-macro/gen-macro-rules-hygiene.stderr index e904b43aaae06..17171ad5c5cc5 100644 --- a/tests/ui/proc-macro/gen-macro-rules-hygiene.stderr +++ b/tests/ui/proc-macro/gen-macro-rules-hygiene.stderr @@ -18,6 +18,11 @@ LL | gen_macro_rules!(); LL | generated!(); | ------------ in this macro invocation | +help: an identifier with the same name exists, but is not accessible due to macro hygiene + --> $DIR/gen-macro-rules-hygiene.rs:19:13 + | +LL | let local_use = 1; + | ^^^^^^^^^ = note: this error originates in the macro `generated` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find value `local_def` in this scope diff --git a/tests/ui/proc-macro/mixed-site-span.stderr b/tests/ui/proc-macro/mixed-site-span.stderr index 2d2d55fe148d5..4bb2508416aaa 100644 --- a/tests/ui/proc-macro/mixed-site-span.stderr +++ b/tests/ui/proc-macro/mixed-site-span.stderr @@ -594,6 +594,11 @@ error[E0425]: cannot find value `local_use` in this scope LL | proc_macro_rules!(); | ^^^^^^^^^^^^^^^^^^^ help: a local variable with a similar name exists: `local_def` | +help: an identifier with the same name exists, but is not accessible due to macro hygiene + --> $DIR/mixed-site-span.rs:21:13 + | +LL | let local_use = 1; + | ^^^^^^^^^ = note: this error originates in the macro `proc_macro_rules` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find value `local_def` in this scope diff --git a/tests/ui/proc-macro/weird-hygiene.stderr b/tests/ui/proc-macro/weird-hygiene.stderr index 0cfac3f89a045..aa3ef9556eb16 100644 --- a/tests/ui/proc-macro/weird-hygiene.stderr +++ b/tests/ui/proc-macro/weird-hygiene.stderr @@ -7,6 +7,11 @@ LL | Value = (stringify!($tokens + hidden_ident), 1).1 LL | other!(50); | ---------- in this macro invocation | +help: an identifier with the same name exists, but is not accessible due to macro hygiene + --> $DIR/weird-hygiene.rs:44:9 + | +LL | let hidden_ident = "Hello1"; + | ^^^^^^^^^^^^ = note: this error originates in the macro `inner` which comes from the expansion of the macro `other` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find value `hidden_ident` in this scope @@ -18,6 +23,11 @@ LL | hidden_ident LL | invoke_it!(25); | -------------- in this macro invocation | +help: an identifier with the same name exists, but is not accessible due to macro hygiene + --> $DIR/weird-hygiene.rs:44:9 + | +LL | let hidden_ident = "Hello1"; + | ^^^^^^^^^^^^ = note: this error originates in the macro `invoke_it` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 2 previous errors From 5ed01e95df9be3accd4d46a9b9c315f602299abd Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 6 Nov 2025 19:40:00 -0600 Subject: [PATCH 2/9] Switch hexagon targets to rust-lld lld is a great choice for a default linker. --- .../src/spec/targets/hexagon_unknown_linux_musl.rs | 1 + .../src/spec/targets/hexagon_unknown_none_elf.rs | 1 + .../src/platform-support/hexagon-unknown-linux-musl.md | 6 ++++++ .../rustc/src/platform-support/hexagon-unknown-none-elf.md | 7 +++++++ 4 files changed, 15 insertions(+) diff --git a/compiler/rustc_target/src/spec/targets/hexagon_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/hexagon_unknown_linux_musl.rs index 17b371f05e530..82811cda00ce9 100644 --- a/compiler/rustc_target/src/spec/targets/hexagon_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/hexagon_unknown_linux_musl.rs @@ -8,6 +8,7 @@ pub(crate) fn target() -> Target { base.features = "-small-data,+hvx-length128b".into(); base.has_rpath = true; + base.linker = Some("rust-lld".into()); base.linker_flavor = LinkerFlavor::Unix(Cc::Yes); base.c_enum_min_bits = Some(8); diff --git a/compiler/rustc_target/src/spec/targets/hexagon_unknown_none_elf.rs b/compiler/rustc_target/src/spec/targets/hexagon_unknown_none_elf.rs index 6379cd30c3559..55ec3697a15e9 100644 --- a/compiler/rustc_target/src/spec/targets/hexagon_unknown_none_elf.rs +++ b/compiler/rustc_target/src/spec/targets/hexagon_unknown_none_elf.rs @@ -27,6 +27,7 @@ pub(crate) fn target() -> Target { max_atomic_width: Some(32), emit_debug_gdb_scripts: false, c_enum_min_bits: Some(8), + linker: Some("rust-lld".into()), ..Default::default() }, } diff --git a/src/doc/rustc/src/platform-support/hexagon-unknown-linux-musl.md b/src/doc/rustc/src/platform-support/hexagon-unknown-linux-musl.md index be6e17883f4eb..d74dd843eb259 100644 --- a/src/doc/rustc/src/platform-support/hexagon-unknown-linux-musl.md +++ b/src/doc/rustc/src/platform-support/hexagon-unknown-linux-musl.md @@ -39,6 +39,12 @@ dynamically linked executables. # /opt/clang+llvm-18.1.0-cross-hexagon-unknown-linux-musl/x86_64-linux-gnu/bin/qemu-hexagon -L /opt/clang+llvm-18.1.0-cross-hexagon-unknown-linux-musl/x86_64-linux-gnu/target/hexagon-unknown-linux-musl/usr/ ./hello ``` +## Linking + +This target selects `rust-lld` by default. Another option to use is +[eld](https://github.com/qualcomm/eld), which is also provided with +the opensource hexagon toolchain and the Hexagon SDK. + ## Building the target Because it is Tier 3, rust does not yet ship pre-compiled artifacts for this target. diff --git a/src/doc/rustc/src/platform-support/hexagon-unknown-none-elf.md b/src/doc/rustc/src/platform-support/hexagon-unknown-none-elf.md index b07b0bb08d60a..a906e895b7743 100644 --- a/src/doc/rustc/src/platform-support/hexagon-unknown-none-elf.md +++ b/src/doc/rustc/src/platform-support/hexagon-unknown-none-elf.md @@ -25,6 +25,13 @@ Functions marked `extern "C"` use the [Hexagon architecture calling convention]( This target generates PIC ELF binaries. +## Linking + +This target selects `rust-lld` by default. Another option to use is +[eld](https://github.com/qualcomm/eld), which is also provided with +[the opensource hexagon toolchain](https://github.com/quic/toolchain_for_hexagon) +and the Hexagon SDK. + ## Building the target You can build Rust with support for the target by adding it to the `target` From b827732898bc52912d2428c9d2d004a3a22378f7 Mon Sep 17 00:00:00 2001 From: Amy Kwan Date: Fri, 7 Nov 2025 04:36:12 +0000 Subject: [PATCH 3/9] Enable std locking functions on AIX This patch enables the std locking functions on AIX by including AIX on the list of supported targets for the locking functions. Excluding AIX from the std locking functions results to compilation errors such as: ("try_lock() not supported"). --- library/std/src/sys/fs/unix.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/library/std/src/sys/fs/unix.rs b/library/std/src/sys/fs/unix.rs index 129fccdbf4197..cadcfddb0f7f8 100644 --- a/library/std/src/sys/fs/unix.rs +++ b/library/std/src/sys/fs/unix.rs @@ -1296,6 +1296,7 @@ impl File { target_os = "openbsd", target_os = "cygwin", target_os = "illumos", + target_os = "aix", target_vendor = "apple", ))] pub fn lock(&self) -> io::Result<()> { @@ -1321,6 +1322,7 @@ impl File { target_os = "cygwin", target_os = "solaris", target_os = "illumos", + target_os = "aix", target_vendor = "apple", )))] pub fn lock(&self) -> io::Result<()> { @@ -1335,6 +1337,7 @@ impl File { target_os = "openbsd", target_os = "cygwin", target_os = "illumos", + target_os = "aix", target_vendor = "apple", ))] pub fn lock_shared(&self) -> io::Result<()> { @@ -1360,6 +1363,7 @@ impl File { target_os = "cygwin", target_os = "solaris", target_os = "illumos", + target_os = "aix", target_vendor = "apple", )))] pub fn lock_shared(&self) -> io::Result<()> { @@ -1374,6 +1378,7 @@ impl File { target_os = "openbsd", target_os = "cygwin", target_os = "illumos", + target_os = "aix", target_vendor = "apple", ))] pub fn try_lock(&self) -> Result<(), TryLockError> { @@ -1415,6 +1420,7 @@ impl File { target_os = "cygwin", target_os = "solaris", target_os = "illumos", + target_os = "aix", target_vendor = "apple", )))] pub fn try_lock(&self) -> Result<(), TryLockError> { @@ -1432,6 +1438,7 @@ impl File { target_os = "openbsd", target_os = "cygwin", target_os = "illumos", + target_os = "aix", target_vendor = "apple", ))] pub fn try_lock_shared(&self) -> Result<(), TryLockError> { @@ -1473,6 +1480,7 @@ impl File { target_os = "cygwin", target_os = "solaris", target_os = "illumos", + target_os = "aix", target_vendor = "apple", )))] pub fn try_lock_shared(&self) -> Result<(), TryLockError> { @@ -1490,6 +1498,7 @@ impl File { target_os = "openbsd", target_os = "cygwin", target_os = "illumos", + target_os = "aix", target_vendor = "apple", ))] pub fn unlock(&self) -> io::Result<()> { @@ -1515,6 +1524,7 @@ impl File { target_os = "cygwin", target_os = "solaris", target_os = "illumos", + target_os = "aix", target_vendor = "apple", )))] pub fn unlock(&self) -> io::Result<()> { From fc20a28776ae6cd9583031c970e1ad2b377ddd9e Mon Sep 17 00:00:00 2001 From: Marijn Schouten Date: Tue, 28 Oct 2025 12:10:18 +0100 Subject: [PATCH 4/9] Modify contributor email entries in .mailmap Updated email addresses for several contributors in the mailmap. --- .mailmap | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/.mailmap b/.mailmap index fc8e83d6493cd..de7001b774276 100644 --- a/.mailmap +++ b/.mailmap @@ -9,7 +9,7 @@ Aaron Todd Abhishek Chanda Abhishek Chanda Abhijeet Bhagat Abroskin Alexander -Adolfo Ochagavía +Adolfo Ochagavía Adrian Heine né Lang Adrien Tétar Ahmed Charles @@ -36,6 +36,7 @@ Amanda Stjerna Amanda Stjerna Amanieu d'Antras Amos Onn +Amos Wenger Ana-Maria Mihalache Anatoly Ikorsky Andre Bogus @@ -276,7 +277,8 @@ Irina Popa Ivan Ivaschenko ivan tkachenko J. J. Weber -Jack Huey +Jack Huey +Jack Huey <31162821+jackh726@users.noreply.github.com> Jacob Jacob Hoffman-Andrews Jacob Greenfield @@ -292,6 +294,8 @@ Jakub Adam Wieczorek Jakub Adam Wieczorek Jakub Adam Wieczorek Jakub Adam Wieczorek +Jakub Adam Wieczorek +Jakub Adam Wieczorek Jakub Beránek James [Undefined] James Deng @@ -303,6 +307,7 @@ Jamie Hill-Daniel Jana Dönszelmann Jana Dönszelmann Jana Dönszelmann +Jane Lusby Jan-Erik Rediger Jaro Fietz Jason Fager @@ -313,6 +318,7 @@ Jason Toffaletti Jason Toffaletti Jauhien Piatlicki Jauhien Piatlicki Jay True Jeremy Letang +Jeremy Soller Jeremy Sorensen Jeremy Stucki Jeremy Stucki @@ -336,6 +342,7 @@ John Kåre Alsaker John Kåre Alsaker John Talling John Van Enk +Jon Gjengset Jonas Tepe Jonathan Bailey Jonathan Chan Kwan Yin @@ -424,7 +431,7 @@ Malo Jaffré Manish Goregaokar Mara Bos Marcell Pardavi -Marco Ieni <11428655+MarcoIeni@users.noreply.github.com> +Marco Ieni <11428655+MarcoIeni@users.noreply.github.com> Marcus Klaas de Vries Margaret Meyerhofer Marijn Schouten @@ -531,6 +538,7 @@ Oliver Scherer Oliver Scherer Oliver Scherer Oliver Scherer +Onur Özkan Onur Özkan Onur Özkan Ömer Sinan Ağacan @@ -591,6 +599,7 @@ Rusty Blitzerr RustyYato Ruud van Asseldonk Ruud van Asseldonk Ryan Leung +Ryan Levick Ryan Scheel Ryan Sullivant Ryan Wiedemann @@ -685,6 +694,8 @@ Weihang Lo Weihang Lo Wesley Wiser whitequark +Will Crichton +Will Crichton William Ting Wim Looman Wim Looman @@ -694,6 +705,8 @@ Xinye Tao Xuefeng Wu Xuefeng Wu Xuefeng Wu XuefengWu York Xiang +Yoshua Wuyts +Yoshua Wuyts <2467194+yoshuawuyts@users.noreply.github.com> Yotam Ofek Youngsoo Son Youngsuk Kim From fdced17e1fb1f1cf00486383d5a90f45987c66b6 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 7 Nov 2025 12:35:12 +0100 Subject: [PATCH 5/9] [bootstrap] Make `--open` option work with `doc src/tools/error_index_generator` --- src/bootstrap/src/core/build_steps/doc.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/build_steps/doc.rs b/src/bootstrap/src/core/build_steps/doc.rs index 6622aae069d5c..d6d6fc67ba5ae 100644 --- a/src/bootstrap/src/core/build_steps/doc.rs +++ b/src/bootstrap/src/core/build_steps/doc.rs @@ -1228,9 +1228,12 @@ impl Step for ErrorIndex { t!(fs::create_dir_all(&out)); tool::ErrorIndex::command(builder, self.compilers) .arg("html") - .arg(out) + .arg(&out) .arg(&builder.version) .run(builder); + + let index = out.join("error-index.html"); + builder.maybe_open_in_browser::(index); } fn metadata(&self) -> Option { From bd23d55f298639de1f108507e6b0a6518fae4a84 Mon Sep 17 00:00:00 2001 From: Pavel Grigorenko Date: Thu, 6 Nov 2025 18:51:01 +0300 Subject: [PATCH 6/9] `invalid_atomic_ordering`: also lint `update` & `try_update` --- compiler/rustc_lint/src/types.rs | 13 +- compiler/rustc_span/src/symbol.rs | 2 + ...nt-invalid-atomic-ordering-fetch-update.rs | 49 ---- ...nvalid-atomic-ordering-fetch-update.stderr | 83 ------ .../lint-invalid-atomic-ordering-update.rs | 144 +++++++++++ ...lint-invalid-atomic-ordering-update.stderr | 243 ++++++++++++++++++ 6 files changed, 399 insertions(+), 135 deletions(-) delete mode 100644 tests/ui/lint/lint-invalid-atomic-ordering-fetch-update.rs delete mode 100644 tests/ui/lint/lint-invalid-atomic-ordering-fetch-update.stderr create mode 100644 tests/ui/lint/lint-invalid-atomic-ordering-update.rs create mode 100644 tests/ui/lint/lint-invalid-atomic-ordering-update.stderr diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index 4895e61069e52..f3e6db6f2d8e4 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -1022,7 +1022,8 @@ declare_lint! { /// /// - Passing `Ordering::Release` or `Ordering::AcqRel` as the failure /// ordering for any of `AtomicType::compare_exchange`, - /// `AtomicType::compare_exchange_weak`, or `AtomicType::fetch_update`. + /// `AtomicType::compare_exchange_weak`, `AtomicType::update`, or + /// `AtomicType::try_update`. INVALID_ATOMIC_ORDERING, Deny, "usage of invalid atomic ordering in atomic operations and memory fences" @@ -1118,13 +1119,19 @@ impl InvalidAtomicOrdering { let Some((method, args)) = Self::inherent_atomic_method_call( cx, expr, - &[sym::fetch_update, sym::compare_exchange, sym::compare_exchange_weak], + &[ + sym::update, + sym::try_update, + sym::fetch_update, + sym::compare_exchange, + sym::compare_exchange_weak, + ], ) else { return; }; let fail_order_arg = match method { - sym::fetch_update => &args[1], + sym::update | sym::try_update | sym::fetch_update => &args[1], sym::compare_exchange | sym::compare_exchange_weak => &args[3], _ => return, }; diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 38718bad9e57e..b447239ea85b0 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -2273,6 +2273,7 @@ symbols! { try_from_fn, try_into, try_trait_v2, + try_update, tt, tuple, tuple_indexing, @@ -2390,6 +2391,7 @@ symbols! { unwrap, unwrap_binder, unwrap_or, + update, use_cloned, use_extern_macros, use_nested_groups, diff --git a/tests/ui/lint/lint-invalid-atomic-ordering-fetch-update.rs b/tests/ui/lint/lint-invalid-atomic-ordering-fetch-update.rs deleted file mode 100644 index bdeacac4957b6..0000000000000 --- a/tests/ui/lint/lint-invalid-atomic-ordering-fetch-update.rs +++ /dev/null @@ -1,49 +0,0 @@ -//@ only-x86_64 -use std::sync::atomic::{AtomicIsize, Ordering}; - -fn main() { - // `fetch_update` testing - let x = AtomicIsize::new(0); - - // Allowed ordering combos - let _ = x.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Relaxed, Ordering::Acquire, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Relaxed, Ordering::SeqCst, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Acquire, Ordering::Relaxed, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Acquire, Ordering::Acquire, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Acquire, Ordering::SeqCst, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Release, Ordering::Relaxed, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Release, Ordering::Acquire, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Release, Ordering::SeqCst, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::AcqRel, Ordering::Relaxed, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::AcqRel, Ordering::Acquire, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::AcqRel, Ordering::SeqCst, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::SeqCst, Ordering::Relaxed, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::SeqCst, Ordering::Acquire, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |old| Some(old + 1)); - - // AcqRel is always forbidden as a failure ordering - let _ = x.fetch_update(Ordering::Relaxed, Ordering::AcqRel, |old| Some(old + 1)); - //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` - let _ = x.fetch_update(Ordering::Acquire, Ordering::AcqRel, |old| Some(old + 1)); - //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` - let _ = x.fetch_update(Ordering::Release, Ordering::AcqRel, |old| Some(old + 1)); - //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` - let _ = x.fetch_update(Ordering::AcqRel, Ordering::AcqRel, |old| Some(old + 1)); - //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` - let _ = x.fetch_update(Ordering::SeqCst, Ordering::AcqRel, |old| Some(old + 1)); - //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` - - // Release is always forbidden as a failure ordering - let _ = x.fetch_update(Ordering::Relaxed, Ordering::Release, |old| Some(old + 1)); - //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` - let _ = x.fetch_update(Ordering::Acquire, Ordering::Release, |old| Some(old + 1)); - //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` - let _ = x.fetch_update(Ordering::Release, Ordering::Release, |old| Some(old + 1)); - //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` - let _ = x.fetch_update(Ordering::AcqRel, Ordering::Release, |old| Some(old + 1)); - //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` - let _ = x.fetch_update(Ordering::SeqCst, Ordering::Release, |old| Some(old + 1)); - //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` - -} diff --git a/tests/ui/lint/lint-invalid-atomic-ordering-fetch-update.stderr b/tests/ui/lint/lint-invalid-atomic-ordering-fetch-update.stderr deleted file mode 100644 index 33829d68fd5c7..0000000000000 --- a/tests/ui/lint/lint-invalid-atomic-ordering-fetch-update.stderr +++ /dev/null @@ -1,83 +0,0 @@ -error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write - --> $DIR/lint-invalid-atomic-ordering-fetch-update.rs:26:47 - | -LL | let _ = x.fetch_update(Ordering::Relaxed, Ordering::AcqRel, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ invalid failure ordering - | - = help: consider using `Acquire` or `Relaxed` failure ordering instead - = note: `#[deny(invalid_atomic_ordering)]` on by default - -error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write - --> $DIR/lint-invalid-atomic-ordering-fetch-update.rs:28:47 - | -LL | let _ = x.fetch_update(Ordering::Acquire, Ordering::AcqRel, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ invalid failure ordering - | - = help: consider using `Acquire` or `Relaxed` failure ordering instead - -error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write - --> $DIR/lint-invalid-atomic-ordering-fetch-update.rs:30:47 - | -LL | let _ = x.fetch_update(Ordering::Release, Ordering::AcqRel, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ invalid failure ordering - | - = help: consider using `Acquire` or `Relaxed` failure ordering instead - -error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write - --> $DIR/lint-invalid-atomic-ordering-fetch-update.rs:32:46 - | -LL | let _ = x.fetch_update(Ordering::AcqRel, Ordering::AcqRel, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ invalid failure ordering - | - = help: consider using `Acquire` or `Relaxed` failure ordering instead - -error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write - --> $DIR/lint-invalid-atomic-ordering-fetch-update.rs:34:46 - | -LL | let _ = x.fetch_update(Ordering::SeqCst, Ordering::AcqRel, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ invalid failure ordering - | - = help: consider using `Acquire` or `Relaxed` failure ordering instead - -error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write - --> $DIR/lint-invalid-atomic-ordering-fetch-update.rs:38:47 - | -LL | let _ = x.fetch_update(Ordering::Relaxed, Ordering::Release, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^^ invalid failure ordering - | - = help: consider using `Acquire` or `Relaxed` failure ordering instead - -error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write - --> $DIR/lint-invalid-atomic-ordering-fetch-update.rs:40:47 - | -LL | let _ = x.fetch_update(Ordering::Acquire, Ordering::Release, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^^ invalid failure ordering - | - = help: consider using `Acquire` or `Relaxed` failure ordering instead - -error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write - --> $DIR/lint-invalid-atomic-ordering-fetch-update.rs:42:47 - | -LL | let _ = x.fetch_update(Ordering::Release, Ordering::Release, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^^ invalid failure ordering - | - = help: consider using `Acquire` or `Relaxed` failure ordering instead - -error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write - --> $DIR/lint-invalid-atomic-ordering-fetch-update.rs:44:46 - | -LL | let _ = x.fetch_update(Ordering::AcqRel, Ordering::Release, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^^ invalid failure ordering - | - = help: consider using `Acquire` or `Relaxed` failure ordering instead - -error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write - --> $DIR/lint-invalid-atomic-ordering-fetch-update.rs:46:46 - | -LL | let _ = x.fetch_update(Ordering::SeqCst, Ordering::Release, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^^ invalid failure ordering - | - = help: consider using `Acquire` or `Relaxed` failure ordering instead - -error: aborting due to 10 previous errors - diff --git a/tests/ui/lint/lint-invalid-atomic-ordering-update.rs b/tests/ui/lint/lint-invalid-atomic-ordering-update.rs new file mode 100644 index 0000000000000..ac41e7cee0c25 --- /dev/null +++ b/tests/ui/lint/lint-invalid-atomic-ordering-update.rs @@ -0,0 +1,144 @@ +//@ only-x86_64 +#![feature(atomic_try_update)] + +use std::sync::atomic::{AtomicIsize, Ordering}; + +fn main() { + // `fetch_update` testing + let x = AtomicIsize::new(0); + + // Allowed ordering combos + let _ = x.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |old| Some(old + 1)); + let _ = x.try_update(Ordering::Relaxed, Ordering::Relaxed, |old| Some(old + 1)); + let _ = x.update(Ordering::Relaxed, Ordering::Relaxed, |old| old + 1); + + let _ = x.fetch_update(Ordering::Relaxed, Ordering::Acquire, |old| Some(old + 1)); + let _ = x.try_update(Ordering::Relaxed, Ordering::Acquire, |old| Some(old + 1)); + let _ = x.update(Ordering::Relaxed, Ordering::Acquire, |old| old + 1); + + let _ = x.fetch_update(Ordering::Relaxed, Ordering::SeqCst, |old| Some(old + 1)); + let _ = x.try_update(Ordering::Relaxed, Ordering::SeqCst, |old| Some(old + 1)); + let _ = x.update(Ordering::Relaxed, Ordering::SeqCst, |old| old + 1); + + let _ = x.fetch_update(Ordering::Acquire, Ordering::Relaxed, |old| Some(old + 1)); + let _ = x.try_update(Ordering::Acquire, Ordering::Relaxed, |old| Some(old + 1)); + let _ = x.update(Ordering::Acquire, Ordering::Relaxed, |old| old + 1); + + let _ = x.fetch_update(Ordering::Acquire, Ordering::Acquire, |old| Some(old + 1)); + let _ = x.try_update(Ordering::Acquire, Ordering::Acquire, |old| Some(old + 1)); + let _ = x.update(Ordering::Acquire, Ordering::Acquire, |old| old + 1); + + let _ = x.fetch_update(Ordering::Acquire, Ordering::SeqCst, |old| Some(old + 1)); + let _ = x.try_update(Ordering::Acquire, Ordering::SeqCst, |old| Some(old + 1)); + let _ = x.update(Ordering::Acquire, Ordering::SeqCst, |old| old + 1); + + let _ = x.fetch_update(Ordering::Release, Ordering::Relaxed, |old| Some(old + 1)); + let _ = x.try_update(Ordering::Release, Ordering::Relaxed, |old| Some(old + 1)); + let _ = x.update(Ordering::Release, Ordering::Relaxed, |old| old + 1); + + let _ = x.fetch_update(Ordering::Release, Ordering::Acquire, |old| Some(old + 1)); + let _ = x.try_update(Ordering::Release, Ordering::Acquire, |old| Some(old + 1)); + let _ = x.update(Ordering::Release, Ordering::Acquire, |old| old + 1); + + let _ = x.fetch_update(Ordering::Release, Ordering::SeqCst, |old| Some(old + 1)); + let _ = x.try_update(Ordering::Release, Ordering::SeqCst, |old| Some(old + 1)); + let _ = x.update(Ordering::Release, Ordering::SeqCst, |old| old + 1); + + let _ = x.fetch_update(Ordering::AcqRel, Ordering::Relaxed, |old| Some(old + 1)); + let _ = x.try_update(Ordering::AcqRel, Ordering::Relaxed, |old| Some(old + 1)); + let _ = x.update(Ordering::AcqRel, Ordering::Relaxed, |old| old + 1); + + let _ = x.fetch_update(Ordering::AcqRel, Ordering::Acquire, |old| Some(old + 1)); + let _ = x.try_update(Ordering::AcqRel, Ordering::Acquire, |old| Some(old + 1)); + let _ = x.update(Ordering::AcqRel, Ordering::Acquire, |old| old + 1); + + let _ = x.fetch_update(Ordering::AcqRel, Ordering::SeqCst, |old| Some(old + 1)); + let _ = x.try_update(Ordering::AcqRel, Ordering::SeqCst, |old| Some(old + 1)); + let _ = x.update(Ordering::AcqRel, Ordering::SeqCst, |old| old + 1); + + let _ = x.fetch_update(Ordering::SeqCst, Ordering::Relaxed, |old| Some(old + 1)); + let _ = x.try_update(Ordering::SeqCst, Ordering::Relaxed, |old| Some(old + 1)); + let _ = x.update(Ordering::SeqCst, Ordering::Relaxed, |old| old + 1); + + let _ = x.fetch_update(Ordering::SeqCst, Ordering::Acquire, |old| Some(old + 1)); + let _ = x.try_update(Ordering::SeqCst, Ordering::Acquire, |old| Some(old + 1)); + let _ = x.update(Ordering::SeqCst, Ordering::Acquire, |old| old + 1); + + let _ = x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |old| Some(old + 1)); + let _ = x.try_update(Ordering::SeqCst, Ordering::SeqCst, |old| Some(old + 1)); + let _ = x.update(Ordering::SeqCst, Ordering::SeqCst, |old| old + 1); + + // AcqRel is always forbidden as a failure ordering + + let _ = x.fetch_update(Ordering::Relaxed, Ordering::AcqRel, |old| Some(old + 1)); + //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.try_update(Ordering::Relaxed, Ordering::AcqRel, |old| Some(old + 1)); + //~^ ERROR `try_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.update(Ordering::Relaxed, Ordering::AcqRel, |old| old + 1); + //~^ ERROR `update`'s failure ordering may not be `Release` or `AcqRel` + + let _ = x.fetch_update(Ordering::Acquire, Ordering::AcqRel, |old| Some(old + 1)); + //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.try_update(Ordering::Acquire, Ordering::AcqRel, |old| Some(old + 1)); + //~^ ERROR `try_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.update(Ordering::Acquire, Ordering::AcqRel, |old| old + 1); + //~^ ERROR `update`'s failure ordering may not be `Release` or `AcqRel` + + let _ = x.fetch_update(Ordering::Release, Ordering::AcqRel, |old| Some(old + 1)); + //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.try_update(Ordering::Release, Ordering::AcqRel, |old| Some(old + 1)); + //~^ ERROR `try_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.update(Ordering::Release, Ordering::AcqRel, |old| old + 1); + //~^ ERROR `update`'s failure ordering may not be `Release` or `AcqRel` + + let _ = x.fetch_update(Ordering::AcqRel, Ordering::AcqRel, |old| Some(old + 1)); + //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.try_update(Ordering::AcqRel, Ordering::AcqRel, |old| Some(old + 1)); + //~^ ERROR `try_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.update(Ordering::AcqRel, Ordering::AcqRel, |old| old + 1); + //~^ ERROR `update`'s failure ordering may not be `Release` or `AcqRel` + + let _ = x.fetch_update(Ordering::SeqCst, Ordering::AcqRel, |old| Some(old + 1)); + //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.try_update(Ordering::SeqCst, Ordering::AcqRel, |old| Some(old + 1)); + //~^ ERROR `try_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.update(Ordering::SeqCst, Ordering::AcqRel, |old| old + 1); + //~^ ERROR `update`'s failure ordering may not be `Release` or `AcqRel` + + // Release is always forbidden as a failure ordering + + let _ = x.fetch_update(Ordering::Relaxed, Ordering::Release, |old| Some(old + 1)); + //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.try_update(Ordering::Relaxed, Ordering::Release, |old| Some(old + 1)); + //~^ ERROR `try_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.update(Ordering::Relaxed, Ordering::Release, |old| old + 1); + //~^ ERROR `update`'s failure ordering may not be `Release` or `AcqRel` + + let _ = x.fetch_update(Ordering::Acquire, Ordering::Release, |old| Some(old + 1)); + //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.try_update(Ordering::Acquire, Ordering::Release, |old| Some(old + 1)); + //~^ ERROR `try_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.update(Ordering::Acquire, Ordering::Release, |old| old + 1); + //~^ ERROR `update`'s failure ordering may not be `Release` or `AcqRel` + + let _ = x.fetch_update(Ordering::Release, Ordering::Release, |old| Some(old + 1)); + //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.try_update(Ordering::Release, Ordering::Release, |old| Some(old + 1)); + //~^ ERROR `try_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.update(Ordering::Release, Ordering::Release, |old| old + 1); + //~^ ERROR `update`'s failure ordering may not be `Release` or `AcqRel` + + let _ = x.fetch_update(Ordering::AcqRel, Ordering::Release, |old| Some(old + 1)); + //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.try_update(Ordering::AcqRel, Ordering::Release, |old| Some(old + 1)); + //~^ ERROR `try_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.update(Ordering::AcqRel, Ordering::Release, |old| old + 1); + //~^ ERROR `update`'s failure ordering may not be `Release` or `AcqRel` + + let _ = x.fetch_update(Ordering::SeqCst, Ordering::Release, |old| Some(old + 1)); + //~^ ERROR `fetch_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.try_update(Ordering::SeqCst, Ordering::Release, |old| Some(old + 1)); + //~^ ERROR `try_update`'s failure ordering may not be `Release` or `AcqRel` + let _ = x.update(Ordering::SeqCst, Ordering::Release, |old| old + 1); + //~^ ERROR `update`'s failure ordering may not be `Release` or `AcqRel` +} diff --git a/tests/ui/lint/lint-invalid-atomic-ordering-update.stderr b/tests/ui/lint/lint-invalid-atomic-ordering-update.stderr new file mode 100644 index 0000000000000..8c266bacf3144 --- /dev/null +++ b/tests/ui/lint/lint-invalid-atomic-ordering-update.stderr @@ -0,0 +1,243 @@ +error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:73:47 + | +LL | let _ = x.fetch_update(Ordering::Relaxed, Ordering::AcqRel, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + = note: `#[deny(invalid_atomic_ordering)]` on by default + +error: `try_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `try_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:75:45 + | +LL | let _ = x.try_update(Ordering::Relaxed, Ordering::AcqRel, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:77:41 + | +LL | let _ = x.update(Ordering::Relaxed, Ordering::AcqRel, |old| old + 1); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:80:47 + | +LL | let _ = x.fetch_update(Ordering::Acquire, Ordering::AcqRel, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `try_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `try_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:82:45 + | +LL | let _ = x.try_update(Ordering::Acquire, Ordering::AcqRel, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:84:41 + | +LL | let _ = x.update(Ordering::Acquire, Ordering::AcqRel, |old| old + 1); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:87:47 + | +LL | let _ = x.fetch_update(Ordering::Release, Ordering::AcqRel, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `try_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `try_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:89:45 + | +LL | let _ = x.try_update(Ordering::Release, Ordering::AcqRel, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:91:41 + | +LL | let _ = x.update(Ordering::Release, Ordering::AcqRel, |old| old + 1); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:94:46 + | +LL | let _ = x.fetch_update(Ordering::AcqRel, Ordering::AcqRel, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `try_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `try_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:96:44 + | +LL | let _ = x.try_update(Ordering::AcqRel, Ordering::AcqRel, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:98:40 + | +LL | let _ = x.update(Ordering::AcqRel, Ordering::AcqRel, |old| old + 1); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:101:46 + | +LL | let _ = x.fetch_update(Ordering::SeqCst, Ordering::AcqRel, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `try_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `try_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:103:44 + | +LL | let _ = x.try_update(Ordering::SeqCst, Ordering::AcqRel, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:105:40 + | +LL | let _ = x.update(Ordering::SeqCst, Ordering::AcqRel, |old| old + 1); + | ^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:110:47 + | +LL | let _ = x.fetch_update(Ordering::Relaxed, Ordering::Release, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `try_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `try_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:112:45 + | +LL | let _ = x.try_update(Ordering::Relaxed, Ordering::Release, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:114:41 + | +LL | let _ = x.update(Ordering::Relaxed, Ordering::Release, |old| old + 1); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:117:47 + | +LL | let _ = x.fetch_update(Ordering::Acquire, Ordering::Release, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `try_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `try_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:119:45 + | +LL | let _ = x.try_update(Ordering::Acquire, Ordering::Release, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:121:41 + | +LL | let _ = x.update(Ordering::Acquire, Ordering::Release, |old| old + 1); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:124:47 + | +LL | let _ = x.fetch_update(Ordering::Release, Ordering::Release, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `try_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `try_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:126:45 + | +LL | let _ = x.try_update(Ordering::Release, Ordering::Release, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:128:41 + | +LL | let _ = x.update(Ordering::Release, Ordering::Release, |old| old + 1); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:131:46 + | +LL | let _ = x.fetch_update(Ordering::AcqRel, Ordering::Release, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `try_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `try_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:133:44 + | +LL | let _ = x.try_update(Ordering::AcqRel, Ordering::Release, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:135:40 + | +LL | let _ = x.update(Ordering::AcqRel, Ordering::Release, |old| old + 1); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `fetch_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `fetch_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:138:46 + | +LL | let _ = x.fetch_update(Ordering::SeqCst, Ordering::Release, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `try_update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `try_update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:140:44 + | +LL | let _ = x.try_update(Ordering::SeqCst, Ordering::Release, |old| Some(old + 1)); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: `update`'s failure ordering may not be `Release` or `AcqRel`, since a failed `update` does not result in a write + --> $DIR/lint-invalid-atomic-ordering-update.rs:142:40 + | +LL | let _ = x.update(Ordering::SeqCst, Ordering::Release, |old| old + 1); + | ^^^^^^^^^^^^^^^^^ invalid failure ordering + | + = help: consider using `Acquire` or `Relaxed` failure ordering instead + +error: aborting due to 30 previous errors + From c07f11ac0aa0651dde845fe3b72393867e0527aa Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 7 Nov 2025 14:38:16 +0100 Subject: [PATCH 7/9] don't completely reset `HeadUsages` --- compiler/rustc_type_ir/src/search_graph/mod.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_type_ir/src/search_graph/mod.rs b/compiler/rustc_type_ir/src/search_graph/mod.rs index 4f3f140af67d1..8e6376b22ce61 100644 --- a/compiler/rustc_type_ir/src/search_graph/mod.rs +++ b/compiler/rustc_type_ir/src/search_graph/mod.rs @@ -23,7 +23,7 @@ use derive_where::derive_where; #[cfg(feature = "nightly")] use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext}; use rustc_type_ir::data_structures::HashMap; -use tracing::{debug, instrument}; +use tracing::{debug, instrument, trace}; mod stack; use stack::{Stack, StackDepth, StackEntry}; @@ -916,6 +916,7 @@ impl, X: Cx> SearchGraph { /// heads from the stack. This may not necessarily mean that we've actually /// reached a fixpoint for that cycle head, which impacts the way we rebase /// provisional cache entries. +#[derive_where(Debug; X: Cx)] enum RebaseReason { NoCycleUsages, Ambiguity(X::AmbiguityInfo), @@ -950,6 +951,7 @@ impl, X: Cx> SearchGraph { /// cache entries to also be ambiguous. This causes some undesirable ambiguity for nested /// goals whose result doesn't actually depend on this cycle head, but that's acceptable /// to me. + #[instrument(level = "trace", skip(self, cx))] fn rebase_provisional_cache_entries( &mut self, cx: X, @@ -969,6 +971,7 @@ impl, X: Cx> SearchGraph { let popped_head = if heads.highest_cycle_head_index() == popped_head_index { heads.remove_highest_cycle_head() } else { + debug_assert!(heads.highest_cycle_head_index() < popped_head_index); return true; }; @@ -1057,6 +1060,8 @@ impl, X: Cx> SearchGraph { new_highest_head_index, )); + trace!(?input, ?entry, "rebased provisional cache entry"); + true }); !entries.is_empty() @@ -1379,7 +1384,8 @@ impl, X: Cx> SearchGraph { } // Clear all provisional cache entries which depend on a previous provisional - // result of this goal and rerun. + // result of this goal and rerun. This does not remove goals which accessed this + // goal without depending on its result. self.clear_dependent_provisional_results_for_rerun(); debug!(?result, "fixpoint changed provisional results"); @@ -1399,7 +1405,12 @@ impl, X: Cx> SearchGraph { // similar to the previous iterations when reevaluating, it's better // for caching if the reevaluation also starts out with `false`. encountered_overflow: false, - usages: None, + // We keep provisional cache entries around if they used this goal + // without depending on its result. + // + // We still need to drop or rebase these cache entries once we've + // finished evaluating this goal. + usages: Some(HeadUsages::default()), candidate_usages: None, }); } From 566a86b02fc81447d922a1bf0a40648466b73dc5 Mon Sep 17 00:00:00 2001 From: 21aslade Date: Mon, 20 Oct 2025 08:42:16 -0600 Subject: [PATCH 8/9] show packed alignment in mir_transform_unaligned_packed_ref --- .../rustc_const_eval/src/util/alignment.rs | 45 +++++++++------- compiler/rustc_const_eval/src/util/mod.rs | 2 +- compiler/rustc_mir_transform/messages.ftl | 7 ++- .../src/add_moves_for_packed_drops.rs | 2 +- .../src/check_packed_ref.rs | 10 +++- .../src/dead_store_elimination.rs | 4 +- compiler/rustc_mir_transform/src/errors.rs | 2 + compiler/rustc_mir_transform/src/validate.rs | 10 ++-- tests/ui/binding/issue-53114-safety-checks.rs | 12 ++--- .../binding/issue-53114-safety-checks.stderr | 24 ++++----- .../diagnostics/repr_packed.rs | 2 +- .../diagnostics/repr_packed.stderr | 4 +- .../lint/unaligned_references.current.stderr | 52 +++++++++---------- .../ui/lint/unaligned_references.next.stderr | 52 +++++++++---------- tests/ui/lint/unaligned_references.rs | 26 +++++----- .../unaligned_references_external_macro.rs | 2 +- ...unaligned_references_external_macro.stderr | 4 +- tests/ui/packed/issue-27060.rs | 8 +-- tests/ui/packed/issue-27060.stderr | 16 +++--- .../packed-struct-borrow-element-64bit.rs | 2 +- .../packed-struct-borrow-element-64bit.stderr | 4 +- .../ui/packed/packed-struct-borrow-element.rs | 4 +- .../packed-struct-borrow-element.stderr | 8 +-- .../ui/packed/packed-union-borrow-element.rs | 26 ++++++++++ .../packed/packed-union-borrow-element.stderr | 23 ++++++++ 25 files changed, 210 insertions(+), 141 deletions(-) create mode 100644 tests/ui/packed/packed-union-borrow-element.rs create mode 100644 tests/ui/packed/packed-union-borrow-element.stderr diff --git a/compiler/rustc_const_eval/src/util/alignment.rs b/compiler/rustc_const_eval/src/util/alignment.rs index 9aafc7efd8a6a..0fab4b288d18f 100644 --- a/compiler/rustc_const_eval/src/util/alignment.rs +++ b/compiler/rustc_const_eval/src/util/alignment.rs @@ -1,24 +1,24 @@ use rustc_abi::Align; use rustc_middle::mir::*; -use rustc_middle::ty::{self, TyCtxt}; +use rustc_middle::ty::{self, AdtDef, TyCtxt}; use tracing::debug; -/// Returns `true` if this place is allowed to be less aligned -/// than its containing struct (because it is within a packed -/// struct). -pub fn is_disaligned<'tcx, L>( +/// If the place may be less aligned than its type requires +/// (because it is in a packed type), returns the AdtDef +/// and packed alignment of its most-unaligned projection. +pub fn place_unalignment<'tcx, L>( tcx: TyCtxt<'tcx>, local_decls: &L, typing_env: ty::TypingEnv<'tcx>, place: Place<'tcx>, -) -> bool +) -> Option<(AdtDef<'tcx>, Align)> where L: HasLocalDecls<'tcx>, { - debug!("is_disaligned({:?})", place); - let Some(pack) = is_within_packed(tcx, local_decls, place) else { - debug!("is_disaligned({:?}) - not within packed", place); - return false; + debug!("unalignment({:?})", place); + let Some((descr, pack)) = most_packed_projection(tcx, local_decls, place) else { + debug!("unalignment({:?}) - not within packed", place); + return None; }; let ty = place.ty(local_decls, tcx).ty; @@ -30,31 +30,34 @@ where || matches!(unsized_tail().kind(), ty::Slice(..) | ty::Str)) => { // If the packed alignment is greater or equal to the field alignment, the type won't be - // further disaligned. + // further unaligned. // However we need to ensure the field is sized; for unsized fields, `layout.align` is // just an approximation -- except when the unsized tail is a slice, where the alignment // is fully determined by the type. debug!( - "is_disaligned({:?}) - align = {}, packed = {}; not disaligned", + "unalignment({:?}) - align = {}, packed = {}; not unaligned", place, layout.align.bytes(), pack.bytes() ); - false + None } _ => { - // We cannot figure out the layout. Conservatively assume that this is disaligned. - debug!("is_disaligned({:?}) - true", place); - true + // We cannot figure out the layout. Conservatively assume that this is unaligned. + debug!("unalignment({:?}) - unaligned", place); + Some((descr, pack)) } } } -pub fn is_within_packed<'tcx, L>( +/// If the place includes a projection from a packed struct, +/// returns the AdtDef and packed alignment of the projection +/// with the lowest pack +pub fn most_packed_projection<'tcx, L>( tcx: TyCtxt<'tcx>, local_decls: &L, place: Place<'tcx>, -) -> Option +) -> Option<(AdtDef<'tcx>, Align)> where L: HasLocalDecls<'tcx>, { @@ -65,9 +68,11 @@ where .take_while(|(_base, elem)| !matches!(elem, ProjectionElem::Deref)) // Consider the packed alignments at play here... .filter_map(|(base, _elem)| { - base.ty(local_decls, tcx).ty.ty_adt_def().and_then(|adt| adt.repr().pack) + let adt = base.ty(local_decls, tcx).ty.ty_adt_def()?; + let pack = adt.repr().pack?; + Some((adt, pack)) }) // ... and compute their minimum. // The overall smallest alignment is what matters. - .min() + .min_by_key(|(_, align)| *align) } diff --git a/compiler/rustc_const_eval/src/util/mod.rs b/compiler/rustc_const_eval/src/util/mod.rs index 5be5ee8d1ae97..39992123882a9 100644 --- a/compiler/rustc_const_eval/src/util/mod.rs +++ b/compiler/rustc_const_eval/src/util/mod.rs @@ -6,7 +6,7 @@ mod check_validity_requirement; mod compare_types; mod type_name; -pub use self::alignment::{is_disaligned, is_within_packed}; +pub use self::alignment::{most_packed_projection, place_unalignment}; pub use self::check_validity_requirement::check_validity_requirement; pub(crate) use self::check_validity_requirement::validate_scalar_in_layout; pub use self::compare_types::{relate_types, sub_types}; diff --git a/compiler/rustc_mir_transform/messages.ftl b/compiler/rustc_mir_transform/messages.ftl index 71ec2db1ef00c..cfc9b8edf7a28 100644 --- a/compiler/rustc_mir_transform/messages.ftl +++ b/compiler/rustc_mir_transform/messages.ftl @@ -69,8 +69,11 @@ mir_transform_tail_expr_local = {$is_generated_name -> *[false] `{$name}` calls a custom destructor } -mir_transform_unaligned_packed_ref = reference to packed field is unaligned - .note = packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses +mir_transform_unaligned_packed_ref = reference to field of packed {$ty_descr} is unaligned + .note = this {$ty_descr} is {$align -> + [one] {""} + *[other] {"at most "} + }{$align}-byte aligned, but the type of this field may require higher alignment .note_ub = creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) .help = copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) diff --git a/compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs b/compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs index 7ae2ebaf4ff09..9950a94d722eb 100644 --- a/compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs +++ b/compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs @@ -51,7 +51,7 @@ impl<'tcx> crate::MirPass<'tcx> for AddMovesForPackedDrops { match terminator.kind { TerminatorKind::Drop { place, .. } - if util::is_disaligned(tcx, body, typing_env, place) => + if util::place_unalignment(tcx, body, typing_env, place).is_some() => { add_move_for_packed_drop( tcx, diff --git a/compiler/rustc_mir_transform/src/check_packed_ref.rs b/compiler/rustc_mir_transform/src/check_packed_ref.rs index 100104e9de03e..9ce244a00fcec 100644 --- a/compiler/rustc_mir_transform/src/check_packed_ref.rs +++ b/compiler/rustc_mir_transform/src/check_packed_ref.rs @@ -37,7 +37,9 @@ impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> { } fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) { - if context.is_borrow() && util::is_disaligned(self.tcx, self.body, self.typing_env, *place) + if context.is_borrow() + && let Some((adt, pack)) = + util::place_unalignment(self.tcx, self.body, self.typing_env, *place) { let def_id = self.body.source.instance.def_id(); if let Some(impl_def_id) = self.tcx.trait_impl_of_assoc(def_id) @@ -48,7 +50,11 @@ impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> { // shouldn't do. span_bug!(self.source_info.span, "builtin derive created an unaligned reference"); } else { - self.tcx.dcx().emit_err(errors::UnalignedPackedRef { span: self.source_info.span }); + self.tcx.dcx().emit_err(errors::UnalignedPackedRef { + span: self.source_info.span, + ty_descr: adt.descr(), + align: pack.bytes(), + }); } } } diff --git a/compiler/rustc_mir_transform/src/dead_store_elimination.rs b/compiler/rustc_mir_transform/src/dead_store_elimination.rs index 732c3dcd44ab8..63ee69322eef0 100644 --- a/compiler/rustc_mir_transform/src/dead_store_elimination.rs +++ b/compiler/rustc_mir_transform/src/dead_store_elimination.rs @@ -23,7 +23,7 @@ use rustc_mir_dataflow::impls::{ }; use crate::simplify::UsedInStmtLocals; -use crate::util::is_within_packed; +use crate::util::most_packed_projection; /// Performs the optimization on the body /// @@ -65,7 +65,7 @@ fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool { // the move may be codegened as a pointer to that field. // Using that disaligned pointer may trigger UB in the callee, // so do nothing. - && is_within_packed(tcx, body, place).is_none() + && most_packed_projection(tcx, body, place).is_none() { call_operands_to_move.push((bb, index)); } diff --git a/compiler/rustc_mir_transform/src/errors.rs b/compiler/rustc_mir_transform/src/errors.rs index a039851681a67..517e4dd692625 100644 --- a/compiler/rustc_mir_transform/src/errors.rs +++ b/compiler/rustc_mir_transform/src/errors.rs @@ -99,6 +99,8 @@ pub(crate) enum ConstMutate { pub(crate) struct UnalignedPackedRef { #[primary_span] pub span: Span, + pub ty_descr: &'static str, + pub align: u64, } #[derive(Diagnostic)] diff --git a/compiler/rustc_mir_transform/src/validate.rs b/compiler/rustc_mir_transform/src/validate.rs index 5a9018a62c574..379af9c442b1d 100644 --- a/compiler/rustc_mir_transform/src/validate.rs +++ b/compiler/rustc_mir_transform/src/validate.rs @@ -20,7 +20,7 @@ use rustc_middle::{bug, span_bug}; use rustc_mir_dataflow::debuginfo::debuginfo_locals; use rustc_trait_selection::traits::ObligationCtxt; -use crate::util::{self, is_within_packed}; +use crate::util::{self, most_packed_projection}; #[derive(Copy, Clone, Debug, PartialEq, Eq)] enum EdgeKind { @@ -409,7 +409,9 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> { // The call destination place and Operand::Move place used as an argument might // be passed by a reference to the callee. Consequently they cannot be packed. - if is_within_packed(self.tcx, &self.body.local_decls, destination).is_some() { + if most_packed_projection(self.tcx, &self.body.local_decls, destination) + .is_some() + { // This is bad! The callee will expect the memory to be aligned. self.fail( location, @@ -423,7 +425,9 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> { for arg in args { if let Operand::Move(place) = &arg.node { - if is_within_packed(self.tcx, &self.body.local_decls, *place).is_some() { + if most_packed_projection(self.tcx, &self.body.local_decls, *place) + .is_some() + { // This is bad! The callee will expect the memory to be aligned. self.fail( location, diff --git a/tests/ui/binding/issue-53114-safety-checks.rs b/tests/ui/binding/issue-53114-safety-checks.rs index f4be2b482a7e6..07dfda77622a2 100644 --- a/tests/ui/binding/issue-53114-safety-checks.rs +++ b/tests/ui/binding/issue-53114-safety-checks.rs @@ -20,12 +20,12 @@ fn let_wild_gets_unsafe_field() { let u1 = U { a: I(0) }; let u2 = U { a: I(1) }; let p = P { a: &2, b: &3 }; - let _ = &p.b; //~ ERROR reference to packed field + let _ = &p.b; //~ ERROR reference to field of packed struct let _ = u1.a; //~ ERROR [E0133] let _ = &u2.a; //~ ERROR [E0133] // variation on above with `_` in substructure - let (_,) = (&p.b,); //~ ERROR reference to packed field + let (_,) = (&p.b,); //~ ERROR reference to field of packed struct let (_,) = (u1.a,); //~ ERROR [E0133] let (_,) = (&u2.a,); //~ ERROR [E0133] } @@ -34,12 +34,12 @@ fn let_ascribe_gets_unsafe_field() { let u1 = U { a: I(0) }; let u2 = U { a: I(1) }; let p = P { a: &2, b: &3 }; - let _: _ = &p.b; //~ ERROR reference to packed field + let _: _ = &p.b; //~ ERROR reference to field of packed struct let _: _ = u1.a; //~ ERROR [E0133] let _: _ = &u2.a; //~ ERROR [E0133] // variation on above with `_` in substructure - let (_,): _ = (&p.b,); //~ ERROR reference to packed field + let (_,): _ = (&p.b,); //~ ERROR reference to field of packed struct let (_,): _ = (u1.a,); //~ ERROR [E0133] let (_,): _ = (&u2.a,); //~ ERROR [E0133] } @@ -48,12 +48,12 @@ fn match_unsafe_field_to_wild() { let u1 = U { a: I(0) }; let u2 = U { a: I(1) }; let p = P { a: &2, b: &3 }; - match &p.b { _ => { } } //~ ERROR reference to packed field + match &p.b { _ => { } } //~ ERROR reference to field of packed struct match u1.a { _ => { } } //~ ERROR [E0133] match &u2.a { _ => { } } //~ ERROR [E0133] // variation on above with `_` in substructure - match (&p.b,) { (_,) => { } } //~ ERROR reference to packed field + match (&p.b,) { (_,) => { } } //~ ERROR reference to field of packed struct match (u1.a,) { (_,) => { } } //~ ERROR [E0133] match (&u2.a,) { (_,) => { } } //~ ERROR [E0133] } diff --git a/tests/ui/binding/issue-53114-safety-checks.stderr b/tests/ui/binding/issue-53114-safety-checks.stderr index 9d909e915c21b..3e8389b77c53f 100644 --- a/tests/ui/binding/issue-53114-safety-checks.stderr +++ b/tests/ui/binding/issue-53114-safety-checks.stderr @@ -1,20 +1,20 @@ -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/issue-53114-safety-checks.rs:23:13 | LL | let _ = &p.b; | ^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/issue-53114-safety-checks.rs:28:17 | LL | let (_,) = (&p.b,); | ^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) @@ -50,23 +50,23 @@ LL | let (_,) = (&u2.a,); | = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/issue-53114-safety-checks.rs:37:16 | LL | let _: _ = &p.b; | ^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/issue-53114-safety-checks.rs:42:20 | LL | let (_,): _ = (&p.b,); | ^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) @@ -102,23 +102,23 @@ LL | let (_,): _ = (&u2.a,); | = note: the field may not be properly initialized: using uninitialized data will cause undefined behavior -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/issue-53114-safety-checks.rs:51:11 | LL | match &p.b { _ => { } } | ^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/issue-53114-safety-checks.rs:56:12 | LL | match (&p.b,) { (_,) => { } } | ^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) diff --git a/tests/ui/closures/2229_closure_analysis/diagnostics/repr_packed.rs b/tests/ui/closures/2229_closure_analysis/diagnostics/repr_packed.rs index fe5106c57af68..4395b70a922fb 100644 --- a/tests/ui/closures/2229_closure_analysis/diagnostics/repr_packed.rs +++ b/tests/ui/closures/2229_closure_analysis/diagnostics/repr_packed.rs @@ -19,7 +19,7 @@ fn test_missing_unsafe_warning_on_repr_packed() { let c = || { println!("{}", foo.x); - //~^ ERROR: reference to packed field is unaligned + //~^ ERROR: reference to field of packed struct is unaligned let _z = foo.x; }; diff --git a/tests/ui/closures/2229_closure_analysis/diagnostics/repr_packed.stderr b/tests/ui/closures/2229_closure_analysis/diagnostics/repr_packed.stderr index c9972c8e7e349..0e93e033c022c 100644 --- a/tests/ui/closures/2229_closure_analysis/diagnostics/repr_packed.stderr +++ b/tests/ui/closures/2229_closure_analysis/diagnostics/repr_packed.stderr @@ -1,10 +1,10 @@ -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/repr_packed.rs:21:24 | LL | println!("{}", foo.x); | ^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui/lint/unaligned_references.current.stderr b/tests/ui/lint/unaligned_references.current.stderr index 0f980c5301f37..39ca072e848cc 100644 --- a/tests/ui/lint/unaligned_references.current.stderr +++ b/tests/ui/lint/unaligned_references.current.stderr @@ -1,130 +1,130 @@ -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:32:13 | LL | &self.x; | ^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is at most 2-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:44:24 | LL | println!("{:?}", &*foo.0); | ^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:46:24 | LL | println!("{:?}", &*foo.0); | ^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:51:24 | LL | println!("{:?}", &*foo.0); | ^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:81:17 | LL | let _ = &good.ptr; | ^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:82:17 | LL | let _ = &good.data; | ^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:84:17 | LL | let _ = &good.data as *const _; | ^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:85:27 | LL | let _: *const _ = &good.data; | ^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:87:17 | LL | let _ = good.data.clone(); | ^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:89:17 | LL | let _ = &good.data2[0]; | ^^^^^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:98:17 | LL | let _ = &packed2.x; | ^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is at most 2-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:137:20 | LL | let _ref = &m1.1.a; | ^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:140:20 | LL | let _ref = &m2.1.a; | ^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) diff --git a/tests/ui/lint/unaligned_references.next.stderr b/tests/ui/lint/unaligned_references.next.stderr index 0f980c5301f37..39ca072e848cc 100644 --- a/tests/ui/lint/unaligned_references.next.stderr +++ b/tests/ui/lint/unaligned_references.next.stderr @@ -1,130 +1,130 @@ -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:32:13 | LL | &self.x; | ^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is at most 2-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:44:24 | LL | println!("{:?}", &*foo.0); | ^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:46:24 | LL | println!("{:?}", &*foo.0); | ^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:51:24 | LL | println!("{:?}", &*foo.0); | ^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:81:17 | LL | let _ = &good.ptr; | ^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:82:17 | LL | let _ = &good.data; | ^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:84:17 | LL | let _ = &good.data as *const _; | ^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:85:27 | LL | let _: *const _ = &good.data; | ^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:87:17 | LL | let _ = good.data.clone(); | ^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:89:17 | LL | let _ = &good.data2[0]; | ^^^^^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:98:17 | LL | let _ = &packed2.x; | ^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is at most 2-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:137:20 | LL | let _ref = &m1.1.a; | ^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references.rs:140:20 | LL | let _ref = &m2.1.a; | ^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) diff --git a/tests/ui/lint/unaligned_references.rs b/tests/ui/lint/unaligned_references.rs index 321e3ed135c47..af922a1031ab6 100644 --- a/tests/ui/lint/unaligned_references.rs +++ b/tests/ui/lint/unaligned_references.rs @@ -29,7 +29,7 @@ trait Foo { impl Foo for Packed2 { fn evil(&self) { unsafe { - &self.x; //~ ERROR reference to packed field + &self.x; //~ ERROR reference to field of packed struct } } } @@ -41,14 +41,14 @@ fn packed_dyn() { let ref local = Unaligned(ManuallyDrop::new([3, 5, 8u64])); let foo: &Unaligned = &*local; - println!("{:?}", &*foo.0); //~ ERROR reference to packed field + println!("{:?}", &*foo.0); //~ ERROR reference to field of packed struct let foo: &Unaligned<[u64]> = &*local; - println!("{:?}", &*foo.0); //~ ERROR reference to packed field + println!("{:?}", &*foo.0); //~ ERROR reference to field of packed struct // Even if the actual alignment is 1, we cannot know that when looking at `dyn Debug.` let ref local = Unaligned(ManuallyDrop::new([3, 5, 8u8])); let foo: &Unaligned = &*local; - println!("{:?}", &*foo.0); //~ ERROR reference to packed field + println!("{:?}", &*foo.0); //~ ERROR reference to field of packed struct // However, we *can* know the alignment when looking at a slice. let foo: &Unaligned<[u8]> = &*local; println!("{:?}", &*foo.0); // no error! @@ -78,15 +78,15 @@ fn main() { unsafe { let good = Good { data: 0, ptr: &0, data2: [0, 0], aligned: [0; 32] }; - let _ = &good.ptr; //~ ERROR reference to packed field - let _ = &good.data; //~ ERROR reference to packed field + let _ = &good.ptr; //~ ERROR reference to field of packed struct + let _ = &good.data; //~ ERROR reference to field of packed struct // Error even when turned into raw pointer immediately. - let _ = &good.data as *const _; //~ ERROR reference to packed field - let _: *const _ = &good.data; //~ ERROR reference to packed field + let _ = &good.data as *const _; //~ ERROR reference to field of packed struct + let _: *const _ = &good.data; //~ ERROR reference to field of packed struct // Error on method call. - let _ = good.data.clone(); //~ ERROR reference to packed field + let _ = good.data.clone(); //~ ERROR reference to field of packed struct // Error for nested fields. - let _ = &good.data2[0]; //~ ERROR reference to packed field + let _ = &good.data2[0]; //~ ERROR reference to field of packed struct let _ = &*good.ptr; // ok, behind a pointer let _ = &good.aligned; // ok, has align 1 @@ -95,7 +95,7 @@ fn main() { unsafe { let packed2 = Packed2 { x: 0, y: 0, z: 0 }; - let _ = &packed2.x; //~ ERROR reference to packed field + let _ = &packed2.x; //~ ERROR reference to field of packed struct let _ = &packed2.y; // ok, has align 2 in packed(2) struct let _ = &packed2.z; // ok, has align 1 packed2.evil(); @@ -134,9 +134,9 @@ fn main() { struct Misalign(u8, T); let m1 = Misalign(0, Wrapper { a: U16(10), b: HasDrop }); - let _ref = &m1.1.a; //~ ERROR reference to packed field + let _ref = &m1.1.a; //~ ERROR reference to field of packed struct let m2 = Misalign(0, Wrapper2 { a: U16(10), b: HasDrop }); - let _ref = &m2.1.a; //~ ERROR reference to packed field + let _ref = &m2.1.a; //~ ERROR reference to field of packed struct } } diff --git a/tests/ui/lint/unaligned_references_external_macro.rs b/tests/ui/lint/unaligned_references_external_macro.rs index 3a97e2112a144..2d6e493dd542f 100644 --- a/tests/ui/lint/unaligned_references_external_macro.rs +++ b/tests/ui/lint/unaligned_references_external_macro.rs @@ -2,7 +2,7 @@ extern crate unaligned_references_external_crate; -unaligned_references_external_crate::mac! { //~ERROR reference to packed field is unaligned +unaligned_references_external_crate::mac! { //~ERROR reference to field of packed struct is unaligned #[repr(packed)] pub struct X { pub field: u16 diff --git a/tests/ui/lint/unaligned_references_external_macro.stderr b/tests/ui/lint/unaligned_references_external_macro.stderr index 9945c78e8ba66..dd8788aec4b5c 100644 --- a/tests/ui/lint/unaligned_references_external_macro.stderr +++ b/tests/ui/lint/unaligned_references_external_macro.stderr @@ -1,4 +1,4 @@ -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/unaligned_references_external_macro.rs:5:1 | LL | / unaligned_references_external_crate::mac! { @@ -9,7 +9,7 @@ LL | | } LL | | } | |_^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) = note: this error originates in the macro `unaligned_references_external_crate::mac` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui/packed/issue-27060.rs b/tests/ui/packed/issue-27060.rs index a0e944caa0b55..7d8be9603b686 100644 --- a/tests/ui/packed/issue-27060.rs +++ b/tests/ui/packed/issue-27060.rs @@ -12,11 +12,11 @@ fn main() { aligned: [0; 32] }; - let _ = &good.data; //~ ERROR reference to packed field - let _ = &good.data2[0]; //~ ERROR reference to packed field + let _ = &good.data; //~ ERROR reference to field of packed struct + let _ = &good.data2[0]; //~ ERROR reference to field of packed struct - let _ = &good.data; //~ ERROR reference to packed field - let _ = &good.data2[0]; //~ ERROR reference to packed field + let _ = &good.data; //~ ERROR reference to field of packed struct + let _ = &good.data2[0]; //~ ERROR reference to field of packed struct let _ = &*good.data; // ok, behind a pointer let _ = &good.aligned; // ok, has align 1 let _ = &good.aligned[2]; // ok, has align 1 diff --git a/tests/ui/packed/issue-27060.stderr b/tests/ui/packed/issue-27060.stderr index 4dc31a283865c..3ede68cf93625 100644 --- a/tests/ui/packed/issue-27060.stderr +++ b/tests/ui/packed/issue-27060.stderr @@ -1,40 +1,40 @@ -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/issue-27060.rs:15:13 | LL | let _ = &good.data; | ^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/issue-27060.rs:16:13 | LL | let _ = &good.data2[0]; | ^^^^^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/issue-27060.rs:18:13 | LL | let _ = &good.data; | ^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/issue-27060.rs:19:13 | LL | let _ = &good.data2[0]; | ^^^^^^^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) diff --git a/tests/ui/packed/packed-struct-borrow-element-64bit.rs b/tests/ui/packed/packed-struct-borrow-element-64bit.rs index 81eac07eaa894..59bd71c80e8c3 100644 --- a/tests/ui/packed/packed-struct-borrow-element-64bit.rs +++ b/tests/ui/packed/packed-struct-borrow-element-64bit.rs @@ -10,6 +10,6 @@ struct Foo4C { pub fn main() { let foo = Foo4C { bar: 1, baz: 2 }; - let brw = &foo.baz; //~ERROR reference to packed field is unaligned + let brw = &foo.baz; //~ERROR reference to field of packed struct is unaligned assert_eq!(*brw, 2); } diff --git a/tests/ui/packed/packed-struct-borrow-element-64bit.stderr b/tests/ui/packed/packed-struct-borrow-element-64bit.stderr index a464b18938784..dcf8e7a51f13d 100644 --- a/tests/ui/packed/packed-struct-borrow-element-64bit.stderr +++ b/tests/ui/packed/packed-struct-borrow-element-64bit.stderr @@ -1,10 +1,10 @@ -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/packed-struct-borrow-element-64bit.rs:13:15 | LL | let brw = &foo.baz; | ^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is at most 4-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) diff --git a/tests/ui/packed/packed-struct-borrow-element.rs b/tests/ui/packed/packed-struct-borrow-element.rs index 24dadbcec7ca6..d639746e067bd 100644 --- a/tests/ui/packed/packed-struct-borrow-element.rs +++ b/tests/ui/packed/packed-struct-borrow-element.rs @@ -21,10 +21,10 @@ struct Foo4C { pub fn main() { let foo = Foo1 { bar: 1, baz: 2 }; - let brw = &foo.baz; //~ERROR reference to packed field is unaligned + let brw = &foo.baz; //~ERROR reference to field of packed struct is unaligned assert_eq!(*brw, 2); let foo = Foo2 { bar: 1, baz: 2 }; - let brw = &foo.baz; //~ERROR reference to packed field is unaligned + let brw = &foo.baz; //~ERROR reference to field of packed struct is unaligned assert_eq!(*brw, 2); } diff --git a/tests/ui/packed/packed-struct-borrow-element.stderr b/tests/ui/packed/packed-struct-borrow-element.stderr index c1f749d6fbbbd..ccdbb3aedc715 100644 --- a/tests/ui/packed/packed-struct-borrow-element.stderr +++ b/tests/ui/packed/packed-struct-borrow-element.stderr @@ -1,20 +1,20 @@ -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/packed-struct-borrow-element.rs:24:15 | LL | let brw = &foo.baz; | ^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) -error[E0793]: reference to packed field is unaligned +error[E0793]: reference to field of packed struct is unaligned --> $DIR/packed-struct-borrow-element.rs:28:15 | LL | let brw = &foo.baz; | ^^^^^^^^ | - = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses + = note: this struct is at most 2-byte aligned, but the type of this field may require higher alignment = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) diff --git a/tests/ui/packed/packed-union-borrow-element.rs b/tests/ui/packed/packed-union-borrow-element.rs new file mode 100644 index 0000000000000..e39d72b4a45c4 --- /dev/null +++ b/tests/ui/packed/packed-union-borrow-element.rs @@ -0,0 +1,26 @@ +#![allow(dead_code)] +//@ ignore-emscripten weird assertion? + +#[repr(packed)] +#[derive(Clone, Copy)] +struct Foo1(usize); + +#[repr(packed(4))] +#[derive(Clone, Copy)] +struct Foo4(usize); + +#[repr(packed(2))] +union Bar2 { + foo1: Foo1, + foo4: Foo4, +} + +pub fn main() { + let bar = Bar2 { foo1: Foo1(2) }; + let brw = unsafe { &bar.foo1.0 }; //~ERROR reference to field of packed struct is unaligned + assert_eq!(*brw, 2); + + let bar = Bar2 { foo4: Foo4(2) }; + let brw = unsafe { &bar.foo4.0 }; //~ERROR reference to field of packed union is unaligned + assert_eq!(*brw, 2); +} diff --git a/tests/ui/packed/packed-union-borrow-element.stderr b/tests/ui/packed/packed-union-borrow-element.stderr new file mode 100644 index 0000000000000..ace7620d97f72 --- /dev/null +++ b/tests/ui/packed/packed-union-borrow-element.stderr @@ -0,0 +1,23 @@ +error[E0793]: reference to field of packed struct is unaligned + --> $DIR/packed-union-borrow-element.rs:20:24 + | +LL | let brw = unsafe { &bar.foo1.0 }; + | ^^^^^^^^^^^ + | + = note: this struct is 1-byte aligned, but the type of this field may require higher alignment + = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) + = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) + +error[E0793]: reference to field of packed union is unaligned + --> $DIR/packed-union-borrow-element.rs:24:24 + | +LL | let brw = unsafe { &bar.foo4.0 }; + | ^^^^^^^^^^^ + | + = note: this union is at most 2-byte aligned, but the type of this field may require higher alignment + = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) + = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers) + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0793`. From 67802e0494034e1c55205a178b2ee1843e6d39da Mon Sep 17 00:00:00 2001 From: MolecularPilot Date: Thu, 30 Oct 2025 17:36:11 +1100 Subject: [PATCH 9/9] rustc_builtin_macros: rename bench parameter to avoid collisions with user-defined function names --- compiler/rustc_builtin_macros/src/test.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index 7a189ee1f4d05..b0155fad139b8 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -207,30 +207,30 @@ pub(crate) fn expand_test_or_bench( }; let test_fn = if is_bench { - // A simple ident for a lambda - let b = Ident::from_str_and_span("b", attr_sp); - + // avoid name collisions by using the function name within the identifier, see bug #148275 + let bencher_param = + Ident::from_str_and_span(&format!("__bench_{}", fn_.ident.name), attr_sp); cx.expr_call( sp, cx.expr_path(test_path("StaticBenchFn")), thin_vec![ // #[coverage(off)] - // |b| self::test::assert_test_result( + // |__bench_fn_name| self::test::assert_test_result( coverage_off(cx.lambda1( sp, cx.expr_call( sp, cx.expr_path(test_path("assert_test_result")), thin_vec![ - // super::$test_fn(b) + // super::$test_fn(__bench_fn_name) cx.expr_call( ret_ty_sp, cx.expr_path(cx.path(sp, vec![fn_.ident])), - thin_vec![cx.expr_ident(sp, b)], + thin_vec![cx.expr_ident(sp, bencher_param)], ), ], ), - b, + bencher_param, )), // ) ], )