Skip to content

Commit 141e350

Browse files
authored
fix: Element access operator should derive index type properly from overloaded signature (#2468)
1 parent 665b937 commit 141e350

File tree

5 files changed

+2327
-1383
lines changed

5 files changed

+2327
-1383
lines changed

Diff for: src/compiler.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -5596,8 +5596,11 @@ export class Compiler extends DiagnosticEmitter {
55965596
}
55975597
return this.module.unreachable();
55985598
}
5599-
assert(indexedSet.signature.parameterTypes.length == 2); // parser must guarantee this
5600-
targetType = indexedSet.signature.parameterTypes[1]; // 2nd parameter is the element
5599+
let parameterTypes = indexedSet.signature.parameterTypes;
5600+
5601+
assert(parameterTypes.length == 2); // parser must guarantee this
5602+
targetType = parameterTypes[1]; // 2nd parameter is the element
5603+
56015604
if (indexedSet.hasDecorator(DecoratorFlags.UNSAFE)) this.checkUnsafe(expression);
56025605
if (!isUnchecked && this.options.pedantic) {
56035606
this.pedantic(
@@ -5788,7 +5791,19 @@ export class Compiler extends DiagnosticEmitter {
57885791
thisType,
57895792
Constraints.CONV_IMPLICIT | Constraints.IS_THIS
57905793
);
5791-
let elementExpr = this.compileExpression(assert(indexExpression), Type.i32, Constraints.CONV_IMPLICIT);
5794+
let setterIndexType = setterInstance.signature.parameterTypes[0];
5795+
let getterIndexType = getterInstance.signature.parameterTypes[0];
5796+
if (!setterIndexType.equals(getterIndexType)) {
5797+
this.errorRelated(
5798+
DiagnosticCode.Index_signature_accessors_in_type_0_differ_in_types,
5799+
getterInstance.identifierAndSignatureRange,
5800+
setterInstance.identifierAndSignatureRange,
5801+
classInstance.internalName,
5802+
);
5803+
this.currentType = tee ? getterInstance.signature.returnType : Type.void;
5804+
return module.unreachable();
5805+
}
5806+
let elementExpr = this.compileExpression(assert(indexExpression), setterIndexType, Constraints.CONV_IMPLICIT);
57925807
let elementType = this.currentType;
57935808
if (tee) {
57945809
let tempTarget = flow.getTempLocal(thisType);

Diff for: src/diagnosticMessages.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"Expression does not compile to a value at runtime.": 234,
4949
"Only variables, functions and enums become WebAssembly module exports.": 235,
5050
"Literal '{0}' does not fit into 'i64' or 'u64' types.": 236,
51+
"Index signature accessors in type '{0}' differ in types.": 237,
5152

5253
"Importing the table disables some indirect call optimizations.": 901,
5354
"Exporting the table disables some indirect call optimizations.": 902,

0 commit comments

Comments
 (0)