www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - DMD different compiler behaviour on Linux and Windows

reply Zans <dlafotok inbox.lv> writes:
import std.stdio;

void main()
{
     char[] mychars;
     mychars ~= 'a';
     long index = 0L;
     writeln(mychars[index]);
}

Why would the code above compile perfectly on Linux (Ubuntu 
16.04), however it would produce the following error on Windows 
10:

source\app.d(8,21): Error: cannot implicitly convert expression 
index of type long to uint

On both operating systems DMD version is 2.085.0.
Apr 25 2019
next sibling parent Patrick Schluter <Patrick.Schluter bbox.fr> writes:
On Thursday, 25 April 2019 at 20:18:28 UTC, Zans wrote:
 import std.stdio;

 void main()
 {
     char[] mychars;
     mychars ~= 'a';
     long index = 0L;
     writeln(mychars[index]);
 }

 Why would the code above compile perfectly on Linux (Ubuntu 
 16.04), however it would produce the following error on Windows 
 10:

 source\app.d(8,21): Error: cannot implicitly convert expression 
 index of type long to uint

 On both operating systems DMD version is 2.085.0.
The issue here is not Windows vs Linux but 32 bits vs 64 bits. On 32 bits architectures size_t is defined as uint, long being 64 bits long, conversion from long to uint is a truncating cast which are not allowed implicitely in D. It is unfortunate that the D compiler on Windows is still delivered by default as a 32 bits binary and generating 32 bits code. I think the next release will start to deliver the compiler as 64 bits binary and generating 64 bits code.
Apr 25 2019
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Thursday, 25 April 2019 at 20:18:28 UTC, Zans wrote:
 import std.stdio;

 void main()
 {
     char[] mychars;
     mychars ~= 'a';
     long index = 0L;
     writeln(mychars[index]);
 }

 Why would the code above compile perfectly on Linux (Ubuntu 
 16.04), however it would produce the following error on Windows 
 10:

 source\app.d(8,21): Error: cannot implicitly convert expression 
 index of type long to uint

 On both operating systems DMD version is 2.085.0.
DMD defaults to 64-bit output on 64-Bit Linux but always to 32-bit output on Windows, If you compile with -m32 on Windows the error goes away. Reasons:: * Array indices are default typed as size_t, which is uint in 32-bit and ulong in 64. long is not implicitly convertible to uint. * On Linux, both 64- and 32-bit builds of DMD are available and the output for each defaults to the same. Only the 32-bit build is distributed on Windows. * Historically, compiling 64-bit binaries on Windows required a separate installation of the Microsoft Build Tools ( or Visual Studio) and/or the Windows SDK. If 64-bit output were the default, DMD would not work out of the box. Recently, DMD has been shipping with the lld linker and some MinGW-based Windows libraries so that the additional installation is not required and 64-bit compiles can work out of the box, but it’s still considered experimental. When out-of-the-box 64-bit compilation is solid and 64-bit builds are distributed on Windows, the default behavior should be the same as on Linux.
Apr 25 2019
parent reply Ron Tarrant <rontarrant gmail.com> writes:
On Thursday, 25 April 2019 at 20:38:31 UTC, Mike Parker wrote:
 If you compile with -m32 on Windows the error goes away.
Not trying to be a <clever donkey> but it also works with -m64 on Windows.
Apr 26 2019
parent Mike Parker <aldacron gmail.com> writes:
On Friday, 26 April 2019 at 15:48:51 UTC, Ron Tarrant wrote:
 On Thursday, 25 April 2019 at 20:38:31 UTC, Mike Parker wrote:
 If you compile with -m32 on Windows the error goes away.
Not trying to be a <clever donkey> but it also works with -m64 on Windows.
Yes, thanks. That's a typo. -m32, where size_t is uint, is the default. In -m64, size_t is ulong.
Apr 26 2019