[Issue 23126] New: wrong result of integer division in presence of assert within conditional operator expression

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu May 19 14:54:18 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=23126

          Issue ID: 23126
           Summary: wrong result of integer division in presence of assert
                    within conditional operator expression
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: kdevel at vogtner.de

Found the function divide in a forum post [1]

```div.d
import std.stdio;
import std.conv;

int divide(int dividend, int divisor)
{
    return divisor == 0
        ? assert(false, "You shall not divide by zero")
        : dividend / divisor;
}

int main (string [] args)
{
   int a = args[1].to!int;
   int b = args[2].to!int;
   int c = divide (a, b);
   writefln!"%d / %d = %d" (a, b, c);
   return 0;
}
```

$ dmd div.d
$ ./div 1 2
1 / 2 = 2

Wrong result since dmd 2.097.2, dmd 2.096.1 complains

   div.d(6): Error: `return` expression expected

dmd -O produces executable returning correct results.

expected:

$ gdc div.d -o div
$ ./div 1 2
1 / 2 = 0

[1]
<http://forum.dlang.org/thread/vfkwhtigkvkmhtzalwaf@forum.dlang.org?page=3#post-ywralegnwtfiztrkzoak:40forum.dlang.org>

--


More information about the Digitalmars-d-bugs mailing list