You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Are (non-free) generic constants guaranteed to be evaluated? #409
The nightly reference guarantees that "free constants are always evaluated at compile time." However, that's not quite what we have here - we evaluate the constant REF_TRANSMUTABLE_INTO but assign it to a runtime variable (let _: () = ...). If we replaced let _ with const _, that would be a free constant, but it would never compile (even when we wanted it to) because a constant in a function body isn't allowed to reference types which are generic in that context.
What I want to know is: Is this constant guaranteed to be evaluated so long as transmute_ref_into is used?
This code is equivalent to an assert at the beginning of the function, so it definitely will never be callable without a panic, i.e. as far as protecting the transmute is concerned you should be fine. Because the assert is in a const body, it must be evaluated at compile time if it ends up in the binary at all, so yes, I think you will get the desired behavior that if transmute_ref_into is used then any errors will happen at compile time (post-monomorphization time, more precisely). If transmute_ref_into is not used (in some sense of 'used') I don't know if we will promise anything here.
This isn't really an unsafe code question though, it's probably t-lang.
We guarantee that every constant mentioned syntactically within a function that executes at runtime, has been previously executed at compiletime. So I agree with @digama0. I don't know if and where we document that guarantee, though.
Activity
digama0 commentedon May 29, 2023
This code is equivalent to an assert at the beginning of the function, so it definitely will never be callable without a panic, i.e. as far as protecting the
transmuteis concerned you should be fine. Because the assert is in aconstbody, it must be evaluated at compile time if it ends up in the binary at all, so yes, I think you will get the desired behavior that iftransmute_ref_intois used then any errors will happen at compile time (post-monomorphization time, more precisely). Iftransmute_ref_intois not used (in some sense of 'used') I don't know if we will promise anything here.This isn't really an unsafe code question though, it's probably t-lang.
joshlf commentedon May 30, 2023
Okay sounds good, thanks! It makes sense to me too that it would be guaranteed, but I'll ask t-lang just to be sure 🙂
RalfJung commentedon May 30, 2023
We guarantee that every constant mentioned syntactically within a function that executes at runtime, has been previously executed at compiletime. So I agree with @digama0. I don't know if and where we document that guarantee, though.
RalfJung commentedon May 30, 2023
Closing in favor of rust-lang/rust#112090.