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

go/types: cannot assign "nil..." to ... parameters #18268

Closed
stjj89 opened this issue Dec 9, 2016 · 2 comments
Closed

go/types: cannot assign "nil..." to ... parameters #18268

stjj89 opened this issue Dec 9, 2016 · 2 comments
Milestone

Comments

@stjj89
Copy link
Contributor

@stjj89 stjj89 commented Dec 9, 2016

The following code is accepted by both gc and gccgo, but not go/types:

func foo(args ...string) {}

func bar() {
  foo(nil...)
}

The error returned by go/types is

cannot use nil (untyped nil value) as parameter of type []string

It appears that go/types is wrong here. Per the spec
( https://golang.org/ref/spec#Passing_arguments_to_..._parameters, https://golang.org/ref/spec#Assignability ):

If f is variadic with a final parameter p of type ...T, then within f the type of p is equivalent to type []T.
...
If the final argument is assignable to a slice type []T, it may be passed unchanged as the value for a ...T parameter if the argument is followed by .... In this case no new slice is created.

A value x is assignable to a variable of type T ("x is assignable to T") in any of these cases:
...

  • x is the predeclared identifier nil and T is a pointer, function, slice, map, channel, or interface type.
@alandonovan
Copy link
Contributor

@alandonovan alandonovan commented Dec 9, 2016

I don't know why one would ever write that code, but you're right, the spec is pretty clear that it's a legal Go program.

/cc @griesemer

Loading

@mdempsky mdempsky added this to the Go1.9 milestone Dec 9, 2016
@gopherbot
Copy link

@gopherbot gopherbot commented Dec 9, 2016

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

Loading

@gopherbot gopherbot closed this in 08bb7cc Feb 7, 2017
@golang golang locked and limited conversation to collaborators Feb 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants