From f68548135b8f9a02beac842646ab89bcaad9d400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= Date: Tue, 21 May 2024 13:46:19 +0200 Subject: [PATCH] [clang][Interp] Fix checking unions for initialization --- clang/lib/AST/Interp/EvaluationResult.cpp | 4 ++++ clang/test/AST/Interp/unions.cpp | 14 ++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 clang/test/AST/Interp/unions.cpp diff --git a/clang/lib/AST/Interp/EvaluationResult.cpp b/clang/lib/AST/Interp/EvaluationResult.cpp index e92d686c724cc8..79f222ce2b30cf 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 00000000000000..08ca39c3cb0892 --- /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, ""); + +