Skip to content

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

Closed
@rhysd

Description

@rhysd

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions