Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ltzmaxwell committed Apr 10, 2024
1 parent e30fe30 commit fa9b293
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 4 deletions.
11 changes: 9 additions & 2 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2954,13 +2954,20 @@ func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (De
ft = ft.UnboundType(rft)
dt := (*DeclaredType)(nil)

debug.Println("---predefine now, funcDecl")
// check against base type, should not be pointer type or interface type as a receiver
if dt, ok := rt.(*DeclaredType); ok {
if _, ok := baseOf(dt).(*PointerType); ok {
panic("invalid receiver type IntPtr (pointer or interface type)")
panic(fmt.Sprintf("invalid receiver type %v (pointer type)\n", rt))
}
if _, ok := baseOf(dt).(*InterfaceType); ok {
panic(fmt.Sprintf("invalid receiver type %v (interface type)\n", rt))
}
}

if pt, ok := rt.(*PointerType); ok {
if _, ok := pt.Elem().(*PointerType); ok {
panic(fmt.Sprintf("invalid receiver type %v (pointer type)\n", rt))
}
dt = pt.Elem().(*DeclaredType)
} else {
dt = rt.(*DeclaredType)
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/type37.gno
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ func main() {
}

// Error:
// main/debug/type37.gno:8: invalid receiver type IntPtr (pointer or interface type)
// main/debug/type37.gno:8: invalid receiver type main.Arr (pointer type)
20 changes: 20 additions & 0 deletions gnovm/tests/files/type37c.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "fmt"

type Integer int

func (i *Integer) Add(x int) { // receiver is val, not ptr
println(int(*i) + x)
}

func main() {
a := new(Integer)
a.Add(4)

fmt.Println(*a)
}

// Output:
// 4
// 0
22 changes: 22 additions & 0 deletions gnovm/tests/files/type37d.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import "fmt"

type Integer int

func (i **Integer) Add(x int) {
**i += Integer(x) // Dereference twice to get to the actual Integer value and modify it
}

func main() {
a := new(Integer) // a is a pointer to Integer
b := &a // b is a pointer to a pointer to Integer

// Since Add is defined on **Integer, you need to pass b
b.Add(4) // Adds 4 to the value **b points to

fmt.Println(**b) // Should print 4, as **b is the same as *a
}

// Error:
// main/debug/type37d.gno:7: invalid receiver type **main.Integer (pointer type)
21 changes: 21 additions & 0 deletions gnovm/tests/files/type37e.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "fmt"

type IntArray []int
type Arr *IntArray

func print(arr Arr) { // receiver is val, not ptr
println(arr)
}

func main() {
a := new(IntArray)
print(a)

fmt.Println(*a)
}

// Output:
// &(nil main.IntArray)
// []
26 changes: 26 additions & 0 deletions gnovm/tests/files/type37e1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import "fmt"

type IntArray []int
type Arr *IntArray

func add(arr Arr) { // receiver is val, not ptr
*arr = append(*arr, 1)
}

func main() {
a := new(IntArray)
add(a)

fmt.Println(a)
fmt.Println(*a)
fmt.Println(len(*a))
fmt.Println((*a)[0])
}

// Output:
// &[1]
// [1]
// 1
// 1
2 changes: 1 addition & 1 deletion gnovm/tests/files/type38.gno
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ func main() {
}

// Error:
// main/debug/type38.gno:7: invalid receiver type IntPtr (pointer or interface type)
// main/debug/type38.gno:7: invalid receiver type main.IntPtr (pointer type)
22 changes: 22 additions & 0 deletions gnovm/tests/files/type39.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

type foo interface {
say()
}

func (f foo) echo() int {
return 1
}

type Bar struct{}

func (b *Bar) say() {}

func main() {
var f foo
f = &Bar{}
println(f.echo())
}

// Error:
// main/debug/type39.gno:7: invalid receiver type main.foo (interface type)
24 changes: 24 additions & 0 deletions gnovm/tests/files/type39a.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

type foo interface {
say()
}

type FF foo

func (f FF) echo() int {
return 1
}

type Bar struct{}

func (b *Bar) say() {}

func main() {
var f foo
f = &Bar{}
println(f.echo())
}

// Error:
// main/debug/type39a.gno:9: invalid receiver type main.FF (interface type)

0 comments on commit fa9b293

Please sign in to comment.