Skip to content

fix: Element access operator should derive index type properly from overloaded signature #2468

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 15 commits into from
Aug 26, 2022
21 changes: 18 additions & 3 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5596,8 +5596,11 @@ export class Compiler extends DiagnosticEmitter {
}
return this.module.unreachable();
}
assert(indexedSet.signature.parameterTypes.length == 2); // parser must guarantee this
targetType = indexedSet.signature.parameterTypes[1]; // 2nd parameter is the element
let parameterTypes = indexedSet.signature.parameterTypes;

assert(parameterTypes.length == 2); // parser must guarantee this
targetType = parameterTypes[1]; // 2nd parameter is the element

if (indexedSet.hasDecorator(DecoratorFlags.UNSAFE)) this.checkUnsafe(expression);
if (!isUnchecked && this.options.pedantic) {
this.pedantic(
Expand Down Expand Up @@ -5788,7 +5791,19 @@ export class Compiler extends DiagnosticEmitter {
thisType,
Constraints.CONV_IMPLICIT | Constraints.IS_THIS
);
let elementExpr = this.compileExpression(assert(indexExpression), Type.i32, Constraints.CONV_IMPLICIT);
let setterIndexType = setterInstance.signature.parameterTypes[0];
let getterIndexType = getterInstance.signature.parameterTypes[0];
if (!setterIndexType.equals(getterIndexType)) {
this.errorRelated(
DiagnosticCode.Index_signature_accessors_in_type_0_differ_in_types,
getterInstance.identifierAndSignatureRange,
setterInstance.identifierAndSignatureRange,
classInstance.internalName,
);
this.currentType = tee ? getterInstance.signature.returnType : Type.void;
return module.unreachable();
}
let elementExpr = this.compileExpression(assert(indexExpression), setterIndexType, Constraints.CONV_IMPLICIT);
let elementType = this.currentType;
if (tee) {
let tempTarget = flow.getTempLocal(thisType);
Expand Down
1 change: 1 addition & 0 deletions src/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"Expression does not compile to a value at runtime.": 234,
"Only variables, functions and enums become WebAssembly module exports.": 235,
"Literal '{0}' does not fit into 'i64' or 'u64' types.": 236,
"Index signature accessors in type '{0}' differ in types.": 237,

"Importing the table disables some indirect call optimizations.": 901,
"Exporting the table disables some indirect call optimizations.": 902,
Expand Down
Loading