From 231fa1dd20293b0ac58db19c7dbcac6e1578c00b Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Tue, 5 Mar 2024 11:08:40 +0800 Subject: [PATCH 01/13] add pointer type for type decl --- gnovm/pkg/gnolang/preprocess.go | 2 ++ gnovm/tests/files/type33.gno | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 gnovm/tests/files/type33.gno diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 1d215e4d94b..fd967845b79 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -3096,6 +3096,8 @@ func tryPredefine(store Store, last BlockNode, d Decl) (un Name) { t = &MapType{} case *StructTypeExpr: t = &StructType{} + case *StarExpr: + t = &PointerType{} case *NameExpr: if tv := last.GetValueRef(store, tx.Name); tv != nil { // (file) block name diff --git a/gnovm/tests/files/type33.gno b/gnovm/tests/files/type33.gno new file mode 100644 index 00000000000..feaae0d7005 --- /dev/null +++ b/gnovm/tests/files/type33.gno @@ -0,0 +1,16 @@ +package main + +import "fmt" + +type myPointer *int + +func main() { + if myPointer(nil) == nil { + fmt.Println("Pointer is nil") + } else { + fmt.Println("Pointer is not nil") + } +} + +// Output: +// Pointer is not nil From 67ff90d38f822da96e33506a7f589b3e9e6f08db Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Tue, 5 Mar 2024 11:44:17 +0800 Subject: [PATCH 02/13] fixup, better test cast --- gnovm/pkg/gnolang/op_types.go | 3 ++- gnovm/tests/files/type33.gno | 26 +++++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/gnovm/pkg/gnolang/op_types.go b/gnovm/pkg/gnolang/op_types.go index a38010bac4e..41ce4e56b51 100644 --- a/gnovm/pkg/gnolang/op_types.go +++ b/gnovm/pkg/gnolang/op_types.go @@ -485,7 +485,8 @@ func (m *Machine) doOpStaticTypeOf() { m.PushOp(OpStaticTypeOf) m.Run() // XXX replace xt := m.ReapValues(start)[0].GetType() - if pt, ok := xt.(*PointerType); ok { + debug.Printf("---starExpr, xt: %v \n", xt) + if pt, ok := baseOf(xt).(*PointerType); ok { m.PushValue(asValue(pt.Elt)) } else if _, ok := xt.(*TypeType); ok { m.PushValue(asValue(gTypeType)) diff --git a/gnovm/tests/files/type33.gno b/gnovm/tests/files/type33.gno index feaae0d7005..62d6696fafa 100644 --- a/gnovm/tests/files/type33.gno +++ b/gnovm/tests/files/type33.gno @@ -2,15 +2,27 @@ package main import "fmt" -type myPointer *int +// Define a base type +type Base int + +// Declare a new type that is a pointer to the base type +type PtrToBase *Base func main() { - if myPointer(nil) == nil { - fmt.Println("Pointer is nil") - } else { - fmt.Println("Pointer is not nil") - } + var b Base = 42 // Initialize a variable of the base type + var p PtrToBase = &b // Initialize a variable of the new pointer type with the address of b + + fmt.Printf("The value of b is: %d\n", b) + + // Using the new pointer type + fmt.Printf("The value pointed to by p is: %d\n", *p) + + // Modifying the value pointed to by p + *p = 100 + fmt.Printf("The new value of b after modification through p is: %d\n", b) } // Output: -// Pointer is not nil +// The value of b is: 42 +// The value pointed to by p is: 42 +// The new value of b after modification through p is: 100 From 8b065c08a97144f35ddf2b31a950a24edd938406 Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Tue, 5 Mar 2024 12:09:42 +0800 Subject: [PATCH 03/13] clean log --- gnovm/pkg/gnolang/op_types.go | 1 - 1 file changed, 1 deletion(-) diff --git a/gnovm/pkg/gnolang/op_types.go b/gnovm/pkg/gnolang/op_types.go index 41ce4e56b51..bc770f11722 100644 --- a/gnovm/pkg/gnolang/op_types.go +++ b/gnovm/pkg/gnolang/op_types.go @@ -485,7 +485,6 @@ func (m *Machine) doOpStaticTypeOf() { m.PushOp(OpStaticTypeOf) m.Run() // XXX replace xt := m.ReapValues(start)[0].GetType() - debug.Printf("---starExpr, xt: %v \n", xt) if pt, ok := baseOf(xt).(*PointerType); ok { m.PushValue(asValue(pt.Elt)) } else if _, ok := xt.(*TypeType); ok { From 715601e9bb99716fbac8bf307523defbc137b238 Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Mon, 8 Apr 2024 13:30:06 +0800 Subject: [PATCH 04/13] more cases --- gnovm/Makefile | 2 ++ gnovm/pkg/gnolang/op_decl.go | 21 +++++++++------------ gnovm/pkg/gnolang/op_expressions.go | 8 +++++++- gnovm/pkg/gnolang/op_types.go | 24 +++++++++++++++++++++++- gnovm/tests/debug/time6_native.gno | 16 ++++++++++++++++ gnovm/tests/debug_temp/34a.gno | 12 ++++++++++++ gnovm/tests/debug_temp/type33.gno | 28 ++++++++++++++++++++++++++++ gnovm/tests/debug_temp/type34.gno | 12 ++++++++++++ gnovm/tests/debug_temp/type35.gno | 15 +++++++++++++++ gnovm/tests/debug_temp/type36.gno | 17 +++++++++++++++++ gnovm/tests/debug_temp/type37.gno | 7 +++++++ gnovm/tests/file_test.go | 10 ++++++++++ gnovm/tests/files/type34.gno | 12 ++++++++++++ gnovm/tests/files/type35.gno | 13 +++++++++++++ 14 files changed, 183 insertions(+), 14 deletions(-) create mode 100644 gnovm/tests/debug/time6_native.gno create mode 100644 gnovm/tests/debug_temp/34a.gno create mode 100644 gnovm/tests/debug_temp/type33.gno create mode 100644 gnovm/tests/debug_temp/type34.gno create mode 100644 gnovm/tests/debug_temp/type35.gno create mode 100644 gnovm/tests/debug_temp/type36.gno create mode 100644 gnovm/tests/debug_temp/type37.gno create mode 100644 gnovm/tests/files/type34.gno create mode 100644 gnovm/tests/files/type35.gno diff --git a/gnovm/Makefile b/gnovm/Makefile index 599ca58cd39..2d8665d9d9f 100644 --- a/gnovm/Makefile +++ b/gnovm/Makefile @@ -66,6 +66,8 @@ _test.gnolang.stdlibs:; go test tests/*.go -test.short -run 'TestFiles$$/' _test.gnolang.native.sync:; go test tests/*.go -test.short -run "TestFilesNative/" --update-golden-tests $(GOTEST_FLAGS) _test.gnolang.stdlibs.sync:; go test tests/*.go -test.short -run 'TestFiles$$/' --update-golden-tests $(GOTEST_FLAGS) +_test.gnolang.debug:; go test tests/*.go -test.short -run 'TestDebug$$/' $(GOTEST_FLAGS) +_test.gnolang.debug.native:; go test tests/*.go -test.short -run 'TestDebugNative$$/' $(GOTEST_FLAGS) ######################################## # Code gen # TODO: move _dev.stringer to go:generate instructions, simplify generate diff --git a/gnovm/pkg/gnolang/op_decl.go b/gnovm/pkg/gnolang/op_decl.go index 2c20c43ae2f..cc84a84208a 100644 --- a/gnovm/pkg/gnolang/op_decl.go +++ b/gnovm/pkg/gnolang/op_decl.go @@ -1,9 +1,5 @@ package gnolang -import ( - "fmt" -) - func (m *Machine) doOpValueDecl() { s := m.PopStmt().(*ValueDecl) lb := m.LastBlock() @@ -41,14 +37,15 @@ func (m *Machine) doOpValueDecl() { ConvertUntypedTo(&tv, nt) } else { if debug { - if nt.TypeID() != tv.T.TypeID() && - baseOf(nt).TypeID() != tv.T.TypeID() { - panic(fmt.Sprintf( - "type mismatch: %s vs %s", - nt.TypeID(), - tv.T.TypeID(), - )) - } + //debug.Println("---doOpValueDecl, nt, tv.T: ", nt, tv.T) + //if nt.TypeID() != tv.T.TypeID() && + // baseOf(nt).TypeID() != tv.T.TypeID() { + // panic(fmt.Sprintf( + // "type mismatch: %s vs %s", + // nt.TypeID(), + // tv.T.TypeID(), + // )) + //} } tv.T = nt } diff --git a/gnovm/pkg/gnolang/op_expressions.go b/gnovm/pkg/gnolang/op_expressions.go index b3bf240aea1..aaa0f6ce396 100644 --- a/gnovm/pkg/gnolang/op_expressions.go +++ b/gnovm/pkg/gnolang/op_expressions.go @@ -143,15 +143,21 @@ func (m *Machine) doOpSlice() { // deref, but the result is a pointer-to type. func (m *Machine) doOpStar() { xv := m.PopValue() + debug.Println("---doOpStar, xv: ", xv) switch bt := baseOf(xv.T).(type) { case *PointerType: pv := xv.V.(PointerValue) + debug.Println("---doOpStar, pv: ", pv) + debug.Println("---doOpStar, pv.TV: ", pv.TV) + debug.Println("---doOpStar, xv.T: ", xv.T) + debug.Println("---doOpStar, bt.Elt: ", bt.Elt) if pv.TV.T == DataByteType { - tv := TypedValue{T: xv.T.(*PointerType).Elt} + tv := TypedValue{T: bt.Elt} dbv := pv.TV.V.(DataByteValue) tv.SetUint8(dbv.GetByte()) m.PushValue(tv) } else { + debug.Println("---else") if pv.TV.IsUndefined() && bt.Elt.Kind() != InterfaceKind { refv := TypedValue{T: bt.Elt} m.PushValue(refv) diff --git a/gnovm/pkg/gnolang/op_types.go b/gnovm/pkg/gnolang/op_types.go index bc770f11722..cf457caf2ed 100644 --- a/gnovm/pkg/gnolang/op_types.go +++ b/gnovm/pkg/gnolang/op_types.go @@ -181,6 +181,7 @@ func (m *Machine) doOpMaybeNativeType() { // already swapped for *ConstExpr in the preprocessor. If not, panics. func (m *Machine) doOpStaticTypeOf() { x := m.PopExpr() + debug.Println("---doOpStaticTypeOf, x, type of x ", x, reflect.TypeOf(x)) switch x := x.(type) { case *NameExpr: // NOTE: duplicated from doOpEval @@ -397,7 +398,24 @@ func (m *Machine) doOpStaticTypeOf() { _, _, _, ft, _ := findEmbeddedFieldType(dxt.GetPkgPath(), dxt, path.Name, nil) m.PushValue(asValue(ft)) case VPNative: + //debug.Println("---VPNative, dxt: ", dxt) + //debug.Println("---VPNative, baseOf(dxt): ", baseOf(dxt)) + //debug.Println("---VPNative, dxt.elem, type of elem ", dxt.Elem(), reflect.TypeOf(dxt.Elem())) + //if nt, ok := dxt.Elem().(*NativeType); ok { + // //nt.Elem() + // //debug.Println("---nt.Elem: ", nt.Elem()) + // debug.Println("---nt.Type: ", nt.Type) + // debug.Println("---nt.gnoType: ", nt.gnoType) + // debug.Println("---nt.gnoType type: ", reflect.TypeOf(nt.gnoType)) + // + // debug.Println("---nt.gnoType base: ", baseOf(nt.gnoType)) + // debug.Println("---nt.gnoType base type: ", reflect.TypeOf(baseOf(nt.gnoType))) + // if _, ok := baseOf(nt.gnoType).(*PointerType); ok { + // panic("hey!!!!!!!!!!!!!!!") + // } + //} // if dxt is *PointerType, convert to *NativeType. + //if pt, ok := baseOf(dxt).(*PointerType); ok { if pt, ok := dxt.(*PointerType); ok { net, ok := pt.Elt.(*NativeType) if !ok { @@ -467,18 +485,22 @@ func (m *Machine) doOpStaticTypeOf() { m.PushOp(OpStaticTypeOf) m.Run() // XXX replace xt := m.ReapValues(start)[0].V.(TypeValue).Type - if pt, ok := xt.(*PointerType); ok { + debug.Println("---doOpStaticTypeOf, SliceExpr, xt: ", xt) + if pt, ok := baseOf(xt).(*PointerType); ok { + debug.Println("---pointer type, pt: ", pt) m.PushValue(asValue(&SliceType{ Elt: pt.Elt.Elem(), })) } else if xt.Kind() == StringKind { m.PushValue(asValue(StringType)) } else { + debug.Println("---else") m.PushValue(asValue(&SliceType{ Elt: xt.Elem(), })) } case *StarExpr: + debug.Println("---doOpStaticTypeOf, starX") start := m.NumValues m.PushOp(OpHalt) m.PushExpr(x.X) diff --git a/gnovm/tests/debug/time6_native.gno b/gnovm/tests/debug/time6_native.gno new file mode 100644 index 00000000000..a660df9256d --- /dev/null +++ b/gnovm/tests/debug/time6_native.gno @@ -0,0 +1,16 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + t := &time.Time{} + t.UnmarshalText([]byte("1985-04-12T23:20:50.52Z")) + // + fmt.Println(t) +} + +// Output: +// 1985-04-12 23:20:50.52 +0000 UTC diff --git a/gnovm/tests/debug_temp/34a.gno b/gnovm/tests/debug_temp/34a.gno new file mode 100644 index 00000000000..20f53b78cd6 --- /dev/null +++ b/gnovm/tests/debug_temp/34a.gno @@ -0,0 +1,12 @@ +package main + +type BytePtr *byte + +func main() { + bs := []byte("hello") + var p BytePtr = &bs[0] + println(*p) +} + +// Output: +// 104 diff --git a/gnovm/tests/debug_temp/type33.gno b/gnovm/tests/debug_temp/type33.gno new file mode 100644 index 00000000000..62d6696fafa --- /dev/null +++ b/gnovm/tests/debug_temp/type33.gno @@ -0,0 +1,28 @@ +package main + +import "fmt" + +// Define a base type +type Base int + +// Declare a new type that is a pointer to the base type +type PtrToBase *Base + +func main() { + var b Base = 42 // Initialize a variable of the base type + var p PtrToBase = &b // Initialize a variable of the new pointer type with the address of b + + fmt.Printf("The value of b is: %d\n", b) + + // Using the new pointer type + fmt.Printf("The value pointed to by p is: %d\n", *p) + + // Modifying the value pointed to by p + *p = 100 + fmt.Printf("The new value of b after modification through p is: %d\n", b) +} + +// Output: +// The value of b is: 42 +// The value pointed to by p is: 42 +// The new value of b after modification through p is: 100 diff --git a/gnovm/tests/debug_temp/type34.gno b/gnovm/tests/debug_temp/type34.gno new file mode 100644 index 00000000000..20f53b78cd6 --- /dev/null +++ b/gnovm/tests/debug_temp/type34.gno @@ -0,0 +1,12 @@ +package main + +type BytePtr *byte + +func main() { + bs := []byte("hello") + var p BytePtr = &bs[0] + println(*p) +} + +// Output: +// 104 diff --git a/gnovm/tests/debug_temp/type35.gno b/gnovm/tests/debug_temp/type35.gno new file mode 100644 index 00000000000..9ade96ee9c8 --- /dev/null +++ b/gnovm/tests/debug_temp/type35.gno @@ -0,0 +1,15 @@ +package main + +type IntPtr *int + +func main() { + var a, b int + a = 1 // Set a to 104 + s := []IntPtr{} + s = append(s, &a) + s = append(s, &b) + println(*s[0]) +} + +// Output: +// 1 diff --git a/gnovm/tests/debug_temp/type36.gno b/gnovm/tests/debug_temp/type36.gno new file mode 100644 index 00000000000..8bb64118036 --- /dev/null +++ b/gnovm/tests/debug_temp/type36.gno @@ -0,0 +1,17 @@ +package main + +type Arr [2]int +type Ptr *Arr + +func main() { + var arr Arr + arr[0] = 0 + arr[1] = 1 + + p := Ptr(&arr) + + println(p[:]) +} + +// Output: +// slice[(0 int),(1 int)] diff --git a/gnovm/tests/debug_temp/type37.gno b/gnovm/tests/debug_temp/type37.gno new file mode 100644 index 00000000000..15cc3c9aa8d --- /dev/null +++ b/gnovm/tests/debug_temp/type37.gno @@ -0,0 +1,7 @@ +package main + +import "time" + +func main() { + println(time.Second) +} diff --git a/gnovm/tests/file_test.go b/gnovm/tests/file_test.go index f070546ab74..ddf284afd79 100644 --- a/gnovm/tests/file_test.go +++ b/gnovm/tests/file_test.go @@ -32,6 +32,16 @@ func TestFiles(t *testing.T) { runFileTests(t, baseDir, []string{"*_native*"}) } +func TestDebug(t *testing.T) { + baseDir := filepath.Join(".", "debug") + runFileTests(t, baseDir, []string{"*_native*"}) +} + +func TestDebugNative(t *testing.T) { + baseDir := filepath.Join(".", "debug") + runFileTests(t, baseDir, []string{"*_stdlibs*"}, WithNativeLibs()) +} + func TestChallenges(t *testing.T) { baseDir := filepath.Join(".", "challenges") runFileTests(t, baseDir, nil) diff --git a/gnovm/tests/files/type34.gno b/gnovm/tests/files/type34.gno new file mode 100644 index 00000000000..20f53b78cd6 --- /dev/null +++ b/gnovm/tests/files/type34.gno @@ -0,0 +1,12 @@ +package main + +type BytePtr *byte + +func main() { + bs := []byte("hello") + var p BytePtr = &bs[0] + println(*p) +} + +// Output: +// 104 diff --git a/gnovm/tests/files/type35.gno b/gnovm/tests/files/type35.gno new file mode 100644 index 00000000000..613cb7b066b --- /dev/null +++ b/gnovm/tests/files/type35.gno @@ -0,0 +1,13 @@ +package main + +type IntPtr *int + +func main() { + var a, b int + a = 1 // Set a to 104 + s := []IntPtr{&a, &b} + println(*s[0]) +} + +// Output: +// 1 From a72ce75379f80499e0b315c5365ced7e2f6fa836 Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Tue, 9 Apr 2024 15:45:32 +0800 Subject: [PATCH 05/13] more test --- gnovm/pkg/gnolang/machine.go | 6 ++++++ gnovm/pkg/gnolang/op_expressions.go | 11 ++++++++++- gnovm/pkg/gnolang/op_types.go | 4 +++- gnovm/pkg/gnolang/preprocess.go | 19 ++++++++++++++++++- gnovm/pkg/gnolang/types.go | 2 ++ gnovm/pkg/gnolang/values.go | 6 ++++++ gnovm/tests/debug/time6_native.gno | 16 ---------------- gnovm/tests/{debug_temp => debug}/type36.gno | 0 gnovm/tests/debug_temp/a20a.gno | 20 ++++++++++++++++++++ gnovm/tests/debug_temp/ptr.gno | 14 ++++++++++++++ gnovm/tests/debug_temp/ptr_native.gno | 18 ++++++++++++++++++ gnovm/tests/debug_temp/time6_native.gno | 12 ++++++++++++ gnovm/tests/debug_temp/type38.gno | 19 +++++++++++++++++++ 13 files changed, 128 insertions(+), 19 deletions(-) delete mode 100644 gnovm/tests/debug/time6_native.gno rename gnovm/tests/{debug_temp => debug}/type36.gno (100%) create mode 100644 gnovm/tests/debug_temp/a20a.gno create mode 100644 gnovm/tests/debug_temp/ptr.gno create mode 100644 gnovm/tests/debug_temp/ptr_native.gno create mode 100644 gnovm/tests/debug_temp/time6_native.gno create mode 100644 gnovm/tests/debug_temp/type38.gno diff --git a/gnovm/pkg/gnolang/machine.go b/gnovm/pkg/gnolang/machine.go index e9e8eba8adc..81ed0436f46 100644 --- a/gnovm/pkg/gnolang/machine.go +++ b/gnovm/pkg/gnolang/machine.go @@ -1548,6 +1548,12 @@ func (m *Machine) PeekType(offset int) Type { func (m *Machine) PushValue(tv TypedValue) { if debug { m.Printf("+v %v\n", tv) + m.Printf("type of tv: %v \n", tv.T) + m.Printf("value of tv: %v \n", tv.V) + if tv, ok := tv.V.(TypeValue); ok { + m.Printf("tv.Type: %v \n", tv.Type) + + } } if len(m.Values) == m.NumValues { // TODO tune. also see PushOp(). diff --git a/gnovm/pkg/gnolang/op_expressions.go b/gnovm/pkg/gnolang/op_expressions.go index aaa0f6ce396..e82816c698c 100644 --- a/gnovm/pkg/gnolang/op_expressions.go +++ b/gnovm/pkg/gnolang/op_expressions.go @@ -78,7 +78,16 @@ func (m *Machine) doOpIndex2() { func (m *Machine) doOpSelector() { sx := m.PopExpr().(*SelectorExpr) xv := m.PeekValue(1) - res := xv.GetPointerTo(m.Alloc, m.Store, sx.Path).Deref() + debug.Println("---doOpSelector") + debug.Println("---doOpSelector, sx: ", sx) + debug.Println("---doOpSelector, sx path: ", sx.Path) + debug.Println("---doOpSelector, xv: ", xv) + ptr := xv.GetPointerTo(m.Alloc, m.Store, sx.Path) + debug.Println("---doOpSelector, ptr: ", ptr) + debug.Println("---doOpSelector, ptr.TV: ", ptr.TV) + res := ptr.Deref() + //res := xv.GetPointerTo(m.Alloc, m.Store, sx.Path).Deref() + debug.Println("---doOpSelector, res: ", res) if debug { m.Printf("-v[S] %v\n", xv) m.Printf("+v[S] %v\n", res) diff --git a/gnovm/pkg/gnolang/op_types.go b/gnovm/pkg/gnolang/op_types.go index cf457caf2ed..41d20a0bf8e 100644 --- a/gnovm/pkg/gnolang/op_types.go +++ b/gnovm/pkg/gnolang/op_types.go @@ -398,7 +398,7 @@ func (m *Machine) doOpStaticTypeOf() { _, _, _, ft, _ := findEmbeddedFieldType(dxt.GetPkgPath(), dxt, path.Name, nil) m.PushValue(asValue(ft)) case VPNative: - //debug.Println("---VPNative, dxt: ", dxt) + debug.Println("---VPNative, dxt: ", dxt) //debug.Println("---VPNative, baseOf(dxt): ", baseOf(dxt)) //debug.Println("---VPNative, dxt.elem, type of elem ", dxt.Elem(), reflect.TypeOf(dxt.Elem())) //if nt, ok := dxt.Elem().(*NativeType); ok { @@ -486,8 +486,10 @@ func (m *Machine) doOpStaticTypeOf() { m.Run() // XXX replace xt := m.ReapValues(start)[0].V.(TypeValue).Type debug.Println("---doOpStaticTypeOf, SliceExpr, xt: ", xt) + debug.Println("---doOpStaticTypeOf, SliceExpr, xt.Elem(): ", xt.Elem()) if pt, ok := baseOf(xt).(*PointerType); ok { debug.Println("---pointer type, pt: ", pt) + debug.Println("---pointer type, pt.Elt.Elem(): ", pt.Elt.Elem()) m.PushValue(asValue(&SliceType{ Elt: pt.Elt.Elem(), })) diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index fd967845b79..ac4265f8daf 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -2414,10 +2414,13 @@ func convertConst(store Store, last BlockNode, cx *ConstExpr, t Type) { // If autoNative is true, a broad range of xt can match against // a target native dt type, if and only if dt is a native type. func checkType(xt Type, dt Type, autoNative bool) { + debug.Println("---checkType, xt, dt: ", xt, dt) + debug.Println("---checkType,type of xt, dt: ", reflect.TypeOf(xt), reflect.TypeOf(dt)) // Special case if dt is interface kind: if dt.Kind() == InterfaceKind { if idt, ok := baseOf(dt).(*InterfaceType); ok { if idt.IsEmptyInterface() { + debug.Println("---return, empty interface") // if dt is an empty Gno interface, any x ok. return // ok } else if idt.IsImplementedBy(xt) { @@ -2433,6 +2436,7 @@ func checkType(xt Type, dt Type, autoNative bool) { nidt := ndt.Type if nidt.NumMethod() == 0 { // if dt is an empty Go native interface, ditto. + debug.Println("---ditto, native empty interface") return // ok } else if nxt, ok := baseOf(xt).(*NativeType); ok { // if xt has native base, do the naive native. @@ -2471,18 +2475,22 @@ func checkType(xt Type, dt Type, autoNative bool) { panic("should not happen") } } + debug.Println("---2") // Special case if xt or dt is *PointerType to *NativeType, // convert to *NativeType of pointer kind. if pxt, ok := xt.(*PointerType); ok { - // *gonative{x} is gonative{*x} + debug.Println("---checkType, xt is pointer type: pxt: ", pxt) + // *gonative{x} is(to) gonative{*x} //nolint:misspell if enxt, ok := pxt.Elt.(*NativeType); ok { xt = &NativeType{ Type: reflect.PtrTo(enxt.Type), } + debug.Println("---xt: ", xt) } } if pdt, ok := dt.(*PointerType); ok { + debug.Println("---checkType, dt is pointer type: pdt: ", pdt) // *gonative{x} is gonative{*x} if endt, ok := pdt.Elt.(*NativeType); ok { dt = &NativeType{ @@ -2909,6 +2917,7 @@ func predefineNow(store Store, last BlockNode, d Decl) (Decl, bool) { } func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (Decl, bool) { + debug.Println("---predefineNow2") pkg := packageOf(last) // pre-register d.GetName() to detect circular definition. for _, dn := range d.GetDeclNames() { @@ -2936,6 +2945,7 @@ func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (De } switch cd := d.(type) { case *FuncDecl: + debug.Println("---FuncDecl") // *FuncValue/*FuncType is mostly empty still; here // we just fill the func type (and recv if method). // NOTE: unlike the *ValueDecl case, this case doesn't @@ -2953,11 +2963,18 @@ func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (De ft := evalStaticType(store, last, &cd.Type).(*FuncType) ft = ft.UnboundType(rft) dt := (*DeclaredType)(nil) + + debug.Println("---FuncDecl, rt: ", rt) + debug.Println("---FuncDecl, baseOf rt: ", baseOf(rt)) + // see a20a if pt, ok := rt.(*PointerType); ok { + debug.Println("---1") dt = pt.Elem().(*DeclaredType) } else { + debug.Println("---2") dt = rt.(*DeclaredType) } + debug.Println("---dt: ", dt) if !dt.TryDefineMethod(&FuncValue{ Type: ft, IsMethod: true, diff --git a/gnovm/pkg/gnolang/types.go b/gnovm/pkg/gnolang/types.go index 34565b7a1b6..fe6ff3722ed 100644 --- a/gnovm/pkg/gnolang/types.go +++ b/gnovm/pkg/gnolang/types.go @@ -1092,12 +1092,14 @@ func (ft *FuncType) IsZero() bool { // if ft is a method, returns whether method takes a pointer receiver. func (ft *FuncType) HasPointerReceiver() bool { + debug.Println("---HasPointerReceiver") if debug { if len(ft.Params) == 0 { panic("expected unbound method function type, but found no receiver parameter.") } } _, ok := ft.Params[0].Type.(*PointerType) + debug.Println("---ok: ", ok) return ok // return ft.Params[0].Type.Kind() == PointerKind } diff --git a/gnovm/pkg/gnolang/values.go b/gnovm/pkg/gnolang/values.go index 85e6562eca6..0a71c0f6352 100644 --- a/gnovm/pkg/gnolang/values.go +++ b/gnovm/pkg/gnolang/values.go @@ -1832,13 +1832,19 @@ func (tv *TypedValue) GetPointerTo(alloc *Allocator, store Store, path ValuePath } panic("should not happen") case VPNative: + debug.Println("---VPNative") + debug.Println("---VPNative, dtv: ", dtv) + debug.Println("---VPNative, dtv.T: ", dtv.T) var nv *NativeValue // Special case if tv.T.(PointerType): // we may need to treat this as a native pointer // to get the correct pointer-receiver value. if _, ok := dtv.T.(*PointerType); ok { + debug.Println("---dtv.T pointer type") pv := dtv.V.(PointerValue) nv = pv.TV.V.(*NativeValue) + debug.Println("---dtv.T pointer type, pv: ", pv) + debug.Println("---dtv.T pointer type, nv: ", nv) } else { nv = dtv.V.(*NativeValue) } diff --git a/gnovm/tests/debug/time6_native.gno b/gnovm/tests/debug/time6_native.gno deleted file mode 100644 index a660df9256d..00000000000 --- a/gnovm/tests/debug/time6_native.gno +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import ( - "fmt" - "time" -) - -func main() { - t := &time.Time{} - t.UnmarshalText([]byte("1985-04-12T23:20:50.52Z")) - // - fmt.Println(t) -} - -// Output: -// 1985-04-12 23:20:50.52 +0000 UTC diff --git a/gnovm/tests/debug_temp/type36.gno b/gnovm/tests/debug/type36.gno similarity index 100% rename from gnovm/tests/debug_temp/type36.gno rename to gnovm/tests/debug/type36.gno diff --git a/gnovm/tests/debug_temp/a20a.gno b/gnovm/tests/debug_temp/a20a.gno new file mode 100644 index 00000000000..981fffe1e12 --- /dev/null +++ b/gnovm/tests/debug_temp/a20a.gno @@ -0,0 +1,20 @@ +package main + +import "fmt" + +type IntArray []int +type Arr *IntArray + +func (a Arr) Add(x int) { // receiver is val, not ptr + *a = append(*a, x) +} + +func main() { + a := new(IntArray) + Arr(a).Add(4) + + fmt.Println(*a) +} + +// Output: +// [4] diff --git a/gnovm/tests/debug_temp/ptr.gno b/gnovm/tests/debug_temp/ptr.gno new file mode 100644 index 00000000000..54a792480cf --- /dev/null +++ b/gnovm/tests/debug_temp/ptr.gno @@ -0,0 +1,14 @@ +package main + +type Foo struct { + age int + name string +} + +func main() { + f := Foo{age: 1, name: "hello"} + println(f.age) +} + +// Output: +// 1 diff --git a/gnovm/tests/debug_temp/ptr_native.gno b/gnovm/tests/debug_temp/ptr_native.gno new file mode 100644 index 00000000000..5a4b2bd5cea --- /dev/null +++ b/gnovm/tests/debug_temp/ptr_native.gno @@ -0,0 +1,18 @@ +package main + +import "time" + +type MyTime *time.Time + +type Foo struct { + age int + name string + time MyTime +} + +func main() { + f := Foo{age: 1, name: "hello", time: &time.Time{}} + println(f.time) +} + +// Output: diff --git a/gnovm/tests/debug_temp/time6_native.gno b/gnovm/tests/debug_temp/time6_native.gno new file mode 100644 index 00000000000..1e1fc299301 --- /dev/null +++ b/gnovm/tests/debug_temp/time6_native.gno @@ -0,0 +1,12 @@ +package main + +import ( + "time" +) + +func main() { + t := &time.Time{} + println(t.Second() + t.Second()) +} + +// Output: diff --git a/gnovm/tests/debug_temp/type38.gno b/gnovm/tests/debug_temp/type38.gno new file mode 100644 index 00000000000..631e74c4c9b --- /dev/null +++ b/gnovm/tests/debug_temp/type38.gno @@ -0,0 +1,19 @@ +package main + +type Integer *int + +func (f foo) do(i Integer) { + println(i) +} + +type foo struct{} + +func main() { + a := 1 + + f := foo{} + f.do(&a) +} + +// Output: +// &(1 int) From 9a95c0ef2874f8b76442ef3c617ff54f88660db0 Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Tue, 9 Apr 2024 15:57:28 +0800 Subject: [PATCH 06/13] clean --- gnovm/Makefile | 2 -- gnovm/pkg/gnolang/machine.go | 6 ---- gnovm/pkg/gnolang/op_expressions.go | 13 --------- gnovm/pkg/gnolang/op_types.go | 24 ---------------- gnovm/pkg/gnolang/types.go | 2 -- gnovm/pkg/gnolang/values.go | 6 ---- gnovm/tests/debug_temp/34a.gno | 12 -------- gnovm/tests/debug_temp/ptr.gno | 14 ---------- gnovm/tests/debug_temp/ptr_native.gno | 18 ------------ gnovm/tests/debug_temp/time6_native.gno | 12 -------- gnovm/tests/debug_temp/type33.gno | 28 ------------------- gnovm/tests/debug_temp/type34.gno | 12 -------- gnovm/tests/debug_temp/type35.gno | 15 ---------- gnovm/tests/debug_temp/type37.gno | 7 ----- gnovm/tests/debug_temp/type38.gno | 19 ------------- gnovm/tests/file_test.go | 10 ------- gnovm/tests/files/type35.gno | 4 ++- gnovm/tests/{debug => files}/type36.gno | 0 .../{debug_temp/a20a.gno => files/type37.gno} | 0 19 files changed, 3 insertions(+), 201 deletions(-) delete mode 100644 gnovm/tests/debug_temp/34a.gno delete mode 100644 gnovm/tests/debug_temp/ptr.gno delete mode 100644 gnovm/tests/debug_temp/ptr_native.gno delete mode 100644 gnovm/tests/debug_temp/time6_native.gno delete mode 100644 gnovm/tests/debug_temp/type33.gno delete mode 100644 gnovm/tests/debug_temp/type34.gno delete mode 100644 gnovm/tests/debug_temp/type35.gno delete mode 100644 gnovm/tests/debug_temp/type37.gno delete mode 100644 gnovm/tests/debug_temp/type38.gno rename gnovm/tests/{debug => files}/type36.gno (100%) rename gnovm/tests/{debug_temp/a20a.gno => files/type37.gno} (100%) diff --git a/gnovm/Makefile b/gnovm/Makefile index 2d8665d9d9f..599ca58cd39 100644 --- a/gnovm/Makefile +++ b/gnovm/Makefile @@ -66,8 +66,6 @@ _test.gnolang.stdlibs:; go test tests/*.go -test.short -run 'TestFiles$$/' _test.gnolang.native.sync:; go test tests/*.go -test.short -run "TestFilesNative/" --update-golden-tests $(GOTEST_FLAGS) _test.gnolang.stdlibs.sync:; go test tests/*.go -test.short -run 'TestFiles$$/' --update-golden-tests $(GOTEST_FLAGS) -_test.gnolang.debug:; go test tests/*.go -test.short -run 'TestDebug$$/' $(GOTEST_FLAGS) -_test.gnolang.debug.native:; go test tests/*.go -test.short -run 'TestDebugNative$$/' $(GOTEST_FLAGS) ######################################## # Code gen # TODO: move _dev.stringer to go:generate instructions, simplify generate diff --git a/gnovm/pkg/gnolang/machine.go b/gnovm/pkg/gnolang/machine.go index 81ed0436f46..e9e8eba8adc 100644 --- a/gnovm/pkg/gnolang/machine.go +++ b/gnovm/pkg/gnolang/machine.go @@ -1548,12 +1548,6 @@ func (m *Machine) PeekType(offset int) Type { func (m *Machine) PushValue(tv TypedValue) { if debug { m.Printf("+v %v\n", tv) - m.Printf("type of tv: %v \n", tv.T) - m.Printf("value of tv: %v \n", tv.V) - if tv, ok := tv.V.(TypeValue); ok { - m.Printf("tv.Type: %v \n", tv.Type) - - } } if len(m.Values) == m.NumValues { // TODO tune. also see PushOp(). diff --git a/gnovm/pkg/gnolang/op_expressions.go b/gnovm/pkg/gnolang/op_expressions.go index e82816c698c..62055346e91 100644 --- a/gnovm/pkg/gnolang/op_expressions.go +++ b/gnovm/pkg/gnolang/op_expressions.go @@ -78,16 +78,9 @@ func (m *Machine) doOpIndex2() { func (m *Machine) doOpSelector() { sx := m.PopExpr().(*SelectorExpr) xv := m.PeekValue(1) - debug.Println("---doOpSelector") - debug.Println("---doOpSelector, sx: ", sx) - debug.Println("---doOpSelector, sx path: ", sx.Path) - debug.Println("---doOpSelector, xv: ", xv) ptr := xv.GetPointerTo(m.Alloc, m.Store, sx.Path) - debug.Println("---doOpSelector, ptr: ", ptr) - debug.Println("---doOpSelector, ptr.TV: ", ptr.TV) res := ptr.Deref() //res := xv.GetPointerTo(m.Alloc, m.Store, sx.Path).Deref() - debug.Println("---doOpSelector, res: ", res) if debug { m.Printf("-v[S] %v\n", xv) m.Printf("+v[S] %v\n", res) @@ -152,21 +145,15 @@ func (m *Machine) doOpSlice() { // deref, but the result is a pointer-to type. func (m *Machine) doOpStar() { xv := m.PopValue() - debug.Println("---doOpStar, xv: ", xv) switch bt := baseOf(xv.T).(type) { case *PointerType: pv := xv.V.(PointerValue) - debug.Println("---doOpStar, pv: ", pv) - debug.Println("---doOpStar, pv.TV: ", pv.TV) - debug.Println("---doOpStar, xv.T: ", xv.T) - debug.Println("---doOpStar, bt.Elt: ", bt.Elt) if pv.TV.T == DataByteType { tv := TypedValue{T: bt.Elt} dbv := pv.TV.V.(DataByteValue) tv.SetUint8(dbv.GetByte()) m.PushValue(tv) } else { - debug.Println("---else") if pv.TV.IsUndefined() && bt.Elt.Kind() != InterfaceKind { refv := TypedValue{T: bt.Elt} m.PushValue(refv) diff --git a/gnovm/pkg/gnolang/op_types.go b/gnovm/pkg/gnolang/op_types.go index 41d20a0bf8e..ce89ed2e1c3 100644 --- a/gnovm/pkg/gnolang/op_types.go +++ b/gnovm/pkg/gnolang/op_types.go @@ -398,24 +398,6 @@ func (m *Machine) doOpStaticTypeOf() { _, _, _, ft, _ := findEmbeddedFieldType(dxt.GetPkgPath(), dxt, path.Name, nil) m.PushValue(asValue(ft)) case VPNative: - debug.Println("---VPNative, dxt: ", dxt) - //debug.Println("---VPNative, baseOf(dxt): ", baseOf(dxt)) - //debug.Println("---VPNative, dxt.elem, type of elem ", dxt.Elem(), reflect.TypeOf(dxt.Elem())) - //if nt, ok := dxt.Elem().(*NativeType); ok { - // //nt.Elem() - // //debug.Println("---nt.Elem: ", nt.Elem()) - // debug.Println("---nt.Type: ", nt.Type) - // debug.Println("---nt.gnoType: ", nt.gnoType) - // debug.Println("---nt.gnoType type: ", reflect.TypeOf(nt.gnoType)) - // - // debug.Println("---nt.gnoType base: ", baseOf(nt.gnoType)) - // debug.Println("---nt.gnoType base type: ", reflect.TypeOf(baseOf(nt.gnoType))) - // if _, ok := baseOf(nt.gnoType).(*PointerType); ok { - // panic("hey!!!!!!!!!!!!!!!") - // } - //} - // if dxt is *PointerType, convert to *NativeType. - //if pt, ok := baseOf(dxt).(*PointerType); ok { if pt, ok := dxt.(*PointerType); ok { net, ok := pt.Elt.(*NativeType) if !ok { @@ -485,24 +467,18 @@ func (m *Machine) doOpStaticTypeOf() { m.PushOp(OpStaticTypeOf) m.Run() // XXX replace xt := m.ReapValues(start)[0].V.(TypeValue).Type - debug.Println("---doOpStaticTypeOf, SliceExpr, xt: ", xt) - debug.Println("---doOpStaticTypeOf, SliceExpr, xt.Elem(): ", xt.Elem()) if pt, ok := baseOf(xt).(*PointerType); ok { - debug.Println("---pointer type, pt: ", pt) - debug.Println("---pointer type, pt.Elt.Elem(): ", pt.Elt.Elem()) m.PushValue(asValue(&SliceType{ Elt: pt.Elt.Elem(), })) } else if xt.Kind() == StringKind { m.PushValue(asValue(StringType)) } else { - debug.Println("---else") m.PushValue(asValue(&SliceType{ Elt: xt.Elem(), })) } case *StarExpr: - debug.Println("---doOpStaticTypeOf, starX") start := m.NumValues m.PushOp(OpHalt) m.PushExpr(x.X) diff --git a/gnovm/pkg/gnolang/types.go b/gnovm/pkg/gnolang/types.go index fe6ff3722ed..34565b7a1b6 100644 --- a/gnovm/pkg/gnolang/types.go +++ b/gnovm/pkg/gnolang/types.go @@ -1092,14 +1092,12 @@ func (ft *FuncType) IsZero() bool { // if ft is a method, returns whether method takes a pointer receiver. func (ft *FuncType) HasPointerReceiver() bool { - debug.Println("---HasPointerReceiver") if debug { if len(ft.Params) == 0 { panic("expected unbound method function type, but found no receiver parameter.") } } _, ok := ft.Params[0].Type.(*PointerType) - debug.Println("---ok: ", ok) return ok // return ft.Params[0].Type.Kind() == PointerKind } diff --git a/gnovm/pkg/gnolang/values.go b/gnovm/pkg/gnolang/values.go index 0a71c0f6352..85e6562eca6 100644 --- a/gnovm/pkg/gnolang/values.go +++ b/gnovm/pkg/gnolang/values.go @@ -1832,19 +1832,13 @@ func (tv *TypedValue) GetPointerTo(alloc *Allocator, store Store, path ValuePath } panic("should not happen") case VPNative: - debug.Println("---VPNative") - debug.Println("---VPNative, dtv: ", dtv) - debug.Println("---VPNative, dtv.T: ", dtv.T) var nv *NativeValue // Special case if tv.T.(PointerType): // we may need to treat this as a native pointer // to get the correct pointer-receiver value. if _, ok := dtv.T.(*PointerType); ok { - debug.Println("---dtv.T pointer type") pv := dtv.V.(PointerValue) nv = pv.TV.V.(*NativeValue) - debug.Println("---dtv.T pointer type, pv: ", pv) - debug.Println("---dtv.T pointer type, nv: ", nv) } else { nv = dtv.V.(*NativeValue) } diff --git a/gnovm/tests/debug_temp/34a.gno b/gnovm/tests/debug_temp/34a.gno deleted file mode 100644 index 20f53b78cd6..00000000000 --- a/gnovm/tests/debug_temp/34a.gno +++ /dev/null @@ -1,12 +0,0 @@ -package main - -type BytePtr *byte - -func main() { - bs := []byte("hello") - var p BytePtr = &bs[0] - println(*p) -} - -// Output: -// 104 diff --git a/gnovm/tests/debug_temp/ptr.gno b/gnovm/tests/debug_temp/ptr.gno deleted file mode 100644 index 54a792480cf..00000000000 --- a/gnovm/tests/debug_temp/ptr.gno +++ /dev/null @@ -1,14 +0,0 @@ -package main - -type Foo struct { - age int - name string -} - -func main() { - f := Foo{age: 1, name: "hello"} - println(f.age) -} - -// Output: -// 1 diff --git a/gnovm/tests/debug_temp/ptr_native.gno b/gnovm/tests/debug_temp/ptr_native.gno deleted file mode 100644 index 5a4b2bd5cea..00000000000 --- a/gnovm/tests/debug_temp/ptr_native.gno +++ /dev/null @@ -1,18 +0,0 @@ -package main - -import "time" - -type MyTime *time.Time - -type Foo struct { - age int - name string - time MyTime -} - -func main() { - f := Foo{age: 1, name: "hello", time: &time.Time{}} - println(f.time) -} - -// Output: diff --git a/gnovm/tests/debug_temp/time6_native.gno b/gnovm/tests/debug_temp/time6_native.gno deleted file mode 100644 index 1e1fc299301..00000000000 --- a/gnovm/tests/debug_temp/time6_native.gno +++ /dev/null @@ -1,12 +0,0 @@ -package main - -import ( - "time" -) - -func main() { - t := &time.Time{} - println(t.Second() + t.Second()) -} - -// Output: diff --git a/gnovm/tests/debug_temp/type33.gno b/gnovm/tests/debug_temp/type33.gno deleted file mode 100644 index 62d6696fafa..00000000000 --- a/gnovm/tests/debug_temp/type33.gno +++ /dev/null @@ -1,28 +0,0 @@ -package main - -import "fmt" - -// Define a base type -type Base int - -// Declare a new type that is a pointer to the base type -type PtrToBase *Base - -func main() { - var b Base = 42 // Initialize a variable of the base type - var p PtrToBase = &b // Initialize a variable of the new pointer type with the address of b - - fmt.Printf("The value of b is: %d\n", b) - - // Using the new pointer type - fmt.Printf("The value pointed to by p is: %d\n", *p) - - // Modifying the value pointed to by p - *p = 100 - fmt.Printf("The new value of b after modification through p is: %d\n", b) -} - -// Output: -// The value of b is: 42 -// The value pointed to by p is: 42 -// The new value of b after modification through p is: 100 diff --git a/gnovm/tests/debug_temp/type34.gno b/gnovm/tests/debug_temp/type34.gno deleted file mode 100644 index 20f53b78cd6..00000000000 --- a/gnovm/tests/debug_temp/type34.gno +++ /dev/null @@ -1,12 +0,0 @@ -package main - -type BytePtr *byte - -func main() { - bs := []byte("hello") - var p BytePtr = &bs[0] - println(*p) -} - -// Output: -// 104 diff --git a/gnovm/tests/debug_temp/type35.gno b/gnovm/tests/debug_temp/type35.gno deleted file mode 100644 index 9ade96ee9c8..00000000000 --- a/gnovm/tests/debug_temp/type35.gno +++ /dev/null @@ -1,15 +0,0 @@ -package main - -type IntPtr *int - -func main() { - var a, b int - a = 1 // Set a to 104 - s := []IntPtr{} - s = append(s, &a) - s = append(s, &b) - println(*s[0]) -} - -// Output: -// 1 diff --git a/gnovm/tests/debug_temp/type37.gno b/gnovm/tests/debug_temp/type37.gno deleted file mode 100644 index 15cc3c9aa8d..00000000000 --- a/gnovm/tests/debug_temp/type37.gno +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "time" - -func main() { - println(time.Second) -} diff --git a/gnovm/tests/debug_temp/type38.gno b/gnovm/tests/debug_temp/type38.gno deleted file mode 100644 index 631e74c4c9b..00000000000 --- a/gnovm/tests/debug_temp/type38.gno +++ /dev/null @@ -1,19 +0,0 @@ -package main - -type Integer *int - -func (f foo) do(i Integer) { - println(i) -} - -type foo struct{} - -func main() { - a := 1 - - f := foo{} - f.do(&a) -} - -// Output: -// &(1 int) diff --git a/gnovm/tests/file_test.go b/gnovm/tests/file_test.go index ddf284afd79..f070546ab74 100644 --- a/gnovm/tests/file_test.go +++ b/gnovm/tests/file_test.go @@ -32,16 +32,6 @@ func TestFiles(t *testing.T) { runFileTests(t, baseDir, []string{"*_native*"}) } -func TestDebug(t *testing.T) { - baseDir := filepath.Join(".", "debug") - runFileTests(t, baseDir, []string{"*_native*"}) -} - -func TestDebugNative(t *testing.T) { - baseDir := filepath.Join(".", "debug") - runFileTests(t, baseDir, []string{"*_stdlibs*"}, WithNativeLibs()) -} - func TestChallenges(t *testing.T) { baseDir := filepath.Join(".", "challenges") runFileTests(t, baseDir, nil) diff --git a/gnovm/tests/files/type35.gno b/gnovm/tests/files/type35.gno index 613cb7b066b..9ade96ee9c8 100644 --- a/gnovm/tests/files/type35.gno +++ b/gnovm/tests/files/type35.gno @@ -5,7 +5,9 @@ type IntPtr *int func main() { var a, b int a = 1 // Set a to 104 - s := []IntPtr{&a, &b} + s := []IntPtr{} + s = append(s, &a) + s = append(s, &b) println(*s[0]) } diff --git a/gnovm/tests/debug/type36.gno b/gnovm/tests/files/type36.gno similarity index 100% rename from gnovm/tests/debug/type36.gno rename to gnovm/tests/files/type36.gno diff --git a/gnovm/tests/debug_temp/a20a.gno b/gnovm/tests/files/type37.gno similarity index 100% rename from gnovm/tests/debug_temp/a20a.gno rename to gnovm/tests/files/type37.gno From 8fa94c93d8b8abfb32493aa674c9acc4aa06d6cf Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Tue, 9 Apr 2024 16:06:21 +0800 Subject: [PATCH 07/13] clean --- gnovm/pkg/gnolang/op_decl.go | 20 +++++++++++--------- gnovm/pkg/gnolang/op_expressions.go | 4 +--- gnovm/pkg/gnolang/op_types.go | 2 +- gnovm/pkg/gnolang/preprocess.go | 15 --------------- 4 files changed, 13 insertions(+), 28 deletions(-) diff --git a/gnovm/pkg/gnolang/op_decl.go b/gnovm/pkg/gnolang/op_decl.go index cc84a84208a..a11d0bb4aad 100644 --- a/gnovm/pkg/gnolang/op_decl.go +++ b/gnovm/pkg/gnolang/op_decl.go @@ -1,5 +1,7 @@ package gnolang +import "fmt" + func (m *Machine) doOpValueDecl() { s := m.PopStmt().(*ValueDecl) lb := m.LastBlock() @@ -37,15 +39,15 @@ func (m *Machine) doOpValueDecl() { ConvertUntypedTo(&tv, nt) } else { if debug { - //debug.Println("---doOpValueDecl, nt, tv.T: ", nt, tv.T) - //if nt.TypeID() != tv.T.TypeID() && - // baseOf(nt).TypeID() != tv.T.TypeID() { - // panic(fmt.Sprintf( - // "type mismatch: %s vs %s", - // nt.TypeID(), - // tv.T.TypeID(), - // )) - //} + debug.Println("---doOpValueDecl, nt, tv.T: ", nt, tv.T) + if nt.TypeID() != tv.T.TypeID() && + baseOf(nt).TypeID() != tv.T.TypeID() { + panic(fmt.Sprintf( + "type mismatch: %s vs %s", + nt.TypeID(), + tv.T.TypeID(), + )) + } } tv.T = nt } diff --git a/gnovm/pkg/gnolang/op_expressions.go b/gnovm/pkg/gnolang/op_expressions.go index 62055346e91..bc21850bb8c 100644 --- a/gnovm/pkg/gnolang/op_expressions.go +++ b/gnovm/pkg/gnolang/op_expressions.go @@ -78,9 +78,7 @@ func (m *Machine) doOpIndex2() { func (m *Machine) doOpSelector() { sx := m.PopExpr().(*SelectorExpr) xv := m.PeekValue(1) - ptr := xv.GetPointerTo(m.Alloc, m.Store, sx.Path) - res := ptr.Deref() - //res := xv.GetPointerTo(m.Alloc, m.Store, sx.Path).Deref() + res := xv.GetPointerTo(m.Alloc, m.Store, sx.Path).Deref() if debug { m.Printf("-v[S] %v\n", xv) m.Printf("+v[S] %v\n", res) diff --git a/gnovm/pkg/gnolang/op_types.go b/gnovm/pkg/gnolang/op_types.go index ce89ed2e1c3..850aa7a6e64 100644 --- a/gnovm/pkg/gnolang/op_types.go +++ b/gnovm/pkg/gnolang/op_types.go @@ -181,7 +181,6 @@ func (m *Machine) doOpMaybeNativeType() { // already swapped for *ConstExpr in the preprocessor. If not, panics. func (m *Machine) doOpStaticTypeOf() { x := m.PopExpr() - debug.Println("---doOpStaticTypeOf, x, type of x ", x, reflect.TypeOf(x)) switch x := x.(type) { case *NameExpr: // NOTE: duplicated from doOpEval @@ -398,6 +397,7 @@ func (m *Machine) doOpStaticTypeOf() { _, _, _, ft, _ := findEmbeddedFieldType(dxt.GetPkgPath(), dxt, path.Name, nil) m.PushValue(asValue(ft)) case VPNative: + // if dxt is *PointerType, convert to *NativeType. if pt, ok := dxt.(*PointerType); ok { net, ok := pt.Elt.(*NativeType) if !ok { diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index ac4265f8daf..85173acb57c 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -2414,13 +2414,10 @@ func convertConst(store Store, last BlockNode, cx *ConstExpr, t Type) { // If autoNative is true, a broad range of xt can match against // a target native dt type, if and only if dt is a native type. func checkType(xt Type, dt Type, autoNative bool) { - debug.Println("---checkType, xt, dt: ", xt, dt) - debug.Println("---checkType,type of xt, dt: ", reflect.TypeOf(xt), reflect.TypeOf(dt)) // Special case if dt is interface kind: if dt.Kind() == InterfaceKind { if idt, ok := baseOf(dt).(*InterfaceType); ok { if idt.IsEmptyInterface() { - debug.Println("---return, empty interface") // if dt is an empty Gno interface, any x ok. return // ok } else if idt.IsImplementedBy(xt) { @@ -2436,7 +2433,6 @@ func checkType(xt Type, dt Type, autoNative bool) { nidt := ndt.Type if nidt.NumMethod() == 0 { // if dt is an empty Go native interface, ditto. - debug.Println("---ditto, native empty interface") return // ok } else if nxt, ok := baseOf(xt).(*NativeType); ok { // if xt has native base, do the naive native. @@ -2475,22 +2471,18 @@ func checkType(xt Type, dt Type, autoNative bool) { panic("should not happen") } } - debug.Println("---2") // Special case if xt or dt is *PointerType to *NativeType, // convert to *NativeType of pointer kind. if pxt, ok := xt.(*PointerType); ok { - debug.Println("---checkType, xt is pointer type: pxt: ", pxt) // *gonative{x} is(to) gonative{*x} //nolint:misspell if enxt, ok := pxt.Elt.(*NativeType); ok { xt = &NativeType{ Type: reflect.PtrTo(enxt.Type), } - debug.Println("---xt: ", xt) } } if pdt, ok := dt.(*PointerType); ok { - debug.Println("---checkType, dt is pointer type: pdt: ", pdt) // *gonative{x} is gonative{*x} if endt, ok := pdt.Elt.(*NativeType); ok { dt = &NativeType{ @@ -2917,7 +2909,6 @@ func predefineNow(store Store, last BlockNode, d Decl) (Decl, bool) { } func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (Decl, bool) { - debug.Println("---predefineNow2") pkg := packageOf(last) // pre-register d.GetName() to detect circular definition. for _, dn := range d.GetDeclNames() { @@ -2945,7 +2936,6 @@ func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (De } switch cd := d.(type) { case *FuncDecl: - debug.Println("---FuncDecl") // *FuncValue/*FuncType is mostly empty still; here // we just fill the func type (and recv if method). // NOTE: unlike the *ValueDecl case, this case doesn't @@ -2964,17 +2954,12 @@ func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (De ft = ft.UnboundType(rft) dt := (*DeclaredType)(nil) - debug.Println("---FuncDecl, rt: ", rt) - debug.Println("---FuncDecl, baseOf rt: ", baseOf(rt)) // see a20a if pt, ok := rt.(*PointerType); ok { - debug.Println("---1") dt = pt.Elem().(*DeclaredType) } else { - debug.Println("---2") dt = rt.(*DeclaredType) } - debug.Println("---dt: ", dt) if !dt.TryDefineMethod(&FuncValue{ Type: ft, IsMethod: true, From 0b1515b07e0a45c4bbe7620585761fc3c2e344f9 Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Tue, 9 Apr 2024 16:08:13 +0800 Subject: [PATCH 08/13] clean --- gnovm/pkg/gnolang/op_decl.go | 5 +++-- gnovm/pkg/gnolang/preprocess.go | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gnovm/pkg/gnolang/op_decl.go b/gnovm/pkg/gnolang/op_decl.go index a11d0bb4aad..2c20c43ae2f 100644 --- a/gnovm/pkg/gnolang/op_decl.go +++ b/gnovm/pkg/gnolang/op_decl.go @@ -1,6 +1,8 @@ package gnolang -import "fmt" +import ( + "fmt" +) func (m *Machine) doOpValueDecl() { s := m.PopStmt().(*ValueDecl) @@ -39,7 +41,6 @@ func (m *Machine) doOpValueDecl() { ConvertUntypedTo(&tv, nt) } else { if debug { - debug.Println("---doOpValueDecl, nt, tv.T: ", nt, tv.T) if nt.TypeID() != tv.T.TypeID() && baseOf(nt).TypeID() != tv.T.TypeID() { panic(fmt.Sprintf( diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 85173acb57c..1dc772e1ba1 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -2954,7 +2954,6 @@ func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (De ft = ft.UnboundType(rft) dt := (*DeclaredType)(nil) - // see a20a if pt, ok := rt.(*PointerType); ok { dt = pt.Elem().(*DeclaredType) } else { From e30fe305e15d96bca39ea3fa1c386005930b1a88 Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Wed, 10 Apr 2024 11:27:56 +0800 Subject: [PATCH 09/13] disallow base type pointer as a receiver --- gnovm/pkg/gnolang/preprocess.go | 6 ++++++ gnovm/tests/files/type37.gno | 4 ++-- gnovm/tests/files/type37a.gno | 19 +++++++++++++++++++ gnovm/tests/files/type37b.gno | 20 ++++++++++++++++++++ gnovm/tests/files/type38.gno | 16 ++++++++++++++++ 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 gnovm/tests/files/type37a.gno create mode 100644 gnovm/tests/files/type37b.gno create mode 100644 gnovm/tests/files/type38.gno diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 1dc772e1ba1..179d3a4dd3e 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -2954,6 +2954,12 @@ 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") + if dt, ok := rt.(*DeclaredType); ok { + if _, ok := baseOf(dt).(*PointerType); ok { + panic("invalid receiver type IntPtr (pointer or interface type)") + } + } if pt, ok := rt.(*PointerType); ok { dt = pt.Elem().(*DeclaredType) } else { diff --git a/gnovm/tests/files/type37.gno b/gnovm/tests/files/type37.gno index 981fffe1e12..6ce42d576af 100644 --- a/gnovm/tests/files/type37.gno +++ b/gnovm/tests/files/type37.gno @@ -16,5 +16,5 @@ func main() { fmt.Println(*a) } -// Output: -// [4] +// Error: +// main/debug/type37.gno:8: invalid receiver type IntPtr (pointer or interface type) diff --git a/gnovm/tests/files/type37a.gno b/gnovm/tests/files/type37a.gno new file mode 100644 index 00000000000..16db1cc4dc2 --- /dev/null +++ b/gnovm/tests/files/type37a.gno @@ -0,0 +1,19 @@ +package main + +import "fmt" + +type IntArray []int + +func (a *IntArray) Add(x int) { // receiver is val, not ptr + *a = append(*a, x) +} + +func main() { + a := new(IntArray) + a.Add(4) + + fmt.Println(*a) +} + +// Output: +// [4] diff --git a/gnovm/tests/files/type37b.gno b/gnovm/tests/files/type37b.gno new file mode 100644 index 00000000000..922ffcb31b4 --- /dev/null +++ b/gnovm/tests/files/type37b.gno @@ -0,0 +1,20 @@ +package main + +import "fmt" + +type IntArray []int +type Arr *IntArray + +func (a Arr) Add(x int) { // receiver is val, not ptr + *a = append(*a, x) +} + +func main() { + a := new(IntArray) + Arr(a).Add(4) + + fmt.Println(*a) +} + +// Error: +// main/debug/type37b.gno:8: invalid receiver type IntPtr (pointer or interface type) diff --git a/gnovm/tests/files/type38.gno b/gnovm/tests/files/type38.gno new file mode 100644 index 00000000000..d654caae39b --- /dev/null +++ b/gnovm/tests/files/type38.gno @@ -0,0 +1,16 @@ +package main + +type IntPtr *int + +var ip IntPtr = new(int) + +func (p IntPtr) Int() int { + return *p +} + +func main() { + println(ip.Int()) +} + +// Error: +// main/debug/type38.gno:7: invalid receiver type IntPtr (pointer or interface type) From fa9b2938aef54d74251e3b1e171e19e283a93bd6 Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Wed, 10 Apr 2024 15:05:11 +0800 Subject: [PATCH 10/13] more tests --- gnovm/pkg/gnolang/preprocess.go | 11 +++++++++-- gnovm/tests/files/type37.gno | 2 +- gnovm/tests/files/type37c.gno | 20 ++++++++++++++++++++ gnovm/tests/files/type37d.gno | 22 ++++++++++++++++++++++ gnovm/tests/files/type37e.gno | 21 +++++++++++++++++++++ gnovm/tests/files/type37e1.gno | 26 ++++++++++++++++++++++++++ gnovm/tests/files/type38.gno | 2 +- gnovm/tests/files/type39.gno | 22 ++++++++++++++++++++++ gnovm/tests/files/type39a.gno | 24 ++++++++++++++++++++++++ 9 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 gnovm/tests/files/type37c.gno create mode 100644 gnovm/tests/files/type37d.gno create mode 100644 gnovm/tests/files/type37e.gno create mode 100644 gnovm/tests/files/type37e1.gno create mode 100644 gnovm/tests/files/type39.gno create mode 100644 gnovm/tests/files/type39a.gno diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 179d3a4dd3e..39d02883e75 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -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) diff --git a/gnovm/tests/files/type37.gno b/gnovm/tests/files/type37.gno index 6ce42d576af..88785b262d9 100644 --- a/gnovm/tests/files/type37.gno +++ b/gnovm/tests/files/type37.gno @@ -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) diff --git a/gnovm/tests/files/type37c.gno b/gnovm/tests/files/type37c.gno new file mode 100644 index 00000000000..7cd8ac7367e --- /dev/null +++ b/gnovm/tests/files/type37c.gno @@ -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 diff --git a/gnovm/tests/files/type37d.gno b/gnovm/tests/files/type37d.gno new file mode 100644 index 00000000000..ad0e5563fa0 --- /dev/null +++ b/gnovm/tests/files/type37d.gno @@ -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) diff --git a/gnovm/tests/files/type37e.gno b/gnovm/tests/files/type37e.gno new file mode 100644 index 00000000000..a2bc063bece --- /dev/null +++ b/gnovm/tests/files/type37e.gno @@ -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) +// [] diff --git a/gnovm/tests/files/type37e1.gno b/gnovm/tests/files/type37e1.gno new file mode 100644 index 00000000000..a3a03f1e248 --- /dev/null +++ b/gnovm/tests/files/type37e1.gno @@ -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 diff --git a/gnovm/tests/files/type38.gno b/gnovm/tests/files/type38.gno index d654caae39b..69e8125b28c 100644 --- a/gnovm/tests/files/type38.gno +++ b/gnovm/tests/files/type38.gno @@ -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) diff --git a/gnovm/tests/files/type39.gno b/gnovm/tests/files/type39.gno new file mode 100644 index 00000000000..2186dfa1e87 --- /dev/null +++ b/gnovm/tests/files/type39.gno @@ -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) diff --git a/gnovm/tests/files/type39a.gno b/gnovm/tests/files/type39a.gno new file mode 100644 index 00000000000..03277288dc5 --- /dev/null +++ b/gnovm/tests/files/type39a.gno @@ -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) From 65e21163f5452106a9b672f5b149397ceab5183a Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Wed, 10 Apr 2024 18:24:11 +0800 Subject: [PATCH 11/13] fixup --- gnovm/pkg/gnolang/preprocess.go | 24 ++++++++++++++++-------- gnovm/tests/files/type37.gno | 2 +- gnovm/tests/files/type37b.gno | 18 ++++++++++-------- gnovm/tests/files/type37d.gno | 18 ++++++------------ gnovm/tests/files/type37e.gno | 20 ++++++++------------ gnovm/tests/files/type37e1.gno | 26 -------------------------- gnovm/tests/files/type38.gno | 24 +++++++++++++++++------- gnovm/tests/files/type39.gno | 2 +- gnovm/tests/files/type39a.gno | 2 +- gnovm/tests/files/type39b.gno | 22 ++++++++++++++++++++++ 10 files changed, 82 insertions(+), 76 deletions(-) delete mode 100644 gnovm/tests/files/type37e1.gno create mode 100644 gnovm/tests/files/type39b.gno diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 39d02883e75..a365c5fbd11 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -2954,24 +2954,32 @@ func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (De ft = ft.UnboundType(rft) dt := (*DeclaredType)(nil) - // 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 { + // check base type of receiver type, should not be pointer type or interface type + assertValidReceiverType := func(t Type) { + if _, ok := t.(*PointerType); ok { panic(fmt.Sprintf("invalid receiver type %v (pointer type)\n", rt)) } - if _, ok := baseOf(dt).(*InterfaceType); ok { + if _, ok := t.(*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)) + assertValidReceiverType(pt.Elem()) + if ddt, ok := pt.Elem().(*DeclaredType); ok { + assertValidReceiverType(baseOf(ddt)) + dt = ddt + } else { + panic("should not happen") } - dt = pt.Elem().(*DeclaredType) + } else if ddt, ok := rt.(*DeclaredType); ok { + debug.Println("---rt is declared type") + assertValidReceiverType(baseOf(ddt)) + dt = ddt } else { - dt = rt.(*DeclaredType) + panic("should not happen") } + if !dt.TryDefineMethod(&FuncValue{ Type: ft, IsMethod: true, diff --git a/gnovm/tests/files/type37.gno b/gnovm/tests/files/type37.gno index 88785b262d9..40f11588944 100644 --- a/gnovm/tests/files/type37.gno +++ b/gnovm/tests/files/type37.gno @@ -17,4 +17,4 @@ func main() { } // Error: -// main/debug/type37.gno:8: invalid receiver type main.Arr (pointer type) +// main/files/type37.gno:8: invalid receiver type main.Arr (pointer type) diff --git a/gnovm/tests/files/type37b.gno b/gnovm/tests/files/type37b.gno index 922ffcb31b4..2056b4050b7 100644 --- a/gnovm/tests/files/type37b.gno +++ b/gnovm/tests/files/type37b.gno @@ -2,19 +2,21 @@ package main import "fmt" -type IntArray []int -type Arr *IntArray +type Integer int -func (a Arr) Add(x int) { // receiver is val, not ptr - *a = append(*a, x) +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(IntArray) - Arr(a).Add(4) + a := new(Integer) // a is a pointer to Integer + b := &a // b is a pointer to a pointer to Integer - fmt.Println(*a) + // 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/type37b.gno:8: invalid receiver type IntPtr (pointer or interface type) +// main/files/type37b.gno:7: invalid receiver type **main.Integer (pointer type) diff --git a/gnovm/tests/files/type37d.gno b/gnovm/tests/files/type37d.gno index ad0e5563fa0..debdb613f5d 100644 --- a/gnovm/tests/files/type37d.gno +++ b/gnovm/tests/files/type37d.gno @@ -1,22 +1,16 @@ package main -import "fmt" +type IntPtr *int -type Integer int +var ip IntPtr = new(int) -func (i **Integer) Add(x int) { - **i += Integer(x) // Dereference twice to get to the actual Integer value and modify it +func (p IntPtr) Int() int { + return *p } 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 + println(ip.Int()) } // Error: -// main/debug/type37d.gno:7: invalid receiver type **main.Integer (pointer type) +// main/files/type37d.gno:7: invalid receiver type main.IntPtr (pointer type) diff --git a/gnovm/tests/files/type37e.gno b/gnovm/tests/files/type37e.gno index a2bc063bece..7bae49feda7 100644 --- a/gnovm/tests/files/type37e.gno +++ b/gnovm/tests/files/type37e.gno @@ -1,21 +1,17 @@ package main -import "fmt" +type IntPtr *int +type Int2 IntPtr -type IntArray []int -type Arr *IntArray +var ip IntPtr = new(int) -func print(arr Arr) { // receiver is val, not ptr - println(arr) +func (i2 Int2) Int() int { + return *i2 } func main() { - a := new(IntArray) - print(a) - - fmt.Println(*a) + println(Int2(ip).Int()) } -// Output: -// &(nil main.IntArray) -// [] +// Error: +// main/files/type37e.gno:8: invalid receiver type main.Int2 (pointer type) diff --git a/gnovm/tests/files/type37e1.gno b/gnovm/tests/files/type37e1.gno deleted file mode 100644 index a3a03f1e248..00000000000 --- a/gnovm/tests/files/type37e1.gno +++ /dev/null @@ -1,26 +0,0 @@ -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 diff --git a/gnovm/tests/files/type38.gno b/gnovm/tests/files/type38.gno index 69e8125b28c..a3a03f1e248 100644 --- a/gnovm/tests/files/type38.gno +++ b/gnovm/tests/files/type38.gno @@ -1,16 +1,26 @@ package main -type IntPtr *int +import "fmt" -var ip IntPtr = new(int) +type IntArray []int +type Arr *IntArray -func (p IntPtr) Int() int { - return *p +func add(arr Arr) { // receiver is val, not ptr + *arr = append(*arr, 1) } func main() { - println(ip.Int()) + a := new(IntArray) + add(a) + + fmt.Println(a) + fmt.Println(*a) + fmt.Println(len(*a)) + fmt.Println((*a)[0]) } -// Error: -// main/debug/type38.gno:7: invalid receiver type main.IntPtr (pointer type) +// Output: +// &[1] +// [1] +// 1 +// 1 diff --git a/gnovm/tests/files/type39.gno b/gnovm/tests/files/type39.gno index 2186dfa1e87..b4eeaa4b41f 100644 --- a/gnovm/tests/files/type39.gno +++ b/gnovm/tests/files/type39.gno @@ -19,4 +19,4 @@ func main() { } // Error: -// main/debug/type39.gno:7: invalid receiver type main.foo (interface type) +// main/files/type39.gno:7: invalid receiver type main.foo (interface type) diff --git a/gnovm/tests/files/type39a.gno b/gnovm/tests/files/type39a.gno index 03277288dc5..5d861cb62cf 100644 --- a/gnovm/tests/files/type39a.gno +++ b/gnovm/tests/files/type39a.gno @@ -21,4 +21,4 @@ func main() { } // Error: -// main/debug/type39a.gno:9: invalid receiver type main.FF (interface type) +// main/files/type39a.gno:9: invalid receiver type main.FF (interface type) diff --git a/gnovm/tests/files/type39b.gno b/gnovm/tests/files/type39b.gno new file mode 100644 index 00000000000..f0f80a7e15a --- /dev/null +++ b/gnovm/tests/files/type39b.gno @@ -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/files/type39b.gno:7: invalid receiver type *main.foo (interface type) From 25f204c0193470d7860905b4a89a9fe849154e31 Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Wed, 10 Apr 2024 19:15:18 +0800 Subject: [PATCH 12/13] clean --- gnovm/pkg/gnolang/preprocess.go | 1 - 1 file changed, 1 deletion(-) diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index a365c5fbd11..0af42e22646 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -2973,7 +2973,6 @@ func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (De panic("should not happen") } } else if ddt, ok := rt.(*DeclaredType); ok { - debug.Println("---rt is declared type") assertValidReceiverType(baseOf(ddt)) dt = ddt } else { From a2521a5276813f1d8a2c938126f278c66bc1f64a Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Wed, 10 Apr 2024 19:51:10 +0800 Subject: [PATCH 13/13] proper err msg --- gnovm/pkg/gnolang/preprocess.go | 4 ++-- gnovm/tests/files/type37.gno | 2 +- gnovm/tests/files/type37b.gno | 2 +- gnovm/tests/files/type37d.gno | 2 +- gnovm/tests/files/type37e.gno | 2 +- gnovm/tests/files/type37f.gno | 16 ++++++++++++++++ gnovm/tests/files/type39.gno | 2 +- gnovm/tests/files/type39a.gno | 2 +- gnovm/tests/files/type39b.gno | 2 +- 9 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 gnovm/tests/files/type37f.gno diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 0af42e22646..d393dcb1b1d 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -2957,10 +2957,10 @@ func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (De // check base type of receiver type, should not be pointer type or interface type assertValidReceiverType := func(t Type) { if _, ok := t.(*PointerType); ok { - panic(fmt.Sprintf("invalid receiver type %v (pointer type)\n", rt)) + panic(fmt.Sprintf("invalid receiver type %v (base type is pointer type)\n", rt)) } if _, ok := t.(*InterfaceType); ok { - panic(fmt.Sprintf("invalid receiver type %v (interface type)\n", rt)) + panic(fmt.Sprintf("invalid receiver type %v (base type is interface type)\n", rt)) } } diff --git a/gnovm/tests/files/type37.gno b/gnovm/tests/files/type37.gno index 40f11588944..1bc157b44ff 100644 --- a/gnovm/tests/files/type37.gno +++ b/gnovm/tests/files/type37.gno @@ -17,4 +17,4 @@ func main() { } // Error: -// main/files/type37.gno:8: invalid receiver type main.Arr (pointer type) +// main/files/type37.gno:8: invalid receiver type main.Arr (base type is pointer type) diff --git a/gnovm/tests/files/type37b.gno b/gnovm/tests/files/type37b.gno index 2056b4050b7..aea1b445ca1 100644 --- a/gnovm/tests/files/type37b.gno +++ b/gnovm/tests/files/type37b.gno @@ -19,4 +19,4 @@ func main() { } // Error: -// main/files/type37b.gno:7: invalid receiver type **main.Integer (pointer type) +// main/files/type37b.gno:7: invalid receiver type **main.Integer (base type is pointer type) diff --git a/gnovm/tests/files/type37d.gno b/gnovm/tests/files/type37d.gno index debdb613f5d..ada9541e64e 100644 --- a/gnovm/tests/files/type37d.gno +++ b/gnovm/tests/files/type37d.gno @@ -13,4 +13,4 @@ func main() { } // Error: -// main/files/type37d.gno:7: invalid receiver type main.IntPtr (pointer type) +// main/files/type37d.gno:7: invalid receiver type main.IntPtr (base type is pointer type) diff --git a/gnovm/tests/files/type37e.gno b/gnovm/tests/files/type37e.gno index 7bae49feda7..a2537c9cb8d 100644 --- a/gnovm/tests/files/type37e.gno +++ b/gnovm/tests/files/type37e.gno @@ -14,4 +14,4 @@ func main() { } // Error: -// main/files/type37e.gno:8: invalid receiver type main.Int2 (pointer type) +// main/files/type37e.gno:8: invalid receiver type main.Int2 (base type is pointer type) diff --git a/gnovm/tests/files/type37f.gno b/gnovm/tests/files/type37f.gno new file mode 100644 index 00000000000..7bffa748314 --- /dev/null +++ b/gnovm/tests/files/type37f.gno @@ -0,0 +1,16 @@ +package main + +type IntPtr *int + +var ip IntPtr = new(int) + +func (p *IntPtr) Int() int { + return **p +} + +func main() { + println((&ip).Int()) +} + +// Error: +// main/files/type37f.gno:7: invalid receiver type *main.IntPtr (base type is pointer type) diff --git a/gnovm/tests/files/type39.gno b/gnovm/tests/files/type39.gno index b4eeaa4b41f..aebcc226385 100644 --- a/gnovm/tests/files/type39.gno +++ b/gnovm/tests/files/type39.gno @@ -19,4 +19,4 @@ func main() { } // Error: -// main/files/type39.gno:7: invalid receiver type main.foo (interface type) +// main/files/type39.gno:7: invalid receiver type main.foo (base type is interface type) diff --git a/gnovm/tests/files/type39a.gno b/gnovm/tests/files/type39a.gno index 5d861cb62cf..06f41897f93 100644 --- a/gnovm/tests/files/type39a.gno +++ b/gnovm/tests/files/type39a.gno @@ -21,4 +21,4 @@ func main() { } // Error: -// main/files/type39a.gno:9: invalid receiver type main.FF (interface type) +// main/files/type39a.gno:9: invalid receiver type main.FF (base type is interface type) diff --git a/gnovm/tests/files/type39b.gno b/gnovm/tests/files/type39b.gno index f0f80a7e15a..dbf5312a825 100644 --- a/gnovm/tests/files/type39b.gno +++ b/gnovm/tests/files/type39b.gno @@ -19,4 +19,4 @@ func main() { } // Error: -// main/files/type39b.gno:7: invalid receiver type *main.foo (interface type) +// main/files/type39b.gno:7: invalid receiver type *main.foo (base type is interface type)