Skip to content

Commit

Permalink
Merge pull request #2771 from onflow/sainati/purity-annotations
Browse files Browse the repository at this point in the history
Add view annotations to newly added array functions
  • Loading branch information
dsainati1 authored Sep 8, 2023
2 parents 2dd5ea3 + 1ae1bec commit e0407b6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 3 additions & 0 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,7 @@ func ArrayReverseFunctionType(arrayType ArrayType) *FunctionType {
return &FunctionType{
Parameters: []Parameter{},
ReturnTypeAnnotation: NewTypeAnnotation(arrayType),
Purity: FunctionPurityView,
}
}

Expand All @@ -2492,6 +2493,7 @@ func ArrayFilterFunctionType(memoryGauge common.MemoryGauge, elementType Type) *
},
},
ReturnTypeAnnotation: NewTypeAnnotation(BoolType),
Purity: FunctionPurityView,
}

return &FunctionType{
Expand All @@ -2503,6 +2505,7 @@ func ArrayFilterFunctionType(memoryGauge common.MemoryGauge, elementType Type) *
},
},
ReturnTypeAnnotation: NewTypeAnnotation(NewVariableSizedType(memoryGauge, elementType)),
Purity: FunctionPurityView,
}
}

Expand Down
9 changes: 5 additions & 4 deletions runtime/tests/checker/arrays_dictionaries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ func TestCheckArrayFilter(t *testing.T) {
fun test() {
let x = [1, 2, 3]
let onlyEven =
fun (_ x: Int): Bool {
view fun (_ x: Int): Bool {
return x % 2 == 0
}
Expand All @@ -1146,7 +1146,7 @@ func TestCheckArrayFilter(t *testing.T) {
fun testFixedSize() {
let x : [Int; 5] = [1, 2, 3, 21, 30]
let onlyEvenInt =
fun (_ x: Int): Bool {
view fun (_ x: Int): Bool {
return x % 2 == 0
}
Expand Down Expand Up @@ -1186,7 +1186,7 @@ func TestCheckArrayFilterInvalidArgs(t *testing.T) {
fun test() {
let x = [1, 2, 3]
let onlyEvenInt16 =
fun (_ x: Int16): Bool {
view fun (_ x: Int16): Bool {
return x % 2 == 0
}
Expand Down Expand Up @@ -1220,9 +1220,10 @@ func TestCheckResourceArrayFilterInvalid(t *testing.T) {
}
`)

errs := RequireCheckerErrors(t, err, 1)
errs := RequireCheckerErrors(t, err, 2)

assert.IsType(t, &sema.InvalidResourceArrayMemberError{}, errs[0])
assert.IsType(t, &sema.TypeMismatchError{}, errs[1])
}

func TestCheckArrayMap(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions runtime/tests/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10330,7 +10330,7 @@ func TestInterpretArrayFilter(t *testing.T) {
let emptyVals: [Int] = []
let onlyEven =
fun (_ x: Int): Bool {
view fun (_ x: Int): Bool {
return x % 2 == 0
}
Expand Down Expand Up @@ -10368,7 +10368,7 @@ func TestInterpretArrayFilter(t *testing.T) {
let xs = [1, 2, 3, 100, 201]
let onlyEven =
fun (_ x: Int): Bool {
view fun (_ x: Int): Bool {
return x % 2 == 0
}
Expand Down Expand Up @@ -10425,7 +10425,7 @@ func TestInterpretArrayFilter(t *testing.T) {
}
let onlyOddStruct =
fun (_ x: TestStruct): Bool {
view fun (_ x: TestStruct): Bool {
return x.test % 2 == 1
}
Expand Down Expand Up @@ -10485,7 +10485,7 @@ func TestInterpretArrayFilter(t *testing.T) {
let emptyVals_fixed: [Int; 0] = []
let onlyEven =
fun (_ x: Int): Bool {
view fun (_ x: Int): Bool {
return x % 2 == 0
}
Expand Down Expand Up @@ -10529,7 +10529,7 @@ func TestInterpretArrayFilter(t *testing.T) {
let xs_fixed: [Int; 5] = [1, 2, 3, 100, 201]
let onlyEven =
fun (_ x: Int): Bool {
view fun (_ x: Int): Bool {
return x % 2 == 0
}
Expand Down Expand Up @@ -10587,7 +10587,7 @@ func TestInterpretArrayFilter(t *testing.T) {
}
let onlyOddStruct =
fun (_ x: TestStruct): Bool {
view fun (_ x: TestStruct): Bool {
return x.test % 2 == 1
}
Expand Down

0 comments on commit e0407b6

Please sign in to comment.