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

Clarify semantics of the various pointer to pointer casts #1451

Merged
merged 3 commits into from
Feb 21, 2024

Conversation

udoprog
Copy link
Contributor

@udoprog udoprog commented Jan 22, 2024

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).

@udoprog udoprog changed the title Clearly document semantics of various pointer casts Clarify semantics of the various pointer to pointer casts Jan 22, 2024
@ehuss
Copy link
Contributor

ehuss commented Jan 22, 2024

@RalfJung Can you review this?


#### Unsized to sized pointer casts

Any wide to narrow pointer cast discards all metadata that completes the wide
Copy link
Contributor

@ehuss ehuss Jan 22, 2024

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.)

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 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.

src/expressions/operator-expr.md Outdated Show resolved Hide resolved
src/expressions/operator-expr.md Outdated Show resolved Hide resolved
src/expressions/operator-expr.md Outdated Show resolved Hide resolved
@RalfJung
Copy link
Member

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:

#### Pointer-to-pointer cast

`*const T`/`*mut T` can be cast to `*const U`/`*mut U` under the following conditions:

- If both `T` and `U` are sized, [...]
- If both `T` and `U` are unsized, [...]
- If `T` is unsized and `U` is sized, [...]

@udoprog
Copy link
Contributor Author

udoprog commented Jan 24, 2024

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.

src/expressions/operator-expr.md Outdated Show resolved Hide resolved
src/expressions/operator-expr.md Outdated Show resolved Hide resolved
Copy link
Member

@RalfJung RalfJung left a 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.

@udoprog
Copy link
Contributor Author

udoprog commented Jan 26, 2024

For what it's worth, you have my +1.

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Feb 15, 2024
…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).
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 15, 2024
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).
@udoprog
Copy link
Contributor Author

udoprog commented Feb 20, 2024

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?

@RalfJung
Copy link
Member

@udoprog please squash the PR into a single commit.

But yeah, for review would be good if @ehuss took a look. Maybe someone from @rust-lang/opsem can help as well.

Co-authored-by: Ralf Jung <post@ralfj.de>
@udoprog
Copy link
Contributor Author

udoprog commented Feb 21, 2024

Done

Copy link
Contributor

@ehuss ehuss left a comment

Choose a reason for hiding this comment

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

Thanks!

@ehuss ehuss enabled auto-merge February 21, 2024 21:20
@ehuss ehuss added this pull request to the merge queue Feb 21, 2024
Merged via the queue into rust-lang:master with commit b25c20b Feb 21, 2024
1 check passed
@udoprog udoprog deleted the document-ptr-casts branch February 21, 2024 21:50
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 26, 2024
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)
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 27, 2024
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 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)
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Feb 27, 2024
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)
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request Apr 7, 2024
…=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).
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this pull request Apr 27, 2024
…=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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants