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

maps: inconsistent panic in Copy with nil dst #64390

Open
gazerro opened this issue Nov 26, 2023 · 6 comments
Open

maps: inconsistent panic in Copy with nil dst #64390

gazerro opened this issue Nov 26, 2023 · 6 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@gazerro
Copy link
Contributor

gazerro commented Nov 26, 2023

The maps.Copy function panics if the destination map is nil and the source map is not empty (dst == nil && len(src) > 0)

var dst map[string]int
maps.Copy(dst, map[string]int(nil))    // no panic
maps.Copy(dst, map[string]int{})       // no panic
maps.Copy(dst, map[string]int{"a": 1}) // panic

This behavior is not currently documented.

I propose to panic if dst is nil, independently from src, and document it. The updated behavior would be

var dst map[string]int
maps.Copy(dst, map[string]int(nil))    // panic
maps.Copy(dst, map[string]int{})       // panic
maps.Copy(dst, map[string]int{"a": 1}) // panic
@seankhliao seankhliao changed the title maps: undocumented and inconsistent behavior in the Copy function maps: inconsistent panic in Copy with nil dst Nov 26, 2023
@randall77
Copy link
Contributor

It's not clear to me why the proposed behavior is any better than the current behavior. The old behavior is consistent with the built-in maps rule that nil maps panic only when written to.

I don't think we can change the behavior now. This API has already been released and people may be depending on the old behavior, even if unspecified in the docs.

@gazerro
Copy link
Contributor Author

gazerro commented Nov 26, 2023

The old behavior is consistent with the built-in maps rule that nil maps panic only when written to.

I would have said the opposite; I find the proposed behavior more consistent with the built-in maps rule, but now I also understand your perspective.

I'd suggest documenting the current behavior at this point; it's not quite evident without delving into the code.

@dmitshur
Copy link
Contributor

CC @eliben, @Deleplace.

@dmitshur dmitshur added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed Documentation labels Nov 27, 2023
@dmitshur dmitshur added this to the Backlog milestone Nov 27, 2023
@Deleplace
Copy link
Contributor

I agree with most of the arguments, though IMO any usage of Copy with dst==nil is a bad idea (not a legit use case, and the developer shouldn't rely on Copy always panicking or never panicking in this case).

Keeping and documenting the existing behaviour should be fine:

Copy panics if dst is nil and src contains at least one element to be copied.

@RodolfoMRibeiro
Copy link

As far as I could see, there are two ways we can take:

  1. Documenting the current behavior, which is acceptable.

  2. Addressing the issue of it throwing an error when the destination is nil and the source isn't.

Personally, I would prefer the second approach. The rationale behind this choice is that if we are copying, it is likely that we are allocating space in a new memory address if it does not exist yet.

The function maps.Copy should retrieve the value from a source and either copy it to the destination if it exists or allocate memory and then copy the values. It shouldn't crash in such a scenario.

I can take that to solve, if you guys don't mind

@Deleplace
Copy link
Contributor

I don't think maps.Copy has any way to allocate memory (a new map) and provide it to the caller who passed nil, because maps.Copy doesn't return a collection like append or maps.Clone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants