[clang] [Clang][Sema] Diagnosis for constexpr constructor not initializing a union member (PR #81042)

Mariya Podchishchaeva via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 8 03:12:34 PST 2024


================
@@ -2393,6 +2393,25 @@ static bool CheckConstexprFunctionBody(Sema &SemaRef, const FunctionDecl *Dcl,
                                              Kind))
             return false;
       }
+    } else if (!Constructor->isDelegatingConstructor()) {
+      for (const Decl *decl : RD->decls()) {
+        if (const auto *inner = dyn_cast<CXXRecordDecl>(decl)) {
+          if (inner->isUnion()) {
+            if (Constructor->getNumCtorInitializers() == 0 &&
+                RD->hasVariantMembers()) {
+              if (Kind == Sema::CheckConstexprKind::Diagnose) {
+                SemaRef.Diag(
+                    Dcl->getLocation(),
+                    SemaRef.getLangOpts().CPlusPlus20
+                        ? diag::warn_cxx17_compat_constexpr_union_ctor_no_init
+                        : diag::ext_constexpr_union_ctor_no_init);
+              } else if (!SemaRef.getLangOpts().CPlusPlus20) {
+                return false;
+              }
----------------
Fznamznon wrote:

That seems to be copying the check for unions from lines 2346-2354. I also observed that non-template version of the code this patch fixes, works fine now. I wonder, why can't we just do same thing for templated contexts as we do now for non-templated? If that is not possible, because for example it now involves constexpr evaluator and it normally chokes on templates, can we unify the new check with the one from lines 2346-2354?

https://github.com/llvm/llvm-project/pull/81042


More information about the cfe-commits mailing list