Skip to content

fix: prevent unique from panicking on unhashable elements - #1700

Open
tony1233200 wants to merge 2 commits into
go-playground:masterfrom
tony1233200:fix-unique-unhashable-1674
Open

tony1233200 wants to merge 2 commits into
go-playground:masterfrom
tony1233200:fix-unique-unhashable-1674

Conversation

@tony1233200

Copy link
Copy Markdown

Fixes Or Enhances

Fixes #1674.

unique panicked with hash of unhashable type on slices or maps whose elements cannot be used as map keys — [][]string, []map[string]string, [][]byte, or a []MyStruct where the struct contains a slice field. isUnique built seen := make(map[interface{}]struct{}) and inserted elem.Interface() directly, so any such input killed the call instead of producing a ValidationErrors.

Change (in isUnique, baked_in.go): track seen values with a small uniqueTracker helper. Values whose dynamic type is comparable keep using the map exactly as before; values whose type is not comparable (reflect.Type.Comparable() is false) are kept in a list and compared with reflect.DeepEqual. The same helper covers all three sites that used the map: unique on slices/arrays, unique=Field (where the compared field itself may be unhashable, e.g. unique=Tags on a []string field), and unique on maps.

Behavior (verified with the issue's example program):

input before after
[][]string{{"a"}, {"b"}} with unique panic nil (valid)
[]WithSlice (struct with a slice field) with unique panic nil (valid)
[][]byte{{1}, {2}} with unique panic nil (valid)
[]map[string]string with unique panic nil (valid)
duplicates in any of the above panic unique validation error

No behavior change for hashable elements: they still go through the map, so duplicate detection and performance there are unchanged. Unhashable duplicates are now detected instead of panicking.

Tests: added TestUniqueValidationUnhashableElements covering distinct-vs-duplicate for each of the four shapes above, plus unique=Field on a slice field and maps with unhashable values. go test ./... passes for all 26 packages, go vet and gofmt are clean. (gofumpt flags a few pre-existing spots elsewhere in the repo, including translations/ko/ko_test.go; none of them are in the changed lines.)

Disclosure (per CONTRIBUTING): this patch was prepared with the help of an AI coding assistant (Muse Spark) and reviewed/tested by me; the commit carries the requested Co-Authored-By trailer.

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

Fixes go-playground#1674.

isUnique built seen as map[interface{}]struct{} and inserted
elem.Interface() directly, so slices of slices, maps, []byte, or
structs containing a slice panicked with "hash of unhashable type".
Track seen values with a small helper: comparable values keep using
the map, non-comparable values are compared with reflect.DeepEqual.
Duplicate detection is unchanged for hashable elements, and
unhashable duplicates are now detected instead of panicking.

Tests: added TestUniqueValidationUnhashableElements covering
[][]string, []struct-with-slice, [][]byte, []map, unique=Field on a
slice field, and maps with unhashable values (distinct passes,
duplicate fails in each case).

Co-Authored-By: Muse Spark <muse-spark@meta.com>
@tony1233200
tony1233200 requested a review from a team as a code owner November 23, 2026 07:41
@coveralls

coveralls commented Nov 23, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 72.664% (+0.02%) from 72.649% — tony1233200:fix-unique-unhashable-1674 into go-playground:master

Comment thread baked_in.go Outdated
// check reports whether key was already recorded; if not, it records
// it and reports false.
func (t *uniqueTracker) check(key interface{}) bool {
if key == nil || reflect.TypeOf(key).Comparable() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI] reflect.Type.Comparable() is true for a struct or array type that has an interface field, but hashing such a value still panics when the interface holds a slice or map. So the panic stays for this input (checked on this branch):

type in struct{ V any }
validator.New().Var([]in{{V: []int{1}}, {V: []int{2}}}, "unique")
// panic: hash of unhashable type: []int

The same happens for []any{in{...}}, [][1]any{...} and map[string]in. reflect.Value.Comparable looks at the value held by the interface, so this is enough:

if key == nil || reflect.ValueOf(key).Comparable() {

With that change the inputs above no longer panic, duplicates are still found, and go test ./... passes. A test case with an interface field would cover it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: uniqueTracker.check now tests reflect.ValueOf(key).Comparable() instead of reflect.TypeOf(key).Comparable(), so a struct or array with an interface field holding a slice or map takes the DeepEqual path instead of panicking on hash. Regression cases added to TestUniqueValidationUnhashableElements cover your examples — []WithIface{{V: []int{1}}, {V: []int{2}}} (distinct passes, duplicate fails), []any{...}, [][1]any{...} and map[string]WithIface — and go test ./... passes for all 26 packages.

reflect.Type.Comparable is true for a struct or array type with an
interface field, but hashing such a value still panics when the
interface holds a slice or map. Check reflect.Value.Comparable
instead, so those elements take the DeepEqual path like any other
unhashable value.

Tests: TestUniqueValidationUnhashableElements gains cases with an
interface field holding a slice ([]T, []any, [][1]any, map values),
distinct passes and duplicates fail. go test ./... passes.

Co-Authored-By: Muse Spark <muse-spark@meta.com>
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

3 participants