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

added core::mem::reshape #130849

Closed
wants to merge 4 commits into from
Closed

Conversation

hazelwiss
Copy link

tracking issue: #130848

@rustbot
Copy link
Collaborator

rustbot commented Sep 25, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @workingjubilee (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 Sep 25, 2024
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@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)
#16 2.838 Building wheels for collected packages: reuse
#16 2.839   Building wheel for reuse (pyproject.toml): started
#16 3.090   Building wheel for reuse (pyproject.toml): finished with status 'done'
#16 3.091   Created wheel for reuse: filename=reuse-4.0.3-cp310-cp310-manylinux_2_35_x86_64.whl size=132715 sha256=dfa09868353292d98f811d3efdb0d54d07389e808efc71d68e3b93c514bf8bec
#16 3.091   Stored in directory: /tmp/pip-ephem-wheel-cache-fnbbmtkz/wheels/3d/8d/0a/e0fc6aba4494b28a967ab5eaf951c121d9c677958714e34532
#16 3.094 Installing collected packages: boolean-py, binaryornot, tomlkit, reuse, python-debian, markupsafe, license-expression, jinja2, chardet, attrs
#16 3.485 Successfully installed attrs-23.2.0 binaryornot-0.4.4 boolean-py-4.0 chardet-5.2.0 jinja2-3.1.4 license-expression-30.3.0 markupsafe-2.1.5 python-debian-0.1.49 reuse-4.0.3 tomlkit-0.13.0
#16 3.486 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#16 DONE 3.6s

Comment on lines +904 to +905
let result = ptr::read(dest);
ptr::write(dest, f(result));
Copy link
Member

Choose a reason for hiding this comment

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

this is not sound if f unwinds, as the value will be dropped twice. see https://docs.rs/replace_with/latest/replace_with/ for details. the design space here is nontrivial, as there are many possible behaviors to do on unwind. this should at least go through an ACP, but probably better for more general discussion on internals.rust-lang.org. i'd also suggesting searching there for previous discussion about "replace_with", which is what this function is generally called

Copy link
Author

Choose a reason for hiding this comment

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

yeah I just detected that myself and ran into a wall. Sounds good 👍

Copy link
Author

Choose a reason for hiding this comment

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

has there been any discussion about a feature to ensure a closure does not panic?

Copy link
Member

Choose a reason for hiding this comment

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

hm. this has probably come up in vague ways in many places but nothing concrete.

Copy link
Member

Choose a reason for hiding this comment

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

either way it's definitely both that will arrive any time soon, so it probably makes sense to close the PR and tracking issue for now

Copy link
Member

Choose a reason for hiding this comment

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

thank you for it anyways :3

Copy link
Author

Choose a reason for hiding this comment

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

👍

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 execute the closure inside an extern "C" wrapper func that would abort on unwind? I think I recently saw a PR on a helper that exposed abort on unwind (mirroring the catch_unwind function) using that technique

Copy link
Member

Choose a reason for hiding this comment

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

You're thinking of #130339

@workingjubilee workingjubilee added S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 26, 2024
///
/// let mut v: Vec<i32> = vec![1, 2];
///
/// mem::replace(&mut v, |mut v| { v.push(3); v });
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// mem::replace(&mut v, |mut v| { v.push(3); v });
/// mem::reshape(&mut v, |mut v| { v.push(3); v });

Should this be reshape?

/// ```
#[inline]
#[unstable(feature = "mem_reshape", issue = "130848")]
pub fn reshape<T>(dest: &mut T, f: impl FnOnce(T) -> T) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is a good name. When I first came across the tracking issue in the issue list, I though this was something transmute related.

@Dylan-DPC
Copy link
Member

@hazelwiss do you still want to go ahead with this? this needs an ACP which hasn't been return and i think above some folks aren't happy with the naming and other things

@hazelwiss
Copy link
Author

@Dylan-DPC Yes! I was under the impression people wanted to close this PR in favour of something else, but I would gladly go ahead with this. I can fix both the naming and look into making an ACP for this.

@workingjubilee
Copy link
Member

This needs a better motivation that is not easily replaced by the mutable reference.

@workingjubilee
Copy link
Member

It also probably needs to be named something similar to replace_with, because that's what this is.

@clarfonthey
Copy link
Contributor

clarfonthey commented Nov 29, 2024

I was under the impression that there were concerns that this kind of function cannot be sound, and that's why it isn't offered, but I could be wrong. The main problem is that you have to ensure that you can never observe the value while it's been moved out of the reference, which requires ensuring that the closure either cannot panic or cannot return after panicking, i.e. aborts on panic.

This could potentially be an option on &mut MaybeUninit<T> though, returning &mut T on a successful call.

@workingjubilee
Copy link
Member

Yes, this is RFC 1736 all over again.

@workingjubilee
Copy link
Member

workingjubilee commented Nov 30, 2024

@Dylan-DPC Yes! I was under the impression people wanted to close this PR in favour of something else, but I would gladly go ahead with this. I can fix both the naming and look into making an ACP for this.

What I want is to either

  1. not close this PR until I am sure you have been given the best opportunity to understand why it will be closed, as I'd rather you not leave with an impression
  2. see this undergo the changes needed to make it something that will not be rejected, which will probably not be a minor edit here and there

Which path you wish to go down is your choice.

You did not respond to the recommendation to close this issue, so I was not certain you had read the latest comments or if you had a question about them. I thus mostly focused on advancing the other PRs in my queue.

@hazelwiss
Copy link
Author

@workingjubilee I don't have a problem closing this issue/pr especially if it's a duplicate! Whichever you guys think is the best decision! I have no particular impression, all the feedback makes sense.

@workingjubilee
Copy link
Member

I don't believe this is what I would call a duplicate. A duplicate implies that the previous issue fully encompasses the current one, but it has been some time since replace_with was first proposed, and Rust has evolved as a language since then. It's not clear the same soundness problems would matter as much now to Rust, or what mitigations would be acceptable, or how opsem differences matter, or if a changed type like clarfonthey proposes would be enough to carry it, or etc.

However, one would need to first absorb why replace_with failed in 2016 before they could make it succeed in 2025. You'd need to identify which of the grounds it was effectively rejected on could now themselves be dismissed.

We can help you do so, but that's a nontrivial task, and I don't believe it's the sort of thing that will happen over a single PR of iterations, so I am closing this for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. 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.

10 participants