Document the behavior of pointer to smaller int casts

Open
#729 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
documentation

Research direction

Start with the linked Rust Playground snippet and investigate the behavior of casting a pointer directly to an integer that cannot hold it. Follow the proposed discussion in rust-lang/rust before editing the Reference. Done means the behavior is explicitly documented, including whether an intermediate cast or warning is expected.

Written by the indexing model from the issue text.

Description

Playground:

let x = 42;
let z = &x as *const _ as u8;

As @rkruppe pointed out, this snippet casts a pointer to an int in which the pointer does not fit. AFAICT, the behavior of this operation is not guaranteed anywhere - I suppose it does a truncation. We should document what it does.

cc @Centril @RalfJung


I personally would probably prefer if it was at least a warning that suggests people to write as X as u8 instead, where X is a type in which the pointer precisely fits, e.g., usize and/or u{target_pointer_width} (e.g. u64) - if that is what that cast actually happens to mean, but this is an issue that should be better discussed in rust-lang/rust, once we know what that cast means.

Dominant language
Rust
Stars
1.6k
Forks
607
Avg merge
1d 1h
Merged PRs (30d)
12

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rust-lang/reference

All issues in rust-lang/reference

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.