Bug 11247 – Error: typeof(i).sizeof is used as a type

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-10-13T13:15:00Z
Last change time
2013-11-14T08:54:53Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
monarchdodra

Comments

Comment #0 by monarchdodra — 2013-10-13T13:15:22Z
Not sure what to make of this: //---- void main() { int i; //This is OK: enum N = typeof(i).sizeof; byte[N] a; //This is also OK: byte[int.sizeof] b; //This errors out byte[typeof(i).sizeof] b; } //---- Error: typeof(i).sizeof is used as a type //---- I *think* the compiler thinks I'm declaring an AA (When I want a SA) ? FYIW, in 2.062 and prior, the error message is: Error: cannot resolve .property for typeof(i).sizeof
Comment #1 by maxim — 2013-10-13T14:13:01Z
(In reply to comment #0) > Not sure what to make of this: > > //---- > void main() > { > int i; > > //This is OK: > enum N = typeof(i).sizeof; > byte[N] a; > > //This is also OK: > byte[int.sizeof] b; > > //This errors out > byte[typeof(i).sizeof] b; > } > //---- > Error: typeof(i).sizeof is used as a type > //---- > > I *think* the compiler thinks I'm declaring an AA (When I want a SA) ? Of course you are declaring AA here because D does not support variable arrays like C. Sizeof property in this context is treated as type, not expression - that's why sizeof yileds ulong, not 4. If you wanted to have static array here, then this is invalid report. However, if you want this to compile as AA array, than this is really rejects-valid. On the other hand, this is useless - in such cases Foo[bar.sizeof] always yileds Foo[ulong].
Comment #2 by monarchdodra — 2013-10-13T14:20:47Z
(In reply to comment #1) > (In reply to comment #0) > > Not sure what to make of this: > > > > //---- > > void main() > > { > > int i; > > > > //This is OK: > > enum N = typeof(i).sizeof; > > byte[N] a; > > > > //This is also OK: > > byte[int.sizeof] b; > > > > //This errors out > > byte[typeof(i).sizeof] b; > > } > > //---- > > Error: typeof(i).sizeof is used as a type > > //---- > > > > I *think* the compiler thinks I'm declaring an AA (When I want a SA) ? > > Of course you are declaring AA here because D does not support variable arrays > like C. sizeof is a compile time constant though... so it's not a variable size array. EG: byte[int.sizeof] b; static sized array. > Sizeof property in this context is treated as type, not expression Why? byte[int.sizeof] b; Here, it's treated correctly. it's the "typeof" that is throwing it off. > that's why sizeof yileds ulong, not 4. sizeof can yield a *type*? That's news to me. > If you wanted to have static array here, then this is invalid report. However, > if you want this to compile as AA array, than this is really rejects-valid. On > the other hand, this is useless - in such cases Foo[bar.sizeof] always yileds > Foo[ulong]. I *really* don't understand how: byte[int.sizeof] b; byte[typeof(i).sizeof] c; Could have two different behaviors...
Comment #3 by maxim — 2013-10-13T14:33:57Z
(In reply to comment #2) > (In reply to comment #1) > > (In reply to comment #0) > > > Not sure what to make of this: > > > > > > //---- > > > void main() > > > { > > > int i; > > > > > > //This is OK: > > > enum N = typeof(i).sizeof; > > > byte[N] a; > > > > > > //This is also OK: > > > byte[int.sizeof] b; > > > > > > //This errors out > > > byte[typeof(i).sizeof] b; > > > } > > > //---- > > > Error: typeof(i).sizeof is used as a type > > > //---- > > > > > > I *think* the compiler thinks I'm declaring an AA (When I want a SA) ? > > > > Of course you are declaring AA here because D does not support variable arrays > > like C. > > sizeof is a compile time constant though... so it's not a variable size array. > EG: > byte[int.sizeof] b; > static sized array. > > > Sizeof property in this context is treated as type, not expression > > Why? > byte[int.sizeof] b; > Here, it's treated correctly. it's the "typeof" that is throwing it off. It looks like it is undocumented whether sizeof should yield ulong or 4 here. > > > that's why sizeof yileds ulong, not 4. > > sizeof can yield a *type*? That's news to me. > > > If you wanted to have static array here, then this is invalid report. However, > > if you want this to compile as AA array, than this is really rejects-valid. On > > the other hand, this is useless - in such cases Foo[bar.sizeof] always yileds > > Foo[ulong]. > > I *really* don't understand how: > byte[int.sizeof] b; > byte[typeof(i).sizeof] c; > Could have two different behaviors... Yes, they should be same.
Comment #4 by yebblies — 2013-11-14T01:18:39Z
(In reply to comment #3) > > It looks like it is undocumented whether sizeof should yield ulong or 4 here. > ... this is just completely wrong. sizeof always yields an integer value. https://github.com/D-Programming-Language/dmd/pull/2761
Comment #5 by github-bugzilla — 2013-11-14T08:54:22Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/2a382f7de4bee34af6e00a2830c4aa19e31aa131 Fix Issue 11247 - Error: typeof(i).sizeof is used as a type https://github.com/D-Programming-Language/dmd/commit/689397009505979a796b439b07a700605ddbd579 Merge pull request #2761 from yebblies/issue11247 Issue 11247 - Error: typeof(i).sizeof is used as a type