Skip to content

Say that dereferencing a pointer to a ZST is no longer undefined #467

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

Merged
merged 2 commits into from
Apr 15, 2025

Conversation

mattheww
Copy link
Contributor

The new rules were tracked in
rust-lang/rust#117945

The corresponding update to the Reference was
rust-lang/reference#1541

See also #198

The new rules were tracked in
rust-lang/rust#117945

The corresponding update to the Reference was
rust-lang/reference#1541
type.
references, must be non-null and suitably aligned. However, dereferencing a
null pointer to a ZST is not [undefined behavior][ub], unlike pointers to
other types.
Copy link
Member

@RalfJung RalfJung Oct 19, 2024

Choose a reason for hiding this comment

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

In fact dereferencing (as in, using the * operator on) a null pointer is never UB. addr_of!(*ptr) is always safe, even if ptr is null.

Only non-zero-sized loads, non-zero-sized stores, and field projections at non-zero field offsets have any requirements.

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've changed it to say "loading or storing through a null pointer to a ZST"

From discussion at
https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/What.20operations.20are.20allowed.20on.20ZST.20null.20pointers.3F

 - applying the dereference operator to a null pointer stopped being UB
   a few months ago

 - it's better to be explicit about whether we're talking about loads
   and stores or about applying the dereference operator

I think we don't need to mention field projections here.
type.
references, must be non-null and suitably aligned. However, loading or storing
through a null pointer to a ZST is not [undefined behavior][ub], unlike
pointers to other types.
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be less confusing to say something like "loading or storing through a null pointer is UB, unless the load/store has size 0". Let's talk about the common case first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The subject of this section is ZSTs, so "loading or storing through a null pointer to a ZST is not undefined behavior" is the information it's supplying, and "unlike pointers to other types" is an aside.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, fair.

@traviscross
Copy link
Contributor

The corresponding lang FCP was:

@traviscross traviscross added this pull request to the merge queue Apr 15, 2025
Merged via the queue into rust-lang:master with commit c76a20f Apr 15, 2025
2 checks passed
ChrisDenton added a commit to ChrisDenton/rust that referenced this pull request Apr 22, 2025
Update books

## rust-lang/nomicon

1 commits in 0c10c30cc54736c5c194ce98c50e2de84eeb6e79..c76a20f0d987145dcedf05c5c073ce8d91f2e82a
2025-04-15 20:54:57 UTC to 2025-04-15 20:54:57 UTC

- Say that dereferencing a pointer to a ZST is no longer undefined (rust-lang/nomicon#467)

## rust-lang/reference

7 commits in 3340922df189bddcbaad17dc3927d51a76bcd5ed..3bf3402aea982b876eb56c87da17b0685c6461d5
2025-04-18 13:44:45 UTC to 2025-04-17 17:27:01 UTC

- Use `cfg(false)` instead of `cfg(FALSE)` (rust-lang/reference#1763)
-     Add `cfg(true)` and `cfg(false)` to conditional compilation (RFC 3695) (rust-lang/reference#1762)
- Refactor rendering with `RenderCtx` (rust-lang/reference#1796)
- attributes/codegen: update aarch64 features (rust-lang/reference#1791)
- Simplify GenericParams grammar (rust-lang/reference#1795)
- Add rule identifiers for the ABI chapter (rust-lang/reference#1793)
- Remove broken footnote links from grammar summary (rust-lang/reference#1794)
ChrisDenton added a commit to ChrisDenton/rust that referenced this pull request Apr 22, 2025
Update books

## rust-lang/nomicon

1 commits in 0c10c30cc54736c5c194ce98c50e2de84eeb6e79..c76a20f0d987145dcedf05c5c073ce8d91f2e82a
2025-04-15 20:54:57 UTC to 2025-04-15 20:54:57 UTC

- Say that dereferencing a pointer to a ZST is no longer undefined (rust-lang/nomicon#467)

## rust-lang/reference

7 commits in 3340922df189bddcbaad17dc3927d51a76bcd5ed..3bf3402aea982b876eb56c87da17b0685c6461d5
2025-04-18 13:44:45 UTC to 2025-04-17 17:27:01 UTC

- Use `cfg(false)` instead of `cfg(FALSE)` (rust-lang/reference#1763)
-     Add `cfg(true)` and `cfg(false)` to conditional compilation (RFC 3695) (rust-lang/reference#1762)
- Refactor rendering with `RenderCtx` (rust-lang/reference#1796)
- attributes/codegen: update aarch64 features (rust-lang/reference#1791)
- Simplify GenericParams grammar (rust-lang/reference#1795)
- Add rule identifiers for the ABI chapter (rust-lang/reference#1793)
- Remove broken footnote links from grammar summary (rust-lang/reference#1794)
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 23, 2025
Rollup merge of rust-lang#140124 - rustbot:docs-update, r=ehuss

Update books

## rust-lang/nomicon

1 commits in 0c10c30cc54736c5c194ce98c50e2de84eeb6e79..c76a20f0d987145dcedf05c5c073ce8d91f2e82a
2025-04-15 20:54:57 UTC to 2025-04-15 20:54:57 UTC

- Say that dereferencing a pointer to a ZST is no longer undefined (rust-lang/nomicon#467)

## rust-lang/reference

7 commits in 3340922df189bddcbaad17dc3927d51a76bcd5ed..3bf3402aea982b876eb56c87da17b0685c6461d5
2025-04-18 13:44:45 UTC to 2025-04-17 17:27:01 UTC

- Use `cfg(false)` instead of `cfg(FALSE)` (rust-lang/reference#1763)
-     Add `cfg(true)` and `cfg(false)` to conditional compilation (RFC 3695) (rust-lang/reference#1762)
- Refactor rendering with `RenderCtx` (rust-lang/reference#1796)
- attributes/codegen: update aarch64 features (rust-lang/reference#1791)
- Simplify GenericParams grammar (rust-lang/reference#1795)
- Add rule identifiers for the ABI chapter (rust-lang/reference#1793)
- Remove broken footnote links from grammar summary (rust-lang/reference#1794)
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Apr 23, 2025
Update books

## rust-lang/nomicon

1 commits in 0c10c30cc54736c5c194ce98c50e2de84eeb6e79..c76a20f0d987145dcedf05c5c073ce8d91f2e82a
2025-04-15 20:54:57 UTC to 2025-04-15 20:54:57 UTC

- Say that dereferencing a pointer to a ZST is no longer undefined (rust-lang/nomicon#467)

## rust-lang/reference

7 commits in 3340922df189bddcbaad17dc3927d51a76bcd5ed..3bf3402aea982b876eb56c87da17b0685c6461d5
2025-04-18 13:44:45 UTC to 2025-04-17 17:27:01 UTC

- Use `cfg(false)` instead of `cfg(FALSE)` (rust-lang/reference#1763)
-     Add `cfg(true)` and `cfg(false)` to conditional compilation (RFC 3695) (rust-lang/reference#1762)
- Refactor rendering with `RenderCtx` (rust-lang/reference#1796)
- attributes/codegen: update aarch64 features (rust-lang/reference#1791)
- Simplify GenericParams grammar (rust-lang/reference#1795)
- Add rule identifiers for the ABI chapter (rust-lang/reference#1793)
- Remove broken footnote links from grammar summary (rust-lang/reference#1794)
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.

3 participants