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: accepts invalid comparisons against converted nil #13480

Closed
mdempsky opened this issue Dec 4, 2015 · 8 comments
Closed

cmd/compile: accepts invalid comparisons against converted nil #13480

mdempsky opened this issue Dec 4, 2015 · 8 comments

Comments

@mdempsky
Copy link
Member

mdempsky commented Dec 4, 2015

cmd/compile accepts this source:

package zero

func isZeroSlice(s []byte) bool    { return s == []byte(nil) }
func isZeroMap(m map[int]int) bool { return m == map[int]int(nil) }
func isZeroFunc(f func()) bool     { return f == (func())(nil) }

but gotype rejects it with:

zero.go:3:45: cannot compare s == []byte(nil) (operator == not defined for []byte)
zero.go:4:45: cannot compare m == map[int]int(nil) (operator == not defined for map[int]int)
zero.go:5:45: cannot compare f == (func())(nil) (operator == not defined for func())

The Go spec says "However, as a special case, a slice, map, or function value may be compared to the predeclared identifier nil." which seems to support gotype's behavior.

@bradfitz
Copy link
Contributor

bradfitz commented Dec 4, 2015

/cc @griesemer @alandonovan

@alandonovan
Copy link
Contributor

I'm not certain the behavior of go/types is wrong. Constants have basic types (string, number, or boolean), so nil is not a constant. Thus the compiler is not required to know the value of an expression such as []byte(nil) at compile time.

EDIT: I'm agreeing with mdemsky. I just didn't realize it till now.

@mdempsky
Copy link
Member Author

mdempsky commented Dec 4, 2015

@alandonovan Sorry, I'm not sure I follow your response. To clarify, the issue I'm trying to point out is that cmd/compile does compile the snippet of source I pasted without errors, whereas my understanding of the Go spec (and supported by gotype's behavior) says it should not.

@bradfitz
Copy link
Contributor

bradfitz commented Dec 4, 2015

Also, surely it's a bug that cmd/compile and gotype differ in their opinion. What does gccgo say?
/cc @ianlancetaylor

@alandonovan
Copy link
Contributor

Hi Matthew, sorry for the confusion. I clarified my response but of course that didn't send mail. I agree with you that go/types is correct and cmd/compile has a bug.

@mdempsky
Copy link
Member Author

mdempsky commented Dec 4, 2015

Ah, okay. :)

@ianlancetaylor
Copy link
Contributor

gccgo says:

foo.go:3:47: error: slice can only be compared to nil
 func isZeroSlice(s []byte) bool    { return s == []byte(nil) }
                                               ^
foo.go:4:47: error: map can only be compared to nil
 func isZeroMap(m map[int]int) bool { return m == map[int]int(nil) }
                                               ^
foo.go:5:47: error: func can only be compared to nil
 func isZeroFunc(f func()) bool     { return f == (func())(nil) }
                                               ^

@mdempsky mdempsky assigned mdempsky and unassigned griesemer Dec 4, 2015
@gopherbot
Copy link

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

@golang golang locked and limited conversation to collaborators Dec 14, 2016
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

6 participants