Skip to content

Commit de174c5

Browse files
authoredMar 8, 2024
fix: select correct override function (#2822)
1 parent 4655745 commit de174c5

File tree

6 files changed

+4472
-3
lines changed

6 files changed

+4472
-3
lines changed
 

‎src/compiler.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -6618,6 +6618,7 @@ export class Compiler extends DiagnosticEmitter {
66186618
);
66196619
let overrideInstances = this.resolver.resolveOverrides(instance);
66206620
if (overrideInstances) {
6621+
let mostRecentInheritanceMapping = new Map<Class, Class>();
66216622
for (let i = 0, k = overrideInstances.length; i < k; ++i) {
66226623
let overrideInstance = overrideInstances[i];
66236624
if (!overrideInstance.is(CommonFlags.Compiled)) continue; // errored
@@ -6680,7 +6681,13 @@ export class Compiler extends DiagnosticEmitter {
66806681
if (instanceMembers && instanceMembers.has(instance.declaration.name.text)) {
66816682
continue; // skip those not inheriting
66826683
}
6683-
builder.addCase(extender.id, stmts);
6684+
if (
6685+
!mostRecentInheritanceMapping.has(extender) ||
6686+
!assert(mostRecentInheritanceMapping.get(extender)).extends(classInstance)
6687+
) {
6688+
mostRecentInheritanceMapping.set(extender, classInstance);
6689+
builder.addOrReplaceCase(extender.id, stmts);
6690+
}
66846691
}
66856692
}
66866693
}

‎src/module.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -3418,16 +3418,32 @@ export class SwitchBuilder {
34183418
this.condition = condition;
34193419
}
34203420

3421+
/** Links a case to the specified branch, replace old case if it is linked. */
3422+
addOrReplaceCase(value: i32, code: ExpressionRef[]): void {
3423+
const valueIndex = this.values.indexOf(value);
3424+
const codeIndex = this.addCode(code);
3425+
if (valueIndex >= 0) {
3426+
this.indexes[valueIndex] = codeIndex;
3427+
} else {
3428+
this.values.push(value);
3429+
this.indexes.push(codeIndex);
3430+
}
3431+
}
3432+
34213433
/** Links a case to the specified branch. */
34223434
addCase(value: i32, code: ExpressionRef[]): void {
3435+
this.values.push(value);
3436+
this.indexes.push(this.addCode(code));
3437+
}
3438+
3439+
private addCode(code: ExpressionRef[]): i32 {
34233440
let cases = this.cases;
34243441
let index = cases.indexOf(code);
34253442
if (index < 0) {
34263443
index = cases.length;
34273444
cases.push(code);
34283445
}
3429-
this.values.push(value);
3430-
this.indexes.push(index);
3446+
return index;
34313447
}
34323448

34333449
/** Links the default branch. */

‎tests/compiler/class-override.debug.wat

+2,673
Large diffs are not rendered by default.

‎tests/compiler/class-override.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"asc_flags": [
3+
]
4+
}

‎tests/compiler/class-override.release.wat

+1,745
Large diffs are not rendered by default.

‎tests/compiler/class-override.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export function fn(n: i32): i32 {
2+
return x.f(n);
3+
}
4+
5+
class A {
6+
f(a: i32): i32 {
7+
return a + 1;
8+
}
9+
}
10+
class B extends A {
11+
f(a: i32): i32 {
12+
return super.f(a) + 10;
13+
}
14+
}
15+
class C extends B {
16+
f(a: i32): i32 {
17+
return super.f(a) + 100;
18+
}
19+
}
20+
class D extends C {}
21+
22+
let x: A = new D();
23+
24+
assert(fn(0) == 111);

0 commit comments

Comments
 (0)
Please sign in to comment.