Repository navigation
checker: reject count() without predicate on non-bool arrays - #1032
Open
minilan677-netizen wants to merge 1 commit into
Open
minilan677-netizen wants to merge 1 commit into
minilan677-netizen wants to merge 1 commit into
Conversation
Fixes expr-lang#1031 Signed-off-by: minilan677-netizen <321019851+minilan677-netizen@users.noreply.github.com>
This branch has not been deployed
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 #1031
Problem
Without a predicate,
count(arr)counts thetrueelements. The VM does this by asserting each element tobool. The checker returnedintfor the one-argument form and never looked at the element type, socount(ints)withints []int(orcount(1..3),count(filter(ints, # > 1)), and so on) compiled fine and then failed at run time withinterface conversion: interface {} is int, not bool. The predicate form already catches the same mistake at compile time (count(ArrayOfInt, {#})givespredicate should return boolean (got int)).Fix
In the one-argument branch of
count, if the collection is an array whose element type is known and isn'tbool, the checker now reports:Nothing changes for arrays of
bool, arrays with an unknown element type ([]any, untyped env, mixed literals), or unknown collections.Tests
checker/checker_test.go: two error cases (count(ArrayOfInt),count(1..3)) and three valid cases (count([true, false]),count(map(ArrayOfInt, # > 0)),count(ArrayOfAny)). The error cases fail without the fix.testdata/generated.txt: I removed the 31 generated lines that callcount()with no predicate onarray/list([]int/[]Foo). They only passed because the call was never reached (ok ?? count(array),false && ...) or the array was empty (count(array[:0])). This is the same kind of cleanup fix(checker): validate sortBy order argument type #912 did forsortBy.go test ./...,go test -race .,go test -tags=expr_debug -run=TestDebugger ./vm,go vet ./checker/,gofmt -l: all clean. I also ranFuzzExprfor 60s with no failures.