From ade9a84987cbef4105ae3e5eda3832c14317c7d8 Mon Sep 17 00:00:00 2001
From: Martin Kinkelin <noone@nowhere.com>
Date: Thu, 16 Jul 2020 02:30:29 +0200
Subject: [PATCH] Fix ICE wrt. inline IR and empty parameter types tuple

---
 gen/inlineir.cpp                   | 12 +++---------
 tests/codegen/inline_ir_noparams.d | 25 +++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 9 deletions(-)
 create mode 100644 tests/codegen/inline_ir_noparams.d

diff --git a/gen/inlineir.cpp b/gen/inlineir.cpp
index b0062204915..b9cedfd706d 100644
--- a/gen/inlineir.cpp
+++ b/gen/inlineir.cpp
@@ -162,9 +162,8 @@ DValue *DtoInlineIRExpr(Loc &loc, FuncDeclaration *fdecl,
     }
     stream << "define " << *DtoType(ret) << " @" << mangled_name << "(";
 
-    for (size_t i = 0;;) {
+    for (size_t i = 0; i < arg_types.length; ++i) {
       Type *ty = isType(arg_types[i]);
-      // assert(ty);
       if (!ty) {
         error(tinst->loc,
               "All parameters of a template defined with pragma "
@@ -172,14 +171,9 @@ DValue *DtoInlineIRExpr(Loc &loc, FuncDeclaration *fdecl,
               ", should be types");
         fatal();
       }
+      if (i != 0)
+        stream << ", ";
       stream << *DtoType(ty);
-
-      i++;
-      if (i >= arg_types.length) {
-        break;
-      }
-
-      stream << ", ";
     }
 
     stream << ")\n{\n" << code;
diff --git a/tests/codegen/inline_ir_noparams.d b/tests/codegen/inline_ir_noparams.d
new file mode 100644
index 00000000000..815d20d1051
--- /dev/null
+++ b/tests/codegen/inline_ir_noparams.d
@@ -0,0 +1,25 @@
+// RUN: %ldc -O -output-ll -of=%t.ll %s && FileCheck %s < %t.ll
+
+// CHECK: define {{.*}}11unreachableFZv
+void unreachable()
+{
+    import ldc.llvmasm;
+    // CHECK-NEXT: unreachable
+    __ir!("unreachable", void)();
+// CHECK-NEXT: }
+}
+
+extern bool flag;
+
+// CHECK: define {{.*}}3bar
+int bar()
+{
+    int r = 123;
+    if (flag)
+    {
+        r = 456;
+        unreachable();
+    }
+    // CHECK: ret i32 123
+    return r;
+}