Can we check the arguments to format() at compile time?

Daniel Murphy via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 1 21:03:11 PDT 2016


On 2/04/2016 8:25 AM, Yuxuan Shui wrote:
> Clang has this nice feature that it will warn you when you passed wrong
> arguments to printf:
>
> #include <stdio.h>
> int main(){
>      long long u = 10;
>      printf("%c", u);
> }
>
> clang something.c:
> something.c:4:15: warning: format specifies type 'int' but the argument
> has type 'long long' [-Wformat]
>
> With the CTFE power of D, we should be able to do the same thing when
> the format string is available at compile time. Instead of throwing
> exceptions at run time.

That's something I want to do with this eventually: 
https://github.com/D-Programming-Language/dmd/pull/3799

When arguments (or anything about the arguments) is known at compile 
time, some subsets of in-contracts can be checked.

Currently only stuff like this is supported:
auto iota(int low, int high) in { assert(low <= high); } body { ... }

iota(23, 7); // Error

But it's not impossible that
assert(checkFormatArgs(format, args));
could work one day.


More information about the Digitalmars-d mailing list