www.digitalmars.com         C & C++   DMDScript  

D - Possible type-system bug

reply Robin Håkanson <Robin_member pathlink.com> writes:
I'm not sure that the following really is a bug, but i definitly think so.

The code below does not compile.
------------------
int main()
{
int a = 2;
int[5] b;
int[] c = new int[b[a]];	

return 0;
}
----------------
t.d(5): need size of rightmost array, not type [a]b


But if i insert some paranthesis around the b[a] argument i give for the
allocation size of c, it compiles perfectly. 
----------------
int main()
{
int a = 2;
int[5] b;
int[] c = new int[(b[a])];	

return 0;
}



It seemse like the D-typechecker dosen't evaluate b[a] to an int in the first
case, perhaps that's completly correct, but i can't see why...

/Robin
Mar 01 2004
next sibling parent reply C <dont respond.com> writes:
Hmm, it seems the ( ) solve many problems ( bugs? ) , like the opCall() =

for the Streams Mik was working on.  Just in time for 1.0 :D.

C

On Mon, 1 Mar 2004 18:21:17 +0000 (UTC), Robin H=E5kanson =

<Robin_member pathlink.com> wrote:

 I'm not sure that the following really is a bug, but i definitly think=
=
 so.

 The code below does not compile.
 ------------------
 int main()
 {
 int a =3D 2;
 int[5] b;
 int[] c =3D new int[b[a]];	=
 return 0;
 }
 ----------------
 t.d(5): need size of rightmost array, not type [a]b


 But if i insert some paranthesis around the b[a] argument i give for t=
he
 allocation size of c, it compiles perfectly.
 ----------------
 int main()
 {
 int a =3D 2;
 int[5] b;
 int[] c =3D new int[(b[a])];	=
 return 0;
 }



 It seemse like the D-typechecker dosen't evaluate b[a] to an int in th=
e =
 first
 case, perhaps that's completly correct, but i can't see why...

 /Robin
-- = Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Mar 01 2004
parent larry cowan <larry_member pathlink.com> writes:
Can't the compiler know in this case that b (thus b[2]) is uninitialized, and be
rejecting the "new int[really=0]" as undefined?  Then when parens wrap it the
compiler knows it will have a value (type int)...  Well, the message is a bit
strange, but is probably trying to say "type member of array of ints", whereas
the () wrapper will force evaluation to some int value.  Convoluted?    

<snip>
 The code below does not compile.
 ------------------
 int main()
 {
 int a = 2;
 int[5] b;
 int[] c = new int[b[a]];  // ok if b[a] is wrapped as (b[a])
 return 0;
 }
 ----------------
 t.d(5): need size of rightmost array, not type [a]b
</snip>
Mar 01 2004
prev sibling parent "Ben Hinkle" <bhinkle4 juno.com> writes:
Definitely looks like a bug. In parse.c the parsing of "new" looks like
it confuses int[b[a]] with an associative array - possibly by thinking
it was parsing a declaration in parseBasicType2... I can't really tell.
You can replace "b[a]" with any expression like 0+b[a] or b[a]+0
or as you found (b[a]) and it'll get out the the jam.

-Ben
Mar 01 2004