Skip to content

[clang][bytecode] Diagnose member calls on inactive union fields #129709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2025

Conversation

tbaederr
Copy link
Contributor

@tbaederr tbaederr commented Mar 4, 2025

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

Unless the function is a constructor, which is allowed to do this since
it will activate the member.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 4, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/129709.diff

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Interp.cpp (+3)
  • (modified) clang/test/AST/ByteCode/unions.cpp (+18)
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

@tbaederr tbaederr merged commit 1d8eb43 into llvm:main Mar 4, 2025
14 checks passed
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
…m#129709)

Unless the function is a constructor, which is allowed to do this since
it will activate the member.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants