www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19458] New: Speculatively-instantiated templates may give

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

          Issue ID: 19458
           Summary: Speculatively-instantiated templates may give
                    incorrect results
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: snarwin+bugzilla gmail.com

Example program:

--- test.d
struct A
{
    static if (__traits(compiles, hasFoo!A)) {
        void foo() {}
    }
}

enum hasFoo(T) = __traits(hasMember, T, "foo");

void main()
{
    import std.stdio;

    writeln(hasFoo!A); // false (should be true)
    writeln(__traits(hasMember, A, "foo")); // true
}
---

Output:

$ dmd test.d && ./test
false
true

When `hasFoo!A` is instantiated in the `__traits(compiles)` expression, it
evaluates to false, because the body of the `static if` statement has not been
compiled yet. The same instantiation is then re-used in `main`, even though it
is no longer semantically correct.

Possibly related to issue 14803.

--
Nov 30 2018