[clang] [clang][bytecode] Fix crash in void functions returning non-void expr… (PR #176550)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 17 04:18:28 PST 2026
https://github.com/Serosh-commits updated https://github.com/llvm/llvm-project/pull/176550
>From 7f3db33e42835b3bf6b04ce64509d4fd4fc660fe 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.
Fixes #176536
---
clang/lib/AST/ByteCode/Compiler.cpp | 5 +++--
clang/test/AST/ByteCode/gh176536.cpp | 6 ++++++
2 files changed, 9 insertions(+), 2 deletions(-)
create mode 100644 clang/test/AST/ByteCode/gh176536.cpp
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 21f8db06919ed..623ba7380e1f3 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -5678,8 +5678,9 @@ bool Compiler<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
return this->emitRet(*ReturnType, RS);
}
- if (RE->getType()->isVoidType()) {
- if (!this->visit(RE))
+ if (RE->getType()->isVoidType() ||
+ (CompilingFunction && CompilingFunction->getReturnType()->isVoidType())) {
+ if (!this->discard(RE))
return false;
} else {
InitLinkScope<Emitter> ILS(this, InitLink::RVO());
diff --git a/clang/test/AST/ByteCode/gh176536.cpp b/clang/test/AST/ByteCode/gh176536.cpp
new file mode 100644
index 0000000000000..2ee45c14cae48
--- /dev/null
+++ b/clang/test/AST/ByteCode/gh176536.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify %s
+
+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