]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: never report "truncated to real" for toint calls
authorAlberto Donizetti <alb.donizetti@gmail.com>
Wed, 21 Dec 2016 10:07:11 +0000 (11:07 +0100)
committerRobert Griesemer <gri@golang.org>
Wed, 1 Feb 2017 21:22:50 +0000 (21:22 +0000)
Whoever called toint() is expecting the {Mpint, Mpflt, Mpcplx} arg to
be converted to an integer expression, so it never makes sense to
report an error as "constant X truncated to real".

Fixes #11580

Change-Id: Iadcb105f0802358a7f77188c2b1e63fe80c5580c
Reviewed-on: https://go-review.googlesource.com/34638
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>

src/cmd/compile/internal/gc/const.go
test/fixedbugs/issue16439.go

index a2146ebef17d48fc95b1a10e4c52df6fe3a89b00..48b98b798a9befced541590271cab97d5cb6af36 100644 (file)
@@ -458,12 +458,10 @@ func toint(v Val) Val {
 
        case *Mpcplx:
                i := new(Mpint)
-               if i.SetFloat(&u.Real) < 0 {
+               if i.SetFloat(&u.Real) < 0 || u.Imag.CmpFloat64(0) != 0 {
                        yyerror("constant %v%vi truncated to integer", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp|FmtSign))
                }
-               if u.Imag.CmpFloat64(0) != 0 {
-                       yyerror("constant %v%vi truncated to real", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp|FmtSign))
-               }
+
                v.U = i
        }
 
index d321b6083efff2b2dec8f11807bd3757b502ddf3..f9382bafcd907e55c3e4fc2edd5f37eaa6952ae7 100644 (file)
@@ -13,6 +13,6 @@ var c []int = []int{2.0: 2}
 var d []int = []int{-2.0: 2} // ERROR "must be non-negative integer constant"
 
 var e []int = []int{3 + 0i: 3}
-var f []int = []int{3i: 3} // ERROR "truncated to real"
+var f []int = []int{3i: 3} // ERROR "truncated to integer"
 
 var g []int = []int{"a": 4} // ERROR "must be non-negative integer constant"