As a thought experiment: pretend there was a situation where C V1 supports X, V2 deprecates X, and then V3 un-deprecates X (“oops, nevermind”). In such a case, I hope you’d agree, there’s no purpose served by introducing a deprecation warning for code using X, even when built for C V2, because we already know the decision was made not to remove X.
Of course, that’s not precisely the situation we’re in, since we have a semantics change, not a simple “un-deprecation”. Yet, the effect seems similar.
ANSI C (V1) supported X, C89 (V2) deprecated X in 1989, C2x (V6) “undeprecates” (after a fashion) X in 2023. There’s a lot of daylight between V2 and V6 (34 years worth). So the effects are similar, but with a lot more time for people to accidentally be using the deprecated code due to the lack of warning about it.
In my view, there’s two separate purposes served by a strict-prototypes warning:
- Warn that you’re doing something which will be incompatible with C2x.
- Warn that you’re doing something which was probably unintended, but is not broken by C2x (declaring a prototype-less function instead of void function, without providing arguments.)
I think #1 very clearly meets the requirements for on-by-default “deprecation warning”, because it will break in C2x.
Agreed.
I think #2 does not fit under the same criteria, because it will not break in C2x. So, to argue for #2 to be on-by-default seemingly requires some other rationale.
Agreed, but only for a strict prototypes warning. Not agreed for a general warning that something is deprecated, however. Deprecation warnings aren’t just “this may be incompatible in the future”, they’re often (and definitely in this case is) “there is a better way to do this today”.
One rationale might be if code in situation #2 is so likely to indicate functionally broken code, that it’s worth a default warning (ala -Wincompatible-pointer-types).
I think we’re in this situation, but we may disagree on what “broken code” means. To me, broken code also includes code that does not behave as the author intended it to. In the case of #2, I think the author expected the function to accept no arguments and the code does not behave the way they intended it to. There is anecdotal evidence that people run into this without realizing it though:
MinGW-w64 - for 32 and 64 bit Windows / Code / [r6638] /trunk/mingw-w64-libraries/winpthreads/include/pthread.h (line 404)
MinGW-w64 - for 32 and 64 bit Windows / Code / [r6638] /trunk/mingw-w64-libraries/winpthreads/src/thread.c (line 922)
The author clearly doesn’t expect anyone using this library interface to pass arguments, but that’s not the semantics of the function in reality. (This is a private library interface, and so perhaps not as compelling as public library interfaces, but it does demonstrate that library authors get this wrong.) To me, this represents broken code, but a less severe break than UB-inducing calls to code like this.
Another rationale might be that there’s so little code that hits situation #2, that it’s not worth the development cost for the compiler to distinguish it from #1. I don’t expect either of those are actually true, but if someone collected data demonstrating one, I think that could be convincing.
There’s extant code this will hit, for sure.
That said, I do think there’s a reasonable argument that #2 should be diagnosed in a warning group like -Wextra or -Wall – under the (weaker) rationale that the code is “probably unintended” – despite not likely being functionally broken now or in the future.
That’s an interesting idea and one that I think I’d be happy enough with.
Yes, by omitting the “void”, your library has made it easier for clients to write buggy code that passes undiagnosed by the compiler. Yet, the functional bug is on the part of the client calling the function incorrectly, not your code.
I believe there’s a bug in the library interface because the semantics of the signature do not match the intent of the author. There may also be an additional bug on the client side, especially if there is documentation that the library interface doesn’t accept arguments. But as an example, I would have a hard time blaming an inattentive user for the user code here:
// LibraryHeader.h
void setSomeGlobalState();
void clearSomeGlobalState();
// LibrarySource.c
void setSomeGlobalState() { GlobalState = true; }
void clearSomeGlobalState() { GlobalState = false; }
// UserCode.c
void whatever(void) {
setSomeGlobalState(true);
...
setSomeGlobalState(false);
}
But this is a good argument for adding the warning to a warning group that well-maintained libraries will be tested with.
Agreed.
Here’s a way forward that I think will be acceptable from everyone’s feedback (please correct me where you disagree):
Changing the behavior of -Wstrict-prototypes to warn on less cases means the warning is less powerful for people who want strict prototypes, but in order to make it palatable to be on by default or within -Wall, we’d need to change it to warn on less cases. So I think what I’m hearing is that we should leave -Wstrict-prototypes alone (leave it off by default, don’t change its behavior), but see the conclusion for a plot twist.
Instead, it sounds like we want two new, related warnings. One which diagnoses functions without a prototype that will change behavior in C2x. This will be on by default (should it be -Werror by default?). A second one which diagnoses functions without a prototype but don’t change behavior in C2x. This one will be off by default, but enabled by -Wall (and won’t be -Werror by default).
The only way to select which diagnostic a function without a prototype will generate is from the function definition. That’s the point at which we’ll know whether the user has an identifier list (broken by C2x) or not (takes no args, doesn’t change behavior in C2x). We will not base the decision to diagnose the declaration on the call sites where the function is called (that requires a CFG analysis and I don’t think is worth the effort for these diagnostics). However, there are other cases where no definition will exist:
void func(void (*fp)()) {
extern void foo();
...
}
I can see arguments for either diagnostic in these kinds of circumstances, I would probably go with “don’t change behavior in C2x” warning to be conservative.
Because these two new diagnostics combined cover exactly the same cases as -Wstrict-prototypes, I think we can use that as the diagnostic group for the two new diagnostics. This way, when a user has already enabled -Wstrict-prototypes explicitly, they don’t get two diagnostics for the on-by-default diagnostic cases. If that’s a reasonable idea, that also helps nail down the diagnostic behavior of the examples I gave above.
Here are examples to demonstrate what I’m thinking (Compiler Explorer):
// Off by default warnings, enabled by -Wall
void other_func(); // expected-warning {{this function declaration without a prototype is deprecated in all versions of C}}
void other_func() {} // expected-warning {{this function declaration without a prototype is deprecated in all versions of C}}
void never_defined(); // expected-warning {{this function declaration without a prototype is deprecated in all versions of C}}
typedef void (*fp)(); // expected-warning {{this function declaration without a prototype is deprecated in all versions of C}}
void whatever(void) {
extern void hoo_boy(); // expected-warning {{this function declaration without a prototype is deprecated in all versions of C}}
}
// On by default warnings (perhaps defaulted as errors)
void func(); // expected-warning {{this function declaration without a prototype is deprecated in all versions of C and changes behavior in C2x}}
void func(a, b) int a, b; {} // expected-warning {{this function declaration without a prototype is deprecated in all versions of C and changes behavior in C2x}}
Additionally, it sounds like there’s also appetite for -fstrict-prototypes which uses the C2x prototype rules in any C language mode. This can be implemented once we actually implement those strict prototype rules for C2x.
Does that seem like an acceptable path forward to folks?