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: better error message for bad constants #20232

Closed
josharian opened this issue May 4, 2017 · 3 comments
Closed

cmd/compile: better error message for bad constants #20232

josharian opened this issue May 4, 2017 · 3 comments

Comments

@josharian
Copy link
Contributor

https://play.golang.org/p/_x_9PkQc5F

package main

const x = 6e5518446744

go/types says yes.

cmd/compile says no:

tmp/sandbox552762073/main.go:3: malformed constant: 6e5518446744
@josharian josharian added this to the Go1.9Maybe milestone May 4, 2017
@josharian
Copy link
Contributor Author

@griesemer @mdempsky

@mdempsky
Copy link
Member

mdempsky commented May 4, 2017

The spec says:

Implementation restriction: Although numeric constants have arbitrary precision in the language, a compiler may implement them using an internal representation with limited precision. That said, every implementation must:

  • Represent integer constants with at least 256 bits.
  • Represent floating-point constants, including the parts of a complex constant, with a mantissa of at least 256 bits and a signed binary exponent of at least 16 bits.
  • Give an error if unable to represent an integer constant precisely.
  • Give an error if unable to represent a floating-point or complex constant due to overflow.

So it's okay for go/types to accept it; but since the exponent exceeds 16 bits, it's also okay for cmd/compile to reject it with an error.

However, we could definitely emit a better error message. This is the TODO in gc/mpfloat.go:

    f, ok := a.Val.SetString(as)
    if !ok {
            // At the moment we lose precise error cause;
            // the old code additionally distinguished between:
            // - malformed hex constant
            // - decimal point in hex constant
            // - constant exponent out of range
            // - decimal point and binary point in constant
            // TODO(gri) use different conversion function or check separately
            yyerror("malformed constant: %s", as)

@mdempsky mdempsky changed the title cmd/compile, go/types: disagreement about whether 6e5518446744 is well-formed cmd/compile: better error message for bad constants May 4, 2017
@gopherbot
Copy link

CL https://golang.org/cl/42930 mentions this issue.

@golang golang locked and limited conversation to collaborators May 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants