golang / go

The Go programming language

Home Page:https://go.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gccgo: bogus integer constant overflow

dvyukov opened this issue · comments

gccgo rejects to compile the following program:

package a
var a = string(0xE000000000000000)
go.go:2:16: error: integer constant overflow

The spec says:

A constant value x can be converted to type T in any of these cases:
...
x is an integer constant and T is a string type. The same rule as for non-constant x applies in this case.
...
Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer. Values outside the range of valid Unicode code points are converted to "\uFFFD".

gcc version 6.0.0 2015070 (experimental) (GCC)

I'm confused by this since it doesn't seem that (my versions of) either compiler accept this. The spec says this is valid if x is an integer constant and T is a string type, but x is not a valid integer constant; it overflows. I'm not sure why gccgo should compile the above program.

The gc compiler also rejects this program:

foo.go:2: overflow in int -> string

I agree with Chris: I don't see why gccgo has to do anything here.

@paranoiacblack This is an academic exercise at best, but the spec doesn't say that a constant argument must fit into an int (or int64). It just says that it must be an integer value, and that values outside the range result in the value "\uFFFD".

go/types accepts this without errors.

I don't think it matters much, but it seems easier to simply produce this constant string (at compile time) than reporting an error.

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