golang / go

The Go programming language

Home Page:https://go.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cmd/compile: overflow in int -> string

dvyukov opened this issue · comments

Gc rejects to compile the following program:

package a
var a = string(9223372036854775808)

saying:

overflow in int -> string

go/types compile it successfully.

Compilers must agree on whether it is a valid Go program or not.

on commit af81789

Given the spec as is, I believe this should compile w/o errors. The spec says:

http://tip.golang.org/ref/spec#Conversions

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.

and:
http://tip.golang.org/ref/spec#Conversions_to_and_from_a_string_type

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".

There's no mention of a compile-time (or even run-time) error. The integer should be converted into "\uFFFD".

Closed by mistake.

Then this is a bug in gc, right?

@dvyukov yes, I believe this is a gc compiler bug.

Note that we already return "\ufffd" for values < 0:

http://play.golang.org/p/1jrP-PnSdB

Too late for Go 1.5. The only possible visible effect of making changes to fix hypothetical bugs like this one is introducing real ones.