Skip to content

fix: validate uniqueness of non-comparable values - #1676

Open
Harshil-1603 wants to merge 1 commit into
go-playground:masterfrom
Harshil-1603:fix/unique-unhashable-values
Open

Harshil-1603 wants to merge 1 commit into
go-playground:masterfrom
Harshil-1603:fix/unique-unhashable-values

Conversation

@Harshil-1603

Copy link
Copy Markdown

Fixes Or Enhances

unique currently inserts values directly into a map, so inputs such as [][]string, []map[string]string, or structs containing slices panic instead of producing a validation result.

Keep the map lookup for comparable values and use reflect.DeepEqual for non-comparable values. Apply this to slice/array elements, map values, and unique=Field, including dynamically non-comparable interface fields. Document the fallback's quadratic comparison cost.

Fixes #1674.

  • Tests cover distinct/duplicate nested collections, structs, interfaces, pointers, nil/empty values, and the public Var/Struct entry points.

Sixteen new subtests panic on the original source and pass with the fix. make test (all packages, coverage and race detector), golangci-lint 2.13.1, and git diff --check pass. Local comparable-integer benchmarks retain 560 B/op and 14 allocs/op; timing samples were noisy, so no speed claim is made. The full suite ran on Go 1.27.3; lint used Go 1.26.0.

AI-assisted contribution: OpenAI Codex (GPT-6) implemented and verified this patch.

Co-Authored-By: OpenAI Codex GPT-6 <noreply@openai.com>
@Harshil-1603
Harshil-1603 requested a review from a team as a code owner October 21, 2026 14:31
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 72.591% (+0.02%) from 72.569% — Harshil-1603:fix/unique-unhashable-values into go-playground:master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: unique panics with "hash of unhashable type" on slices of non-comparable elements

2 participants