D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5188 - alias this and compare expression generates wrong code
Summary: alias this and compare expression generates wrong code
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: patch, wrong-code
Depends on:
Blocks:
 
Reported: 2010-11-08 06:17 UTC by Kenji Hara
Modified: 2011-08-11 01:28 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kenji Hara 2010-11-08 06:17:11 UTC
// 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 kennytm 2011-07-22 03:13:19 UTC
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 Kenji Hara 2011-07-26 10:39:58 UTC
(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