Skip to content

fix(assert): treat a pointer to an empty interface as empty - #1973

Open
piyush295 wants to merge 1 commit into
stretchr:masterfrom
piyush295:fix/isempty-ptr-to-interface
Open

piyush295 wants to merge 1 commit into
stretchr:masterfrom
piyush295:fix/isempty-ptr-to-interface

Conversation

@piyush295

Copy link
Copy Markdown

Fixes #1955.

Problem

isEmptyValue (rewritten in #1761, released in v1.11.0) regressed for a pointer to an interface. Since v1.11.0, Empty can never pass for a *SomeInterface and NotEmpty always passes for one, even when the pointee is unambiguously empty.

Root cause: the reflect.Ptr case recurses with objValue.Elem(). For a *I, Elem() yields a reflect.Value whose Kind() is reflect.Interface, which matched no case in the switch, so it fell through to return false.

The pre-v1.11.0 implementation used objValue.Elem().Interface(), which re-boxed through interface{}; reflect.ValueOf on the result collapsed the interface layer, so recursion continued into the concrete pointee.

Fix

Add a reflect.Interface case that recurses into the concrete value it holds, restoring the previous behaviour. A nil interface is already handled by the IsZero() fast-path at the top of the function.

Testing

  • Added regression cases to Test_isEmpty (pointer to a nil interface, and a False case for a pointer to an interface holding a non-empty value).
  • go test ./assert/ — full suite passes; go vet ./assert/ clean.

isEmptyValue (rewritten in stretchr#1761, v1.11.0) regressed for a pointer to an
interface. objValue.Elem() on a *I yields a reflect.Value with
Kind()==Interface, which matched no case in the switch and fell through to
return false. As a result Empty never passed for a *SomeInterface and
NotEmpty always did, even when the pointee was unambiguously empty.

Add a reflect.Interface case that recurses into the concrete value, mirroring
the pre-v1.11.0 behaviour where Elem().Interface() re-boxed through
interface{} and collapsed the interface layer.

Adds regression cases to Test_isEmpty.

Fixes stretchr#1955
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.

assert.Empty regression in v1.11.0: a pointer to an interface is never empty

1 participant