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
18 changes: 15 additions & 3 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4629,6 +4629,7 @@ export class Compiler extends DiagnosticEmitter {
this.currentType,
right,
resolver.currentThisExpression,
Type.i32,
resolver.currentElementExpression,
contextualType != Type.void
);
Expand Down Expand Up @@ -5477,6 +5478,7 @@ export class Compiler extends DiagnosticEmitter {
if (!target) return this.module.unreachable();
var thisExpression = resolver.currentThisExpression;
var elementExpression = resolver.currentElementExpression;
var elementType = Type.i32;

// to compile just the value, we need to know the target's type
var targetType: Type;
Expand Down Expand Up @@ -5543,8 +5545,12 @@ 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
elementType = parameterTypes[0]; // 1st parameter is the index
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 @@ -5573,6 +5579,7 @@ export class Compiler extends DiagnosticEmitter {
valueType,
valueExpression,
thisExpression,
elementType,
elementExpression,
contextualType != Type.void
);
Expand All @@ -5590,6 +5597,8 @@ export class Compiler extends DiagnosticEmitter {
valueExpression: Expression,
/** `this` expression reference if a field or property set. */
thisExpression: Expression | null,
/** Index expression type. */
indexType: Type,
/** Index expression reference if an indexed set. */
indexExpression: Expression | null,
/** Whether to tee the value. */
Expand Down Expand Up @@ -5735,7 +5744,7 @@ export class Compiler extends DiagnosticEmitter {
thisType,
Constraints.CONV_IMPLICIT | Constraints.IS_THIS
);
let elementExpr = this.compileExpression(assert(indexExpression), Type.i32, Constraints.CONV_IMPLICIT);
let elementExpr = this.compileExpression(assert(indexExpression), indexType, Constraints.CONV_IMPLICIT);
let elementType = this.currentType;
if (tee) {
let tempTarget = flow.getTempLocal(thisType);
Expand Down Expand Up @@ -9164,6 +9173,7 @@ export class Compiler extends DiagnosticEmitter {
this.currentType,
expression.operand,
resolver.currentThisExpression,
Type.i32,
resolver.currentElementExpression,
false
);
Expand All @@ -9176,6 +9186,7 @@ export class Compiler extends DiagnosticEmitter {
this.currentType,
expression.operand,
resolver.currentThisExpression,
Type.i32,
resolver.currentElementExpression,
false
);
Expand Down Expand Up @@ -9541,6 +9552,7 @@ export class Compiler extends DiagnosticEmitter {
this.currentType,
expression.operand,
resolver.currentThisExpression,
Type.i32,
resolver.currentElementExpression,
contextualType != Type.void
);
Expand Down
Loading