From 2e60882fc780378af58c12d82ff7f62ed88f4ee0 Mon Sep 17 00:00:00 2001 From: Alberto Donizetti Date: Wed, 12 Apr 2017 20:57:13 +0200 Subject: [PATCH] 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/40453 Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/gc/const.go | 31 ++++++++++++++-------------- test/fixedbugs/issue19947.go | 15 ++++++++++++++ 2 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 test/fixedbugs/issue19947.go diff --git a/src/cmd/compile/internal/gc/const.go b/src/cmd/compile/internal/gc/const.go index 853a0569e6..f9ea92ce50 100644 --- a/src/cmd/compile/internal/gc/const.go +++ b/src/cmd/compile/internal/gc/const.go @@ -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 index 0000000000..3233469e39 --- /dev/null +++ b/test/fixedbugs/issue19947.go @@ -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" -- 2.48.1