Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1955.
Problem
isEmptyValue(rewritten in #1761, released in v1.11.0) regressed for a pointer to an interface. Since v1.11.0,Emptycan never pass for a*SomeInterfaceandNotEmptyalways passes for one, even when the pointee is unambiguously empty.Root cause: the
reflect.Ptrcase recurses withobjValue.Elem(). For a*I,Elem()yields areflect.ValuewhoseKind()isreflect.Interface, which matched no case in the switch, so it fell through toreturn false.The pre-v1.11.0 implementation used
objValue.Elem().Interface(), which re-boxed throughinterface{};reflect.ValueOfon the result collapsed the interface layer, so recursion continued into the concrete pointee.Fix
Add a
reflect.Interfacecase that recurses into the concrete value it holds, restoring the previous behaviour. A nil interface is already handled by theIsZero()fast-path at the top of the function.Testing
Test_isEmpty(pointer to a nil interface, and aFalsecase for a pointer to an interface holding a non-empty value).go test ./assert/— full suite passes;go vet ./assert/clean.