Skip to content

nil literal should not be parsed as ast.Ident #28918

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

Closed
rhysd opened this issue Nov 22, 2018 · 2 comments
Closed

nil literal should not be parsed as ast.Ident #28918

rhysd opened this issue Nov 22, 2018 · 2 comments

Comments

@rhysd
Copy link
Contributor

rhysd commented Nov 22, 2018

What version of Go are you using (go version)?

$ go version
go version go1.11.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/rhysd/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/rhysd/.go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11.2/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/xb/352hz3xn31z9zthlfl2pc03m0000gn/T/go-build927957953=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I found nil literal is actually nil identifier in Go. So it can be replaced by declaring nil in outer scope.

https://play.golang.org/p/itEzumDVEGF

package main

import (
	"fmt"
)

var nil error = fmt.Errorf("hello")

func main() {
	_, err := fmt.Println("hey")
	if err != nil {
		println("oops")
	}
}

This seems because nil in expression is parsed as identifier, not literal. I guess true and false have the same issue. golint and go vet does not complain this.

What did you expect to see?

I expected not to see oops in output

What did you see instead?

I saw oops in output

@josharian
Copy link
Contributor

nil is a predeclared identifier, not a keyword: https://golang.org/ref/spec#Predeclared_identifiers. This was an intentional decision; I don’t anticipate it changing. Going to close this as working as intended. Feel free to ask to re-open if I’ve misunderstood. If you want to discuss the design, I suggest the golang-nuts mailing list. Thanks for taking the time to file an issue.

@rhysd
Copy link
Contributor Author

rhysd commented Nov 22, 2018

Thank you for quick reply. I understood it is an intentional desicion. Let me consider to discuss this in ML.

@golang golang locked and limited conversation to collaborators Nov 22, 2019
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

3 participants