summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimm Bäder <tbaeder@redhat.com>2024-05-21 13:46:19 +0200
committerTimm Bäder <tbaeder@redhat.com>2024-05-22 13:12:48 +0200
commitf68548135b8f9a02beac842646ab89bcaad9d400 (patch)
tree72f63f58997a549dd15c2be640bc4ce277bf1494
parentbbc4c2e047107c62d49ce1e0474635ea55a2b006 (diff)
downloadllvm-libc-f68548135b8f9a02beac842646ab89bcaad9d400.tar.gz
[clang][Interp] Fix checking unions for initialization
-rw-r--r--clang/lib/AST/Interp/EvaluationResult.cpp4
-rw-r--r--clang/test/AST/Interp/unions.cpp14
2 files changed, 18 insertions, 0 deletions
diff --git a/clang/lib/AST/Interp/EvaluationResult.cpp b/clang/lib/AST/Interp/EvaluationResult.cpp
index e92d686c724c..79f222ce2b30 100644
--- a/clang/lib/AST/Interp/EvaluationResult.cpp
+++ b/clang/lib/AST/Interp/EvaluationResult.cpp
@@ -115,6 +115,10 @@ static bool CheckFieldsInitialized(InterpState &S, SourceLocation Loc,
DiagnoseUninitializedSubobject(S, Loc, F.Decl);
Result = false;
}
+
+ // Only the first member of a union needs to be initialized.
+ if (R->isUnion())
+ break;
}
// Check Fields in all bases
diff --git a/clang/test/AST/Interp/unions.cpp b/clang/test/AST/Interp/unions.cpp
new file mode 100644
index 000000000000..08ca39c3cb08
--- /dev/null
+++ b/clang/test/AST/Interp/unions.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both %s
+// RUN: %clang_cc1 -verify=ref,both %s
+
+// both-no-diagnostics
+
+union U {
+ int a;
+ int b;
+};
+
+constexpr U a = {12};
+static_assert(a.a == 12, "");
+
+