www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21605] New: Instead of giving error on printf format


          Issue ID: 21605
           Summary: Instead of giving error on printf format mismatch,
                    correct it
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: bugzilla digitalmars.com

For example,

  import core.stdc.stdio;
  void test(int i) {
    printf("the number is %s\n", i);


  Error: argument `i` for format specification `"%s"` must be `char*`, not

I propose that instead, if the format specifier is `%s`, the format string
literal gets rewritten to use the correct format specifier for type `int`, i.e.
code is generated for:

    printf("the number is %d\n", i);

For most uses of printf, using %s will "just work", making it usable with
generic code. If the format specifier is not `%s`, and is mismatched with its
corresponding argument, there is no change in behavior (i.e. the mismatch error
is given).

The baseline implementation should handle all integral and floating point


1. Transferring the modifiers like field width over to the replacement format

2. Replacing `%x` format specifiers with `%a` for floating point arguments.

3. Handling D strings by replacing the format specifier with `%.*s` and
rewriting the argument to give the length and pointer.

This enhancement would be added to the existing code that does the printf
format/argument checking.

The user advantages are:

1. works with generic code

2. reduces the number of recompiles necessary to fix printf mismatch errors

3. reduces the inherent frustration of the compiler knowing what the fix is but
demanding that the user apply the fix

4. user can change the types of variables without having to go through and
adjust all the printf formats

Feb 02 2021