www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - 'fopen64 cannot be interpreted at compile time' for __gshared File

reply crimaniak <crimaniak gmail.com> writes:
Simple File test:

     void main()
     {
         import std.stdio;

         File f = File("test.txt", "w");

         f.writeln("hello");
     }

All works as expected.

Now let's add __gshared:

     void main()
     {
         import std.stdio;

         __gshared File f = File("test.txt", "w");

         f.writeln("hello");
     }

Now we have:

/usr/include/dmd/phobos/std/stdio.d(3797): Error: fopen64 cannot 
be interpreted at compile time, because it has no available 
source code
/usr/include/dmd/phobos/std/stdio.d(3804):        called from 
here: fopenImpl(namez.ptr(), modez.ptr())
/usr/include/dmd/phobos/std/stdio.d(404):        called from 
here: fopen(name, stdioOpenmode)
/usr/include/dmd/phobos/std/stdio.d(404):        called from 
here: errnoEnforce(fopen(name, stdioOpenmode), delegate string() 
=> text("Cannot open file `", name, "' in mode `", stdioOpenmode, 
"'"))
/usr/include/dmd/phobos/std/stdio.d(404):        called from 
here: this.this(errnoEnforce(fopen(name, stdioOpenmode), delegate 
string() => text("Cannot open file `", name, "' in mode `", 
stdioOpenmode, "'")), name, 1u, false)
file.d(5):        called from here: ((File __slFile648 = 
File(null, null);) , __slFile648).this("test.txt", "w")

Oooops! Who can explain this error message to me?

DMD64 D Compiler v2.073.0
Mar 25 2017
parent reply NotSpooky <asistentedeprincipios2 gmail.com> writes:
On Saturday, 25 March 2017 at 15:52:15 UTC, crimaniak wrote:
 Simple File test:

     void main()
     {
         import std.stdio;

         File f = File("test.txt", "w");

         f.writeln("hello");
     }

 All works as expected.

 Now let's add __gshared:

     void main()
     {
         import std.stdio;

         __gshared File f = File("test.txt", "w");

         f.writeln("hello");
     }

 Now we have:

 /usr/include/dmd/phobos/std/stdio.d(3797): Error: fopen64 
 cannot be interpreted at compile time, because it has no 
 available source code
 /usr/include/dmd/phobos/std/stdio.d(3804):        called from 
 here: fopenImpl(namez.ptr(), modez.ptr())
 /usr/include/dmd/phobos/std/stdio.d(404):        called from 
 here: fopen(name, stdioOpenmode)
 /usr/include/dmd/phobos/std/stdio.d(404):        called from 
 here: errnoEnforce(fopen(name, stdioOpenmode), delegate 
 string() => text("Cannot open file `", name, "' in mode `", 
 stdioOpenmode, "'"))
 /usr/include/dmd/phobos/std/stdio.d(404):        called from 
 here: this.this(errnoEnforce(fopen(name, stdioOpenmode), 
 delegate string() => text("Cannot open file `", name, "' in 
 mode `", stdioOpenmode, "'")), name, 1u, false)
 file.d(5):        called from here: ((File __slFile648 = 
 File(null, null);) , __slFile648).this("test.txt", "w")

 Oooops! Who can explain this error message to me?

 DMD64 D Compiler v2.073.0
__gshared implies static, that is, known at compile time (ct), that means the File constructor should be done at ct and thus open the file. Compile time function execution doesn't allow IO operations, however you can use https://dlang.org/spec/expression.html#ImportExpression if you want to read a file at compile time.
Mar 25 2017
parent reply crimaniak <crimaniak gmail.com> writes:
On Saturday, 25 March 2017 at 16:08:49 UTC, NotSpooky wrote:

 __gshared implies static,...
Thanks! Confusing for me moment.
Mar 25 2017
parent StarGrazer <Stary Night.com> writes:
On Saturday, 25 March 2017 at 16:46:00 UTC, crimaniak wrote:
 On Saturday, 25 March 2017 at 16:08:49 UTC, NotSpooky wrote:

 __gshared implies static,...
Thanks! Confusing for me moment.
try this: import std.stdio; void main() { import std.stdio; __gshared File f; f = File("test.txt", "w"); f.writeln("hello"); }
Mar 25 2017