]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: do not print duplicate error on ideal->float{32,64} overflow
authorAlberto Donizetti <alb.donizetti@gmail.com>
Wed, 12 Apr 2017 18:57:13 +0000 (20:57 +0200)
committerRobert Griesemer <gri@golang.org>
Wed, 12 Apr 2017 21:00:15 +0000 (21:00 +0000)
Also adjust truncfltlit to make it more similar to trunccmplxlit, and
make it report an error for bad Etypes.

Fixes #19947

Change-Id: I6684523e989c2293b8a8e85bd2bfb9c399c5ea36
Reviewed-on: https://go-review.googlesource.com/40453
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/gc/const.go
test/fixedbugs/issue19947.go [new file with mode: 0644]

index 853a0569e6046f4e17f354aa0697b0ffe9b06f15..f9ea92ce502de7383eeb8fd4e31e82150762a940 100644 (file)
@@ -138,23 +138,24 @@ func truncfltlit(oldv *Mpflt, t *types.Type) *Mpflt {
                return oldv
        }
 
-       var v Val
-       v.U = oldv
-       overflow(v, t)
+       if overflow(Val{oldv}, t) {
+               // If there was overflow, simply continuing would set the
+               // value to Inf which in turn would lead to spurious follow-on
+               // errors. Avoid this by returning the existing value.
+               return oldv
+       }
 
        fv := newMpflt()
-       fv.Set(oldv)
 
        // convert large precision literal floating
        // into limited precision (float64 or float32)
        switch t.Etype {
+       case types.TFLOAT32:
+               fv.SetFloat64(oldv.Float32())
        case types.TFLOAT64:
-               d := fv.Float64()
-               fv.SetFloat64(d)
-
-       case TFLOAT32:
-               d := fv.Float32()
-               fv.SetFloat64(d)
+               fv.SetFloat64(oldv.Float64())
+       default:
+               Fatalf("truncfltlit: unexpected Etype %v", t.Etype)
        }
 
        return fv
@@ -169,19 +170,19 @@ func trunccmplxlit(oldv *Mpcplx, t *types.Type) *Mpcplx {
        }
 
        if overflow(Val{oldv}, t) {
-               // Avoid setting to Inf if there was an overflow. It's never
-               // useful, and it'll cause spourious and confusing 'constant Inf
-               // overflows float32' errors down the road.
+               // If there was overflow, simply continuing would set the
+               // value to Inf which in turn would lead to spurious follow-on
+               // errors. Avoid this by returning the existing value.
                return oldv
        }
 
        cv := newMpcmplx()
 
        switch t.Etype {
-       case TCOMPLEX64:
+       case types.TCOMPLEX64:
                cv.Real.SetFloat64(oldv.Real.Float32())
                cv.Imag.SetFloat64(oldv.Imag.Float32())
-       case TCOMPLEX128:
+       case types.TCOMPLEX128:
                cv.Real.SetFloat64(oldv.Real.Float64())
                cv.Imag.SetFloat64(oldv.Imag.Float64())
        default:
diff --git a/test/fixedbugs/issue19947.go b/test/fixedbugs/issue19947.go
new file mode 100644 (file)
index 0000000..3233469
--- /dev/null
@@ -0,0 +1,15 @@
+// errorcheck
+
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// No double error on ideal -> float{32,64} conversion overflow
+
+package issue19947
+
+var _ = float32(1) * 1e200 // ERROR "constant 1e\+200 overflows float32"
+var _ = float64(1) * 1e500 // ERROR "constant 1e\+500 overflows float64"
+
+var _ = complex64(1) * 1e200  // ERROR "constant 1e\+200 overflows complex64"
+var _ = complex128(1) * 1e500 // ERROR "constant 1e\+500 overflows complex128"