From feaf010fa2c2952eaa6659ea2decaaa2493d4bb8 Mon Sep 17 00:00:00 2001
From: Liam <byteslice@airmail.cc>
Date: Sun, 12 Jun 2022 18:11:02 -0400
Subject: [PATCH] common/assert: rework ASSERT handling to avoid std::function
 usage

---
 src/common/assert.cpp | 10 +++-------
 src/common/assert.h   | 45 ++++++++++++++++---------------------------
 2 files changed, 20 insertions(+), 35 deletions(-)

diff --git a/src/common/assert.cpp b/src/common/assert.cpp
index 1a85faccfe..6026b7dc22 100644
--- a/src/common/assert.cpp
+++ b/src/common/assert.cpp
@@ -6,13 +6,9 @@
 
 #include "common/settings.h"
 
-void assert_check_condition(bool cond, std::function<void()>&& on_failure) {
-    if (!cond) [[unlikely]] {
-        on_failure();
-
-        if (Settings::values.use_debug_asserts) {
-            Crash();
-        }
+void assert_fail_impl() {
+    if (Settings::values.use_debug_asserts) {
+        Crash();
     }
 }
 
diff --git a/src/common/assert.h b/src/common/assert.h
index fb78086573..8c927fcc04 100644
--- a/src/common/assert.h
+++ b/src/common/assert.h
@@ -4,47 +4,36 @@
 
 #pragma once
 
-#include <functional>
-
 #include "common/logging/log.h"
 
 // Sometimes we want to try to continue even after hitting an assert.
 // However touching this file yields a global recompilation as this header is included almost
 // everywhere. So let's just move the handling of the failed assert to a single cpp file.
 
-// For asserts we'd like to keep all the junk executed when an assert happens away from the
-// important code in the function. One way of doing this is to put all the relevant code inside a
-// lambda and force the compiler to not inline it.
-void assert_check_condition(bool cond, std::function<void()>&& on_failure);
-
+void assert_fail_impl();
 [[noreturn]] void unreachable_impl();
 
+#ifdef _MSC_VER
+#define YUZU_NO_INLINE __declspec(noinline)
+#else
+#define YUZU_NO_INLINE __attribute__((noinline))
+#endif
+
 #define ASSERT(_a_)                                                                                \
-    do {                                                                                           \
-        if (std::is_constant_evaluated()) {                                                        \
-            if (!(_a_)) {                                                                          \
-                /* Will trigger compile error here */                                              \
-                assert_check_condition(bool(_a_),                                                  \
-                                       [] { LOG_CRITICAL(Debug, "Assertion Failed!"); });          \
-            }                                                                                      \
-        } else {                                                                                   \
-            assert_check_condition(bool(_a_), [] { LOG_CRITICAL(Debug, "Assertion Failed!"); });   \
+    ([&]() YUZU_NO_INLINE {                                                                        \
+        if (!(_a_)) [[unlikely]] {                                                                 \
+            LOG_CRITICAL(Debug, "Assertion Failed!");                                              \
+            assert_fail_impl();                                                                    \
         }                                                                                          \
-    } while (0)
+    }())
 
 #define ASSERT_MSG(_a_, ...)                                                                       \
-    do {                                                                                           \
-        if (std::is_constant_evaluated()) {                                                        \
-            if (!(_a_)) {                                                                          \
-                /* Will trigger compile error here */                                              \
-                assert_check_condition(bool(_a_),                                                  \
-                                       [] { LOG_CRITICAL(Debug, "Assertion Failed!"); });          \
-            }                                                                                      \
-        } else {                                                                                   \
-            assert_check_condition(                                                                \
-                bool(_a_), [&] { LOG_CRITICAL(Debug, "Assertion Failed!\n" __VA_ARGS__); });       \
+    ([&]() YUZU_NO_INLINE {                                                                        \
+        if (!(_a_)) [[unlikely]] {                                                                 \
+            LOG_CRITICAL(Debug, "Assertion Failed!\n" __VA_ARGS__);                                \
+            assert_fail_impl();                                                                    \
         }                                                                                          \
-    } while (0)
+    }())
 
 #define UNREACHABLE()                                                                              \
     do {                                                                                           \