std.conv.ConvException from double to uint64_t, but only locally in a large project
Steven Schveighoffer
schveiguy at gmail.com
Tue Aug 4 18:15:01 UTC 2020
On 8/4/20 1:36 PM, drathier wrote:
> I'm getting a crash when I'm converting a double to an uint64_t.
>
> ```
> std.conv.ConvException@/usr/local/opt/dmd/include/dlang/dmd/std/conv.d(2054):
> Value (1596) does not match any member value of enum '__c_ulonglong'
> ```
>
> I've narrowed down the code to this:
> ```
> static import std.conv;
> double thing = 42.0;
> std.conv.to!(uint64_t)(thing);
> ```
> which works just fine on https://run.dlang.io/ or in a single-file dmd
> invocation. When I compile and run it locally on my mac as part of a
> specific large app using dub, it always crashes like this.
>
> Where would I even start debugging this?
>
> DMD64 D Compiler v2.093.0
> DUB version 1.22.0, built on Jul 9 2020
>
So a common way to typedef something is to use enum:
enum uint64_t : ulong;
This gives you a new type that works pretty much just like ulong, but
will not implicitly convert from ulong.
However, std.conv.to is likely interpreting this as an enumeration type,
where it has to match one of the enum members. But since this isn't an
enumeration in the standard way, it fails (it has no members!)
This code reproduces the problem on run.dlang.io:
void main()
{
import core.stdc.config;
import std.conv;
auto x = 0.5;
auto y = x.to!__c_ulonglong;
}
Note the code in druntime which defines __c_ulonglong:
https://github.com/dlang/druntime/blob/0db2e65bba7cc319309bd32957763882870d5b03/src/core/stdc/config.d#L121
I'll file a bug.
-Steve
More information about the Digitalmars-d-learn
mailing list