Skip to content

Commit

Permalink
fix: goexports performs correct conversion on float32 constants
Browse files Browse the repository at this point in the history
  • Loading branch information
mvertes authored and traefiker committed Oct 29, 2019
1 parent 1568687 commit 75a696a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
13 changes: 13 additions & 0 deletions _test/math1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "math"

func main() {
var f float32
if f < math.MaxFloat32 {
println("ok")
}
}

// Output:
// ok
13 changes: 11 additions & 2 deletions cmd/goexports/goexports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion stdlib/go1_12_math.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion stdlib/go1_13_math.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 75a696a

Please sign in to comment.