[clang] [clang][bytecode] Fix assert when calling static functions via CallVirt (PR #224253)

via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 17 03:01:56 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

This only happens in error cases.

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


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Interp.cpp (+6) 
- (modified) clang/test/AST/ByteCode/cxx20.cpp (+13) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 5b0422d9ae9bd..de7d8ce188f97 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -2427,6 +2427,12 @@ bool DynamicCast(InterpState &S, CodePtr OpPC, const Type *DestTypePtr,
 
 bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
               uint32_t VarArgSize) {
+  // This happens in error cases.
+  if (!Func->hasThisPointer()) {
+    assert(!Func->isValid());
+    return diagnoseCallableDecl(S, OpPC, Func->getDecl());
+  }
+
   assert(Func->hasThisPointer());
   assert(Func->isVirtual());
   size_t ArgSize = Func->getArgSize() + VarArgSize;
diff --git a/clang/test/AST/ByteCode/cxx20.cpp b/clang/test/AST/ByteCode/cxx20.cpp
index b06a3ed9149cb..c5ade52a66b40 100644
--- a/clang/test/AST/ByteCode/cxx20.cpp
+++ b/clang/test/AST/ByteCode/cxx20.cpp
@@ -1531,3 +1531,16 @@ namespace SubPtr {
   }
   static_assert(dynAlloc() == 1);
 }
+
+namespace InvalidVirtualCall {
+  struct A {
+    virtual void foo(); // both-note {{overridden virtual function is here}}
+  };
+
+  struct B : A {
+    constexpr void bar() { foo(); } // both-error {{never produces a constant expression}} \
+                                    // both-note {{non-constexpr function 'foo' cannot be used in a constant expression}}
+    static void foo(); // both-error {{'static' member function 'foo' overrides a virtual function in a base class}} \
+                       // both-note {{declared here}}
+  };
+}

``````````

</details>


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


More information about the cfe-commits mailing list