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

smarter semantics for right shift by comptime_int #1544

Open
thejoshwolfe opened this issue Sep 18, 2018 · 0 comments
Open

smarter semantics for right shift by comptime_int #1544

thejoshwolfe opened this issue Sep 18, 2018 · 0 comments
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@thejoshwolfe
Copy link
Contributor

When the shift amount of a right shift is not comptime known, everything makes sense. However, I discovered in #1543 and discussed in #1530 (comment) that there are cases for shifting by a comptime_int that would be nice if they "just worked". For example, right-shifting a u8 by 8 should result in 0.

More generally, right-shifting a @IntType(false, N) by M will always fit in a @IntType(false, N - M). For example, a u32 right shifted by 30 will always be < 4. The extreme case where N == M means that the result of the shift will fit in a u0, in other words, the value is 0. For example, right-shifting a u32 by 32 should just be 0.

Even though the result of this shift is guaranteed to fit in a smaller integer type, that doesn't mean we should make the expression be of that type. For example, it would be surprising if an implicitly typed var was a smaller type than you expect.

fn foo(x: u32) u32 {
    var result = x >> 16;
    result += x << 16;
    return result;
}

result was probably meant to be a u32, so we don't want it to get declared as a u16. If it's a u16, at least you will get a compile error that the u32 resulting from result + x << 16 doesn't fit back into result.

Here are some scoped-down ideas that are easier to reason about. Which of these sound like good ideas?

  1. Right-shifting a @IntType(false, N) by comptime-known N results in 0.
  2. Right-shifting a @IntType(false, N) by comptime-known value at least N results in 0.
  3. Right-shifting a @IntType(true, N) by comptime-known N - 1 results in -1.
  4. Right-shifting a @IntType(true, N) by comptime-known value at least N - 1 results in -1.

One concern here is that values that are accepted at comptime are not accepted at runtime. Curently, you have to shift a u32 by a u5, and 32 is not representable as a u5. We would have different rules for comptime-known values and runtime values.

thejoshwolfe added a commit that referenced this issue Sep 18, 2018
@andrewrk andrewrk added this to the 0.4.0 milestone Sep 18, 2018
@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Sep 18, 2018
@andrewrk andrewrk modified the milestones: 0.4.0, 0.5.0 Sep 28, 2018
@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Jul 2, 2019
@andrewrk andrewrk added the accepted This proposal is planned. label Feb 2, 2020
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Feb 10, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 10, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 20, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

2 participants