Rfcs: Add (limited) CTFE

Created on 26 Sep 2014  ·  16Comments  ·  Source: rust-lang/rfcs

See https://github.com/rust-lang/rfcs/pull/253 for a limited form of CTFE. We can use this issue to track a more complete form too.

T-lang

Most helpful comment

Can we revisit this discussion now that the 1.0 launch craze has passed?

All 16 comments

Can we revisit this discussion now that the 1.0 launch craze has passed?

This seems like it's covered by the const fn work?

@sfackler Yeah, but const fn needs to be able to perform non-trivial functionality. For this to be done, we need const fn methods in traits and impls of traits.

Moreover the functions mentioned in #253 (size_of, min_align_of, pref_align_of), just like several other intrinsic-based functions, are not yet const.

I was quite surprised that the following code does not compile:

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

This seems like it's covered by the const fn work?

I thought so too, but this doesn't compile:

#![feature(const_fn)]
pub struct BitField (u8, u8);
impl BitField
{
    const fn get_ms_bit_index(&self) -> u8 {
        if self.0 > self.1 { self.0 } else { self.1 }
    }
}

Its in progress work. See the “postponed” label on the right hand side.

@JinShil
The workaround is to use an array of two elements:

const CONDITION: usize = 1; // true as usize
const RESULT: u8 = [0, 1][CONDITION];

The lack of if in constant expressions is a pretty serious deficiency, its C analogue ?: works even in preprocessor constant expressions. And C code does rely on this property of :?, this creates problems when porting.

If we allow if in constant expressions then it would make sense to allow match as well.

Both if and match sound reasonable to me.

Does anyone know what the status of conditionals within const fns are? Is it blocked on something in particular? An RFC? Implementation?

Found this RFC after creating https://github.com/rust-lang/rust/issues/42906 . What can I do to help? What's the next step?

@BenWiederhake See https://github.com/solson/miri.

MIRI is merged into rustc and is will be used for all const evaluation soon (https://github.com/rust-lang/rust/pull/46882), and size_of is already constexpr, so I don't think there's value in keeping this issue open.

46882 doesn't seem to have added support for if in constant expressions: https://github.com/rust-lang/rust/pull/46882#issuecomment-354600638. On rust stable 1.34.0 this still gives error E019. Is this being working on?

error[E0019]: constant contains unimplemented expression type
 --> if.rs:3:25
  |
3 |     const number: u32 = if condition { 5 } else { 6 };
  |                         ^^^^^^^^^^

@jyn514 It's being worked on but slowly; see https://github.com/rust-lang/rust/issues/49146 and more generally https://github.com/rust-lang/rust/issues/57563 for all things const (fn).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mqudsi picture mqudsi  ·  3Comments

silversolver1 picture silversolver1  ·  3Comments

mqudsi picture mqudsi  ·  3Comments

onelson picture onelson  ·  3Comments

steveklabnik picture steveklabnik  ·  4Comments