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: != operator incorrect for literal nil pointer compared to literal nil interface #19911

Closed
bcmills opened this issue Apr 10, 2017 · 5 comments

Comments

Projects
None yet
5 participants
@bcmills
Copy link
Member

commented Apr 10, 2017

See https://play.golang.org/p/fAb8AX92nf:

package main

import (
	"fmt"
)

type ET struct{}

func (*ET) Error() string { return "err" }

func main() {
	fmt.Printf("(*ET)(nil) == error(nil): %v\n", (*ET)(nil) == error(nil))
	fmt.Printf("(*ET)(nil) != error(nil): %v\n", (*ET)(nil) != error(nil))

	nilET := (*ET)(nil)
	nilError := error(nil)

	fmt.Printf("nilET == nilError: %v\n", nilET == nilError)
	fmt.Printf("nilET != nilError: %v\n", nilET != nilError)
}

Expected output:

(*ET)(nil) == error(nil): false
(*ET)(nil) != error(nil): true
nilET == nilError: false
nilET != nilError: true

Actual output:

(*ET)(nil) == error(nil): false
(*ET)(nil) != error(nil): false
nilET == nilError: false
nilET != nilError: true

Note that the (*ET)(nil) != error(nil) case returns false. Unless I'm mistaken, it should return true.

@josharian

This comment has been minimized.

Copy link
Contributor

commented Apr 10, 2017

Fun! Bisecting.

@josharian

This comment has been minimized.

Copy link
Contributor

commented Apr 10, 2017

Go 1.8 agrees with tip. Go 1.7 rejects the code with:

# command-line-arguments
./x.go:12: illegal constant expression: *ET == error
./x.go:13: illegal constant expression: *ET != error
@griesemer

This comment has been minimized.

Copy link
Contributor

commented Apr 10, 2017

Go 1.7 would be incorrect as well, as these are not constant expressions (nil is not a constant).

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Apr 10, 2017

gccgo says

(*ET)(nil) == error(nil): false
(*ET)(nil) != error(nil): true
nilET == nilError: false
nilET != nilError: true
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Apr 10, 2017

Go 1.5 and 1.6 also say illegal constant expression. Go 1.0 through Go 1.4 generate the same output as gccgo.

@gopherbot gopherbot closed this in b83a916 Apr 10, 2017

lparth added a commit to lparth/go that referenced this issue Apr 13, 2017

cmd/compile: make iface == iface const evaluation respect !=
Fixes golang#19911

Change-Id: Ib2b2505fe31ce00c6ffc021a0fe5df510633b44b
Reviewed-on: https://go-review.googlesource.com/40251
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>

@golang golang locked and limited conversation to collaborators Apr 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.