-
Notifications
You must be signed in to change notification settings - Fork 18k
go: compiler: possible bug in variadic function arguments supplied from "ellipsed" slice #60362
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
I believe that:
and
together is sufficient to define the current behavior as working as intended. |
@seankhliao I am not sure I understand you I think. I don't want to debate the decision, just understand it better. Consider this:
Isn't that strange? To me it is, but maybe I am expecting to much of the spread operator. Please note that there are two questions:
I am just curious to know if I miss something obvious for why (B) is not needed or is not a good idea. Thank you! |
|
@zephyrtronium Thanks, it is clear now. I misinterpreted "..." as an actual (spread) operator, while it is not. Since a new slice is created for individual arguments given to a variadic function call, could this also not be done if those arguments come from a "...T" construct instruct instead? I suspect the reason might be that with actual individual arguments the size of the array backing the slice is known at compile time and so can be statically allocated. Allowing this with "...T" would potentially do huge dynamic array allocation. But from a language perspective it feels a bit weird not being able to do it, might be just me of course. EDIT: (added an extra note) Converting |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
(Play: https://go.dev/play/p/amaSBp_AJjW)
What did you expect to see?
I expected this to compile because the language specification states: "[...] the value passed is a new slice of type []T with a new underlying array whose successive elements are the actual arguments, which all must be assignable to T"
In my example
A
is assignable toI
, so I would expect this to work (F(a, a)
also compiles fine).A bit later this is stated in the spec:
"If the final argument is assignable to a slice type []T and is followed by ..., it is passed unchanged as the value for a ...T parameter. In this case no new slice is created."
This seems to be an optimisation, I think. However, in the given example
aSlice
is not assignable to a variable of typeI[]
so this shortcut (optimisation) is not applicable. Or is the specification is just unclear (to me)?What did you see instead?
"error: cannot use aSlice (variable of type []A) as []I value in argument to F"
The text was updated successfully, but these errors were encountered: