www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2379] New: CTFE fails to evaluate if unless it is very straightforward

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2379

           Summary: CTFE fails to evaluate if unless it is very
                    straightforward
           Product: D
           Version: 2.019
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: critical
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: 2korden gmail.com


First attempt:
-----------
template Test0(invariant(char)[] text)
{
    auto Test0 = text;
}

writefln(Test0!("hello")); // success
pragma(msg, Test0!("hello")); // failure:
// pragma msg string expected for message, not 'Test0'

Let's replace invariant(char)[] with alias :(
-----------
template Test0(alias text)
{
    auto Test0 = text;
}
pragma(msg, Test0!("hello")); // success

Let's make it slightly more complex:
-----------
template Test0(alias text)
{
    auto Test0 = text[0..1];
}
pragma(msg, Test0!("hello")); // failure:
// pragma msg string expected for message, not 'Test0'

Other example:
-----------
template Test0(invariant(char)[] text)
{
    auto Test0 = text;
}

template Test1(invariant(char)[] text)
{
    auto Test1 = Test0!(text);
}

writefln(Test0!("hello")); // success
writefln(Test1!("hello")); // failure:
// Error: non-constant expression Test0


-- 
Sep 29 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2379





------- Comment #1 from shro8822 vandals.uidaho.edu  2008-09-29 19:02 -------

Something is wrong here. The examples don't use CTFE. Is the title wrong?


-- 
Sep 29 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2379





------- Comment #2 from snake.scaly gmail.com  2008-09-30 07:56 -------
Everything works as expected.

template Test0(invariant(char)[] text)
{
    auto Test0 = text;
}

effectively declares

invariant(char)[] Test0 = "hello";

which is a variable initialized at runtime. The correct way
to write this template is

template Test0(invariant(char)[] text)
{
    enum Test0 = text;
}

which declares a manifest constant.


-- 
Sep 30 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2379


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #3 from bugzilla digitalmars.com  2008-10-02 04:05 -------
Sergey is correct.


-- 
Oct 02 2008