[Issue 18945] immutable variable is used as if it's an enum

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jun 5 11:34:50 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18945

Jonathan M Davis <issues.dlang at jmdavisProg.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |issues.dlang at jmdavisProg.co
                   |                            |m

--- Comment #1 from Jonathan M Davis <issues.dlang at jmdavisProg.com> ---
Yeah. And bizarrely, this code actually results in an error about not being
able to call stuff during CTFE:

int foo()
{
    import std.datetime;
    return cast(int)Clock.currTime().stdTime;
}

void main()
{
    immutable i = foo();
    int[i] arr;
}

Getting rid of the declaration for arr fixes the problem. So, clearly, the
compiler is currently deciding whether it should do CTFE on an immutable, local
variable based on whether it's then used in a context where its value must be
known at compile time instead of forcing enum to be used instead. It also seems
to do the same with const.

The fact that the static array's size can use a local variable is completely
inconsistent with how CTFE normally works and makes the whole situation that
much more confusing. CTFE should only be kicking in based on whether the value
is actually needed at compile time and not based on whether the variable that
it's assigned to is then used at compile time.

--


More information about the Digitalmars-d-bugs mailing list