From 37bb774219659d5fc7f72c83ca2481eea87ff3dd Mon Sep 17 00:00:00 2001 From: Florian Bartels Date: Thu, 12 Dec 2024 14:03:25 +0100 Subject: [PATCH 01/11] Reduce the need to set archiver via environment variables --- src/bootstrap/src/utils/cc_detect.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/bootstrap/src/utils/cc_detect.rs b/src/bootstrap/src/utils/cc_detect.rs index e8d5b60948aa8..10611490ce35b 100644 --- a/src/bootstrap/src/utils/cc_detect.rs +++ b/src/bootstrap/src/utils/cc_detect.rs @@ -44,6 +44,16 @@ fn cc2ar(cc: &Path, target: TargetSelection) -> Option { Some(PathBuf::from("ar")) } else if target.contains("vxworks") { Some(PathBuf::from("wr-ar")) + } else if target.contains("-nto-") { + if target.starts_with("i586") { + Some(PathBuf::from("ntox86-ar")) + } else if target.starts_with("aarch64") { + Some(PathBuf::from("ntoaarch64-ar")) + } else if target.starts_with("x86_64") { + Some(PathBuf::from("ntox86_64-ar")) + } else { + panic!("Unknown architecture, cannot determine archiver for Neutrino QNX"); + } } else if target.contains("android") || target.contains("-wasi") { Some(cc.parent().unwrap().join(PathBuf::from("llvm-ar"))) } else { From 6b93fac9ff967aa6bb6119307e13b07fe5750888 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 12 Dec 2024 11:00:02 -0800 Subject: [PATCH 02/11] Update wasi-sdk used to build WASI targets Bump to the latest wasi-sdk-25 release which brings in various wasi-libc updates as well as LLVM 19 as the version used to compile wasi-libc. --- src/ci/docker/host-x86_64/dist-various-2/Dockerfile | 4 ++-- src/ci/docker/host-x86_64/test-various/Dockerfile | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ci/docker/host-x86_64/dist-various-2/Dockerfile b/src/ci/docker/host-x86_64/dist-various-2/Dockerfile index c40de76abbfee..03ec77f507e75 100644 --- a/src/ci/docker/host-x86_64/dist-various-2/Dockerfile +++ b/src/ci/docker/host-x86_64/dist-various-2/Dockerfile @@ -90,9 +90,9 @@ RUN /tmp/build-solaris-toolchain.sh sparcv9 sparcv9 solaris-sparc sun COPY host-x86_64/dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh /tmp/ RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh -RUN curl -L https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-23/wasi-sdk-23.0-x86_64-linux.tar.gz | \ +RUN curl -L https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/wasi-sdk-25.0-x86_64-linux.tar.gz | \ tar -xz -ENV WASI_SDK_PATH=/tmp/wasi-sdk-23.0-x86_64-linux +ENV WASI_SDK_PATH=/tmp/wasi-sdk-25.0-x86_64-linux COPY scripts/freebsd-toolchain.sh /tmp/ RUN /tmp/freebsd-toolchain.sh i686 diff --git a/src/ci/docker/host-x86_64/test-various/Dockerfile b/src/ci/docker/host-x86_64/test-various/Dockerfile index c2f5a87b1234f..8d2e45ae497ef 100644 --- a/src/ci/docker/host-x86_64/test-various/Dockerfile +++ b/src/ci/docker/host-x86_64/test-various/Dockerfile @@ -40,9 +40,9 @@ WORKDIR / COPY scripts/sccache.sh /scripts/ RUN sh /scripts/sccache.sh -RUN curl -L https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-23/wasi-sdk-23.0-x86_64-linux.tar.gz | \ +RUN curl -L https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/wasi-sdk-25.0-x86_64-linux.tar.gz | \ tar -xz -ENV WASI_SDK_PATH=/wasi-sdk-23.0-x86_64-linux +ENV WASI_SDK_PATH=/wasi-sdk-25.0-x86_64-linux ENV RUST_CONFIGURE_ARGS \ --musl-root-x86_64=/usr/local/x86_64-linux-musl \ From 6ce7ba4300fde2afe1cd74958ec4293b49950bfe Mon Sep 17 00:00:00 2001 From: Alisa Sireneva Date: Thu, 12 Dec 2024 22:48:50 +0300 Subject: [PATCH 03/11] Fix typos in docs on provenance --- library/core/src/ptr/mod.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index bc4c4e168a369..51ab2054b3beb 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -200,7 +200,7 @@ //! //! But it *is* still sound to: //! -//! * Create a pointer without provenance from just an address (see [`ptr::dangling`]). Such a +//! * Create a pointer without provenance from just an address (see [`without_provenance`]). Such a //! pointer cannot be used for memory accesses (except for zero-sized accesses). This can still be //! useful for sentinel values like `null` *or* to represent a tagged pointer that will never be //! dereferenceable. In general, it is always sound for an integer to pretend to be a pointer "for @@ -314,8 +314,8 @@ //! } //! ``` //! -//! (Yes, if you've been using AtomicUsize for pointers in concurrent datastructures, you should -//! be using AtomicPtr instead. If that messes up the way you atomically manipulate pointers, +//! (Yes, if you've been using [`AtomicUsize`] for pointers in concurrent datastructures, you should +//! be using [`AtomicPtr`] instead. If that messes up the way you atomically manipulate pointers, //! we would like to know why, and what needs to be done to fix it.) //! //! Situations where a valid pointer *must* be created from just an address, such as baremetal code @@ -381,7 +381,8 @@ //! [`with_addr`]: pointer::with_addr //! [`map_addr`]: pointer::map_addr //! [`addr`]: pointer::addr -//! [`ptr::dangling`]: core::ptr::dangling +//! [`AtomicUsize`]: crate::sync::atomic::AtomicUsize +//! [`AtomicPtr`]: crate::sync::atomic::AtomicPtr //! [`expose_provenance`]: pointer::expose_provenance //! [`with_exposed_provenance`]: with_exposed_provenance //! [Miri]: https://github.com/rust-lang/miri From 7880abac443a7c514026f71129e061e44992c36d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Thu, 12 Dec 2024 22:55:31 +0100 Subject: [PATCH 04/11] crashes: more tests v2 --- tests/crashes/133426.rs | 12 ++++++++++++ tests/crashes/133597.rs | 11 +++++++++++ tests/crashes/133639.rs | 33 +++++++++++++++++++++++++++++++++ tests/crashes/133808.rs | 15 +++++++++++++++ tests/crashes/133868.rs | 13 +++++++++++++ tests/crashes/133965.rs | 9 +++++++++ tests/crashes/133966.rs | 3 +++ tests/crashes/134005.rs | 5 +++++ tests/crashes/134061.rs | 4 ++++ tests/crashes/134162.rs | 8 ++++++++ tests/crashes/134217.rs | 9 +++++++++ 11 files changed, 122 insertions(+) create mode 100644 tests/crashes/133426.rs create mode 100644 tests/crashes/133597.rs create mode 100644 tests/crashes/133639.rs create mode 100644 tests/crashes/133808.rs create mode 100644 tests/crashes/133868.rs create mode 100644 tests/crashes/133965.rs create mode 100644 tests/crashes/133966.rs create mode 100644 tests/crashes/134005.rs create mode 100644 tests/crashes/134061.rs create mode 100644 tests/crashes/134162.rs create mode 100644 tests/crashes/134217.rs diff --git a/tests/crashes/133426.rs b/tests/crashes/133426.rs new file mode 100644 index 0000000000000..307a94c0f6ca6 --- /dev/null +++ b/tests/crashes/133426.rs @@ -0,0 +1,12 @@ +//@ known-bug: #133426 + +fn a( + _: impl Iterator< + Item = [(); { + match *todo!() { ! }; + }], + >, +) { +} + +fn b(_: impl Iterator) {} diff --git a/tests/crashes/133597.rs b/tests/crashes/133597.rs new file mode 100644 index 0000000000000..f716d5e7bc74f --- /dev/null +++ b/tests/crashes/133597.rs @@ -0,0 +1,11 @@ +//@ known-bug: #133597 + +pub trait Foo2 { + fn boxed<'a: 'a>() -> impl Sized + FnOnce<()>; +} + +impl Foo2 for () {} + + +fn f() -> impl FnOnce<()> { || () } +fn main() { () = f(); } diff --git a/tests/crashes/133639.rs b/tests/crashes/133639.rs new file mode 100644 index 0000000000000..d522b0730cf86 --- /dev/null +++ b/tests/crashes/133639.rs @@ -0,0 +1,33 @@ +//@ known-bug: #133639 + +#![feature(with_negative_coherence)] +#![feature(min_specialization)] +#![feature(generic_const_exprs)] + +#![crate_type = "lib"] +use std::str::FromStr; + +struct a; + +trait c {} + +impl FromStr for e +where + a<{ d <= 2 }>: c, +{ + type Err = (); + fn from_str(f: &str) -> Result { + unimplemented!() + } +} +struct e; + +impl FromStr for e +where + a<{ d <= 2 }>: c, +{ + type Err = (); + fn from_str(f: &str) -> Result { + unimplemented!() + } +} diff --git a/tests/crashes/133808.rs b/tests/crashes/133808.rs new file mode 100644 index 0000000000000..9c6a23d1e35b5 --- /dev/null +++ b/tests/crashes/133808.rs @@ -0,0 +1,15 @@ +//@ known-bug: #133808 + +#![feature(generic_const_exprs, transmutability)] + +mod assert { + use std::mem::TransmuteFrom; + + pub fn is_transmutable() + where + Dst: TransmuteFrom, + { + } +} + +pub fn main() {} diff --git a/tests/crashes/133868.rs b/tests/crashes/133868.rs new file mode 100644 index 0000000000000..dc25cb9df288e --- /dev/null +++ b/tests/crashes/133868.rs @@ -0,0 +1,13 @@ +//@ known-bug: #133868 + +trait Foo { + type Assoc; +} + +trait Bar { + fn method() -> impl Sized; +} +impl Bar for T where ::Assoc: Sized +{ + fn method() {} +} diff --git a/tests/crashes/133965.rs b/tests/crashes/133965.rs new file mode 100644 index 0000000000000..69f533ccbe987 --- /dev/null +++ b/tests/crashes/133965.rs @@ -0,0 +1,9 @@ +//@ known-bug: #133965 +//@ needs-rustc-debug-assertions + +struct NonGeneric {} + +#[derive(Default)] +struct NonGeneric<'a, const N: usize> {} + +pub fn main() {} diff --git a/tests/crashes/133966.rs b/tests/crashes/133966.rs new file mode 100644 index 0000000000000..25a881ae99b4f --- /dev/null +++ b/tests/crashes/133966.rs @@ -0,0 +1,3 @@ +//@ known-bug: #133966 +pub struct Data([[&'static str]; 5_i32]); +const _: &'static Data = unsafe { &*(&[] as *const Data) }; diff --git a/tests/crashes/134005.rs b/tests/crashes/134005.rs new file mode 100644 index 0000000000000..c1f4c758a14ec --- /dev/null +++ b/tests/crashes/134005.rs @@ -0,0 +1,5 @@ +//@ known-bug: #134005 + +fn main() { + let _ = [std::ops::Add::add, std::ops::Mul::mul, main as fn(_, &_)]; +} diff --git a/tests/crashes/134061.rs b/tests/crashes/134061.rs new file mode 100644 index 0000000000000..e00eb7603fead --- /dev/null +++ b/tests/crashes/134061.rs @@ -0,0 +1,4 @@ +//@ known-bug: #134061 +//@ needs-rustc-debug-assertions + +const x: () = |&'a diff --git a/tests/crashes/134162.rs b/tests/crashes/134162.rs new file mode 100644 index 0000000000000..9e5a4a1cb0bf3 --- /dev/null +++ b/tests/crashes/134162.rs @@ -0,0 +1,8 @@ +//@ known-bug: #134162 + +fn main() { + struct X; + + let xs = [X, X, X]; + let eq = xs == [panic!("panic evaluated"); 2]; +} diff --git a/tests/crashes/134217.rs b/tests/crashes/134217.rs new file mode 100644 index 0000000000000..1b14c660e8b4c --- /dev/null +++ b/tests/crashes/134217.rs @@ -0,0 +1,9 @@ +//@ known-bug: #134217 + +impl std::ops::CoerceUnsized for A {} + +fn main() { + if let _ = true + && true + {} +} From 4c6d793c66993a0f5455f35e73a1549d232c3ae5 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 12 Dec 2024 17:06:03 -0800 Subject: [PATCH 05/11] Only dist `llvm-objcopy` if llvm tools are enabled --- src/bootstrap/src/core/build_steps/dist.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/build_steps/dist.rs b/src/bootstrap/src/core/build_steps/dist.rs index 0c739115165ec..89b2d73f74a81 100644 --- a/src/bootstrap/src/core/build_steps/dist.rs +++ b/src/bootstrap/src/core/build_steps/dist.rs @@ -471,7 +471,7 @@ impl Step for Rustc { } } - { + if builder.config.llvm_enabled(compiler.host) && builder.config.llvm_tools_enabled { let src_dir = builder.sysroot_target_bindir(compiler, host); let llvm_objcopy = exe("llvm-objcopy", compiler.host); let rust_objcopy = exe("rust-objcopy", compiler.host); From 65a609b4f21b53f55535f0f6cf51e0381ac0f95b Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Thu, 12 Dec 2024 15:38:50 +0300 Subject: [PATCH 06/11] validate `--skip` and `--exclude` paths Signed-off-by: onur-ozkan --- src/bootstrap/src/core/config/config.rs | 26 ++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index b06147055f2a7..aff3ac3454bf6 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -1314,7 +1314,31 @@ impl Config { // Set flags. config.paths = std::mem::take(&mut flags.paths); - config.skip = flags.skip.into_iter().chain(flags.exclude).collect(); + config.skip = flags + .skip + .into_iter() + .chain(flags.exclude) + .map(|p| { + let p = if cfg!(windows) { + PathBuf::from(p.to_str().unwrap().replace('/', "\\")) + } else { + p + }; + + // Jump to top-level project path to support passing paths + // from sub directories. + let top_level_path = config.src.join(&p); + if !config.src.join(&top_level_path).exists() { + eprintln!("WARNING: '{}' does not exist.", top_level_path.display()); + } + + // Never return top-level path here as it would break `--skip` + // logic on rustc's internal test framework which is utilized + // by compiletest. + p + }) + .collect(); + config.include_default_paths = flags.include_default_paths; config.rustc_error_format = flags.rustc_error_format; config.json_output = flags.json_output; From 907846e01b759cda8da7f3ac369b657e29bdff54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Fri, 13 Dec 2024 11:50:40 +0100 Subject: [PATCH 07/11] Fix `Path::is_absolute` on Hermit --- library/std/src/path.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 5b277a982eeb8..35e920ab34476 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2327,7 +2327,9 @@ impl Path { // FIXME: Allow Redox prefixes self.has_root() || has_redox_scheme(self.as_u8_slice()) } else { - self.has_root() && (cfg!(any(unix, target_os = "wasi")) || self.prefix().is_some()) + self.has_root() + && (cfg!(any(unix, target_os = "hermit", target_os = "wasi")) + || self.prefix().is_some()) } } From e17ca31b22f26209bbdf28a95e045aa0da337733 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Fri, 13 Dec 2024 06:56:49 +0100 Subject: [PATCH 08/11] rustc_borrowck: Make suggest_ampmut() return type match its use So that it becomes easy for a later commit to return `None`. --- .../src/diagnostics/mutability_errors.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index c5ebf3c547e9d..48fc0c331b8bf 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -1150,7 +1150,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { None } None => { - let (has_sugg, decl_span, sugg) = if name != kw::SelfLower { + if name != kw::SelfLower { suggest_ampmut( self.infcx.tcx, local_decl.ty, @@ -1165,7 +1165,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { .. })) => { let sugg = suggest_ampmut_self(self.infcx.tcx, decl_span); - (true, decl_span, sugg) + Some((true, decl_span, sugg, None)) } // explicit self (eg `self: &'a Self`) _ => suggest_ampmut( @@ -1176,8 +1176,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { opt_ty_info, ), } - }; - Some((has_sugg, decl_span, sugg, None)) + } } } } @@ -1443,7 +1442,7 @@ fn suggest_ampmut<'tcx>( decl_span: Span, opt_assignment_rhs_span: Option, opt_ty_info: Option, -) -> (bool, Span, String) { +) -> Option<(bool, Span, String, Option<(Span, String)>)> { // if there is a RHS and it starts with a `&` from it, then check if it is // mutable, and if not, put suggest putting `mut ` to make it mutable. // we don't have to worry about lifetime annotations here because they are @@ -1479,7 +1478,7 @@ fn suggest_ampmut<'tcx>( // FIXME(Ezrashaw): returning is bad because we still might want to // update the annotated type, see #106857. - return (true, span, "mut ".to_owned()); + return Some((true, span, "mut ".to_owned(), None)); } } @@ -1504,18 +1503,18 @@ fn suggest_ampmut<'tcx>( && let Some(ws_pos) = src.find(char::is_whitespace) { let span = span.with_lo(span.lo() + BytePos(ws_pos as u32)).shrink_to_lo(); - (true, span, " mut".to_owned()) + Some((true, span, " mut".to_owned(), None)) // if there is already a binding, we modify it to be `mut` } else if binding_exists { // shrink the span to just after the `&` in `&variable` let span = span.with_lo(span.lo() + BytePos(1)).shrink_to_lo(); - (true, span, "mut ".to_owned()) + Some((true, span, "mut ".to_owned(), None)) } else { // otherwise, suggest that the user annotates the binding; we provide the // type of the local. let ty = decl_ty.builtin_deref(true).unwrap(); - (false, span, format!("{}mut {}", if decl_ty.is_ref() { "&" } else { "*" }, ty)) + Some((false, span, format!("{}mut {}", if decl_ty.is_ref() { "&" } else { "*" }, ty), None)) } } From d7fa8ee6802a2c1121dca0925d3db143dd1a793c Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Fri, 13 Dec 2024 06:44:52 +0100 Subject: [PATCH 09/11] Add regression test for issue 127562 The test fails in this commit. The next commit fixes it. --- ...-mut-suggestion-for-raw-pointer-issue-127562.rs | 8 ++++++++ ...-suggestion-for-raw-pointer-issue-127562.stderr | 14 ++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.rs create mode 100644 tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr diff --git a/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.rs b/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.rs new file mode 100644 index 0000000000000..5425e571af061 --- /dev/null +++ b/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.rs @@ -0,0 +1,8 @@ +//! Regression test for invalid suggestion for `&raw const expr` reported in +//! . + +fn main() { + let val = 2; + let ptr = &raw const val; + unsafe { *ptr = 3; } //~ ERROR cannot assign to `*ptr`, which is behind a `*const` pointer +} diff --git a/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr b/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr new file mode 100644 index 0000000000000..0da5d15cf7f82 --- /dev/null +++ b/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr @@ -0,0 +1,14 @@ +error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer + --> $DIR/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.rs:7:14 + | +LL | unsafe { *ptr = 3; } + | ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written + | +help: consider changing this to be a mutable pointer + | +LL | let ptr = &mut raw const val; + | +++ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0594`. From 2d2c6f2a80167d9f244d8c926fdf3b3aebf3f42f Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Fri, 13 Dec 2024 07:00:08 +0100 Subject: [PATCH 10/11] rustc_borrowck: Stop suggesting the invalid syntax `&mut raw const` A legitimate suggestion would be to change from &raw const val to &raw mut val But until we have figured out how to make that happen we should at least stop suggesting invalid syntax. --- compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs | 5 +++++ ...nvalid-mut-suggestion-for-raw-pointer-issue-127562.stderr | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 48fc0c331b8bf..044a828b34679 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -1455,6 +1455,11 @@ fn suggest_ampmut<'tcx>( && let Ok(src) = tcx.sess.source_map().span_to_snippet(assignment_rhs_span) && let Some(stripped) = src.strip_prefix('&') { + let is_raw_ref = stripped.trim_start().starts_with("raw "); + // We don't support raw refs yet + if is_raw_ref { + return None; + } let is_mut = if let Some(rest) = stripped.trim_start().strip_prefix("mut") { match rest.chars().next() { // e.g. `&mut x` diff --git a/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr b/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr index 0da5d15cf7f82..c27dcc19827d6 100644 --- a/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr +++ b/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr @@ -3,11 +3,6 @@ error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer | LL | unsafe { *ptr = 3; } | ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written - | -help: consider changing this to be a mutable pointer - | -LL | let ptr = &mut raw const val; - | +++ error: aborting due to 1 previous error From f6cb227f6151abfd44536e613cbf4b800e814a2b Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Fri, 13 Dec 2024 06:51:17 +0100 Subject: [PATCH 11/11] rustc_borrowck: Convert suggest_ampmut() 4-tuple to struct for readability --- .../src/diagnostics/mutability_errors.rs | 59 +++++++++++++++---- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 044a828b34679..4fb7b22f2896e 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -1100,12 +1100,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { } let decl_span = local_decl.source_info.span; - let label = match *local_decl.local_info() { + let amp_mut_sugg = match *local_decl.local_info() { LocalInfo::User(mir::BindingForm::ImplicitSelf(_)) => { let suggestion = suggest_ampmut_self(self.infcx.tcx, decl_span); let additional = local_trait.map(|span| (span, suggest_ampmut_self(self.infcx.tcx, span))); - Some((true, decl_span, suggestion, additional)) + Some(AmpMutSugg { has_sugg: true, span: decl_span, suggestion, additional }) } LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm { @@ -1165,7 +1165,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { .. })) => { let sugg = suggest_ampmut_self(self.infcx.tcx, decl_span); - Some((true, decl_span, sugg, None)) + Some(AmpMutSugg { + has_sugg: true, + span: decl_span, + suggestion: sugg, + additional: None, + }) } // explicit self (eg `self: &'a Self`) _ => suggest_ampmut( @@ -1186,15 +1191,24 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { .. })) => { let pattern_span: Span = local_decl.source_info.span; - suggest_ref_mut(self.infcx.tcx, pattern_span) - .map(|span| (true, span, "mut ".to_owned(), None)) + suggest_ref_mut(self.infcx.tcx, pattern_span).map(|span| AmpMutSugg { + has_sugg: true, + span, + suggestion: "mut ".to_owned(), + additional: None, + }) } _ => unreachable!(), }; - match label { - Some((true, err_help_span, suggested_code, additional)) => { + match amp_mut_sugg { + Some(AmpMutSugg { + has_sugg: true, + span: err_help_span, + suggestion: suggested_code, + additional, + }) => { let mut sugg = vec![(err_help_span, suggested_code)]; if let Some(s) = additional { sugg.push(s); @@ -1216,7 +1230,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { ); } } - Some((false, err_label_span, message, _)) => { + Some(AmpMutSugg { + has_sugg: false, span: err_label_span, suggestion: message, .. + }) => { let def_id = self.body.source.def_id(); let hir_id = if let Some(local_def_id) = def_id.as_local() && let Some(body) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id) @@ -1421,6 +1437,13 @@ fn suggest_ampmut_self<'tcx>(tcx: TyCtxt<'tcx>, span: Span) -> String { } } +struct AmpMutSugg { + has_sugg: bool, + span: Span, + suggestion: String, + additional: Option<(Span, String)>, +} + // When we want to suggest a user change a local variable to be a `&mut`, there // are three potential "obvious" things to highlight: // @@ -1442,7 +1465,7 @@ fn suggest_ampmut<'tcx>( decl_span: Span, opt_assignment_rhs_span: Option, opt_ty_info: Option, -) -> Option<(bool, Span, String, Option<(Span, String)>)> { +) -> Option { // if there is a RHS and it starts with a `&` from it, then check if it is // mutable, and if not, put suggest putting `mut ` to make it mutable. // we don't have to worry about lifetime annotations here because they are @@ -1483,7 +1506,12 @@ fn suggest_ampmut<'tcx>( // FIXME(Ezrashaw): returning is bad because we still might want to // update the annotated type, see #106857. - return Some((true, span, "mut ".to_owned(), None)); + return Some(AmpMutSugg { + has_sugg: true, + span, + suggestion: "mut ".to_owned(), + additional: None, + }); } } @@ -1508,18 +1536,23 @@ fn suggest_ampmut<'tcx>( && let Some(ws_pos) = src.find(char::is_whitespace) { let span = span.with_lo(span.lo() + BytePos(ws_pos as u32)).shrink_to_lo(); - Some((true, span, " mut".to_owned(), None)) + Some(AmpMutSugg { has_sugg: true, span, suggestion: " mut".to_owned(), additional: None }) // if there is already a binding, we modify it to be `mut` } else if binding_exists { // shrink the span to just after the `&` in `&variable` let span = span.with_lo(span.lo() + BytePos(1)).shrink_to_lo(); - Some((true, span, "mut ".to_owned(), None)) + Some(AmpMutSugg { has_sugg: true, span, suggestion: "mut ".to_owned(), additional: None }) } else { // otherwise, suggest that the user annotates the binding; we provide the // type of the local. let ty = decl_ty.builtin_deref(true).unwrap(); - Some((false, span, format!("{}mut {}", if decl_ty.is_ref() { "&" } else { "*" }, ty), None)) + Some(AmpMutSugg { + has_sugg: false, + span, + suggestion: format!("{}mut {}", if decl_ty.is_ref() { "&" } else { "*" }, ty), + additional: None, + }) } }