[clang] 1d8eb43 - [clang][bytecode] Diagnose member calls on inactive union fields (#129709)

via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 4 07:14:50 PST 2025


Author: Timm Baeder
Date: 2025-03-04T16:14:47+01:00
New Revision: 1d8eb436ca694a9e215066e0b2dbd18b2d3943ea

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

LOG: [clang][bytecode] Diagnose member calls on inactive union fields (#129709)

Unless the function is a constructor, which is allowed to do this since
it will activate the member.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.cpp
    clang/test/AST/ByteCode/unions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index bf9fdb6a690db..9f641541ad4b6 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1341,6 +1341,9 @@ bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
     } else {
       if (!CheckInvoke(S, OpPC, ThisPtr))
         return cleanup();
+      if (!Func->isConstructor() &&
+          !CheckActive(S, OpPC, ThisPtr, AK_MemberCall))
+        return false;
     }
 
     if (Func->isConstructor() && !checkConstructor(S, OpPC, Func, ThisPtr))

diff  --git a/clang/test/AST/ByteCode/unions.cpp b/clang/test/AST/ByteCode/unions.cpp
index 2064cae11e970..72b9b18609720 100644
--- a/clang/test/AST/ByteCode/unions.cpp
+++ b/clang/test/AST/ByteCode/unions.cpp
@@ -504,4 +504,22 @@ namespace AnonymousUnion {
   static_assert(return_init_all().a.p == 7); // both-error {{}} \
                                              // both-note {{read of member 'p' of union with no active member}}
 }
+
+namespace MemberCalls {
+  struct S {
+    constexpr bool foo() const { return true; }
+  };
+
+  constexpr bool foo() { // both-error {{never produces a constant expression}}
+    union {
+      int a;
+      S s;
+    } u;
+
+    u.a = 10;
+    return u.s.foo(); // both-note 2{{member call on member 's' of union with active member 'a'}}
+  }
+  static_assert(foo()); // both-error {{not an integral constant expression}} \
+                        // both-note {{in call to}}
+}
 #endif


        


More information about the cfe-commits mailing list