From 0d086112128c24bc49b8f5fc92c8d09fb39983ef Mon Sep 17 00:00:00 2001
From: Xinquan XU <Xinquan.XU@partner.bmw.com>
Date: Wed, 3 Apr 2024 17:12:43 +0800
Subject: [PATCH 1/2] fix: addDebugInfo before convertExpression

---
 src/compiler.ts | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/compiler.ts b/src/compiler.ts
index 8e5ee85615..877a9c1805 100644
--- a/src/compiler.ts
+++ b/src/compiler.ts
@@ -3449,6 +3449,9 @@ export class Compiler extends DiagnosticEmitter {
         expr = this.module.unreachable();
       }
     }
+    // debug location is added here so the caller doesn't have to. means: compilation of an expression
+    // must go through this function, with the respective per-kind functions not being used directly.
+    if (this.options.sourceMap) this.addDebugLocation(expr, expression.range);
     // ensure conversion and wrapping in case the respective function doesn't on its own
     let currentType = this.currentType;
     let wrap = (constraints & Constraints.MustWrap) != 0;
@@ -3462,9 +3465,6 @@ export class Compiler extends DiagnosticEmitter {
       }
     }
     if (wrap) expr = this.ensureSmallIntegerWrap(expr, currentType);
-    // debug location is added here so the caller doesn't have to. means: compilation of an expression
-    // must go through this function, with the respective per-kind functions not being used directly.
-    if (this.options.sourceMap) this.addDebugLocation(expr, expression.range);
     return expr;
   }
 

From 42f559fa3257786feea91f8428ee71895163706e Mon Sep 17 00:00:00 2001
From: Xinquan XU <Xinquan.XU@partner.bmw.com>
Date: Sun, 7 Apr 2024 10:38:23 +0800
Subject: [PATCH 2/2] add debug location before conversion

---
 src/compiler.ts | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/compiler.ts b/src/compiler.ts
index 877a9c1805..7a701616a7 100644
--- a/src/compiler.ts
+++ b/src/compiler.ts
@@ -3449,22 +3449,28 @@ export class Compiler extends DiagnosticEmitter {
         expr = this.module.unreachable();
       }
     }
-    // debug location is added here so the caller doesn't have to. means: compilation of an expression
-    // must go through this function, with the respective per-kind functions not being used directly.
-    if (this.options.sourceMap) this.addDebugLocation(expr, expression.range);
     // ensure conversion and wrapping in case the respective function doesn't on its own
     let currentType = this.currentType;
     let wrap = (constraints & Constraints.MustWrap) != 0;
     if (currentType != contextualType.nonNullableType) { // allow assigning non-nullable to nullable
       if (constraints & Constraints.ConvExplicit) {
+        // emit debug location for inner expression before conversion
+        if (this.options.sourceMap) this.addDebugLocation(expr, expression.range);
         expr = this.convertExpression(expr, currentType, contextualType, true, expression);
         this.currentType = currentType = contextualType;
       } else if (constraints & Constraints.ConvImplicit) {
+        if (this.options.sourceMap) this.addDebugLocation(expr, expression.range);
         expr = this.convertExpression(expr, currentType, contextualType, false, expression);
         this.currentType = currentType = contextualType;
       }
     }
-    if (wrap) expr = this.ensureSmallIntegerWrap(expr, currentType);
+    if (wrap) {
+      if (this.options.sourceMap) this.addDebugLocation(expr, expression.range);
+      expr = this.ensureSmallIntegerWrap(expr, currentType);
+    }
+    // debug location is added here so the caller doesn't have to. means: compilation of an expression
+    // must go through this function, with the respective per-kind functions not being used directly.
+    if (this.options.sourceMap) this.addDebugLocation(expr, expression.range);
     return expr;
   }