[clang] f685481 - [clang][Interp] Fix checking unions for initialization

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Wed May 22 04:13:08 PDT 2024


Author: Timm Bäder
Date: 2024-05-22T13:12:48+02:00
New Revision: f68548135b8f9a02beac842646ab89bcaad9d400

URL: https://github.com/llvm/llvm-project/commit/f68548135b8f9a02beac842646ab89bcaad9d400
DIFF: https://github.com/llvm/llvm-project/commit/f68548135b8f9a02beac842646ab89bcaad9d400.diff

LOG: [clang][Interp] Fix checking unions for initialization

Added: 
    clang/test/AST/Interp/unions.cpp

Modified: 
    clang/lib/AST/Interp/EvaluationResult.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/EvaluationResult.cpp b/clang/lib/AST/Interp/EvaluationResult.cpp
index e92d686c724cc..79f222ce2b30c 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 0000000000000..08ca39c3cb089
--- /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, "");
+
+


        


More information about the cfe-commits mailing list