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

Constant expression for smallest i32 emits undef #34

Open
ergawy opened this issue Oct 5, 2016 · 2 comments
Open

Constant expression for smallest i32 emits undef #34

ergawy opened this issue Oct 5, 2016 · 2 comments
Assignees

Comments

@ergawy
Copy link

ergawy commented Oct 5, 2016

The following code:

extern "C" {
    fn print_int(i32) -> ();
}

fn main() -> i32 {
    let test : i32 = 1 << 31;
    print_int(test);

    0
}

emits test value as undef while it should be the smallest i32 value.

@ergawy ergawy self-assigned this Oct 6, 2016
@leissa
Copy link
Member

leissa commented Oct 6, 2016

Currently, we follow the C-standard in terms of what is considered undefined and what not. The C-Standard says to E1 << E2:

If E1 has a signed type and nonnegative value, and E1 × 2^E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

But 2^31 is not representable in i32, hence the behavior is undefined and the behavior of the compiler is correct.

However, that being said, I checked what clang does. Doing things like 1 << 31 seems to be such a common pattern that clang has the mercy to simply do what you would expect. We have three options:

  1. Stick to the C-Standard and leave everything as it is.
  2. Do what clang (and others) are doing.
  3. Do sth completely different.

After discussion with Richard we are likely doing option 2). In the long run, however, I'd like bake this into the type system...

richardmembarth added a commit to AnyDSL/thorin that referenced this issue Oct 13, 2016
@richardmembarth
Copy link
Member

For the moment, we decided for option 2, but in the long run we would like to track the wrap-around property in the type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants