Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of sync_nonpoison and nonpoison_mutex #134663

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Aandreba
Copy link

Implementation of sync_nonpoison and nonpoison_mutex
Docs come straight from the poisoned Mutex, so they need to be updated.

Tracked by:

@rustbot
Copy link
Collaborator

rustbot commented Dec 22, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @thomcc (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 22, 2024
@rust-log-analyzer

This comment has been minimized.

/// # Examples
///
/// ```
/// use std::sync::Mutex;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These doctests need to be updated, they are using the poisioning mutex

use crate::ptr::NonNull;
use crate::sys::sync as sys;

/// A mutual exclusion primitive useful for protecting shared data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs for this one could probably just have a single example and then suggest looking at std::sync::poison::Mutex for more, so that we don't have the detailed documentation repeated in multiple locations. Also make sure the doctests are using the right type here.

@tgross35
Copy link
Contributor

@Amanieu you may want to double check this

@rustbot rustbot added A-compiletest Area: The compiletest test runner A-meta Area: Issues & PRs about the rust-lang/rust repository itself A-run-make Area: port run-make Makefiles to rmake.rs A-testsuite Area: The testsuite used to check the correctness of rustc PG-exploit-mitigations Project group: Exploit mitigations T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Dec 24, 2024
@rustbot

This comment was marked as resolved.

@Noratrieb
Copy link
Member

Looks like you messed up a rebase. No worries, mistakes like this happen sadly (thanks Git for being so hard to use!). To get rid of all these commits, do a git rebase -i upstream/master (where upstream is the rust-lang/rust remote) as described in https://rustc-dev-guide.rust-lang.org/git.html#i-made-a-merge-commit-by-accident.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#22 exporting to docker image format
#22 sending tarball 30.6s done
#22 DONE 35.6s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-18]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-18', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-18/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
---- [ui] tests/ui/async-await/issue-64130-non-send-future-diags.rs stdout ----
Saved the actual stderr to "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/async-await/issue-64130-non-send-future-diags/issue-64130-non-send-future-diags.stderr"
diff of stderr:

4 LL |     is_send(foo());
5    |             ^^^^^ future returned by `foo` is not `Send`
-    = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `MutexGuard<'_, u32>`
+    = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `std::sync::MutexGuard<'_, u32>`
8 note: future is not `Send` as this value is used across an await
9   --> $DIR/issue-64130-non-send-future-diags.rs:17:11
9   --> $DIR/issue-64130-non-send-future-diags.rs:17:11
10    |

11 LL |     let g = x.lock().unwrap();
-    |         - has type `MutexGuard<'_, u32>` which is not `Send`
+    |         - has type `std::sync::MutexGuard<'_, u32>` which is not `Send`
13 LL |     baz().await;
14    |           ^^^^^ await occurs here, with `g` maybe used later


The actual stderr differed from the expected stderr.
To update references, rerun the tests and pass the `--bless` flag
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args async-await/issue-64130-non-send-future-diags.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/async-await/issue-64130-non-send-future-diags.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/async-await/issue-64130-non-send-future-diags" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2018"
--- stderr -------------------------------
error: future cannot be sent between threads safely
##[error]  --> /checkout/tests/ui/async-await/issue-64130-non-send-future-diags.rs:23:13
   |
   |
LL |     is_send(foo());
   |             ^^^^^ future returned by `foo` is not `Send`
   = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `std::sync::MutexGuard<'_, u32>`
note: future is not `Send` as this value is used across an await
  --> /checkout/tests/ui/async-await/issue-64130-non-send-future-diags.rs:17:11
   |
   |
LL |     let g = x.lock().unwrap();
   |         - has type `std::sync::MutexGuard<'_, u32>` which is not `Send`
LL |     baz().await;
   |           ^^^^^ await occurs here, with `g` maybe used later
  --> /checkout/tests/ui/async-await/issue-64130-non-send-future-diags.rs:9:15
   |
   |
LL | fn is_send<T: Send>(t: T) { }

error: aborting due to 1 previous error
------------------------------------------



---- [ui] tests/ui/async-await/issue-71137.rs stdout ----
Saved the actual stderr to "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/async-await/issue-71137/issue-71137.stderr"
diff of stderr:

4 LL |   fake_spawn(wrong_mutex());
5    |              ^^^^^^^^^^^^^ future returned by `wrong_mutex` is not `Send`
-    = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `MutexGuard<'_, i32>`
+    = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `std::sync::MutexGuard<'_, i32>`
8 note: future is not `Send` as this value is used across an await
9   --> $DIR/issue-71137.rs:14:26
9   --> $DIR/issue-71137.rs:14:26
10    |

11 LL |     let mut guard = m.lock().unwrap();
-    |         --------- has type `MutexGuard<'_, i32>` which is not `Send`
+    |         --------- has type `std::sync::MutexGuard<'_, i32>` which is not `Send`
13 LL |     (async { "right"; }).await;
14    |                          ^^^^^ await occurs here, with `mut guard` maybe used later
15 note: required by a bound in `fake_spawn`

The actual stderr differed from the expected stderr.
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args async-await/issue-71137.rs`
To only update this specific test, also pass `--test-args async-await/issue-71137.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/async-await/issue-71137.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/async-await/issue-71137" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2018"
--- stderr -------------------------------
error: future cannot be sent between threads safely
##[error]  --> /checkout/tests/ui/async-await/issue-71137.rs:22:14
   |
   |
LL |   fake_spawn(wrong_mutex()); //~ Error future cannot be sent between threads safely
   |              ^^^^^^^^^^^^^ future returned by `wrong_mutex` is not `Send`
   = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `std::sync::MutexGuard<'_, i32>`
note: future is not `Send` as this value is used across an await
  --> /checkout/tests/ui/async-await/issue-71137.rs:14:26
   |
   |
LL |     let mut guard = m.lock().unwrap();
   |         --------- has type `std::sync::MutexGuard<'_, i32>` which is not `Send`
LL |     (async { "right"; }).await;
   |                          ^^^^^ await occurs here, with `mut guard` maybe used later
note: required by a bound in `fake_spawn`
   |
   |
LL | fn fake_spawn<F: Future + Send + 'static>(f: F) { }
   |                           ^^^^ required by this bound in `fake_spawn`
error: aborting due to 1 previous error
------------------------------------------


---
- error[E0277]: `MutexGuard<'_, ()>` cannot be sent between threads safely
+ error[E0277]: `std::sync::MutexGuard<'_, ()>` cannot be sent between threads safely
2   --> $DIR/issue-67893.rs:9:7
3    |
4 LL |     g(issue_67893::run())

-    |     - ^^^^^^^^^^^^^^^^^^ `MutexGuard<'_, ()>` cannot be sent between threads safely
+    |     - ^^^^^^^^^^^^^^^^^^ `std::sync::MutexGuard<'_, ()>` cannot be sent between threads safely
7    |     required by a bound introduced by this call
8    |

11 LL | pub async fn run() {
---
To only update this specific test, also pass `--test-args async-await/issues/issue-67893.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/async-await/issues/issue-67893.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/async-await/issues/issue-67893" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/async-await/issues/issue-67893/auxiliary" "--edition=2018"
--- stderr -------------------------------
error[E0277]: `std::sync::MutexGuard<'_, ()>` cannot be sent between threads safely
##[error]  --> /checkout/tests/ui/async-await/issues/issue-67893.rs:9:7
   |
   |
LL |     g(issue_67893::run())
   |     - ^^^^^^^^^^^^^^^^^^ `std::sync::MutexGuard<'_, ()>` cannot be sent between threads safely
   |     required by a bound introduced by this call
   |
  ::: /checkout/tests/ui/async-await/issues/auxiliary/issue_67893.rs:9:1
   |
---
  --> /checkout/tests/ui/async-await/issues/auxiliary/issue_67893.rs:9:20
   |
LL |   pub async fn run() {
   |  ____________________^
LL | |     let x: Arc<Mutex<()>> = make_arc();
LL | |     f(*x.lock().unwrap()).await;
   | |_^
note: required by a bound in `g`
  --> /checkout/tests/ui/async-await/issues/issue-67893.rs:6:14
   |
   |
LL | fn g(_: impl Send) {}
   |              ^^^^ required by this bound in `g`
error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
------------------------------------------
---
To only update this specific test, also pass `--test-args issues/issue-54062.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/issues/issue-54062.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/issues/issue-54062" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers"
--- stderr -------------------------------
error[E0616]: field `inner` of struct `std::sync::Mutex` is private
##[error]  --> /checkout/tests/ui/issues/issue-54062.rs:10:24
   |
---
---- [ui] tests/ui/lint/must_not_suspend/mutex.rs stdout ----
Saved the actual stderr to "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/lint/must_not_suspend/mutex/mutex.stderr"
diff of stderr:

- error: `MutexGuard` held across a suspend point, but should not be
+ error: `std::sync::MutexGuard` held across a suspend point, but should not be
3    |
3    |
4 LL |     let _guard = m.lock().unwrap();

The actual stderr differed from the expected stderr.
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args lint/must_not_suspend/mutex.rs`
To only update this specific test, also pass `--test-args lint/must_not_suspend/mutex.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/lint/must_not_suspend/mutex.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/lint/must_not_suspend/mutex" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2018"
--- stderr -------------------------------
error: `std::sync::MutexGuard` held across a suspend point, but should not be
##[error]  --> /checkout/tests/ui/lint/must_not_suspend/mutex.rs:8:9
   |
   |
LL |     let _guard = m.lock().unwrap(); //~ ERROR `MutexGuard` held across
LL |     other().await;
   |             ----- the value is held across this suspend point
   |
   |
note: holding a MutexGuard across suspend points can cause deadlocks, delays, and cause Futures to not implement `Send`
   |
   |
LL |     let _guard = m.lock().unwrap(); //~ ERROR `MutexGuard` held across
   |         ^^^^^^
help: consider using a block (`{ ... }`) to shrink the value's scope, ending before the suspend point
   |
   |
LL |     let _guard = m.lock().unwrap(); //~ ERROR `MutexGuard` held across
note: the lint level is defined here
  --> /checkout/tests/ui/lint/must_not_suspend/mutex.rs:3:9
   |
   |
LL | #![deny(must_not_suspend)]

error: aborting due to 1 previous error
------------------------------------------



---- [ui] tests/ui/suggestions/inner_type.rs stdout ----
Saved the actual stderr to "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/suggestions/inner_type/inner_type.stderr"
diff of stderr:

30 LL |     other_item.borrow_mut().some_mutable_method();
32 
- error[E0599]: no method named `method` found for struct `Mutex` in the current scope
+ error[E0599]: no method named `method` found for struct `std::sync::Mutex` in the current scope
34   --> $DIR/inner_type.rs:27:18
---
To only update this specific test, also pass `--test-args suggestions/inner_type.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/suggestions/inner_type.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/suggestions/inner_type" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2021"
--- stderr -------------------------------
error[E0599]: no method named `method` found for struct `RefCell` in the current scope
##[error]  --> /checkout/tests/ui/suggestions/inner_type.rs:17:16
   |
   |
LL |     other_item.method();
   |                ^^^^^^ method not found in `RefCell<Struct<u32>>`
   |
note: the method `method` exists on the type `Struct<u32>`
   |
LL |     pub fn method(&self) {}
   |     ^^^^^^^^^^^^^^^^^^^^
   |     ^^^^^^^^^^^^^^^^^^^^
help: use `.borrow()` to borrow the `Struct<u32>`, panicking if a mutable borrow exists
   |
LL |     other_item.borrow().method();

error[E0599]: no method named `some_mutable_method` found for struct `RefCell` in the current scope
##[error]  --> /checkout/tests/ui/suggestions/inner_type.rs:21:16
   |
   |
LL |     other_item.some_mutable_method();
   |                ^^^^^^^^^^^^^^^^^^^ method not found in `RefCell<Struct<u32>>`
   |
note: the method `some_mutable_method` exists on the type `Struct<u32>`
   |
LL |     pub fn some_mutable_method(&mut self) {}
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any borrows exist
   |
LL |     other_item.borrow_mut().some_mutable_method();

error[E0599]: no method named `method` found for struct `std::sync::Mutex` in the current scope
##[error]  --> /checkout/tests/ui/suggestions/inner_type.rs:27:18
   |
   |
LL |     another_item.method();
   |                  ^^^^^^ method not found in `Mutex<Struct<u32>>`
   |
note: the method `method` exists on the type `Struct<u32>`
   |
LL |     pub fn method(&self) {}
   |     ^^^^^^^^^^^^^^^^^^^^
   |     ^^^^^^^^^^^^^^^^^^^^
help: use `.lock().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
   |
LL |     another_item.lock().unwrap().method();


error[E0599]: no method named `method` found for struct `RwLock` in the current scope
   |
LL |     another_item.method();
LL |     another_item.method();
   |                  ^^^^^^ method not found in `RwLock<Struct<u32>>`
   |
note: the method `method` exists on the type `Struct<u32>`
   |
LL |     pub fn method(&self) {}
   |     ^^^^^^^^^^^^^^^^^^^^
   |     ^^^^^^^^^^^^^^^^^^^^
help: use `.read().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
   |
LL |     another_item.read().unwrap().method();

error[E0599]: no method named `some_mutable_method` found for struct `RwLock` in the current scope
##[error]  --> /checkout/tests/ui/suggestions/inner_type.rs:37:18
   |
   |
LL |     another_item.some_mutable_method();
   |                  ^^^^^^^^^^^^^^^^^^^ method not found in `RwLock<Struct<u32>>`
   |
note: the method `some_mutable_method` exists on the type `Struct<u32>`
   |
LL |     pub fn some_mutable_method(&mut self) {}
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.write().unwrap()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired
   |
LL |     another_item.write().unwrap().some_mutable_method();

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0599`.
---
Saved the actual stderr to "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/sync/mutexguard-sync/mutexguard-sync.stderr"
diff of stderr:

8    |
9    = help: the trait `Sync` is not implemented for `Cell<i32>`
10    = note: if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicI32` instead
-    = note: required for `MutexGuard<'_, Cell<i32>>` to implement `Sync`
+    = note: required for `std::sync::MutexGuard<'_, Cell<i32>>` to implement `Sync`
13   --> $DIR/mutexguard-sync.rs:5:17
14    |



The actual stderr differed from the expected stderr.
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args sync/mutexguard-sync.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/sync/mutexguard-sync.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/sync/mutexguard-sync" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers"
--- stderr -------------------------------
--- stderr -------------------------------
error[E0277]: `Cell<i32>` cannot be shared between threads safely
   |
LL |     test_sync(guard);
LL |     test_sync(guard);
   |     --------- ^^^^^ `Cell<i32>` cannot be shared between threads safely
   |     required by a bound introduced by this call
   |
   = help: the trait `Sync` is not implemented for `Cell<i32>`
   = help: the trait `Sync` is not implemented for `Cell<i32>`
   = note: if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` or `std::sync::atomic::AtomicI32` instead
   = note: required for `std::sync::MutexGuard<'_, Cell<i32>>` to implement `Sync`
  --> /checkout/tests/ui/sync/mutexguard-sync.rs:5:17
   |
   |
LL | fn test_sync<T: Sync>(_t: T) {}

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
---
Saved the actual stderr to "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/traits/const-traits/span-bug-issue-121418/span-bug-issue-121418.stderr"
diff of stderr:

16    |                  |
17    |                  implicitly returns `()` as its body has no tail or `return` expression
18    |
-    = note: expected struct `Mutex<(dyn T + 'static)>`
+    = note: expected struct `std::sync::Mutex<(dyn T + 'static)>`
21 
21 
22 error[E0277]: the size for values of type `(dyn T + 'static)` cannot be known at compilation time
25 LL |     pub const fn new() -> std::sync::Mutex<dyn T> {}
26    |                           ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
27    |
27    |
-    = help: within `Mutex<(dyn T + 'static)>`, the trait `Sized` is not implemented for `(dyn T + 'static)`
- note: required because it appears within the type `Mutex<(dyn T + 'static)>`
+    = help: within `std::sync::Mutex<(dyn T + 'static)>`, the trait `Sized` is not implemented for `(dyn T + 'static)`
+ note: required because it appears within the type `std::sync::Mutex<(dyn T + 'static)>`
30   --> $SRC_DIR/std/src/sync/mutex.rs:LL:COL
32 


The actual stderr differed from the expected stderr.
The actual stderr differed from the expected stderr.
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args traits/const-traits/span-bug-issue-121418.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/traits/const-traits/span-bug-issue-121418.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/traits/const-traits/span-bug-issue-121418" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers"
--- stderr -------------------------------
error: inherent impls cannot be `const`
##[error]  --> /checkout/tests/ui/traits/const-traits/span-bug-issue-121418.rs:6:12
   |
---

error[E0308]: mismatched types
##[error]  --> /checkout/tests/ui/traits/const-traits/span-bug-issue-121418.rs:8:27
   |
LL |     pub const fn new() -> std::sync::Mutex<dyn T> {}
   |                  ---      ^^^^^^^^^^^^^^^^^^^^^^^ expected `Mutex<dyn T>`, found `()`
   |                  |
   |                  implicitly returns `()` as its body has no tail or `return` expression
   |
   = note: expected struct `std::sync::Mutex<(dyn T + 'static)>`


error[E0277]: the size for values of type `(dyn T + 'static)` cannot be known at compilation time
   |
LL |     pub const fn new() -> std::sync::Mutex<dyn T> {}
   |                           ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   |
   = help: within `std::sync::Mutex<(dyn T + 'static)>`, the trait `Sized` is not implemented for `(dyn T + 'static)`
note: required because it appears within the type `std::sync::Mutex<(dyn T + 'static)>`
   = note: the return type of a function must have a statically known size

error: aborting due to 3 previous errors

---
---- [ui] tests/ui/typeck/assign-non-lval-derefmut.rs stdout ----
Saved the actual stderr to "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/typeck/assign-non-lval-derefmut/assign-non-lval-derefmut.stderr"
diff of stderr:

11 LL |     *x.lock().unwrap() = 2;
13 
13 
- error[E0368]: binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>`
+ error[E0368]: binary assignment operation `+=` cannot be applied to type `std::sync::MutexGuard<'_, usize>`
16    |
16    |
17 LL |     x.lock().unwrap() += 1;
18    |     -----------------^^^^^
19    |     |
19    |     |
-    |     cannot use `+=` on type `MutexGuard<'_, usize>`
+    |     cannot use `+=` on type `std::sync::MutexGuard<'_, usize>`
21    |
- note: the foreign item type `MutexGuard<'_, usize>` doesn't implement `AddAssign<{integer}>`
+ note: the foreign item type `std::sync::MutexGuard<'_, usize>` doesn't implement `AddAssign<{integer}>`
23   --> $SRC_DIR/std/src/sync/mutex.rs:LL:COL
24    |
25    = note: not implement `AddAssign<{integer}>`
36 LL |     y = 2;
37    |         ^ expected `MutexGuard<'_, usize>`, found integer
38    |
-    = note: expected struct `MutexGuard<'_, usize>`
-    = note: expected struct `MutexGuard<'_, usize>`
+    = note: expected struct `std::sync::MutexGuard<'_, usize>`
40                 found type `{integer}`
41 help: consider dereferencing here to assign to the mutably borrowed value
42    |

43 LL |     *y = 2;
45 
45 
- error[E0368]: binary assignment operation `+=` cannot be applied to type `MutexGuard<'_, usize>`
+ error[E0368]: binary assignment operation `+=` cannot be applied to type `std::sync::MutexGuard<'_, usize>`
48    |
49 LL |     y += 1;

50    |     -^^^^^
50    |     -^^^^^
51    |     |
-    |     cannot use `+=` on type `MutexGuard<'_, usize>`
+    |     cannot use `+=` on type `std::sync::MutexGuard<'_, usize>`
53    |
- note: the foreign item type `MutexGuard<'_, usize>` doesn't implement `AddAssign<{integer}>`
+ note: the foreign item type `std::sync::MutexGuard<'_, usize>` doesn't implement `AddAssign<{integer}>`
55   --> $SRC_DIR/std/src/sync/mutex.rs:LL:COL
56    |
57    = note: not implement `AddAssign<{integer}>`

The actual stderr differed from the expected stderr.
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args typeck/assign-non-lval-derefmut.rs`
To only update this specific test, also pass `--test-args typeck/assign-non-lval-derefmut.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/typeck/assign-non-lval-derefmut.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/typeck/assign-non-lval-derefmut" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers"
--- stderr -------------------------------
error[E0070]: invalid left-hand side of assignment
##[error]  --> /checkout/tests/ui/typeck/assign-non-lval-derefmut.rs:5:23
   |
   |
LL |     x.lock().unwrap() = 2;
   |     ----------------- ^
   |     |
   |     cannot assign to this expression
   |
help: consider dereferencing here to assign to the mutably borrowed value
   |
LL |     *x.lock().unwrap() = 2;


error[E0368]: binary assignment operation `+=` cannot be applied to type `std::sync::MutexGuard<'_, usize>`
   |
   |
LL |     x.lock().unwrap() += 1;
   |     |
   |     |
   |     cannot use `+=` on type `std::sync::MutexGuard<'_, usize>`
   |
note: the foreign item type `std::sync::MutexGuard<'_, usize>` doesn't implement `AddAssign<{integer}>`
   |
   |
   = note: not implement `AddAssign<{integer}>`
help: `+=` can be used on `usize` if you dereference the left-hand side
   |
LL |     *x.lock().unwrap() += 1;

error[E0308]: mismatched types
##[error]  --> /checkout/tests/ui/typeck/assign-non-lval-derefmut.rs:11:9
   |
---
   = note: expected struct `std::sync::MutexGuard<'_, usize>`
                found type `{integer}`
help: consider dereferencing here to assign to the mutably borrowed value
   |
LL |     *y = 2;


error[E0368]: binary assignment operation `+=` cannot be applied to type `std::sync::MutexGuard<'_, usize>`
   |
LL |     y += 1;
   |     -^^^^^
   |     |
   |     |
   |     cannot use `+=` on type `std::sync::MutexGuard<'_, usize>`
   |
note: the foreign item type `std::sync::MutexGuard<'_, usize>` doesn't implement `AddAssign<{integer}>`
   |
   |
   = note: not implement `AddAssign<{integer}>`
help: `+=` can be used on `usize` if you dereference the left-hand side
   |
LL |     *y += 1;

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0070, E0308, E0368.
---
-             found struct `MutexGuard<'_, &i32>`
+             found struct `std::sync::MutexGuard<'_, &i32>`
65 help: consider dereferencing the type
66    |
67 LL |     **x.lock().unwrap()

The actual stderr differed from the expected stderr.
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args typeck/deref-multi.rs`
To only update this specific test, also pass `--test-args typeck/deref-multi.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/typeck/deref-multi.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/typeck/deref-multi" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers"
--- stderr -------------------------------
error[E0308]: mismatched types
##[error]  --> /checkout/tests/ui/typeck/deref-multi.rs:2:5
   |
   |
LL | fn a(x: &&i32) -> i32 {
   |                   --- expected `i32` because of return type
   |     ^ expected `i32`, found `&&i32`
   |
help: consider dereferencing the borrow
   |
   |
LL |     **x
   |     ++

error[E0308]: mismatched types
##[error]  --> /checkout/tests/ui/typeck/deref-multi.rs:7:5
   |
LL | fn a2(x: &&&&&i32) -> i32 {
   |                       --- expected `i32` because of return type
LL |     x
   |     ^ expected `i32`, found `&&&&&i32`
help: consider dereferencing the borrow
   |
LL |     *****x
   |     +++++
   |     +++++

error[E0308]: mismatched types
##[error]  --> /checkout/tests/ui/typeck/deref-multi.rs:12:5
   |
LL | fn b(x: &i32) -> i32 {
   |                  --- expected `i32` because of return type
LL |     &x
   |     ^^ expected `i32`, found `&&i32`
help: consider removing the `&` and dereferencing the borrow instead
   |
LL |     *x
   |     ~
   |     ~

error[E0308]: mismatched types
##[error]  --> /checkout/tests/ui/typeck/deref-multi.rs:17:5
   |
LL | fn c(x: Box<i32>) -> i32 {
   |                      --- expected `i32` because of return type
LL |     &x
   |     ^^ expected `i32`, found `&Box<i32>`
   = note:   expected type `i32`
           found reference `&Box<i32>`
help: consider removing the `&` and dereferencing the borrow instead
   |
   |
LL |     *x
   |     ~

error[E0308]: mismatched types
##[error]  --> /checkout/tests/ui/typeck/deref-multi.rs:22:5
   |
LL | fn d(x: std::sync::Mutex<&i32>) -> i32 {
   |                                    --- expected `i32` because of return type
LL |     x.lock().unwrap()
   |     ^^^^^^^^^^^^^^^^^ expected `i32`, found `MutexGuard<'_, &i32>`
   = note: expected type `i32`
            found struct `std::sync::MutexGuard<'_, &i32>`
help: consider dereferencing the type
   |
   |
LL |     **x.lock().unwrap()

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0308`.

@Amanieu
Copy link
Member

Amanieu commented Dec 24, 2024

This seems fine as a first step. Longer term we may want to consider changing the poisoning mutexes to be a wrapper around a non-poisoning Mutex<Poison<T>>, but that can happen in a later PR.

@jieyouxu jieyouxu removed A-testsuite Area: The testsuite used to check the correctness of rustc T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-meta Area: Issues & PRs about the rust-lang/rust repository itself labels Dec 24, 2024
@jieyouxu jieyouxu removed PG-exploit-mitigations Project group: Exploit mitigations A-compiletest Area: The compiletest test runner A-run-make Area: port run-make Makefiles to rmake.rs T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Dec 24, 2024

#[unstable(feature = "nonpoison_mutex", issue = "134645")]
impl<T: ?Sized> !Send for MutexGuard<'_, T> {}
#[stable(feature = "mutexguard", since = "1.19.0")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be unstable under nonpoison_mutex as well

///
/// rx.recv().unwrap();
/// ```
///
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add an example to show the nonpoisoning behavior? Possibly:

use std::thread;
use std::sync::{Arc, nonpoison::Mutex};

let mutex = Arc::new(Mutex::new(0u32));
let mut handles = Vec::new();

for n in 0..10 {
    let m = Arc::clone(&mutex);
    let handle = thread::spawn(move || {
        let mut guard = m.lock();
        *guard += 1;
        panic!("panic from thread {n} {guard}")
    });
    handles.push(handle);
}

for h in handles {
    h.join();
}

println!("Finished, locked {} times", mutex.lock());

It would be good to have something similar in tests.rs.

/// The exact behavior on locking a mutex in the thread which already holds
/// the lock is left unspecified. However, this function will not return on
/// the second call (it might panic or deadlock, for example).
///
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add something like "If this thread panics while the lock is held, the lock will be released like normal."

Comment on lines +330 to +338
#[stable(feature = "std_debug", since = "1.16.0")]
impl<T: ?Sized + fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}

#[stable(feature = "std_guard_impls", since = "1.20.0")]
impl<T: ?Sized + fmt::Display> fmt::Display for MutexGuard<'_, T> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be unstable

@tgross35
Copy link
Contributor

tgross35 commented Dec 27, 2024

For the errors like

  -	   = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `MutexGuard<'_, u32>`
  +	   = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `std::sync::MutexGuard<'_, u32>`

You will need to run ./x test --bless.

r? tgross35

@rustbot author

@rustbot rustbot assigned tgross35 and unassigned thomcc Dec 27, 2024
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants