AssemblyScript / assemblyscript

A TypeScript-like language for WebAssembly.
https://www.assemblyscript.org
Apache License 2.0
16.6k stars 650 forks source link

Emit incorrect debugLocation while convertExpression() #2833

Open XMadrid opened 3 months ago

XMadrid commented 3 months ago

Bug description

function main(): void {
  add(1, 2);
}

For above code snippet, assemblyscript wraps a drop for the call add instruction. But there is no debuglocation for call instruction.

Steps to reproduce

The root cause is that, in the following code in assemblyscript. When asc wrap a drop epxr for the call epxr by execute convertExpression(), the expr variable(call expr) will be overwritten with (drop expr). And then addDebugLocation(drop). When returning to compileStatement after compileExpression, asc will addDebugLocation(drop) again. So we missed the debugLocation for call expr.

In src/compiler.ts line 3453-3468,

    if (currentType != contextualType.nonNullableType) { // allow assigning non-nullable to nullable
      if (constraints & Constraints.ConvExplicit) {
        expr = this.convertExpression(expr, currentType, contextualType, true, expression);
        this.currentType = currentType = contextualType;
      } else if (constraints & Constraints.ConvImplicit) {
        expr = this.convertExpression(expr, currentType, contextualType, false, expression);
        this.currentType = currentType = contextualType;
      }
    }
    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);

AssemblyScript version

0.27.25