Skip to content

Commit f685481

Browse files
committed
[clang][Interp] Fix checking unions for initialization
1 parent bbc4c2e commit f685481

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

clang/lib/AST/Interp/EvaluationResult.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ static bool CheckFieldsInitialized(InterpState &S, SourceLocation Loc,
115115
DiagnoseUninitializedSubobject(S, Loc, F.Decl);
116116
Result = false;
117117
}
118+
119+
// Only the first member of a union needs to be initialized.
120+
if (R->isUnion())
121+
break;
118122
}
119123

120124
// Check Fields in all bases

clang/test/AST/Interp/unions.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both %s
2+
// RUN: %clang_cc1 -verify=ref,both %s
3+
4+
// both-no-diagnostics
5+
6+
union U {
7+
int a;
8+
int b;
9+
};
10+
11+
constexpr U a = {12};
12+
static_assert(a.a == 12, "");
13+
14+

0 commit comments

Comments
 (0)