Comment #0 by bearophile_hugs — 2012-06-27T09:39:16Z
This is wrong D2 code (because currently D doesn't have variable length arrays, and n is a run-time value):
import std.stdio;
void main() {
uint n = 1;
uint[n + 1] foo;
writeln(foo);
}
I think it prints too many error messages (DMD 2.060alpha):
test.d(4): Error: variable n cannot be read at compile time
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: variable n cannot be read at compile time
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: variable n cannot be read at compile time
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: variable n cannot be read at compile time
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: variable n cannot be read at compile time
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: Integer constant expression expected instead of n + 1u
test.d(4): Error: Integer constant expression expected instead of n + 1u
Comment #1 by bearophile_hugs — 2012-07-08T15:04:14Z
A related case shown in D.learn (reduced):
import std.stdio: writeln;
void main() {
immutable int[] A = [1];
int[A.length] B;
writeln(A);
writeln(B);
}
DMD 2.060alpha:
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
temp.d(3): Error: Integer constant expression expected instead of (A = [1]).length
Comment #4 by bearophile_hugs — 2012-07-25T07:24:39Z
With this program:
import std.stdio;
void main() {
uint n = 1;
uint[n + 1] foo;
writeln(foo);
}
DMD 2.060beta prints:
temp.d(4): Error: variable n cannot be read at compile time
temp.d(4): Error: Integer constant expression expected instead of n + 1u
temp.d(5): Error: template std.stdio.writeln does not match any function template declaration
...\dmd2\src\phobos\std\stdio.d(1578): Error: template std.stdio.writeln(T...) cannot deduce template function from argument types !()(_error_)
That I think is now acceptable.
Probably the errors of the second program too are improved:
import std.stdio: writeln;
void main() {
immutable int[] A = [1];
int[A.length] B;
writeln(A);
writeln(B);
}
but I can't verify the error messages because now it compiles and runs with output:
[1]
[0]
So I have added a dependency to Issue 8400. Until Issue 8400 is undone I can't verify this Issue 8312 is truly fixed.
Comment #5 by k.hara.pg — 2012-07-25T08:02:42Z
(In reply to comment #4)
> Probably the errors of the second program too are improved:
>
> import std.stdio: writeln;
> void main() {
> immutable int[] A = [1];
> int[A.length] B;
> writeln(A);
> writeln(B);
> }
>
> but I can't verify the error messages because now it compiles and runs with
> output:
>
> [1]
> [0]
>
> So I have added a dependency to Issue 8400. Until Issue 8400 is undone I can't
> verify this Issue 8312 is truly fixed.
No, this is expected behavior. With the declaration `int[A.lehgth] B;`, A.length is interpreted to integer constant expression 1, then the type of B is compiled to int[1], it is _static_ array has 1 length, and the B's elements are initialized with int.init.
Comment #6 by bearophile_hugs — 2012-07-25T08:27:31Z
(In reply to comment #5)
> No, this is expected behavior. With the declaration `int[A.lehgth] B;`,
> A.length is interpreted to integer constant expression 1, then the type of B is
> compiled to int[1], it is _static_ array has 1 length, and the B's elements are
> initialized with int.init.
If you think so then close down this bug (and maybe Issue 8400 too).
Comment #7 by k.hara.pg — 2012-07-25T08:32:46Z
(In reply to comment #6)
> (In reply to comment #5)
>
> > No, this is expected behavior. With the declaration `int[A.lehgth] B;`,
> > A.length is interpreted to integer constant expression 1, then the type of B is
> > compiled to int[1], it is _static_ array has 1 length, and the B's elements are
> > initialized with int.init.
>
> If you think so then close down this bug (and maybe Issue 8400 too).
OK, closed.