[Issue 21605] New: Instead of giving error on printf format mismatch, correct it
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Feb 3 07:59:24 UTC 2021
https://issues.dlang.org/show_bug.cgi?id=21605
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 at puremagic.com
Reporter: bugzilla at digitalmars.com
For example,
import core.stdc.stdio;
void test(int i) {
printf("the number is %s\n", i);
}
gives:
Error: argument `i` for format specification `"%s"` must be `char*`, not
`int`
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
arguments.
Extensions:
1. Transferring the modifiers like field width over to the replacement format
specifier.
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
--
More information about the Digitalmars-d-bugs
mailing list