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

Add (limited) CTFE #322

Closed
nrc opened this issue Sep 25, 2014 · 16 comments
Closed

Add (limited) CTFE #322

nrc opened this issue Sep 25, 2014 · 16 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@nrc
Copy link
Member

nrc commented Sep 25, 2014

See #253 for a limited form of CTFE. We can use this issue to track a more complete form too.

@nrc nrc added the postponed RFCs that have been postponed and may be revisited at a later time. label Sep 25, 2014
@nrc nrc added the T-lang Relevant to the language team, which will review and decide on the RFC. label May 15, 2015
@archshift
Copy link
Contributor

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

@sfackler
Copy link
Member

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

@ticki
Copy link
Contributor

ticki commented Feb 16, 2016

@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.

@ranma42
Copy link
Contributor

ranma42 commented Feb 16, 2016

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.

@JinShil
Copy link
Contributor

JinShil commented Nov 26, 2016

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 }
    }
}

@nagisa
Copy link
Member

nagisa commented Nov 26, 2016

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

@petrochenkov
Copy link
Contributor

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

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

@petrochenkov
Copy link
Contributor

petrochenkov commented Nov 26, 2016

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.

@Amanieu
Copy link
Member

Amanieu commented Nov 26, 2016

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

@kevincox
Copy link

Both if and match sound reasonable to me.

@frewsxcv
Copy link
Member

frewsxcv commented May 5, 2017

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

@BenWiederhake
Copy link
Contributor

Found this RFC after creating rust-lang/rust#42906 . What can I do to help? What's the next step?

@eddyb
Copy link
Member

eddyb commented Jun 25, 2017

@petrochenkov
Copy link
Contributor

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

@petrochenkov petrochenkov removed the postponed RFCs that have been postponed and may be revisited at a later time. label Feb 24, 2018
wycats pushed a commit to wycats/rust-rfcs that referenced this issue Mar 5, 2019
@jyn514
Copy link
Member

jyn514 commented Apr 23, 2019

#46882 doesn't seem to have added support for if in constant expressions: rust-lang/rust#46882 (comment). 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 };
  |                         ^^^^^^^^^^

@Centril
Copy link
Contributor

Centril commented Apr 23, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests