Skip to content

fix: report error when generic method attempts to implement non-generic interface method#3007

Open
Changqing-JING wants to merge 4 commits intoAssemblyScript:mainfrom
Changqing-JING:bugfix/interface-template-type
Open

fix: report error when generic method attempts to implement non-generic interface method#3007
Changqing-JING wants to merge 4 commits intoAssemblyScript:mainfrom
Changqing-JING:bugfix/interface-template-type

Conversation

@Changqing-JING
Copy link
Copy Markdown
Contributor

@Changqing-JING Changqing-JING commented Apr 3, 2026

Fixes #3006 .

Changes proposed in this pull request:
TypeScript allows this because it uses type erasure — generics disappear at runtime, so foo<T>() and foo() are the same function. AssemblyScript cannot do the same because it uses monomorphization — each type argument produces a distinct function, and the vtable needs a single fixed entry. This is the same reason C++ forbids template functions from overriding virtual functions.

The fix treats a generic method as not satisfying a non-generic interface method, matching C++ semantics.

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

@Changqing-JING Changqing-JING changed the title fix: report error when generic method attempts to implement non-gener… fix: report error when generic method attempts to implement non-generic interface method Apr 3, 2026
@Changqing-JING Changqing-JING marked this pull request as draft April 3, 2026 08:54
@Changqing-JING Changqing-JING marked this pull request as ready for review April 3, 2026 09:02
@Changqing-JING Changqing-JING marked this pull request as draft April 3, 2026 09:03
@Changqing-JING Changqing-JING marked this pull request as ready for review April 3, 2026 09:06
@Changqing-JING Changqing-JING force-pushed the bugfix/interface-template-type branch from 6463821 to c1f84f8 Compare April 3, 2026 09:08
@MaxGraey MaxGraey requested a review from CountBleck April 3, 2026 09:24
@Changqing-JING Changqing-JING requested a review from MaxGraey April 3, 2026 09:57
Copy link
Copy Markdown
Member

@MaxGraey MaxGraey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MaxGraey
Copy link
Copy Markdown
Member

MaxGraey commented Apr 3, 2026

The only thing that could be improved is to add a new error specific to AS. Such errors:

“TS2515: Non-abstract class ‘override-typeparam-mismatch/CC’ does not implement inherited 
abstract member ‘foo’ from ‘override-typeparam-mismatch/I’.”,

isn’t very informative, since the method is actually defined and has the correct signature. The only issue is incorrect parametric polymorphism.

Perhaps may sense to add new AS241 error for this

Comment on lines +3071 to +3073
if (numOverrideTypeParams == numBaseTypeArgs) {
overrideInstance = this.resolveFunction(boundFuncPrototype, baseTypeArgs);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we allow this implement when the generic number are the same? I think it will crash also if the typing mismatched.
IMO, maybe we should forbidden all generic function in interface?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I see only one edge case in this scenario:

interface I {
  add<T, U>(x: T, y: U): U;
}

class C2 implements I {
  add<U, T>(x: T, y: U): U {
    return x;
  }
}

Copy link
Copy Markdown
Contributor Author

@Changqing-JING Changqing-JING Apr 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. If we follow C++ design:

  1. template function aka. generic function can't be member in interface
  2. template function also can't override virtual function in interface.

Shall we do the same?

I would prefer to follow C++ design because generic function in interface is a confused behaviour
But it may break some existing code on user side.
What do maintainers prefer?

@Changqing-JING
Copy link
Copy Markdown
Contributor Author

The only thing that could be improved is to add a new error specific to AS. Such errors:

“TS2515: Non-abstract class ‘override-typeparam-mismatch/CC’ does not implement inherited 
abstract member ‘foo’ from ‘override-typeparam-mismatch/I’.”,

isn’t very informative, since the method is actually defined and has the correct signature. The only issue is incorrect parametric polymorphism.

Perhaps may sense to add new AS241 error for this

There I just referred the hehavior of clang. In clang, a generic(template) function isn't regarded as overriding of virtual interface function, so the interface function is regarded as "unimplemented"

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 crashes when a generic method is used to implement a non-generic interface method

3 participants