Skip to content

Commit 64425ab

Browse files
committed
fix bugs
1 parent cbc29df commit 64425ab

4 files changed

+4235
-84
lines changed

src/compiler.ts

+28-40
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,6 @@ export const enum RuntimeFeatures {
395395
setArgumentsLength = 1 << 6
396396
}
397397

398-
class CheckBinaryOperatorOverloadResult {
399-
constructor(
400-
public overload: Function | null,
401-
public isAmbiguity: bool,
402-
) {}
403-
}
404-
405398
/** Imported default names of compiler-generated elements. */
406399
export namespace ImportNames {
407400
/** Name of the default namespace */
@@ -3790,22 +3783,6 @@ export class Compiler extends DiagnosticEmitter {
37903783
private i32PowInstance: Function | null = null;
37913784
private i64PowInstance: Function | null = null;
37923785

3793-
private checkCommutativeBinaryOperatorOverload(
3794-
leftType: Type, rightType: Type, token: Token, reportNode: Expression
3795-
): CheckBinaryOperatorOverloadResult {
3796-
// check operator overload
3797-
const operator = OperatorKind.fromBinaryToken(token);
3798-
const leftOverload = leftType.lookupOverload(operator, this.program);
3799-
const rightOverload = rightType.lookupOverload(operator, this.program);
3800-
if (leftOverload && rightOverload && leftOverload != rightOverload) {
3801-
this.error(DiagnosticCode.Operator_0_overloading_ambiguity, reportNode.range, operatorTokenToString(token));
3802-
return new CheckBinaryOperatorOverloadResult(null, true);
3803-
}
3804-
if (leftOverload) return new CheckBinaryOperatorOverloadResult(leftOverload, false);
3805-
if (rightOverload) return new CheckBinaryOperatorOverloadResult(rightOverload, false);
3806-
return new CheckBinaryOperatorOverloadResult(null, false);
3807-
}
3808-
38093786
private compileCommutativeBinaryExpression(
38103787
expression: BinaryExpression,
38113788
contextualType: Type,
@@ -3828,21 +3805,32 @@ export class Compiler extends DiagnosticEmitter {
38283805

38293806
rightExpr = this.compileExpression(right, leftType);
38303807
rightType = this.currentType;
3831-
3832-
let checkOverloadResult = this.checkCommutativeBinaryOperatorOverload(leftType, rightType, operator, expression);
3833-
if (checkOverloadResult.isAmbiguity) {
3808+
3809+
// check operator overload
3810+
const operatorKind = OperatorKind.fromBinaryToken(operator);
3811+
const leftOverload = leftType.lookupOverload(operatorKind, this.program);
3812+
const rightOverload = rightType.lookupOverload(operatorKind, this.program);
3813+
if (leftOverload && rightOverload && leftOverload != rightOverload) {
3814+
this.error(DiagnosticCode.Operator_0_overloading_ambiguity, expression.range, operatorTokenToString(operator));
38343815
this.currentType = contextualType;
38353816
return module.unreachable();
38363817
}
3837-
let overload = checkOverloadResult.overload;
3838-
if (overload) {
3818+
if (leftOverload) {
38393819
return this.compileCommutativeBinaryOverload(
3840-
overload,
3820+
leftOverload,
38413821
left, leftExpr, leftType,
38423822
right, rightExpr, rightType,
38433823
expression
38443824
);
38453825
}
3826+
if (rightOverload) {
3827+
return this.compileCommutativeBinaryOverload(
3828+
rightOverload,
3829+
right, rightExpr, rightType,
3830+
left, leftExpr, leftType,
3831+
expression
3832+
);
3833+
}
38463834
const signednessIsRelevant =
38473835
operator == Token.LessThan ||
38483836
operator == Token.GreaterThan ||
@@ -5493,24 +5481,24 @@ export class Compiler extends DiagnosticEmitter {
54935481

54945482
private compileCommutativeBinaryOverload(
54955483
operatorInstance: Function,
5496-
left: Expression,
5497-
leftExpr: ExpressionRef,
5498-
leftType: Type,
5499-
right: Expression,
5500-
rightExpr: ExpressionRef,
5501-
rightType: Type,
5484+
first: Expression,
5485+
firstExpr: ExpressionRef,
5486+
firstType: Type,
5487+
second: Expression,
5488+
secondExpr: ExpressionRef,
5489+
secondType: Type,
55025490
reportNode: Node
55035491
): ExpressionRef {
55045492
let signature = operatorInstance.signature;
55055493
let parameterTypes = signature.parameterTypes;
55065494
if (operatorInstance.is(CommonFlags.Instance)) {
5507-
leftExpr = this.convertExpression(leftExpr, leftType, assert(signature.thisType), false, left);
5508-
rightExpr = this.convertExpression(rightExpr, rightType, parameterTypes[0], false, right);
5495+
firstExpr = this.convertExpression(firstExpr, firstType, assert(signature.thisType), false, first);
5496+
secondExpr = this.convertExpression(secondExpr, secondType, parameterTypes[0], false, second);
55095497
} else {
5510-
leftExpr = this.convertExpression(leftExpr, leftType, parameterTypes[0], false, left);
5511-
rightExpr = this.convertExpression(rightExpr, rightType, parameterTypes[1], false, right);
5498+
firstExpr = this.convertExpression(firstExpr, firstType, parameterTypes[0], false, first);
5499+
secondExpr = this.convertExpression(secondExpr, secondType, parameterTypes[1], false, second);
55125500
}
5513-
return this.makeCallDirect(operatorInstance, [ leftExpr, rightExpr ], reportNode);
5501+
return this.makeCallDirect(operatorInstance, [ firstExpr, secondExpr ], reportNode);
55145502
}
55155503

55165504
private compileAssignment(

0 commit comments

Comments
 (0)