-
Notifications
You must be signed in to change notification settings - Fork 14k
stop specializing on Copy
#135634
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
stop specializing on Copy
#135634
Conversation
|
rustbot has assigned @Mark-Simulacrum. Use |
|
Changes to the code generated for builtin derived traits. cc @nnethercote |
This comment has been minimized.
This comment has been minimized.
|
Going to nominate for libs-api (and libs) since this is both a breaking change (allowed since fixing soundness). I feel like I recall an RFC or some other discussion about us explicitly saying libraries shouldn't do the unsound thing here, but I don't know what that was. https://rust-lang.github.io/rfcs/1521-copy-clone-semantics.html is a bit related but not directly :) |
|
RFC 1521 could be interpreted so. Since it requires that Clone is equivalent to Copy when both are implemented. Since
I still don't think this is unsound in itself. So far all demonstrations of unsoundness required some other Noratrieb also argues that lifetime-conditional Copy currently is unsupported in MIR. So ISTM that this could be a documentation shortcoming and a compiler/lang issue that such implementations should be prevented but aren't. That said, I agree that the current situation is brittle. |
|
Without saying anything about specialization on Copy, there's definitely been past land discussion of splitting the "memcpyable" part of But that gets back to needing, as the8472 said, a way to actually block lifetime-bad implementations before it could be stable. |
|
We discussed this during today's libs-API meeting. We currently are not aware of any safe code that is unsound due to these specializations and there were concerns about performance regressions for user types that manually implement So we're leaning towards keeping the implementations as they are and instead improving things in other ways such as adding compiler warnings or improving the Copy documentation or unsafe-code-guidelines. We'd like input from T-types whether they agree with this assessment and if something should be changed on the language side, e.g. by forbidding or at least warning on lifetime-conditional implementations, similar to how A compiler-team member has indicated that lifetime-dependent Copy impls are de-facto unsupported. |
|
Forbidding lifetime dependent copy impls seems like it would be rather breaking (but that's pure speculation, we ought to do a crater run to check if anyone feels strongly we should forbid such impls), though generally I don't feel great about forbidding lifetime dependent copy impls. I also don't think a warning on lifetime dependent copy impls really helps anything for std as warnings cannot be relied upon for soundness and so std's usage of specialization would still be wrong. In general I would prefer std to not be using specialization in any ways that affect behaviour in any way, it's stably exposing unstable broken parts of the type system in ways that are arguably unsound (allows you to prove trait bounds hold when they do not). imo what should have happened is that years ago when specialization was found to be unsound all these specializations should have been ripped out regardless of the performance cost and re-added with a PR like this that respects lifetime constraints and treats the unsafe specialization marker attr as something unsafe with invariants to be upheld. I cant speak for the whole types team but that's atleast my opinion as a types member 🤷♀️ On a semi-related note, does std still specialize fused iterator stuff in ways that exposes specialization to stable too? I remember that being a thing some years ago but haven't kept up to date with how std is using specialization |
Yes, but #86765 changed the specialization so that incorrect specializations only result in correctness issues and not soundness ones. And we have |
|
The types team discussed this on zulip: https://rust-lang.zulipchat.com/#narrow/channel/326866-t-types.2Fnominated/topic/.23135634.3A.20stop.20specializing.20on.20.60Copy.60 My opinion/summary from there:
I would like to avoid specializing on |
|
☔ The latest upstream changes (presumably #136448) made this pull request unmergeable. Please resolve the merge conflicts. |
|
Should we add manual conditional impls for types like I know we're not going to perfectly recover everything that |
ff45e47 to
f2d28fe
Compare
This comment has been minimized.
This comment has been minimized.
Maybe, but let's just try the performance of this first: |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
In general, this seems fine (given FCP completion): r=me with CI fixed. |
|
@bors r=@Mark-Simulacrum rollup=never |
|
@bors ping |
|
😪 I'm awake I'm awake |
|
@bors retry |
|
(Infra checking bors) |
|
💡 This pull request was already approved, no need to approve it again. |
|
Checking if bors is back |
|
☀️ Test successful - checks-actions |
What is this?This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.Comparing a7b3715 (parent) -> 055d0d6 (this PR) Test differencesShow 4834 test diffsStage 1
Stage 2
Additionally, 4830 doctest diffs were found. These are ignored, as they are noisy. Job group index
Test dashboardRun cargo run --manifest-path src/ci/citool/Cargo.toml -- \
test-dashboard 055d0d6aaf937cc11b3d2a5b5725972723b7f3c6 --output-dir test-dashboardAnd then open Job duration changes
How to interpret the job duration changes?Job durations can vary a lot, based on the actual runner instance |
|
Finished benchmarking commit (055d0d6): comparison URL. Overall result: ❌✅ regressions and improvements - please read the text belowOur benchmarks found a performance regression caused by this PR. Next Steps:
@rustbot label: +perf-regression Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary 1.0%, secondary -0.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary 3.7%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (primary 0.3%, secondary 0.2%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 476.631s -> 476.7s (0.01%) |
fixes #132442
stdspecializes onCopyto optimize certain library functions such asclone_from_slice. This is unsound, however, as theCopyimplementation may not be always applicable because of lifetime bounds, which specialization does not take into account; the result being that values are copied even though they are notCopy. For instance, this code:should not panic, but does (playground).
To solve this, this PR introduces a new
unsafetrait:TrivialClone. This trait may be implemented whenever theCloneimplementation is equivalent to copying the value (so e.g.fn clone(&self) -> Self { *self }). Because of lifetime erasure, there is no way for theCloneimplementation to observe lifetime bounds, meaning that even if theTrivialClonehas stricter bounds than theCloneimplementation, its invariant still holds. Therefore, it is sound to specialize onTrivialClone.I've changed all
Copyspecializations in the standard library to specialize onTrivialCloneinstead. Unfortunately, the unsound#[rustc_unsafe_specialization_marker]attribute onCopycannot be removed in this PR ashashbrownstill depends on it. I'll make a PR updatinghashbrownonce this lands.With
Copyno longer being considered for specialization, this change alone would result in the standard library optimizations not being applied for user types unaware ofTrivialClone. To avoid this and restore the optimizations in most cases, I have changed the expansion of#[derive(Clone)]: Currently, whenever bothCloneandCopyare derived, theclonemethod performs a copy of the value. With this PR, the derive macro also adds aTrivialCloneimplementation to make this case observable using specialization. I anticipate that most users will use#[derive(Clone, Copy)]whenever both are applicable, so most users will still profit from the library optimizations.Unfortunately, Hyrum's law applies to this PR: there are some popular crates which rely on the precise specialization behaviour of
coreto implement "specialization at home", e.g.libAFL. I have no remorse for breaking such horrible code, but perhaps we should open other, better ways to satisfy their needs – for example by dropping the'staticbound onTypeId::of...