Skip to content

Commit 0acbc53

Browse files
Fznamznonahatanaka
authored andcommitted
[clang] Fix designated initializers inside templates (llvm#69712)
Skip anonymous members when rebuilding `DesignatedInitExpr` since designated inits for them are meant to be created during `InitListChecker::CheckDesignatedInitializer` routine. Fixes llvm#65143
1 parent 5bfa5ad commit 0acbc53

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,9 @@ Bug Fixes in This Version
732732
``thread_local`` instead of ``_Thread_local``.
733733
Fixes (`#70068 <https://github.com/llvm/llvm-project/issues/70068>`_) and
734734
(`#69167 <https://github.com/llvm/llvm-project/issues/69167>`_)
735+
- Clang now accepts anonymous members initialized with designated initializers
736+
inside templates.
737+
Fixes (`#65143 <https://github.com/llvm/llvm-project/issues/65143>`_)
735738

736739
Bug Fixes to Compiler Builtins
737740
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/TreeTransform.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11722,21 +11722,23 @@ TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
1172211722
bool ExprChanged = false;
1172311723
for (const DesignatedInitExpr::Designator &D : E->designators()) {
1172411724
if (D.isFieldDesignator()) {
11725-
Desig.AddDesignator(Designator::CreateFieldDesignator(
11726-
D.getFieldName(), D.getDotLoc(), D.getFieldLoc()));
1172711725
if (D.getFieldDecl()) {
1172811726
FieldDecl *Field = cast_or_null<FieldDecl>(
1172911727
getDerived().TransformDecl(D.getFieldLoc(), D.getFieldDecl()));
1173011728
if (Field != D.getFieldDecl())
1173111729
// Rebuild the expression when the transformed FieldDecl is
1173211730
// different to the already assigned FieldDecl.
1173311731
ExprChanged = true;
11732+
if (Field->isAnonymousStructOrUnion())
11733+
continue;
1173411734
} else {
1173511735
// Ensure that the designator expression is rebuilt when there isn't
1173611736
// a resolved FieldDecl in the designator as we don't want to assign
1173711737
// a FieldDecl to a pattern designator that will be instantiated again.
1173811738
ExprChanged = true;
1173911739
}
11740+
Desig.AddDesignator(Designator::CreateFieldDesignator(
11741+
D.getFieldName(), D.getDotLoc(), D.getFieldLoc()));
1174011742
continue;
1174111743
}
1174211744

clang/test/SemaCXX/cxx2b-designated-initializers.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,36 @@ union S {
99
};
1010

1111
void f(int x, auto) {
12-
const S result { // expected-error {{field designator (null) does not refer to any field in type 'const S'}}
12+
const S result {
1313
.a = x
1414
};
1515
}
1616

1717
void g(void) {
18-
f(0, 0); // expected-note {{in instantiation of function template specialization 'PR61118::f<int>' requested here}}
18+
f(0, 0);
1919
}
2020

2121
} // end namespace PR61118
2222

23+
namespace GH65143 {
24+
struct Inner {
25+
int a;
26+
};
27+
28+
struct Outer {
29+
struct {
30+
Inner inner;
31+
};
32+
};
33+
34+
template <int val> void f() {
35+
constexpr Outer x{.inner = {val}};
36+
static_assert(x.inner.a == val);
37+
}
38+
39+
void bar() { f<4>(); }
40+
}
41+
2342
namespace GH62156 {
2443
union U1 {
2544
int x;

0 commit comments

Comments
 (0)