"Error: `TypeInfo` cannot be used with -betterC" on a CTFE function

Liam McGillivray yoshi.pit.link.mario at gmail.com
Sun Apr 7 06:46:39 UTC 2024


I'm making a modification to a D binding for a C library. I made 
a CTFE function which takes a function declaration with one or 
more `const(char)*` or `char*` parameters and makes an overload 
that accepts D strings. While this function is only used during 
compile time, unfortunately, I have found that it results in the 
library no longer supporting `-betterC`. The compiler gives the 
following error.

```
/usr/include/dlang/dmd/core/lifetime.d(2760,42): Error: 
`TypeInfo` cannot be used with -betterC
/usr/include/dlang/dmd/std/utf.d(1556,24):        instantiated 
from here: `_d_newclassT!(UTFException)`
/usr/include/dlang/dmd/std/utf.d(1563,32):        instantiated 
from here: `exception!(const(char)[])`
/usr/include/dlang/dmd/std/utf.d(1186,54):        instantiated 
from here: `decodeImpl!(true, Flag.no, const(char)[])`
/usr/include/dlang/dmd/std/range/primitives.d(2551,18):        
instantiated from here: `decode!(Flag.no, const(char)[])`
/usr/include/dlang/dmd/std/range/primitives.d(178,40):        
instantiated from here: `front!char`
Error /usr/bin/dmd failed with exit code 1.
```

I don't know what part of the function uses `TypeInfo` (as I 
don't really understand what that is), but I know that it is this 
CTFE function causing the error, because commenting out the 
mixins that use it results in the `-betterC` program compiling 
properly. Here is the CTFE function I wrote:

```
string MakeStringOverload(alias func)() {
     string def = ReturnType!func.stringof ~" "~ 
__traits(identifier, func) ~ "(";

     auto paramNames = ParameterIdentifierTuple!func;

     import std.algorithm.searching;
     foreach(i, paramType; Parameters!func) {
         if (paramType.stringof.canFind("char")) def ~= "ref 
string"; // Made a reference so that D string literals know to 
use the base version of the function and not this one.
         else def ~= paramType.stringof;
         def ~= " "~paramNames[i] ~ ", ";
     }
     def.length -= 2;
     def ~= ") { ";
     if (ReturnType!func.stringof != "void") def ~= "return ";
     def ~= __traits(identifier, func) ~ "(";
     foreach(i, argument; paramNames) {
         if (Parameters!func[i].stringof.canFind("char")) def ~= 
"cast(char*)";
         def ~= argument ~ ", ";
     }
     def.length -= 2;
     def ~= "); }";

     return def;
}
```

In the module where the base versions of functions are declared, 
I do the following to generate overloads:
```
mixin(MakeStringOverload!SetWindowTitle);
mixin(MakeStringOverload!LoadShader);
mixin(MakeStringOverload!LoadShaderFromMemory);
mixin(MakeStringOverload!GetShaderLocation);
```

So what part of my function is using "TypeInfo"? Can I somehow 
label my function as compile-time only so that it stops 
complaining? If not what else can I do to get it to stop 
complaining and compile with `betterC`?


More information about the Digitalmars-d-learn mailing list