docs(core): clarify that signed integers use two's complement#147960
docs(core): clarify that signed integers use two's complement#147960AudaciousAxiom wants to merge 2 commits intorust-lang:mainfrom
Conversation
|
Yeah this is definitely guaranteed. I don't have a strong opinion on whether it makes sense to repeat lang-level guarantees in the libs docs.
I would argue that But I understand that currently that's not how we specify Cc @traviscross |
|
Regarding this PR specifically, even though I see that we do something a bit like this for floats, I'd prefer to not cram this bit of information into the summary line of the doc comment for the type. Putting it in the body does seem OK though. r? traviscross cc @ehuss
Are you interested in making a PR to the Reference here to more cleanly make this guarantee? Probably I'd like to see the Reference guarantee this clearly before we do it in the standard library in this case. |
|
@rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
That makes sense, opened rust-lang/reference#2064 to this end. |
library/core/src/primitive_docs.rs
Outdated
| #[rustc_doc_primitive = "i8"] | ||
| // | ||
| /// The 8-bit signed integer type. | ||
| /// The 8-bit signed integer type (represented using two's complement). |
There was a problem hiding this comment.
As mentioned in #147960 (comment):
Regarding this PR specifically, even though I see that we do something a bit like this for floats, I'd prefer to not cram this bit of information into the summary line of the doc comment for the type. Putting it in the body does seem OK though.
Other than this change, the Reference change I had asked for has now been approved by the lang team in rust-lang/reference#2064 and merged, so this PR can be merged after this editorial adjustment.
There was a problem hiding this comment.
It took me a while to come back to this: I've now rebased and added a fixup commit with a new proposition for documenting this (I'll squash when you ask me before merging). As I mention, this now proposes to introduce a one-sentence section for each signed integer. I can see it giving too much importance to this small change, but I'm not sure how to introduce this into the existing docs otherwise, without cramming it into the summary paragraph.
44b0d8b to
caad4fd
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
| // | ||
| /// The 8-bit signed integer type. | ||
| /// | ||
| /// # Binary representation |
There was a problem hiding this comment.
Maybe adding a dedicated section is a bit too much, but that single-sentence paragraph looks a bit lonely otherwise, and we can't add it to the summary paragraph as it gets too long.
| // | ||
| /// The 8-bit signed integer type. | ||
| /// | ||
| /// # Binary representation |
There was a problem hiding this comment.
I'm not sure whether it should be title case (if we do keep that dedicated section, that is): most of titles in the std docs don't seem to use it, but I've still found a few that do.
| /// location in memory. For example, on a 32 bit target, this is 4 bytes | ||
| /// and on a 64 bit target, this is 8 bytes. | ||
| /// | ||
| /// # Binary representation |
There was a problem hiding this comment.
I've placed this after the above paragraph because I think how the type is encoded should be documented only after its meaning is entirely defined.
|
@rustbot ready |
Description
This attempts to clarify in the std documentation how signed integers (
i8–i128andisize) are encoded. I do not mean this to introduce a new stability guarantee, but only to document an already obvious (?) property, which is already documented elsewhere (mostly).Motivation
My understanding is that signed integers are already guaranteed to be encoded in two's complement. The Reference does state this for
i8–i128(but technically does not forisize, at least not in the same place), and in documenting numeric casts and negation operators:This therefore does not seem architecture or platform-dependent. However it is also not specified explicitly in the FLS.
In addition, I could not find this in the std documentation, while this seems required to understand the behavior of shr operations and of
ascasts in particular.If this is something you think is worth documenting explicitly, the exact phrasing can likely be refined (it is currently modeled on floats'). If not, feel free to close this PR.