aboutsummaryrefslogtreecommitdiff
path: root/test/method.go
diff options
context:
space:
mode:
authorAlan Donovan <adonovan@google.com>2013-02-11 18:20:52 -0500
committerAlan Donovan <adonovan@google.com>2013-02-11 18:20:52 -0500
commit1c1096ea31ed50f3553382ebb81a6a16396e56ec (patch)
treee5694fb132d8aae3f55a8d340d14b173467a2193 /test/method.go
parentd282532901c02e3f2dde4ed3f2258bcb7a61d510 (diff)
downloadgo-1c1096ea31ed50f3553382ebb81a6a16396e56ec.tar.gz
go-1c1096ea31ed50f3553382ebb81a6a16396e56ec.zip
test: a number of fixes.
Details: - reorder.go: delete p8. (Once expectation is changed per b/4627 it is identical to p1.) - switch.go: added some more (degenerate) switches. - range.go: improved error messages in a few cases. - method.go: added tests of calls to promoted methods. R=iant CC=golang-dev https://golang.org/cl/7306087
Diffstat (limited to 'test/method.go')
-rw-r--r--test/method.go71
1 files changed, 64 insertions, 7 deletions
diff --git a/test/method.go b/test/method.go
index 0c239afbd1..d97bc4a7d0 100644
--- a/test/method.go
+++ b/test/method.go
@@ -128,13 +128,13 @@ func main() {
panic("fail")
}
- var zs struct { S }
- var zps struct { *S1 }
- var zi struct { I }
- var zpi struct { *I1 }
- var zpt struct { *T1 }
- var zt struct { T }
- var zv struct { Val }
+ var zs struct{ S }
+ var zps struct{ *S1 }
+ var zi struct{ I }
+ var zpi struct{ *I1 }
+ var zpt struct{ *T1 }
+ var zt struct{ T }
+ var zv struct{ Val }
if zs.val() != 1 {
println("zs.val:", zs.val())
@@ -247,4 +247,61 @@ func main() {
println("zv.val():", zv.val())
panic("fail")
}
+
+ promotion()
+}
+
+type A struct{ B }
+type B struct {
+ C
+ *D
+}
+type C int
+
+func (C) f() {} // value receiver, direct field of A
+func (*C) g() {} // pointer receiver
+
+type D int
+
+func (D) h() {} // value receiver, indirect field of A
+func (*D) i() {} // pointer receiver
+
+func expectPanic() {
+ if r := recover(); r == nil {
+ panic("expected nil dereference")
+ }
+}
+
+func promotion() {
+ var a A
+ // Addressable value receiver.
+ a.f()
+ a.g()
+ func() {
+ defer expectPanic()
+ a.h() // dynamic error: nil dereference in a.B.D->f()
+ }()
+ a.i()
+
+ // Non-addressable value receiver.
+ A(a).f()
+ // A(a).g() // static error: cannot call pointer method on A literal.B.C
+ func() {
+ defer expectPanic()
+ A(a).h() // dynamic error: nil dereference in A().B.D->f()
+ }()
+ A(a).i()
+
+ // Pointer receiver.
+ (&a).f()
+ (&a).g()
+ func() {
+ defer expectPanic()
+ (&a).h() // dynamic error: nil deref: nil dereference in (&a).B.D->f()
+ }()
+ (&a).i()
+
+ c := new(C)
+ c.f() // makes a copy
+ c.g()
}