Commit 2e60882f authored by Alberto Donizetti's avatar Alberto Donizetti Committed by Robert Griesemer

cmd/compile: do not print duplicate error on ideal->float{32,64} overflow

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/40453Reviewed-by: 's avatarRobert Griesemer <gri@golang.org>
parent c253ea47
...@@ -138,23 +138,24 @@ func truncfltlit(oldv *Mpflt, t *types.Type) *Mpflt { ...@@ -138,23 +138,24 @@ func truncfltlit(oldv *Mpflt, t *types.Type) *Mpflt {
return oldv return oldv
} }
var v Val if overflow(Val{oldv}, t) {
v.U = oldv // If there was overflow, simply continuing would set the
overflow(v, t) // 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 := newMpflt()
fv.Set(oldv)
// convert large precision literal floating // convert large precision literal floating
// into limited precision (float64 or float32) // into limited precision (float64 or float32)
switch t.Etype { switch t.Etype {
case types.TFLOAT32:
fv.SetFloat64(oldv.Float32())
case types.TFLOAT64: case types.TFLOAT64:
d := fv.Float64() fv.SetFloat64(oldv.Float64())
fv.SetFloat64(d) default:
Fatalf("truncfltlit: unexpected Etype %v", t.Etype)
case TFLOAT32:
d := fv.Float32()
fv.SetFloat64(d)
} }
return fv return fv
...@@ -169,19 +170,19 @@ func trunccmplxlit(oldv *Mpcplx, t *types.Type) *Mpcplx { ...@@ -169,19 +170,19 @@ func trunccmplxlit(oldv *Mpcplx, t *types.Type) *Mpcplx {
} }
if overflow(Val{oldv}, t) { if overflow(Val{oldv}, t) {
// Avoid setting to Inf if there was an overflow. It's never // If there was overflow, simply continuing would set the
// useful, and it'll cause spourious and confusing 'constant Inf // value to Inf which in turn would lead to spurious follow-on
// overflows float32' errors down the road. // errors. Avoid this by returning the existing value.
return oldv return oldv
} }
cv := newMpcmplx() cv := newMpcmplx()
switch t.Etype { switch t.Etype {
case TCOMPLEX64: case types.TCOMPLEX64:
cv.Real.SetFloat64(oldv.Real.Float32()) cv.Real.SetFloat64(oldv.Real.Float32())
cv.Imag.SetFloat64(oldv.Imag.Float32()) cv.Imag.SetFloat64(oldv.Imag.Float32())
case TCOMPLEX128: case types.TCOMPLEX128:
cv.Real.SetFloat64(oldv.Real.Float64()) cv.Real.SetFloat64(oldv.Real.Float64())
cv.Imag.SetFloat64(oldv.Imag.Float64()) cv.Imag.SetFloat64(oldv.Imag.Float64())
default: default:
......
// 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"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment