New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
missing-field-initializers warning not reporting missing designated initializers #56628
Comments
Is there any updates/workarounds for this? |
I don't see a workaround, it looks out of date at this point. Someone needs to remove the |
I see how this would be a breaking change, although i guess if someone enabled Wextra and Anyways, i felt like hacking at this a bit and maybe introducing a dedicated switch for this might be an idea? I think the warning is useful, and i would certainly enable it in my projects (although discovery of this flag might be a problem...) |
I'm actually hit by this issue and I actually missed a bug in my code because I assumed the warning was working on Clang too. |
@AaronBallman sounds like missing this case is not ideal, wdyt about making this change? |
I'm comfortable with the change. If it turns out to be highly disruptive, we can always put the diagnostic under a new warning flag grouped under |
Prior to this change clang didn't emit missing-field-initializers warning for designated initializers. The comments say that it is done to match gcc behavior. However, gcc behaves so only for C. For C++ warnings are emitted. Fixes llvm#56628 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D157879
Prior to this change clang didn't emit missing-field-initializers warning for designated initializers. The comments say that it is done to match gcc behavior. However, gcc behaves so only for C. For C++ warnings are emitted. Fixes llvm#56628 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D157879
Prior to this change clang didn't emit missing-field-initializers warning for designated initializers. The comments say that it is done to match gcc behavior. However, gcc behaves so only for C. For C++ warnings are emitted. Fixes llvm#56628 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D157879
Prior to this change clang didn't emit missing-field-initializers warning for designated initializers. The comments say that it is done to match gcc behavior. However, gcc behaves so only for C. For C++ warnings are emitted. Fixes llvm#56628 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D157879
Prior to this change clang didn't emit missing-field-initializers warning for designated initializers. The comments say that it is done to match gcc behavior. However, gcc behaves so only for C. For C++ warnings are emitted. Fixes llvm#56628 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D157879
Prior to this change clang didn't emit missing-field-initializers warning for designated initializers. The comments say that it is done to match gcc behavior. However, gcc behaves so only for C. For C++ warnings are emitted. Fixes llvm#56628 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D157879
Prior to this change clang didn't emit missing-field-initializers warning for designated initializers. The comments say that it is done to match gcc behavior. However, gcc behaves so only for C. For C++ warnings are emitted. Fixes llvm#56628 Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D157879
It is how we understand designated initializers that matters. Our project builds failed with this change. We want the diagnostic flag to report for fields we are really forget to init instead of reporting designated initializers that DO means other fields to be default initialized. The GCC community is still discussing this issue and has not drawn a conclusion yet ( for C++ ). |
So we are hitting dcl.init.aggr p5.2:
and if we try another example w/o designated init: https://godbolt.org/z/Yn48KWvrM ExampleStruct buildExampleStruct(int a) {
ExampleStruct e{1};
return e;
} We obtain the same diagnostic, so we are being entirely consistent here. I don't see a convincing argument that we should not warn for designated init but we should warn for non-designated aggregate init. The standard does not differentiate these cases. |
I think that any field initializer not explicitly mentioned is missing (per the usual English sense of the word) and to get your reading of the diagnostic, we'd want it named differently.
That said, this could be a reason to add a new group under |
@jamesruan Also note that with this warning, it's really easy for an implementer to specify when that warning happen. struct AB {
int a = {}; // A is an optional struct parameter
int b;
};
AB test{
.b = 0 // no warning for a since it has a default value
}; That way this warning only happen when users really make a mistake. |
It looks to me like the missing-field-initializers warning for designated initialization in Clang 18.0.0 is also triggered in C, which definitely looks like a bug (since missing field initializers for designated init in C are a well-defined feature, such items are explicitly initialized to zero, which can be detected and then patched to default values in libraries which are designed with designated init in mind - FWIW, this change breaks all my C code when compiling with The Clang 18 release notes say this:
...but it also happens in C mode. I'm seeing this issue in the latest Emscripten SDK (3.1.51) which is using Clang 18.0.0. See code like this to understand why the warning is a serious issue: The |
Note there are several discussions in the review: http://108.170.204.19/D157879 |
#56628 changed the behavior of `-Wmissing-field-initializers`, which introduces many new warnings in C++ code that uses partial designated initializers. If such code is being built with `-Wextra -Werror`, this change will break the build. This PR adds a new flag that allows to disable these new warnings and keep the old ones, as was suggested by @AaronBallman in the original issue: #56628 (comment) Fixes #68933
In the following code, the
missing-field-initializers
does not produce a warning thatb
is not explicitly initialized.Changing
CheckForMissingFields
toTrue
here:llvm-project/clang/lib/Sema/SemaInit.cpp
Lines 2167 to 2169 in a9a60f2
seems to produce the expected warning.
The comment suggests
However, GCC does produce the warning in that case.
Comparison with GCC: https://godbolt.org/z/WeYdY11q3
Build flags:
-Werror=missing-field-initializers -std=c++20
Expected behavior:
main.cpp:10:5: error: missing field 'b' initializer [-Werror,-Wmissing-field-initializers]
matching GCCs
<source>:9:5: error: missing initializer for member 'ExampleStruct::b' [-Werror=missing-field-initializers]
Actual behavior: Compiles without warning/error.
The text was updated successfully, but these errors were encountered: