Skip to content
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

fix #2727 Type checker does not detect a signature mismatch #2728

Merged
merged 3 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6611,7 +6611,9 @@ export class Compiler extends DiagnosticEmitter {
if (!overrideInstance.is(CommonFlags.Compiled)) continue; // errored
let overrideType = overrideInstance.type;
let originalType = instance.type;
if (!overrideType.isAssignableTo(originalType)) {

assert(originalType.getSignature() != null && overrideType.getSignature() != null);
if (!(overrideType.getSignature() as Signature).isAssignableTo(originalType.getSignature() as Signature, true)) {
Changqing-JING marked this conversation as resolved.
Show resolved Hide resolved
this.error(
DiagnosticCode.Type_0_is_not_assignable_to_type_1,
overrideInstance.identifierNode.range, overrideType.toString(), originalType.toString()
Expand Down
28 changes: 15 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1051,21 +1051,23 @@ export class Signature {
isAssignableTo(target: Signature, checkCompatibleOverride: bool = false): bool {
let thisThisType = this.thisType;
let targetThisType = target.thisType;
if (checkCompatibleOverride) {
// check kind of `this` type
if (thisThisType) {
if (!targetThisType || !thisThisType.canExtendOrImplement(targetThisType)) {

if (
(thisThisType == null && targetThisType != null) ||
(thisThisType != null && targetThisType == null)
) {
return false;
}else if(thisThisType != null && targetThisType != null){
if(checkCompatibleOverride){
Changqing-JING marked this conversation as resolved.
Show resolved Hide resolved
// check kind of `this` type
if(!thisThisType.canExtendOrImplement(targetThisType)){
return false;
}
}else{
// check `this` type (invariant)
if(!targetThisType.isAssignableTo(thisThisType)){
return false;
}
} else if (targetThisType) {
return false;
}
} else {
// check `this` type (invariant)
if (thisThisType) {
if (targetThisType != targetThisType) return false;
} else if (targetThisType) {
return false;
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/compiler/class-member-function-as-parameter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"asc_flags": [],
"stderr": [
"TS2322: Type '(this: class-member-function-as-parameter/C, i32) => i32' is not assignable to type '(i32) => i32'.",
"TS2322: Type '() => void' is not assignable to type '(this: class-member-function-as-parameter/B) => void'.",
Changqing-JING marked this conversation as resolved.
Show resolved Hide resolved
"EOF"
]
}
52 changes: 52 additions & 0 deletions tests/compiler/class-member-function-as-parameter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class C {
aa: i32 = 1;
callback(a: i32): i32 {
return this.aa + a + 3;
}
}

function expectCallback(c1: (arg0: i32) => i32): i32 {
return c1(4);
}

export function fut(): i32 {
const c1 = new C();
return expectCallback(c1.callback);
}

fut();

class A {
foo(): void {
console.log("A");
}
}

class B extends A {
foo(): void {
console.log("B");
}
}

function foo(): void {
console.log("nothing");
}

function consume(callback: (this: B) => void): void {
const b = new B();
callback.call(b);
}

export function testNull(): void {
consume(foo); // This should (and does) error; this is fine.
}

export function testA(): void {
const a = new A();
consume(a.foo); // This shouldn't error
}

testNull();
testA();

ERROR("EOF");
Changqing-JING marked this conversation as resolved.
Show resolved Hide resolved