-
Notifications
You must be signed in to change notification settings - Fork 18k
runtime: inconsistent behaviour of make with negative lengths #46909
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
The map value is just a hint, so we don't panic in that case. In other words, there's something reasonable we can do and the program can continue (unlike the slice or chan situations). More discussion at #24308 |
I see, thanks. Perhaps the fact that this is a hint as opposed to an actual size should also be documented in package builtin. The docs currently say (emphasis added): Map: An empty map is allocated with enough space to hold the specified number of elements. The size may be omitted, in which case a small starting size is allocated. The Spec calls the parameter “hint”, which is correct, I assume. |
I think we decided in #14965 that we weren't going to add the "runtime error:" prefix to errors we were already generating, just to avoid breaking people that depended on the text. So I don't think there's anything to do here. Closing as unfortunate, but not worth fixing. |
@randall77, should the documentation in package builtin mention that invalid hints are set to zero, or would that be unnecessary? If it should, I am ready to send a CL about that. If not, oh well. |
I don't think it is necessary.
That seems correct and complete. If you specify -3, then the map is allocated with enough space for -3 elements. The "size" part in the next sentence (which you quoted above) is just the formal parameter name in the declaration. If anything, we could document the panic behavior for the other kinds (slice, chan). But we generally don't get that formal (for instance, |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, and also on Go 1.15.11 and 1.16.4.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://play.golang.org/p/K8ZFRxqVfpj
What did you expect to see?
What did you see instead?
The map
make
doesn't panic at all, while the other two panic with inconsistent messages. The documentation doesn't specify any constraints on these parameters, besidescap >= len
, and neither does the Spec, as far as I can see. At the very least the behaviour w.r.t. invalid lengths should be documented, I think.The text was updated successfully, but these errors were encountered: