-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
typestrong const integers #30587
typestrong const integers #30587
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Aatch (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
e2e8e47
to
0354271
Compare
☔ The latest upstream changes (presumably #30588) made this pull request unmergeable. Please resolve the merge conflicts. |
e03966c
to
50245af
Compare
@@ -3677,10 +3677,10 @@ sites are: | |||
|
|||
* `let` statements where an explicit type is given. | |||
|
|||
For example, `128` is coerced to have type `i8` in the following: |
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.
Nice catch, but probably should go into a separate rollable-up PR.
☔ The latest upstream changes (presumably #30660) made this pull request unmergeable. Please resolve the merge conflicts. |
This will have to be reviewed outside-of-github because of its tendency to censor. |
50245af
to
e24a1de
Compare
☔ The latest upstream changes (presumably #30676) made this pull request unmergeable. Please resolve the merge conflicts. |
7b9bc2e
to
b4f78a1
Compare
I removed the eager const eval to do that on its own. For now it's just the typestrong const integers. I'm not sure what the expected behavior of something like |
I'll run crater. |
Report. Only regression is a false positive. |
wooohoo xD I'll split it up into multiple PRs for easier reviewing |
☔ The latest upstream changes (presumably #30890) made this pull request unmergeable. Please resolve the merge conflicts. |
b4f78a1
to
50245af
Compare
6b61528
to
b6b192b
Compare
`assert_eq!(-9223372036854775808isize as u64, 0x8000000000000000);` fails on 32 bit and succeeds on 64 bit. These commits don't change that behavior. The following worked before my changes, because the discriminant was always processed as `u64`. Now it fails, because the discriminant of `Bu64` is now `0` instead of `-9223372036854775808`. This is more in line with the above assertion's code, since `-9223372036854775808isize as u64` on 32 bit yielded `0`. ```rust enum Eu64 { Au64 = 0, Bu64 = 0x8000_0000_0000_0000 } ```
b6b192b
to
fcee002
Compare
cb7b0a1
to
08c448a
Compare
As the commit message of fcee002 states:
fails on 32 bit and succeeds on 64 bit. These commits don't change that behavior. The following worked before my changes, because the discriminant was enum Eu64 {
Au64 = 0,
Bu64 = 0x8000_0000_0000_0000
} |
@oli-obk thanks; can't say I'm entirely happy with the rules here, but this seems like the best step for now. |
@bors r+ |
📌 Commit 08c448a has been approved by |
⌛ Testing commit 08c448a with merge f5dbfb4... |
💔 Test failed - auto-linux-64-opt-rustbuild |
Try to add |
Yeah if this is adding a new crate it'll just need a |
can't test locally, because the but I added the dependencies to every crate that has |
typestrong const integers ~~It would be great if someone could run crater on this PR, as this has a high danger of breaking valid code~~ Crater ran. Good to go. ---- So this PR does a few things: 1. ~~const eval array values when const evaluating an array expression~~ 2. ~~const eval repeat value when const evaluating a repeat expression~~ 3. ~~const eval all struct and tuple fields when evaluating a struct/tuple expression~~ 4. remove the `ConstVal::Int` and `ConstVal::Uint` variants and replace them with a single enum (`ConstInt`) which has variants for all integral types * `usize`/`isize` are also enums with variants for 32 and 64 bit. At creation and various usage steps there are assertions in place checking if the target bitwidth matches with the chosen enum variant 5. enum discriminants (`ty::Disr`) are now `ConstInt` 6. trans has its own `Disr` type now (newtype around `u64`) This obviously can't be done without breaking changes (the ones that are noticable in stable) We could probably write lints that find those situations and error on it for a cycle or two. But then again, those situations are rare and really bugs imo anyway: ```rust let v10 = 10 as i8; let v4 = 4 as isize; assert_eq!(v10 << v4 as usize, 160 as i8); ``` stops compiling because 160 is not a valid i8 ```rust struct S<T, S> { a: T, b: u8, c: S } let s = S { a: 0xff_ff_ff_ffu32, b: 1, c: 0xaa_aa_aa_aa as i32 }; ``` stops compiling because `0xaa_aa_aa_aa` is not a valid i32 ---- cc @eddyb @pnkfelix related: rust-lang/rfcs#1071
🎉 🎈 |
It would be great if someone could run crater on this PR, as this has a high danger of breaking valid codeCrater ran. Good to go.So this PR does a few things:
const eval array values when const evaluating an array expressionconst eval repeat value when const evaluating a repeat expressionconst eval all struct and tuple fields when evaluating a struct/tuple expressionConstVal::Int
andConstVal::Uint
variants and replace them with a single enum (ConstInt
) which has variants for all integral typesusize
/isize
are also enums with variants for 32 and 64 bit. At creation and various usage steps there are assertions in place checking if the target bitwidth matches with the chosen enum variantty::Disr
) are nowConstInt
Disr
type now (newtype aroundu64
)This obviously can't be done without breaking changes (the ones that are noticable in stable)
We could probably write lints that find those situations and error on it for a cycle or two. But then again, those situations are rare and really bugs imo anyway:
stops compiling because 160 is not a valid i8
stops compiling because
0xaa_aa_aa_aa
is not a valid i32cc @eddyb @pnkfelix
related: rust-lang/rfcs#1071
#13768
#23833