Skip to content

fix: instance method cannot inherit class generic (#2166) #2167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 8, 2021

Conversation

HerrCai0907
Copy link
Member

⯈For class instance method, ctxtypes should use class generic type instead of context. So override is more reasonable.

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

@dcodeIO
Copy link
Member

dcodeIO commented Dec 8, 2021

Iirc the check was there for situations like

class Foo<T> {
  bar<T>() { baz<T>(); } // here
}
function baz<U>() { ... }

where contextual types of the method take precedence over the class's. Before I merge, can you confirm that this case isn't regressing? :)

@HerrCai0907
Copy link
Member Author

Iirc the check was there for situations like

class Foo<T> {
  bar<T>() { baz<T>(); } // here
}
function baz<U>() { ... }

where contextual types of the method take precedence over the class's. Before I merge, can you confirm that this case isn't regressing? :)

The result is OK, I add them into test.
After overridden by class generic, ctxtype will be overridden by function generic type again. see below code.

    // override whatever is contextual with actual function type arguments
    var signatureNode = prototype.functionTypeNode;
    var typeParameterNodes = prototype.typeParameterNodes;
    var numFunctionTypeArguments: i32;
    if (typeArguments !== null && (numFunctionTypeArguments = typeArguments.length) > 0) {
      assert(typeParameterNodes !== null && numFunctionTypeArguments == typeParameterNodes.length);
      for (let i = 0; i < numFunctionTypeArguments; ++i) {
        ctxTypes.set(
          (<TypeParameterNode[]>typeParameterNodes)[i].name.text,
          typeArguments[i]
        );
      }
    } else {
      assert(!typeParameterNodes || typeParameterNodes.length == 0);
    }

They ctxtype process flow is like that:

  • maybeInferCall will infer generic of fn by outside function type (T-1) . In this step, class generic should not be used.
  • resolveFunction will override context type with class generic of instance method (T-2) and then override it again with fn generic types which are inferred in step1.

So it seems like matching the expectations.

function testfunc2166<T-1>(): void {
  let a = new Test2166Ref1<string>();
  // class generic should override non-related function generic(testfunc2166): T in fn is string
  a.fn("11", 1);
}
class Test2166Ref1<T-2> {
  fn<U>(a1: T, a2: U): void {
  }
}
testfunc2166<i64>();

@dcodeIO
Copy link
Member

dcodeIO commented Dec 8, 2021

Makes sense, thanks. Good to now have a test case as well :)

@dcodeIO dcodeIO merged commit fd1e1df into AssemblyScript:main Dec 8, 2021
@HerrCai0907 HerrCai0907 deleted the issue-2166 branch May 15, 2022 11:21
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.

2 participants