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

Add a conversion from &mut T to &mut UnsafeCell<T> #107917

Closed
wants to merge 1 commit into from

Conversation

joseph-gio
Copy link
Contributor

@joseph-gio joseph-gio commented Feb 11, 2023

Provides a safe way of downgrading an exclusive reference into an alias-able &UnsafeCell<T> reference.

I would appreciate guidance on what exactly I should put for the stability attribute.

ACP: rust-lang/libs-team#198.

@rustbot
Copy link
Collaborator

rustbot commented Feb 11, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joshtriplett (or someone else) soon.

Please see the contribution instructions for more information.

@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 Feb 11, 2023
@rustbot
Copy link
Collaborator

rustbot commented Feb 11, 2023

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@joseph-gio
Copy link
Contributor Author

@rustbot label +T-libs-api -T-libs

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 11, 2023
@joseph-gio
Copy link
Contributor Author

@rustbot label -T-libs-api +T-libs

@rustbot rustbot added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Feb 11, 2023
@rust-log-analyzer

This comment has been minimized.

@clubby789
Copy link
Contributor

clubby789 commented Feb 11, 2023

This is a libs-api change:

Introducing new or changing existing unstable library APIs

You'll also need to make an ACP and mark this as S-waiting-on-ACP once you've created it

@joseph-gio
Copy link
Contributor Author

I see, thank you.

@rustbot label +T-libs-api +S-waiting-on-ACP -T-libs

@rustbot rustbot added S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 11, 2023
@KamilaBorowska
Copy link
Contributor

KamilaBorowska commented Feb 11, 2023

I'm not sure if that should return &mut UnsafeCell<T> when Cell::from_mut returns &Cell<T>. That said, I'm not seeing obvious safety problems, so it may as well be fine, and &mut UnsafeCell<T> is more useful as it has get_mut method.

It's worth noting that we already guarantee converting from &mut T to &UnsafeCell<T> to be possible, https://doc.rust-lang.org/stable/std/cell/struct.UnsafeCell.html has the following function in the examples:

fn get_shared<T>(ptr: &mut T) -> &UnsafeCell<T> {
  let t = ptr as *mut T as *const UnsafeCell<T>;
  // SAFETY: `T` and `UnsafeCell<T>` have the same memory layout
  unsafe { &*t }
}

@rust-log-analyzer

This comment has been minimized.

@clarfonthey
Copy link
Contributor

I'm not sure if that should return &mut UnsafeCell<T> when Cell::from_mut returns &Cell<T>. That said, I'm not seeing obvious safety problems, so it may as well be fine, and &mut UnsafeCell<T> is more useful as it has get_mut method.

Note that Cell<T> also has a get_mut method, so, I would say that this argument doesn't apply here.

@joseph-gio
Copy link
Contributor Author

I think it was probably a mistake for Cell::from_mut to return &Cell -- &mut Cell is more useful and I'm not aware of any issues introduced by returning it.

@anden3 anden3 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 Apr 5, 2023
@anden3
Copy link
Contributor

anden3 commented Apr 5, 2023

@JoJoJet Just want to ping you as part of the triage procedure to let you know that CI fails for this PR :)

@joseph-gio
Copy link
Contributor Author

@anden3 Yes, thank you. Like I said, I'll need a bit of guidance, as I haven't contributed to this repo before :).

I believe that I need to add a feature flag for this and gate the new function behind it, but I wasn't able to locate the guidelines for this type of thing.

@anden3
Copy link
Contributor

anden3 commented Apr 5, 2023

I believe that I need to add a feature flag for this and gate the new function behind it, but I wasn't able to locate the guidelines for this type of thing.

Would this help?
https://rustc-dev-guide.rust-lang.org/feature-gates.html

@joseph-gio
Copy link
Contributor Author

Yes that helps, thank you.

@rust-log-analyzer

This comment has been minimized.

@Dylan-DPC
Copy link
Member

@JoJoJet this needs an ACP, have you created one already for it? If so can you post it here or preferably add it to the title of the pr? thanks

@joseph-gio
Copy link
Contributor Author

The ACP is at rust-lang/libs-team#198. What do you want me to add to the title?

@Dylan-DPC
Copy link
Member

The ACP link but that's fine i'll do it :P

@joseph-gio
Copy link
Contributor Author

@rustbot label -S-waiting-on-author

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 6, 2023
@joshtriplett
Copy link
Member

Please add a tracking issue for this. Once that's done, r=me (you can write @ bors r = joshtriplett without spaces).

@bors delegate+

@bors
Copy link
Contributor

bors commented May 16, 2023

✌️ @JoJoJet can now approve this pull request

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@@ -533,6 +533,8 @@ declare_features! (
(active, type_changing_struct_update, "1.58.0", Some(86555), None),
/// Enables rustc to generate code that instructs libstd to NOT ignore SIGPIPE.
(active, unix_sigpipe, "1.65.0", Some(97889), None),
/// Allows a safe conversion from `&mut T` to `&mut UnsafeCell<T>`.
(incomplete, unsafe_cell_from_mut, "1.70.0", Some(111645), None),
Copy link
Member

Choose a reason for hiding this comment

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

This isn't necessary for library features, the feature name will picked up from the #[unstable] attribute directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, thank you.

@joseph-gio
Copy link
Contributor Author

@bors r=joshtriplett

@bors
Copy link
Contributor

bors commented May 16, 2023

📌 Commit 810a87bb92347101cf6230d944993a19983544cb has been approved by joshtriplett

It is now in the queue for this repository.

@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label May 16, 2023
@clubby789 clubby789 removed the S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. label May 16, 2023
@klensy
Copy link
Contributor

klensy commented May 16, 2023

0de00d1 https://rustc-dev-guide.rust-lang.org/git.html#no-merge-policy. 10 commits for 22 lines == squash commits.

@Noratrieb
Copy link
Member

Noratrieb commented May 16, 2023

Yeah, we have a no merge policy and also there are indeed a lot of commits. Could you squash them together? If you need help, the linked page above should contain instructions.
@bors r-
nice catch @klensy

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels May 16, 2023
@joseph-gio joseph-gio force-pushed the unsafe-cell-from-mut branch from 810a87b to 767b6b2 Compare May 16, 2023 19:20
@rustbot
Copy link
Collaborator

rustbot commented May 16, 2023

Some changes occurred in src/tools/cargo

cc @ehuss

@joseph-gio joseph-gio closed this May 16, 2023
@rust-log-analyzer
Copy link
Collaborator

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
   Compiling clap v4.2.4
   Compiling clap_complete v4.2.2
    Finished dev [unoptimized] target(s) in 17.08s
##[endgroup]
error: current package believes it's in a workspace when it's not:
workspace: /checkout/Cargo.toml


this may be fixable by adding `src/tools/cargo` to the `workspace.members` array of the manifest located at: /checkout/Cargo.toml
Alternatively, to keep it out of the workspace, add the package to the `workspace.exclude` array, or add an empty `[workspace]` table to the package's manifest.
thread 'main' panicked at 'command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "metadata" "--format-version" "1" "--no-deps" "--manifest-path" "/checkout/src/tools/cargo/Cargo.toml"
expected success, got: exit status: 101', metadata.rs:83:31
Build completed unsuccessfully in 0:00:28
make: *** [Makefile:58: prepare] Error 1
Command failed. Attempt 2/5:
##[group]Building bootstrap
##[group]Building bootstrap
    Finished dev [unoptimized] target(s) in 0.05s
##[endgroup]
error: current package believes it's in a workspace when it's not:
workspace: /checkout/Cargo.toml


this may be fixable by adding `src/tools/cargo` to the `workspace.members` array of the manifest located at: /checkout/Cargo.toml
Alternatively, to keep it out of the workspace, add the package to the `workspace.exclude` array, or add an empty `[workspace]` table to the package's manifest.
thread 'main' panicked at 'command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "metadata" "--format-version" "1" "--no-deps" "--manifest-path" "/checkout/src/tools/cargo/Cargo.toml"
expected success, got: exit status: 101', metadata.rs:83:31
Build completed unsuccessfully in 0:00:00
make: *** [Makefile:58: prepare] Error 1
Command failed. Attempt 3/5:
##[group]Building bootstrap
##[group]Building bootstrap
    Finished dev [unoptimized] target(s) in 0.04s
##[endgroup]
error: current package believes it's in a workspace when it's not:
workspace: /checkout/Cargo.toml


this may be fixable by adding `src/tools/cargo` to the `workspace.members` array of the manifest located at: /checkout/Cargo.toml
Alternatively, to keep it out of the workspace, add the package to the `workspace.exclude` array, or add an empty `[workspace]` table to the package's manifest.
thread 'main' panicked at 'command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "metadata" "--format-version" "1" "--no-deps" "--manifest-path" "/checkout/src/tools/cargo/Cargo.toml"
expected success, got: exit status: 101', metadata.rs:83:31
Build completed unsuccessfully in 0:00:00
make: *** [Makefile:58: prepare] Error 1
Command failed. Attempt 4/5:
##[group]Building bootstrap
##[group]Building bootstrap
    Finished dev [unoptimized] target(s) in 0.04s
##[endgroup]
error: current package believes it's in a workspace when it's not:
workspace: /checkout/Cargo.toml


this may be fixable by adding `src/tools/cargo` to the `workspace.members` array of the manifest located at: /checkout/Cargo.toml
Alternatively, to keep it out of the workspace, add the package to the `workspace.exclude` array, or add an empty `[workspace]` table to the package's manifest.
thread 'main' panicked at 'command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "metadata" "--format-version" "1" "--no-deps" "--manifest-path" "/checkout/src/tools/cargo/Cargo.toml"
expected success, got: exit status: 101', metadata.rs:83:31
Build completed unsuccessfully in 0:00:00
make: *** [Makefile:58: prepare] Error 1
Command failed. Attempt 5/5:
##[group]Building bootstrap
##[group]Building bootstrap
    Finished dev [unoptimized] target(s) in 0.04s
##[endgroup]
error: current package believes it's in a workspace when it's not:
workspace: /checkout/Cargo.toml


this may be fixable by adding `src/tools/cargo` to the `workspace.members` array of the manifest located at: /checkout/Cargo.toml
Alternatively, to keep it out of the workspace, add the package to the `workspace.exclude` array, or add an empty `[workspace]` table to the package's manifest.
thread 'main' panicked at 'command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "metadata" "--format-version" "1" "--no-deps" "--manifest-path" "/checkout/src/tools/cargo/Cargo.toml"
expected success, got: exit status: 101', metadata.rs:83:31
Build completed unsuccessfully in 0:00:00
make: *** [Makefile:58: prepare] Error 1
The command has failed after 5 attempts.

@joseph-gio joseph-gio deleted the unsafe-cell-from-mut branch May 16, 2023 19:27
@Dylan-DPC Dylan-DPC removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.