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

If expressions not evaluated for constants #37948

Closed
JinShil opened this issue Nov 23, 2016 · 5 comments
Closed

If expressions not evaluated for constants #37948

JinShil opened this issue Nov 23, 2016 · 5 comments

Comments

@JinShil
Copy link

JinShil commented Nov 23, 2016

This works fine:

fn main() {
    let condition: bool = true;
    let result: u8 = if condition { 1 } else { 0 };
    println!("{}", result);
}

If assume, therefore, that this should work as well...

const CONDITION: bool = true;
const RESULT: u8 = if CONDITION { 1 } else { 0 };

fn main() {
    println!("{}", RESULT);
}

...but it fails with

error[E0019]: constant contains unimplemented expression type
 --> <anon>:2:20
  |
2 | const RESULT: u8 = if CONDITION { 1 } else { 0 };
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@Stebalien
Copy link
Contributor

Your first example is evaluated at runtime, the second is evaluated at compile time. Rust doesn't currently support evaluating conditionals at compile time (support for calling functions is in the works but that doesn't cover conditionals).

@JinShil
Copy link
Author

JinShil commented Nov 23, 2016

Your first example is evaluated at runtime, the second is evaluated at compile time.

Just to clarify, I understand that one is evaluated at runtime, and the other at compile time. The reason for submitting this issue is to point out the inconsistency. That is, if if expressions are expressions, I think they should be usable just like any other expression. For example if the expression A + B in const NUM: i32 = A + B; evaluates fine at compile time, I think the if expressions should also.

@Stebalien
Copy link
Contributor

@JinShil Got it. That kind of issue usually goes in the rust-lang/rfcs repo (see rust-lang/rfcs#322, specifically).

@est31
Copy link
Member

est31 commented Nov 23, 2016

miri will make this possible, and much more.

@steveklabnik
Copy link
Member

Yes, @Stebalien is right here. Thanks @JinShil , but we should track this in the RFCs repo (and already are)

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

No branches or pull requests

4 participants