Skip to content

fix: select correct override function #2820

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 13 additions & 1 deletion src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6618,6 +6618,7 @@ export class Compiler extends DiagnosticEmitter {
);
let overrideInstances = this.resolver.resolveOverrides(instance);
if (overrideInstances) {
let mostRecentInheritanceMapping = new Map<Class, Class>();
for (let i = 0, k = overrideInstances.length; i < k; ++i) {
let overrideInstance = overrideInstances[i];
if (!overrideInstance.is(CommonFlags.Compiled)) continue; // errored
Expand Down Expand Up @@ -6680,7 +6681,18 @@ export class Compiler extends DiagnosticEmitter {
if (instanceMembers && instanceMembers.has(instance.declaration.name.text)) {
continue; // skip those not inheriting
}
builder.addCase(extender.id, stmts);
if (mostRecentInheritanceMapping.has(extender)) {
let currentRecentParent = assert(mostRecentInheritanceMapping.get(extender));
if (currentRecentParent.extends(classInstance)) {
// already find recent parent, keep old stmt
} else {
mostRecentInheritanceMapping.set(extender, classInstance);
builder.replaceCase(extender.id, stmts);
}
} else {
mostRecentInheritanceMapping.set(extender, classInstance);
builder.addCase(extender.id, stmts);
}
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3418,16 +3418,27 @@ export class SwitchBuilder {
this.condition = condition;
}

/** Replace case to the specified branch. */
replaceCase(value: i32, code: ExpressionRef[]): void {
let valueIndex = this.values.indexOf(value);
assert(valueIndex >= 0);
this.indexes[valueIndex] = this.addCode(code);
}

/** Links a case to the specified branch. */
addCase(value: i32, code: ExpressionRef[]): void {
this.values.push(value);
this.indexes.push(this.addCode(code));
}

private addCode(code: ExpressionRef[]): i32 {
let cases = this.cases;
let index = cases.indexOf(code);
if (index < 0) {
index = cases.length;
cases.push(code);
}
this.values.push(value);
this.indexes.push(index);
return index;
}

/** Links the default branch. */
Expand Down
Loading