Skip to content

fix: forbidden override between generic function and concrete generic function#3014

Open
Changqing-JING wants to merge 2 commits intoAssemblyScript:mainfrom
Changqing-JING:bugfix/generic-override
Open

fix: forbidden override between generic function and concrete generic function#3014
Changqing-JING wants to merge 2 commits intoAssemblyScript:mainfrom
Changqing-JING:bugfix/generic-override

Conversation

@Changqing-JING
Copy link
Copy Markdown
Contributor

Fixes #3013 .

Changes proposed in this pull request:
In current assemblyscript,

  1. when concrete generic function override generic function
    Compile crashed
let called: string = "";

class A {
  foo<T>(x: T): void {
    called = "A";
  }
}

class B extends A {
  foo(x: i32): void {
    called = "B";
  }
}

let a: A = new B();
a.foo<i32>(1);
assert(called == "B");

Error message

node ./bin/asc.js --debug -o build/test.wasm -t build/test.wat test.ts
2
Debugger attached.

▌ Whoops, the AssemblyScript compiler has crashed during compile :-(
▌ 
▌ Here is the stack trace hinting at the problem, perhaps it's useful?

▌ AssertionError: assertion failed
▌     at assert2 (/Users/Q479807/workspace/github/assemblyscript/std/portable/index.js:216:11)
▌     at Resolver.resolveFunction (/Users/Q479807/workspace/github/assemblyscript/src/resolver.ts:2790:7)
▌     at Resolver.resolveOverrides (/Users/Q479807/workspace/github/assemblyscript/src/resolver.ts:3077:39)
▌     at _Compiler.compile (/Users/Q479807/workspace/github/assemblyscript/src/compiler.ts:615:42)
▌     at Module.compile (/Users/Q479807/workspace/github/assemblyscript/src/index-wasm.ts:358:32)
▌     at Module.main (/Users/Q479807/workspace/github/assemblyscript/cli/index.js:732:31)
▌     at async file:///Users/Q479807/workspace/github/assemblyscript/bin/asc.js:33:22

▌ If you see where the error is, feel free to send us a pull request. If not,
▌ please let us know: https://github.com/AssemblyScript/assemblyscript/issues

▌ Thank you!
  1. When generic function override concrete generic function
    The output is different with typescript.
    In typescript the output is B
    But in assemblyscript, the output is A
type i32 = number;
let trace = console.log;
class A {
  foo(x: i32): void {
    trace("A");
  }
}

class B extends A {
  foo<T>(x: T): void {
    trace("B");
  }
}

let a:A = new B();
a.foo(1);

Proposed solution

Assemblyscript can't follow typescript's behavior when override between generic and concrete generic function. Because typescript uses structural typing for type checking and erase type for code emitting.
But assemblyscript use nominal typing and need the type for code emitting. For example, in case 2, if assemblyscript need to follow typescript's behavior, it need a complex typing inference system to emit function B::foo. I don't think it's worth the effort

So this PR just totally forbidden override between generic function and concrete generic function

  • I've read the contributing guidelines
  • I've added my name and email to the NOTICE file

@Changqing-JING Changqing-JING marked this pull request as draft April 11, 2026 13:12
@Changqing-JING Changqing-JING marked this pull request as ready for review April 11, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

compiler crashed when generic function override non generic function

1 participant