-
Notifications
You must be signed in to change notification settings - Fork 498
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
Clarify semantics of the various pointer to pointer casts #1451
Conversation
@RalfJung Can you review this? |
src/expressions/operator-expr.md
Outdated
|
||
#### Unsized to sized pointer casts | ||
|
||
Any wide to narrow pointer cast discards all metadata that completes the wide |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The word "metadata" isn't defined anywhere. Can you add a description here that defines what it means? (Or wherever seems appropriate.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if I'm just promoting kicking the can down the road, but I opted not to define metadata since:
- It's not necessary to define the semantics of this cast, the only distinction is that unsized pointers have metadata and this cast removes it.
- Another part of the reference "behaviors considered undefined" uses metadata without defining it.
That being said, if I were to define it, where do you think I should do it and to what extent?
The representation of pointers and references to DSTs is made up of two components; A base pointer and metadata. The metadata is the necessary extra data that has to be attached to pointers and references of DSTs which completes their representation. This is stored adjacent to the base pointer causing them to be wider than sized pointers earning them the name "wide" or "fat" pointers. The base pointer can be extracted through an unsized to sized pointer cast.
I'm a bit concerned that I'll end up having to define everything metadata adjacent 😓, so at least I'd want to avoid that as much as possible.
I think this might be better structured in a different way... I'd make a single big section for raw-to-raw casts, which also handles once and for all the mut/const part:
|
5d9c879
to
188a38a
Compare
Thanks for taking the time! Note that if you want to change something, feel free to edit it directly (assuming that's enabled) if that is your preference. I only have an interest in the cast being documented - I don't mind how it's done or who does it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but since I now largely rewrote this it should be reviewed by someone else.
For what it's worth, you have my +1. |
…om-raw, r=m-ou-se Document requirements for unsized {Rc,Arc}::from_raw This seems to be implied due to these types supporting operation-less unsized coercions. Taken together with the [established behavior of a wide to thin pointer cast](rust-lang/reference#1451) it would enable unsafe downcasting of these containers. Note that the term "data pointer" is adopted from rust-lang/rfcs#3559 See also this [internals thread](https://internals.rust-lang.org/t/can-unsafe-smart-pointer-downcasts-be-correct/20229/2).
Rollup merge of rust-lang#120449 - udoprog:document-unsized-rc-arc-from-raw, r=m-ou-se Document requirements for unsized {Rc,Arc}::from_raw This seems to be implied due to these types supporting operation-less unsized coercions. Taken together with the [established behavior of a wide to thin pointer cast](rust-lang/reference#1451) it would enable unsafe downcasting of these containers. Note that the term "data pointer" is adopted from rust-lang/rfcs#3559 See also this [internals thread](https://internals.rust-lang.org/t/can-unsafe-smart-pointer-downcasts-be-correct/20229/2).
This seems a bit stalled. Since I'm pretty much OK with this change, can we say that I wrote it and move forward? 😄 I think this would pretty much emulate the same scenario as if this was fully an external change proposed by me which was reviewed by Ralf. Or maybe @ehuss can give this a quick look over? |
Co-authored-by: Ralf Jung <[email protected]>
28bde5b
to
8c7cdd3
Compare
Done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Update books ## rust-lang/book 1 commits in 71352deb20727b4dda9ebfe8182709d5bf17dfea..19c40bfd2d57641d962f3119a1c343355f1b3c5e 2024-02-19 20:39:35 UTC to 2024-02-19 20:39:35 UTC - Rust upgrades (rust-lang/book#3844) ## rust-lang/edition-guide 3 commits in 76bd48a273a0e0413a3bf22c699112d41497b99e..e1eead1181a691e56299294d5f1d62fe7a26d317 2024-02-24 21:15:28 UTC to 2024-02-17 21:44:36 UTC - Rename static_mut_refs (rust-lang/edition-guide#293) - Add 2024 prelude (rust-lang/edition-guide#292) - Add some more initial stub docs for 2024. (rust-lang/edition-guide#291) ## rust-lang/reference 5 commits in 8227666de13f6e7bb32dea9dc42e841adb5ce4b7..3417f866932cb1c09c6be0f31d2a02ee01b4b95d 2024-02-25 19:37:14 UTC to 2024-02-15 13:28:59 UTC - Document `target_abi` (rust-lang/reference#1446) - Add `rust-toolchain.toml` and revise README (rust-lang/reference#1474) - Clarify semantics of the various pointer to pointer casts (rust-lang/reference#1451) - Update aarch64 target feature docs to match LLVM (rust-lang/reference#1470) - Fix grammar for TypePathFn (rust-lang/reference#1281) ## rust-lang/rust-by-example 4 commits in e188d5d466f7f3ff9f1d518393235f4fe951be46..57f1e708f5d5850562bc385aaf610e6af14d6ec8 2024-02-24 18:31:19 UTC to 2024-02-14 13:06:53 UTC - bug: fix typo (rust-lang/rust-by-example#1822) - Fix typo in attribute.md (rust-lang/rust-by-example#1818) - Add Japanese translation (rust-lang/rust-by-example#1794) - Remove unused .travis.yml file (rust-lang/rust-by-example#1816) ## rust-lang/rustc-dev-guide 9 commits in 1f30cc7..7b0ef5b 2024-02-22 19:15:52 UTC to 2024-02-13 12:13:06 UTC - Update compiletest directives to be in `ui_test` style `//`@`` (rust-lang/rustc-dev-guide#1895) - Add notes on tweak rust-analyzer.check.overrideCommand (rust-lang/rustc-dev-guide#1890) - Fix more links (rust-lang/rustc-dev-guide#1884) - Remove references to -Z ast-json and -Z ast-json-noexpand (rust-lang/rustc-dev-guide#1893) - Specify ui test suite directives and add note on ignore-tidy directives (rust-lang/rustc-dev-guide#1892) - Update docs about ui tests now using `//`@`` headers (rust-lang/rustc-dev-guide#1885) - Make git rebase commands more bulletproof (rust-lang/rustc-dev-guide#1889) - updating-llvm.md: command does not work (rust-lang/rustc-dev-guide#1887) - fix update llvm build command (rust-lang/rustc-dev-guide#1886)
Rollup merge of rust-lang#121639 - rustbot:docs-update, r=ehuss Update books ## rust-lang/book 1 commits in 71352deb20727b4dda9ebfe8182709d5bf17dfea..19c40bfd2d57641d962f3119a1c343355f1b3c5e 2024-02-19 20:39:35 UTC to 2024-02-19 20:39:35 UTC - Rust upgrades (rust-lang/book#3844) ## rust-lang/edition-guide 3 commits in 76bd48a273a0e0413a3bf22c699112d41497b99e..e1eead1181a691e56299294d5f1d62fe7a26d317 2024-02-24 21:15:28 UTC to 2024-02-17 21:44:36 UTC - Rename static_mut_refs (rust-lang/edition-guide#293) - Add 2024 prelude (rust-lang/edition-guide#292) - Add some more initial stub docs for 2024. (rust-lang/edition-guide#291) ## rust-lang/reference 5 commits in 8227666de13f6e7bb32dea9dc42e841adb5ce4b7..3417f866932cb1c09c6be0f31d2a02ee01b4b95d 2024-02-25 19:37:14 UTC to 2024-02-15 13:28:59 UTC - Document `target_abi` (rust-lang/reference#1446) - Add `rust-toolchain.toml` and revise README (rust-lang/reference#1474) - Clarify semantics of the various pointer to pointer casts (rust-lang/reference#1451) - Update aarch64 target feature docs to match LLVM (rust-lang/reference#1470) - Fix grammar for TypePathFn (rust-lang/reference#1281) ## rust-lang/rust-by-example 4 commits in e188d5d466f7f3ff9f1d518393235f4fe951be46..57f1e708f5d5850562bc385aaf610e6af14d6ec8 2024-02-24 18:31:19 UTC to 2024-02-14 13:06:53 UTC - bug: fix typo (rust-lang/rust-by-example#1822) - Fix typo in attribute.md (rust-lang/rust-by-example#1818) - Add Japanese translation (rust-lang/rust-by-example#1794) - Remove unused .travis.yml file (rust-lang/rust-by-example#1816) ## rust-lang/rustc-dev-guide 9 commits in 1f30cc7..7b0ef5b 2024-02-22 19:15:52 UTC to 2024-02-13 12:13:06 UTC - Update compiletest directives to be in `ui_test` style `//`@`` (rust-lang/rustc-dev-guide#1895) - Add notes on tweak rust-analyzer.check.overrideCommand (rust-lang/rustc-dev-guide#1890) - Fix more links (rust-lang/rustc-dev-guide#1884) - Remove references to -Z ast-json and -Z ast-json-noexpand (rust-lang/rustc-dev-guide#1893) - Specify ui test suite directives and add note on ignore-tidy directives (rust-lang/rustc-dev-guide#1892) - Update docs about ui tests now using `//`@`` headers (rust-lang/rustc-dev-guide#1885) - Make git rebase commands more bulletproof (rust-lang/rustc-dev-guide#1889) - updating-llvm.md: command does not work (rust-lang/rustc-dev-guide#1887) - fix update llvm build command (rust-lang/rustc-dev-guide#1886)
Update books ## rust-lang/book 1 commits in 71352deb20727b4dda9ebfe8182709d5bf17dfea..19c40bfd2d57641d962f3119a1c343355f1b3c5e 2024-02-19 20:39:35 UTC to 2024-02-19 20:39:35 UTC - Rust upgrades (rust-lang/book#3844) ## rust-lang/edition-guide 3 commits in 76bd48a273a0e0413a3bf22c699112d41497b99e..e1eead1181a691e56299294d5f1d62fe7a26d317 2024-02-24 21:15:28 UTC to 2024-02-17 21:44:36 UTC - Rename static_mut_refs (rust-lang/edition-guide#293) - Add 2024 prelude (rust-lang/edition-guide#292) - Add some more initial stub docs for 2024. (rust-lang/edition-guide#291) ## rust-lang/reference 5 commits in 8227666de13f6e7bb32dea9dc42e841adb5ce4b7..3417f866932cb1c09c6be0f31d2a02ee01b4b95d 2024-02-25 19:37:14 UTC to 2024-02-15 13:28:59 UTC - Document `target_abi` (rust-lang/reference#1446) - Add `rust-toolchain.toml` and revise README (rust-lang/reference#1474) - Clarify semantics of the various pointer to pointer casts (rust-lang/reference#1451) - Update aarch64 target feature docs to match LLVM (rust-lang/reference#1470) - Fix grammar for TypePathFn (rust-lang/reference#1281) ## rust-lang/rust-by-example 4 commits in e188d5d466f7f3ff9f1d518393235f4fe951be46..57f1e708f5d5850562bc385aaf610e6af14d6ec8 2024-02-24 18:31:19 UTC to 2024-02-14 13:06:53 UTC - bug: fix typo (rust-lang/rust-by-example#1822) - Fix typo in attribute.md (rust-lang/rust-by-example#1818) - Add Japanese translation (rust-lang/rust-by-example#1794) - Remove unused .travis.yml file (rust-lang/rust-by-example#1816) ## rust-lang/rustc-dev-guide 9 commits in 1f30cc7cca9a3433bc1872abdc98960b36c21ca0..7b0ef5b0bea5e3ce3b9764aa5754a60e2cc05c52 2024-02-22 19:15:52 UTC to 2024-02-13 12:13:06 UTC - Update compiletest directives to be in `ui_test` style `//`@`` (rust-lang/rustc-dev-guide#1895) - Add notes on tweak rust-analyzer.check.overrideCommand (rust-lang/rustc-dev-guide#1890) - Fix more links (rust-lang/rustc-dev-guide#1884) - Remove references to -Z ast-json and -Z ast-json-noexpand (rust-lang/rustc-dev-guide#1893) - Specify ui test suite directives and add note on ignore-tidy directives (rust-lang/rustc-dev-guide#1892) - Update docs about ui tests now using `//`@`` headers (rust-lang/rustc-dev-guide#1885) - Make git rebase commands more bulletproof (rust-lang/rustc-dev-guide#1889) - updating-llvm.md: command does not work (rust-lang/rustc-dev-guide#1887) - fix update llvm build command (rust-lang/rustc-dev-guide#1886)
…=m-ou-se Document requirements for unsized {Rc,Arc}::from_raw This seems to be implied due to these types supporting operation-less unsized coercions. Taken together with the [established behavior of a wide to thin pointer cast](rust-lang/reference#1451) it would enable unsafe downcasting of these containers. Note that the term "data pointer" is adopted from rust-lang/rfcs#3559 See also this [internals thread](https://internals.rust-lang.org/t/can-unsafe-smart-pointer-downcasts-be-correct/20229/2).
…=m-ou-se Document requirements for unsized {Rc,Arc}::from_raw This seems to be implied due to these types supporting operation-less unsized coercions. Taken together with the [established behavior of a wide to thin pointer cast](rust-lang/reference#1451) it would enable unsafe downcasting of these containers. Note that the term "data pointer" is adopted from rust-lang/rfcs#3559 See also this [internals thread](https://internals.rust-lang.org/t/can-unsafe-smart-pointer-downcasts-be-correct/20229/2).
Relates #1448
I've tried to the best of my ability to reproduce the verbiage of the reference, some parts feel a bit wordy to me so any feedback would be appreciated.
To fully describe the semantics of unsized to sized pointer casts, I felt it was necessary to document that the reinterpreting sized to sized casts produce identical values. I couldn't immediately find this documented.
This is because unsized to sized pointer casts also permits a reintepreting cast in the same sweep (such as
*const [u8] as *const u32
).