Skip to content

Commit

Permalink
fix: assign operator falling in unreachable case when operand is of p…
Browse files Browse the repository at this point in the history
…rimitive type
  • Loading branch information
saffage committed May 6, 2024
1 parent 484758f commit 2d059ea
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions checker/type_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,17 +413,21 @@ func typeCheckInfixOp(node *ast.InfixOp, scope *Scope) (types.Type, error) {
return nil, NewErrorf(node, "type mismatch (%s and %s)", tOperandX, tOperandY)
}

if p, ok := types.SkipAlias(tOperandX).Underlying().(*types.Primitive); ok {
if node.Opr.Kind == ast.OperatorAssign {
return types.Unit, nil
}

if primitive := types.AsPrimitive(tOperandX); primitive != nil {
switch node.Opr.Kind {
case ast.OperatorAdd, ast.OperatorSub, ast.OperatorMul, ast.OperatorDiv, ast.OperatorMod,
ast.OperatorBitAnd, ast.OperatorBitOr, ast.OperatorBitXor, ast.OperatorBitShl, ast.OperatorBitShr:
switch p.Kind() {
switch primitive.Kind() {
case types.UntypedInt, types.UntypedFloat, types.I32:
return tOperandX, nil
}

case ast.OperatorEq, ast.OperatorNe, ast.OperatorLt, ast.OperatorLe, ast.OperatorGt, ast.OperatorGe:
switch p.Kind() {
switch primitive.Kind() {
case types.UntypedBool, types.UntypedInt, types.UntypedFloat:
return types.Primitives[types.UntypedBool], nil

Expand All @@ -436,10 +440,6 @@ func typeCheckInfixOp(node *ast.InfixOp, scope *Scope) (types.Type, error) {
}
}

if node.Opr.Kind == ast.OperatorAssign {
return types.Unit, nil
}

return nil, NewErrorf(
node.Opr,
"operator '%s' is not defined for the type '%s'",
Expand Down

0 comments on commit 2d059ea

Please sign in to comment.