Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/types: disagrees with compiler on float truncation when using export data #53803

Closed
dominikh opened this issue Jul 12, 2022 · 6 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@dominikh
Copy link
Member

What version of Go are you using (go version)?

$ go version
go version devel go1.19-d3d7998756 Tue Jul 12 04:04:17 2022 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

Consider

func fn(int) {}

func bar() {
	const oracle = assist.A * assist.B
	fn(oracle)
}

with

const A = 0.05
const B = 10000

The two constants are untyped floats, and their product is 500.0. The Go compiler accepts the function call. When using go/types, however, this only type checks if we're not loading assist from export data. If we are, then it fails with

cannot use oracle (untyped float constant 500) as int value in argument to fn (truncated)

The following txtar archive contains a module with a package bug that compiles fine using go build, but fails to type check when loading with go/packages. Notably, this only fails when a) the constants are declared in an imported package b) the imported package is loaded from export data instead of from source.

Output:

$ go build ./...  
$ go run run_me.go
example.com/bug [/tmp/foo/bug/foo.go:11:5: cannot use oracle (untyped float constant 500) as int value in argument to fn (truncated)]

Input:

https://go.dev/play/p/LZfQZ7oKOcW.go

Possibly related to #45837

/cc @griesemer

@firelizzard18
Copy link
Contributor

Interestingly it works if I add a const C = A * B to package assist and use that instead (see https://go.dev/play/p/LPUVVG4WWXJ.go)

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 12, 2022
@mknyszek
Copy link
Contributor

CC @griesemer @findleyr

@mknyszek mknyszek added this to the Go1.19 milestone Jul 12, 2022
@mknyszek
Copy link
Contributor

Is this new in Go 1.19?

@findleyr
Copy link
Contributor

@mknyszek no I don't think this is new. This may be a bug with x/tools/go/internal/gcimporter. I will investigate.

@mknyszek mknyszek modified the milestones: Go1.19, Backlog Jul 12, 2022
@gopherbot
Copy link

Change https://go.dev/cl/417580 mentions this issue: go/internal/gcimporter: adjust importer to match compiler importer

@findleyr
Copy link
Contributor

Thanks for reporting this bug.

@golang golang locked and limited conversation to collaborators Jul 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants