Bug 5188 – alias this and compare expression generates wrong code

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-11-08T06:17:00Z
Last change time
2011-08-11T01:28:57Z
Keywords
patch, wrong-code
Assigned to
nobody
Creator
k.hara.pg

Comments

Comment #0 by k.hara.pg — 2010-11-08T06:17:11Z
// bug.d struct S { int v = 10; alias v this; } void main() { S s; assert(s <= 20); // fail!? } // use ddbg 0.11 beta bug.d:6 void main() 00402010: c8040000 enter 0x4, 0x0 bug.d:8 S s; 00402014: a180504100 mov eax, [0x415080] 00402019: 8945fc mov [ebp-0x4], eax bug.d:9 assert(s <= 20); 0040201c: 837dfc14 cmp dword [ebp-0x4], 0x14 00402020: b901000000 mov ecx, 0x1 00402025: 7e02 jle 0x402029 _Dmain bug.d:9 // jump to 0x402029 00402027: 31c9 xor ecx, ecx // not run 00402029: 31d2 xor edx, edx // edx = 0 0040202b: 3bca cmp ecx, edx // ecx==1, edx==0 0040202d: 7e0a jle 0x402039 _Dmain bug.d:9 // fail 0040202f: b809000000 mov eax, 0x9 // eax = 9 00402034: e807000000 call 0x402040 bug.__assert // assert! 00402039: 31c0 xor eax, eax bug.d:10 } 0040203b: c9 leave 0040203c: c3 ret
Comment #1 by kennytm — 2011-07-22T03:13:19Z
If we set a break point at FuncDeclaration::toObjFile at glue.c and print 'fbody->toChars()', we'll see that the function void main() { S s; assert(s <= 20); // fail!? } has been semantic into int main() { S s = _D1x1S6__initZ; assert(cast(int)(s.v <= 20) <= 0); // ^^ return 0; } The extra '<= 0' causes the assertion to fail. Apparently DMD thinks there is an opCmp/opEquals. This also applies for '!=': assert(s != 14); which is semantic into assert(!(s.v != 14));
Comment #2 by k.hara.pg — 2011-07-26T10:39:58Z
(In reply to comment #1) Thanks for your explaining. Today I can create patch for this bug. https://github.com/D-Programming-Language/dmd/pull/275
Comment #3 by bugzilla — 2011-08-11T01:28:57Z