golang/go

x/tools/go/types: compiles invalid program with overflow

dvyukov opened this issue · 2 comments

On the following program:

package main
func main() {
    var a = -("0"[0])
    println(a)
}

ssadump -run outputs:

208

The program is invalid and must not be compiled (type of "0"[0] is byte, -byte is an overflow).

on commit e9a746d

The program is valid and the result is correct. Per the spec:

For a of string type:
- a constant index must be in range if the string a is also constant
- if x is out of range at run time, a run-time panic occurs
- a[x] is the non-constant byte value at index x and the type of a[x] is byte

(http://tip.golang.org/ref/spec#Index_expressions)

That is, "0"[0] is not a constant, and the value is the byte value of '0' which is 48; with (-48) mod 256 = 208.

Note that gc rejects this code correctly: http://play.golang.org/p/6b4juoS9km

Filing a gc bug instead.

FTR, the gc issue is #11370