[Issue 19458] New: Speculatively-instantiated templates may give incorrect results
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Dec 1 03:21:09 UTC 2018
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 at puremagic.com
Reporter: snarwin+bugzilla at 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.
--
More information about the Digitalmars-d-bugs
mailing list