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
[clang] Fix designated initializers inside templates #69712
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Skip anonymous members when rebuilding DesignatedInitExpr since designated inits for them are meant to be created during `InitListChecker::CheckDesignatedInitializer` routine. Fixes llvm#65143
Fznamznon
requested review from
AaronBallman,
bwendling,
shafik and
erichkeane
October 20, 2023 11:33
llvmbot
added
clang
Clang issues not falling into any other category
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
labels
Oct 20, 2023
@llvm/pr-subscribers-clang Author: Mariya Podchishchaeva (Fznamznon) ChangesSkip anonymous members when rebuilding DesignatedInitExpr since designated inits for them are meant to be created during Fixes #65143 Full diff: https://github.com/llvm/llvm-project/pull/69712.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index fc8caf9221b9d29..ffb32c254b256dd 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -412,6 +412,9 @@ Bug Fixes in This Version
- Clang no longer permits using the `_BitInt` types as an underlying type for an
enumeration as specified in the C23 Standard.
Fixes (`#69619 <https://github.com/llvm/llvm-project/issues/69619>`_)
+- Clang now accepts anonymous members initialized with designated initializers
+ inside templates.
+ Fixes (`#65143 <https://github.com/llvm/llvm-project/issues/65143>`_)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 8fafdd4f5caa1ed..094e5efa939f4d1 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -11783,8 +11783,6 @@ TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
bool ExprChanged = false;
for (const DesignatedInitExpr::Designator &D : E->designators()) {
if (D.isFieldDesignator()) {
- Desig.AddDesignator(Designator::CreateFieldDesignator(
- D.getFieldName(), D.getDotLoc(), D.getFieldLoc()));
if (D.getFieldDecl()) {
FieldDecl *Field = cast_or_null<FieldDecl>(
getDerived().TransformDecl(D.getFieldLoc(), D.getFieldDecl()));
@@ -11792,12 +11790,16 @@ TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
// Rebuild the expression when the transformed FieldDecl is
// different to the already assigned FieldDecl.
ExprChanged = true;
+ if (Field->isAnonymousStructOrUnion())
+ continue;
} else {
// Ensure that the designator expression is rebuilt when there isn't
// a resolved FieldDecl in the designator as we don't want to assign
// a FieldDecl to a pattern designator that will be instantiated again.
ExprChanged = true;
}
+ Desig.AddDesignator(Designator::CreateFieldDesignator(
+ D.getFieldName(), D.getDotLoc(), D.getFieldLoc()));
continue;
}
diff --git a/clang/test/SemaCXX/cxx2b-designated-initializers.cpp b/clang/test/SemaCXX/cxx2b-designated-initializers.cpp
index 5588926f419a932..1d9b9183b3679f1 100644
--- a/clang/test/SemaCXX/cxx2b-designated-initializers.cpp
+++ b/clang/test/SemaCXX/cxx2b-designated-initializers.cpp
@@ -9,17 +9,36 @@ union S {
};
void f(int x, auto) {
- const S result { // expected-error {{field designator (null) does not refer to any field in type 'const S'}}
+ const S result {
.a = x
};
}
void g(void) {
- f(0, 0); // expected-note {{in instantiation of function template specialization 'PR61118::f<int>' requested here}}
+ f(0, 0);
}
} // end namespace PR61118
+namespace GH65143 {
+struct Inner {
+ int a;
+};
+
+struct Outer {
+ struct {
+ Inner inner;
+ };
+};
+
+template <int val> void f() {
+ constexpr Outer x{.inner = {val}};
+ static_assert(x.inner.a == val);
+}
+
+void bar() { f<4>(); }
+}
+
namespace GH62156 {
union U1 {
int x;
|
erichkeane
approved these changes
Oct 20, 2023
This was referenced Oct 25, 2023
ahatanaka
pushed a commit
to apple/llvm-project
that referenced
this pull request
Nov 3, 2023
Skip anonymous members when rebuilding `DesignatedInitExpr` since designated inits for them are meant to be created during `InitListChecker::CheckDesignatedInitializer` routine. Fixes llvm#65143
ahatanaka
pushed a commit
to apple/llvm-project
that referenced
this pull request
Nov 7, 2023
Skip anonymous members when rebuilding `DesignatedInitExpr` since designated inits for them are meant to be created during `InitListChecker::CheckDesignatedInitializer` routine. Fixes llvm#65143
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Skip anonymous members when rebuilding DesignatedInitExpr since designated inits for them are meant to be created during
InitListChecker::CheckDesignatedInitializer
routine.Fixes #65143