www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - cast to pure function stop work after upgrade

reply baby_tiger <tiger2021y gmail.com> writes:
this used work for me, after upgrade I get this error.  how to 
fix it ?


     import std.traits;
             enum LogLevel : ubyte {
                     INFO = 0,
                     WARN,
                     ERROR,
                     FATAL,
             }


     extern (C) string VFORMAT(LogLevel level, string file, size_t 
line, char[] tmp, bool line_break, string tag, string fmt, ...) 
 nogc nothrow {
             return null;
     }

     template asPure(string P, alias Fn) if 
(isFunctionPointer!(typeof(&Fn))) {
             enum N = __traits(identifier, Fn);
             enum string asPure = "private alias " ~ N ~ "_PURE = 
" ~ typeof(&Fn).stringof ~ " pure;\n" ~ "__gshared immutable " ~ 
N ~ "_PURE " ~ P ~"= cast(" ~ N ~ "_PURE) &" ~ N ~ " ;" ;
     }
     enum xx = asPure!("VFORMATP", VFORMAT);
     mixin(xx);

void main(){

}


      reinterpreting cast from `nothrow  nogc extern (C) 
string(LogLevel level, string file, ulong line, char[] tmp, bool 
line_break, string tag, string fmt, ...)*` to `pure nothrow  nogc 
extern (C) string(LogLevel, string, ulong, char[], bool, string, 
string, ...)*` is not supported in CTFE
Jun 14 2021
next sibling parent reply Tejas <notrealemail gmail.com> writes:
On Monday, 14 June 2021 at 13:31:51 UTC, baby_tiger wrote:
 this used work for me, after upgrade I get this error.  how to 
 fix it ?


     import std.traits;
             enum LogLevel : ubyte {
                     INFO = 0,
                     WARN,
                     ERROR,
                     FATAL,
             }


     extern (C) string VFORMAT(LogLevel level, string file, 
 size_t line, char[] tmp, bool line_break, string tag, string 
 fmt, ...)  nogc nothrow {
             return null;
     }

     template asPure(string P, alias Fn) if 
 (isFunctionPointer!(typeof(&Fn))) {
             enum N = __traits(identifier, Fn);
             enum string asPure = "private alias " ~ N ~ "_PURE 
 = " ~ typeof(&Fn).stringof ~ " pure;\n" ~ "__gshared immutable 
 " ~ N ~ "_PURE " ~ P ~"= cast(" ~ N ~ "_PURE) &" ~ N ~ " ;" ;
     }
     enum xx = asPure!("VFORMATP", VFORMAT);
     mixin(xx);

 void main(){

 }


      reinterpreting cast from `nothrow  nogc extern (C) 
 string(LogLevel level, string file, ulong line, char[] tmp, 
 bool line_break, string tag, string fmt, ...)*` to `pure 
 nothrow  nogc extern (C) string(LogLevel, string, ulong, 
 char[], bool, string, string, ...)*` is not supported in CTFE
It seems to not work at runtime either. Maybe they've made this behaviour illegal now? Hopefully someone answers. ```d import std.traits; import core.stdc.stdarg; enum LogLevel : ubyte { INFO = 0, WARN, ERROR, FATAL, } extern (C) string VFORMAT(LogLevel level, string file, size_t line, char[] tmp, bool line_break, string tag, string fmt, ...) nogc nothrow { return null; } extern (C) string function(LogLevel level, string file, size_t line, char[] tmp, bool line_break, string tag, string fmt, ...) nogc nothrow pure fp; void main(){ fp = &VFORMAT; //fails } ```
Aug 02 2021
parent reply vit <vit vit.vit> writes:
On Monday, 2 August 2021 at 11:28:46 UTC, Tejas wrote:
 On Monday, 14 June 2021 at 13:31:51 UTC, baby_tiger wrote:
 [...]
It seems to not work at runtime either. Maybe they've made this behaviour illegal now? Hopefully someone answers. ```d import std.traits; import core.stdc.stdarg; enum LogLevel : ubyte { INFO = 0, WARN, ERROR, FATAL, } extern (C) string VFORMAT(LogLevel level, string file, size_t line, char[] tmp, bool line_break, string tag, string fmt, ...) nogc nothrow { return null; } extern (C) string function(LogLevel level, string file, size_t line, char[] tmp, bool line_break, string tag, string fmt, ...) nogc nothrow pure fp; void main(){ fp = &VFORMAT; //fails } ```
Try this: ```d void main(){ fp = cast(typeof(fp))&VFORMAT; //fails } ```
Aug 02 2021
parent Tejas <notrealemail gmail.com> writes:
On Monday, 2 August 2021 at 15:10:06 UTC, vit wrote:
 On Monday, 2 August 2021 at 11:28:46 UTC, Tejas wrote:
 [...]
Try this: ```d void main(){ fp = cast(typeof(fp))&VFORMAT; //fails } ```
Agh, stupid me. You the man!!
Aug 02 2021
prev sibling parent vit <vit vit.vit> writes:
On Monday, 14 June 2021 at 13:31:51 UTC, baby_tiger wrote:
 this used work for me, after upgrade I get this error.  how to 
 fix it ?


     import std.traits;
             enum LogLevel : ubyte {
                     INFO = 0,
                     WARN,
                     ERROR,
                     FATAL,
             }


     extern (C) string VFORMAT(LogLevel level, string file, 
 size_t line, char[] tmp, bool line_break, string tag, string 
 fmt, ...)  nogc nothrow {
             return null;
     }

     template asPure(string P, alias Fn) if 
 (isFunctionPointer!(typeof(&Fn))) {
             enum N = __traits(identifier, Fn);
             enum string asPure = "private alias " ~ N ~ "_PURE 
 = " ~ typeof(&Fn).stringof ~ " pure;\n" ~ "__gshared immutable 
 " ~ N ~ "_PURE " ~ P ~"= cast(" ~ N ~ "_PURE) &" ~ N ~ " ;" ;
     }
     enum xx = asPure!("VFORMATP", VFORMAT);
     mixin(xx);

 void main(){

 }


      reinterpreting cast from `nothrow  nogc extern (C) 
 string(LogLevel level, string file, ulong line, char[] tmp, 
 bool line_break, string tag, string fmt, ...)*` to `pure 
 nothrow  nogc extern (C) string(LogLevel, string, ulong, 
 char[], bool, string, string, ...)*` is not supported in CTFE
```d int foo(int x) safe{ return x; } /* This is valid. safe CTFE cast, systemFoo has less restrictive function type. */ auto systemFoo = cast(int function(int) system)&foo; /* ERROR UNSAFE CTFE cast, pureFoo has more restrictive function type. */ //auto pureFoo = cast(void function(int)pure)&foo; int function(int)pure pureFoo; shared static this(){ /* This is valid. UNSAFE RUNTIME cast. */ pureFoo = cast(int function(int)pure)&foo; } void main(){ assert(pureFoo(42) == 42); } ```
Aug 02 2021