fix: goexports performs correct conversion on float32 constants

This commit is contained in:
Marc Vertes
2019-10-29 10:20:05 +01:00
committed by Traefiker Bot
parent 15686873e0
commit 75a696a5c8
4 changed files with 26 additions and 4 deletions

13
_test/math1.go Normal file
View File

@@ -0,0 +1,13 @@
package main
import "math"
func main() {
var f float32
if f < math.MaxFloat32 {
println("ok")
}
}
// Output:
// ok

View File

@@ -254,7 +254,14 @@ func genContent(dest, pkgName, license string) ([]byte, error) {
// fixConst checks untyped constant value, converting it if necessary to avoid overflow
func fixConst(name string, val constant.Value) string {
if val.Kind() == constant.Int {
switch val.Kind() {
case constant.Float:
str := val.ExactString()
if _, err := strconv.ParseFloat(str, 32); err == nil {
return "float32(" + name + ")"
}
return name
case constant.Int:
str := val.ExactString()
i, err := strconv.ParseInt(str, 0, 64)
if err == nil {
@@ -271,8 +278,10 @@ func fixConst(name string, val constant.Value) string {
if err == nil {
return "uint64(" + name + ")"
}
return name
default:
return name
}
return name
}
// genLicense generates the correct LICENSE header text from the provided

View File

@@ -61,7 +61,7 @@ func init() {
"Log2E": reflect.ValueOf(math.Log2E),
"Logb": reflect.ValueOf(math.Logb),
"Max": reflect.ValueOf(math.Max),
"MaxFloat32": reflect.ValueOf(math.MaxFloat32),
"MaxFloat32": reflect.ValueOf(float32(math.MaxFloat32)),
"MaxFloat64": reflect.ValueOf(math.MaxFloat64),
"MaxInt16": reflect.ValueOf(math.MaxInt16),
"MaxInt32": reflect.ValueOf(math.MaxInt32),

View File

@@ -61,7 +61,7 @@ func init() {
"Log2E": reflect.ValueOf(math.Log2E),
"Logb": reflect.ValueOf(math.Logb),
"Max": reflect.ValueOf(math.Max),
"MaxFloat32": reflect.ValueOf(math.MaxFloat32),
"MaxFloat32": reflect.ValueOf(float32(math.MaxFloat32)),
"MaxFloat64": reflect.ValueOf(math.MaxFloat64),
"MaxInt16": reflect.ValueOf(math.MaxInt16),
"MaxInt32": reflect.ValueOf(math.MaxInt32),