Skip to content

Commit dc374d3

Browse files
hajimehoshineelance
authored andcommitted
Fix evaluation order of type conversions
1 parent 7cde3d3 commit dc374d3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

compiler/expressions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ func (c *funcContext) translateExpr(expr ast.Expr) *expression {
539539
plainFun := astutil.RemoveParens(e.Fun)
540540

541541
if astutil.IsTypeExpr(plainFun, c.p.Info.Info) {
542-
return c.formatExpr("%s", c.translateConversion(e.Args[0], c.p.TypeOf(plainFun)))
542+
return c.formatExpr("(%s)", c.translateConversion(e.Args[0], c.p.TypeOf(plainFun)))
543543
}
544544

545545
sig := c.p.TypeOf(plainFun).Underlying().(*types.Signature)

tests/misc_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,14 @@ func TestReceiverCapture(t *testing.T) {
612612
t.Fail()
613613
}
614614
}
615+
616+
func TestTypeConversion(t *testing.T) {
617+
i1, i2, i3 := 4, 2, 2
618+
if (i1-i2)/i3 != int(i1-i2)/int(i3) {
619+
t.Fail()
620+
}
621+
f1, f2, f3 := 4.0, 2.0, 2.0
622+
if (f1-f2)/f3 != float64(f1-f2)/float64(f3) {
623+
t.Fail()
624+
}
625+
}

0 commit comments

Comments
 (0)