Patch against dmd r680, fixes VarDeclaration::semantic()
text/plain
1928
Comments
Comment #0 by rsinfu — 2010-09-14T11:03:49Z
If a compile-time constant of type T[N] is converted to a manifest constant of type T[], then the manifest constant gets a wrong type T[N] in static-if or template constraints.
In the following code, typeof(dynamic) is correct in static-assert, but wrong in static-if.
-------------------- test1.d
immutable int[3] statik = [ 1, 2, 3 ];
enum immutable(int)[] dynamic = statik;
static assert(is(typeof(dynamic) == immutable(int)[]));
static if (! is(typeof(dynamic) == immutable(int)[]))
{
static assert(0); // (7)
}
pragma(msg, "!! ", typeof(dynamic));
--------------------
% dmd -c -o- test1.d
!! immutable(int[3u])
test1.d(7): Error: static assert (0) is false
--------------------
Note the pragma output. Its type is the original static one.
The same happens in template constraint:
-------------------- test2.d
void main()
{
immutable char[3] statik = "abc";
enum string dynamic = statik;
foo(dynamic); // (5)
}
void foo(T)(T a) if (is(typeof(T) == string)) {}
--------------------
% dmd -c -o- test2.d
test2.d(5): Error: template test2.foo(T) if (is(typeof(T) == string)) does not match any function template declaration
test2.d(5): Error: template test2.foo(T) if (is(typeof(T) == string)) cannot deduce template function from argument types !()(string)
--------------------
It's ok if a constant is a scalar:
-------------------- test3.d
immutable int original = 10;
enum real converted = original;
static assert(is(typeof(converted) == real));
static if (! is(typeof(converted) == real)) static assert(0);
--------------------
% dmd -c -o- test3.d
% _
--------------------
Comment #1 by rsinfu — 2010-09-22T10:11:55Z
Created attachment 763
Patch against dmd r680, fixes VarDeclaration::semantic()
The problem was that VarDeclaration::semantic() doesn't take it into account when the variable is a manifest constant or the initializer is non-scalar.