Bug 20278 – ICE: calling std.math.sqrt with int cast to double
Status
RESOLVED
Resolution
WORKSFORME
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2019-10-07T18:13:45Z
Last change time
2020-06-21T02:47:34Z
Keywords
ice
Assigned to
No Owner
Creator
hsteoh
Comments
Comment #0 by hsteoh — 2019-10-07T18:13:45Z
Code:
------
struct S {
int x,y;
double f() {
import std.math : sqrt;
return sqrt(cast(double)x*x + y);
}
}
------
Compiler output:
------
dmd: /usr/src/d/dmd/src/dmd/backend/cgcod.d:2489: Assertion `cast(int)(*e).Ecomsub <= cast(int)(*e).Ecount' failed.
Aborted
------
Expected behaviour: the compiler should either emit an error indicating why the code doesn't compile, or else compile the code successfully. It should not crash with an assertion.
Submitting this as critical because it's an ICE.
Comment #1 by kozzi11 — 2019-10-07T18:33:26Z
Can you please post complete example with compile args and dmd version? I am unable to reproduce it
Comment #2 by hsteoh — 2019-10-07T18:44:15Z
dmd --version:
------
DMD64 D Compiler v2.088.0-247-gfafa982ae
Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved written by Walter Bright
------
Compile command: dmd test.d
Output:
------
dmd: /usr/src/d/dmd/src/dmd/backend/cgcod.d:2489: Assertion `cast(int)(*e).Ecomsub <= cast(int)(*e).Ecount' failed.
Aborted
------
Comment #3 by hsteoh — 2019-10-07T18:44:53Z
The code quoted above is the entire content of test.d, by the way.
Comment #4 by hsteoh — 2019-10-07T18:45:53Z
P.P.S. This is dmd from git master, btw. It could be a bug recently introduced, perhaps?
Comment #5 by hsteoh — 2019-10-07T18:48:53Z
Just updated to latest git master:
dmd --version:
------
DMD64 D Compiler v2.088.1-beta.1-265-g5f168a3b7
Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved written by Walter Bright
------
Same error.
Comment #6 by hsteoh — 2019-10-07T18:53:59Z
Writing the return statement as:
------
return sqrt(cast(double)(x*x + y));
------
makes the problem go away. Parenthesizing (x*x) also makes the problem go away. Somehow something goes wrong when the cast operator is applied only to the first element of the product x*x. The +y appears to be necessary, since removing that also makes the error go away.
Comment #7 by kozzi11 — 2019-10-07T19:05:17Z
(In reply to hsteoh from comment #4)
> P.P.S. This is dmd from git master, btw. It could be a bug recently
> introduced, perhaps?
my latest dmd stable works ok
DMD64 D Compiler v2.088.0-dirty
Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved written by Walter Bright
So it is probably only issue with latest git code.
(In reply to hsteoh from comment #0)
> Submitting this as critical because it's an ICE.
That's because you have a custom build of DMD with enabled assertions (thumbs up for that). The generated code seems to be fine, https://run.dlang.io/is/gO3wJh yields the expected result with DMD nightly.
Comment #10 by kozzi11 — 2019-10-07T19:15:26Z
dmd-nightly is old:
DMD64 D Compiler v2.088.0-beta.1-master-ede5969
Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved written by Walter Bright
Comment #11 by kozzi11 — 2019-10-07T19:29:01Z
I have try it with latest dmd git and works ok too
Comment #12 by hsteoh — 2019-10-07T21:13:44Z
@kinke: so the assert is the problem??
Comment #13 by hsteoh — 2019-10-07T21:15:05Z
I didn't deliberately build dmd with asserts, btw. All I did was to run `make -f posix.mak` from a script.
Comment #14 by kinke — 2019-10-07T21:46:26Z
(In reply to hsteoh from comment #12)
> @kinke: so the assert is the problem??
Perhaps it's overzealous, but the generated code might also just happen to accidentally work.