First Draft: Making printf @safe

Tim tim.dlang at t-online.de
Sat Aug 3 15:28:35 UTC 2024


On Saturday, 3 August 2024 at 11:27:19 UTC, Nick Treleaven wrote:
> This DIP doesn't only apply to C's printf, it (potentially) 
> applies to other pragma(printf) functions. AIUI a major 
> motivation for it is dmd's frontend which has a lot of calls to 
> its own extern(C) functions with printf formatting strings and 
> C varargs. (I'd be interested in seeing an alternative proposal 
> too on how to make those calls safe without this DIP, whilst 
> still compiling with GDC & LDC).

One alternative would be a wrapper around printf, which checks 
the format string at compile time and modifies it and other 
parameters, so they match. It could look something like this:

```D
void printfWrapper(string fmt, P...)(P params) @trusted
{
     // ...
}

void main() @safe
{
     string s = "World";
     printfWrapper!"Hello %s\n"(s);
}
```

An advantage would be, that it could be used immediately in DMD. 
A new language feature could only be used after the bootstrap 
compiler is updated to this version, too.


More information about the dip.development mailing list