-
Notifications
You must be signed in to change notification settings - Fork 18k
go/types: fails to report type error for ill-formed call #9473
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
Comments
Interesting! go/types handles parameter passing to built-ins essentially the same as for regular functions and so this should not be restricted to the built-in append, but also user-defined functions (see below). Furthermore, gccgo also accepts this code. In fact, both, go/types and gccgo accept his code: package main import "fmt" func f() (x, y []int) { func append2(x []int, y ...int) { func main() { gccgo even runs it "correctly": $ gccgo x.go && ./a.out That is, it's not obviously clear that this is a go/types issue. Per the spec:
And:
Thus, if 2) is done after 1), one might argue that the final argument after 1) is indeed a slice of the form []T and thus ... is applicable. This is indeed what go/types does: it first "unpacks" f() and treats the results of f() as a sequence of individual results. The ... application to the last argument happens afterwards. I suspect gccgo does something analogous. The spec is not specific enough in this case as evidenced by the different interpretations of it by go/types and gccco, vs gc, and we should clarify it. Permitting the code above could be an unintended consequence of the spec as written, or we can disallow it. |
As your example shows, there is nothing special about append here. Although gccgo supports this kind of code, I'm inclined to think that it should not. The rule about ... in a call applies to the final argument. When the argument is a function call that returns multiple results, there is no clear final argument for ... to apply to. Applying it to the final function call result makes logical sense, but it is quite obscure, and I think better prohibited. |
I came here to say what I saw Ian has already said. The issue is the ..., and it should not apply to the second result like this. Gc is correct to reject this program. |
Fixed by golang/tools@44ee65f . |
package main
func f() (x, y []int)
var _ = append(f()...) // compile error: multiple-value f() in single-value context
In the program above, gc correctly reports a type error for the ill-formed call to append.
go/types does not.
The text was updated successfully, but these errors were encountered: