Add numberArrayType linter type

Add `numberArrayType` specialised type for cases where we know we have
an array of numbers.  Use in some stdlib functions and `importbin`.
This commit is contained in:
Angus Lees 2022-01-24 23:27:16 +11:00 committed by Stanisław Barzowski
parent 856bd58872
commit f699b0ea42
6 changed files with 19 additions and 6 deletions

View File

@ -218,7 +218,7 @@ func calcTP(node ast.Node, varAt map[ast.Node]*common.Variable, g *typeGraph) ty
case *ast.ImportStr:
return tpRef(stringType)
case *ast.ImportBin:
return tpRef(anyArrayType)
return tpRef(numberArrayType)
case *ast.LiteralBoolean:
return tpRef(boolType)
case *ast.LiteralNull:

View File

@ -132,6 +132,13 @@ func newTypeGraph(importFunc ImportFunc) *typeGraph {
ArrayDesc: anyArrayDesc,
})
g.newPlaceholder()
g._placeholders[numberArrayType] = concreteTP(TypeDesc{
ArrayDesc: &arrayDesc{
furtherContain: []placeholderID{numberType},
},
})
g.newPlaceholder()
g._placeholders[anyObjectType] = concreteTP(TypeDesc{
ObjectDesc: anyObjectDesc,

View File

@ -13,6 +13,7 @@ const (
stringType
nullType
anyArrayType
numberArrayType
anyObjectType
anyFunctionType
stdlibType

View File

@ -6,7 +6,6 @@ func prepareStdlib(g *typeGraph) {
g.newPlaceholder()
arrayOfString := anyArrayType
arrayOfNumber := anyArrayType
stringOrArray := anyType
stringOrNumber := anyType
jsonType := anyType // It actually cannot functions anywhere
@ -75,7 +74,7 @@ func prepareStdlib(g *typeGraph) {
"codepoint": g.newSimpleFuncType(numberType, "str"),
"char": g.newSimpleFuncType(stringType, "n"),
"substr": g.newSimpleFuncType(stringType, "str", "from", "len"),
"findSubstr": g.newSimpleFuncType(arrayOfNumber, "pat", "str"),
"findSubstr": g.newSimpleFuncType(numberArrayType, "pat", "str"),
"startsWith": g.newSimpleFuncType(boolType, "a", "b"),
"endsWith": g.newSimpleFuncType(boolType, "a", "b"),
"stripChars": g.newSimpleFuncType(stringType, "str", "chars"),
@ -101,7 +100,7 @@ func prepareStdlib(g *typeGraph) {
"parseHex": g.newSimpleFuncType(numberType, "str"),
"parseJson": g.newSimpleFuncType(jsonType, "str"),
"parseYaml": g.newSimpleFuncType(jsonType, "str"),
"encodeUTF8": g.newSimpleFuncType(arrayOfNumber, "str"),
"encodeUTF8": g.newSimpleFuncType(numberArrayType, "str"),
"decodeUTF8": g.newSimpleFuncType(stringType, "arr"),
// Manifestation
@ -119,7 +118,7 @@ func prepareStdlib(g *typeGraph) {
"makeArray": g.newSimpleFuncType(anyArrayType, "sz", "func"),
"count": g.newSimpleFuncType(numberType, "arr", "x"),
"member": g.newSimpleFuncType(boolType, "arr", "x"),
"find": g.newSimpleFuncType(arrayOfNumber, "value", "arr"),
"find": g.newSimpleFuncType(numberArrayType, "value", "arr"),
"map": g.newSimpleFuncType(anyArrayType, "func", "arr"),
"mapWithIndex": g.newSimpleFuncType(anyArrayType, "func", "arr"),
"filterMap": g.newSimpleFuncType(anyArrayType, "filter_func", "map_func", "arr"),
@ -129,7 +128,7 @@ func prepareStdlib(g *typeGraph) {
"foldr": g.newSimpleFuncType(anyType, "func", "arr", "init"),
"repeat": g.newSimpleFuncType(anyArrayType, "what", "count"),
"slice": g.newSimpleFuncType(arrayOfString, "indexable", "index", "end", "step"),
"range": g.newSimpleFuncType(arrayOfNumber, "from", "to"),
"range": g.newSimpleFuncType(numberArrayType, "from", "to"),
"join": g.newSimpleFuncType(stringOrArray, "sep", "arr"),
"lines": g.newSimpleFuncType(arrayOfString, "arr"),
"flattenArrays": g.newSimpleFuncType(anyArrayType, "arrs"),

View File

@ -0,0 +1 @@
!std.range(1, 3)[0]

View File

@ -0,0 +1,5 @@
testdata/stdlib_call_returned_numberarray:1:1-20 Operand is not a boolean, it is assumed to be a number
!std.range(1, 3)[0]