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

cmd/compile: "constant V overflows T" now reported as "cannot convert V to type T" #63563

Closed
odeke-em opened this issue Oct 16, 2023 · 2 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@odeke-em
Copy link
Member

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

Go1.21

Does this issue reproduce with the latest release?

Yes

What did you do?

I ran this program

package main

import "fmt"

const maxInt = 18446744073709551614

func main() {
	fmt.Printf("%d => %d\n", int64(maxInt), int64(maxInt*2))
}

which prints out

Go1.21.1

./main.go:8:33: cannot convert maxInt (untyped int constant 18446744073709551614) to type int64
./main.go:8:48: cannot convert maxInt * 2 (untyped int constant 36893488147419103228) to type int64

Go 1.17

./main.go:8:32: constant 18446744073709551614 overflows int64
./main.go:8:47: constant 36893488147419103228 overflows int64

What did you expect to see?

The old message until Go1.17 was much clearer

What did you see instead?

A clear message about an overflow

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Oct 16, 2023
@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 16, 2023
@cagedmantis cagedmantis added this to the Backlog milestone Oct 16, 2023
@mdempsky
Copy link
Member

I agree that technically we've lost some error message details here. The compiler used to specifically report that the issue was the constant value wasn't representable as the converted-to type, whereas now it just reports that the expression can't be converted to the type without mentioning why not.

The only reason an untyped constant can't be converted to a type is because it's not representable in that type, but non-expert users may not know that.

Maybe we can tweak the wording slightly to clarify the conversion is not allowed because of overflow or that the value isn't representable? (Note that if the destination type is instead a user-defined type like MyInt, it's not obviously apparently from the error message what the representable set of values is for the named type.)

/cc @griesemer @findleyr @alandonovan

@mdempsky mdempsky added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Oct 25, 2023
@mdempsky mdempsky changed the title cmd/compile: missing overflow message when converting constant to numeric type; was present before until Go1.17 cmd/compile: "constant V overflows T" now reported as "cannot convert V to type T" Oct 25, 2023
@gopherbot
Copy link

Change https://go.dev/cl/538058 mentions this issue: go/types, types2: more concise error if conversion fails due to integer overflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

4 participants