Summary: Static typing for format strings, when possible
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: diagnostic
          Severity: enhancement
          Priority: P2
         Component: DMD

--- Comment #0 from 2010-07-14 04:38:11 PDT ---
In some situations the format string of writef/writefln is not known at
compile-time, but it most situations it is. So in the frequent cases where the
format string is known at compile-time I'd like an error at compile-time if the
type in the format string and the type of the arguments don't match. 

An error at compile-time is better, it gives the same advantages of static
typing, and it allows the programmer to catch format string bugs before
runtime, in all the program, even in parts of the code that aren't run yet (a
runtime bugs happens only with a specific writefln comes into the thread of
code being run).

Currently (dmd v2.047) this compiles with no errors:

import std.stdio: writefln;
void main() {
    float f = 10.5;
    writefln("%d", f);

But I'd like an error similar to:

test.d(4): Error: writefln format string type error, used format '%d' but
argument 'f' is of type float

Once written, this new testing routine can be useful for other functions too,
for the format(), some I/O functions, and for C functions like printf() too
that sometimes are present in D programs.

This is a similar C program:

#include "stdio.h"
int main() {
    float f = 10.5;
    printf("%d\n", f);
    return 0;

If compiled with GCC 4.5:
gcc -Wall testc.c -o testc

It outputs at compile-time:
test.c: In function 'main':
test.c:4:5: warning: format '%d' expects type 'int', but argument 2 has type

GCC 4.5 is not able to spot the bug in this program:

#include "stdio.h"
int main() {
    float f = 10.5;
    const char* format = "%d\n";
    printf(format, f);
    return 0;

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to