Bug 23126 – wrong result of integer division in presence of assert within conditional operator expression

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2022-05-19T14:54:18Z
Last change time
2024-12-13T19:22:58Z
Keywords
wrong-code
Assigned to
No Owner
Creator
kdevel
Moved to GitHub: dmd#20108 →

Comments

Comment #0 by kdevel — 2022-05-19T14:54:18Z
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/[email protected]?page=3#post-ywralegnwtfiztrkzoak:40forum.dlang.org>
Comment #1 by snarwin+bugzilla — 2022-05-19T15:27:32Z
Reduced: --- int fun() { return 123456; } int divide(int dividend) { return dividend == 0 ? assert(0) : dividend / dividend; } void main() { fun(); int c = divide(1); assert(c == 1); } --- As of DMD 2.100.0, with -checkaction=context, this produces the following output: --- [email protected](14): 123456 != 1 ---
Comment #2 by kdevel — 2023-12-27T20:12:38Z
(In reply to kdevel from comment #0) > $ dmd div.d > $ ./div 1 2 > 1 / 2 = 2 dmd 2.100.2: bad $ ./div 1 2 1 / 2 = 2 dmd 2.101.1 and newer: good $ ./div 1 2 1 / 2 = 0 (In reply to Paul Backus from comment #1) > Reduced: > > As of DMD 2.100.0, with -checkaction=context, this produces the following > output: Works since dmd 2.101.1.
Comment #3 by siarhei.siamashka — 2023-12-27T20:31:26Z
(In reply to kdevel from comment #2) > Works since dmd 2.101.1. Does the DMD test suite have a test for it? To ensure that this problem does not re-appear again in the future.
Comment #4 by kdevel — 2023-12-30T11:46:29Z
(In reply to Siarhei Siamashka from comment #3) > (In reply to kdevel from comment #2) > > Works since dmd 2.101.1. > > Does the DMD test suite have a test for it? To ensure that this problem does > not re-appear again in the future. Simple question, not easy to answer. For now more than two hours I am trying to bisect dmd between v2.100.2 and v2.101.1 to no avail. I have stopped when git asked me to compile a commit of 2016 [e8ab5ca21f3010bb40509ca7aaf575440886795b] Merge pull request #1639 from WalterBright/rm-PostBlitType $ make -f posix.mak ../dmd/src/dmd -conf= -c -o- -Isrc -Iimport -Hfimport/core/sync/barrier.di src/core/sync/barrier.d make: ../dmd/src/dmd: Command not found make: *** [import/core/sync/barrier.di] Error 127 which fails. I could not git bisect skip this often enough in a reasonable amount of time. dmd 2.105.3 could not compile the old compiler: [...] (DC) COMMON The following operation failed: Name: common Sources: -> /tmp/k/dmd/src/dmd/common/bitfields.d -> /tmp/k/dmd/src/dmd/common/file.d -> /tmp/k/dmd/src/dmd/common/int128.d -> /tmp/k/dmd/src/dmd/common/outbuffer.d -> /tmp/k/dmd/src/dmd/common/string.d Targets: -> /tmp/k/dmd/generated/linux/release/64/common.o Command: [...]dmd2/linux/bin64/dmd -c -of/tmp/k/dmd/generated/linux/release/64/common.o -version=MARS -w -de -fPIC -m64 -J/tmp/k/dmd/generated/linux/release/64 -I/tmp/k/dmd/src -dip25 -g -color=on src/dmd/common/bitfields.d src/dmd/common/file.d src/dmd/common/int128.d src/dmd/common/outbuffer.d src/dmd/common/string.d ----------------------------------------------------------- Deprecation: `-dip25` no longer has any effect make[1]: *** [dmd] Error 1 Therefor I used dmd 2.098.1 as HOST_DMD
Comment #5 by b2.temp — 2023-12-30T12:24:30Z
> Simple question, not easy to answer. Use Vladimir `digger --bisect` it will be easier: https://github.com/CyberShadow/Digger
Comment #6 by kdevel — 2023-12-30T13:31:38Z
(In reply to kdevel from comment #4) > (In reply to Siarhei Siamashka from comment #3) > > (In reply to kdevel from comment #2) > > > Works since dmd 2.101.1. > > > > Does the DMD test suite have a test for it? To ensure that this problem does > > not re-appear again in the future. > Simple question, not easy to answer. It was fixed between v2.101.0 and v2.101.1 (named "bad" commit because of inverted roles): d836c8bcec16205644e3aacf15f1a1cf7d655dd5 is the first bad commit commit d836c8bcec16205644e3aacf15f1a1cf7d655dd5 Author: mhh <[email protected]> Date: Thu Dec 15 06:15:28 2022 +0000 Fix Issue 23549, 22587 - Lower certain noreturn expressions to a comma expression rather than a cast. This avoids a backend segfault. GDC already does this lowering so it may have to be made optional via a frontend parameter. :040000 040000 29998096bd165299cd08f15f32ab2dd047461bd8 70e6048af4184c4d4edb72add21fcbb0dac57957 M compiler
Comment #7 by robert.schadek — 2024-12-13T19:22:58Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20108 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB