golang/go

cmd/compile: use of untyped nil in switch

dvyukov opened this issue · 9 comments

gc fails on the following program:

package a
func f() {
    switch nil {
    }
}
go.go:4: use of untyped nil

The program is correct and must be compiled without errors.

go version devel +9b04852 Sat Jul 11 00:08:50 2015 +0000 linux/amd64

@griesemer to judge compiler disagreement

As far as I can tell, the spec doesn't say what should happen if the switch expression is untyped. E.g., switch nil {} above or something like switch 42 { case byte(42): } or type Bool bool; switch { case Bool(true): }.

cmd/compile seems to apply the implicit typing logic from section "Variable declarations" (i.e., use the default type for untyped constants, and reject untyped nil).

@mdempsky is correct. That said, we know that the spec is underspecified when it comes to the switch statement. I believe that nil could be permitted, but I am not feeling strongly about it. I think we need to nail down the spec issue first.

A similar case:

func f() {
    switch 1<<100 {
    }
}

gotype compiles without errors, gc says constant 1267650600228229401496703205376 overflows int.

This is related to #6398.

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

Per https://golang.org/cl/12711 this program is incorrect and gc correctly reports an error.

@griesemer should we file a bug on go/types?