[clang] [clang][bytecode] Fix crash in void functions returning non-void expr… (PR #176550)

via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 18 08:21:55 PST 2026


https://github.com/Serosh-commits updated https://github.com/llvm/llvm-project/pull/176550

>From cbd24cecea0de2080071e9203940a812dce434f1 Mon Sep 17 00:00:00 2001
From: Serosh <janmejayapanda400 at gmail.com>
Date: Sat, 17 Jan 2026 15:57:11 +0530
Subject: [PATCH] [clang][bytecode] Fix crash in void functions returning
 non-void expressions

The bytecode compiler was erroneously emitting an RVOPtr opcode for
void functions when they contained a non-void return expression.
This led to an assertion failure in the interpreter.

This fix ensures that discard() is used instead of visit() for such
expressions in void functions, and prevents the emission of RVO pointers.
It checks RE->containsErrors() to handle invalid return expressions
gracefully.

Fixes #176536
---
 clang/lib/AST/ByteCode/Compiler.cpp   | 11 +++++------
 clang/test/AST/ByteCode/functions.cpp |  7 +++++++
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 21f8db06919ed..def73fb46719b 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -5678,11 +5678,11 @@ bool Compiler<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
       return this->emitRet(*ReturnType, RS);
     }
 
-    if (RE->getType()->isVoidType()) {
-      if (!this->visit(RE))
-        return false;
-    } else {
-      InitLinkScope<Emitter> ILS(this, InitLink::RVO());
+    if (RE->containsErrors() || RE->getType()->isVoidType() ||
+        (CompilingFunction && CompilingFunction->getReturnType()->isVoidType()))
+      return this->discard(RE);
+
+    InitLinkScope<Emitter> ILS(this, InitLink::RVO());
       // RVO - construct the value in the return location.
       if (!this->emitRVOPtr(RE))
         return false;
@@ -5693,7 +5693,6 @@ bool Compiler<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
 
       this->emitCleanup();
       return this->emitRetVoid(RS);
-    }
   }
 
   // Void return.
diff --git a/clang/test/AST/ByteCode/functions.cpp b/clang/test/AST/ByteCode/functions.cpp
index 21d3ddaafaee3..7fba1150ab736 100644
--- a/clang/test/AST/ByteCode/functions.cpp
+++ b/clang/test/AST/ByteCode/functions.cpp
@@ -735,3 +735,10 @@ namespace PtrPtrCast {
   void foo() { ; }
   void bar(int *a) { a = (int *)(void *)(foo); }
 }
+
+namespace GH176536 {
+  constexpr void foo(int n) {
+    return n > 1 ? foo(n - 1) : 0; // expected-error {{return type 'void' must match the return type 'int' of the expression}}
+  }
+  static_assert((foo(2), true), "");
+}



More information about the cfe-commits mailing list