diff options
author | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
---|---|---|
committer | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
commit | 554fd8c5195424bdbcabf5de30fdc183aba391bd (patch) | |
tree | 976dc5ab7fddf506dadce60ae936f43f58787092 /gcc/testsuite/go.test/test/interface | |
download | cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.bz2 cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.xz |
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig;
imported gcc-4.6.4 source tree from verified upstream tarball.
downloading a git-generated archive based on the 'upstream' tag
should provide you with a source tree that is binary identical
to the one extracted from the above tarball.
if you have obtained the source via the command 'git clone',
however, do note that line-endings of files in your working
directory might differ from line-endings of the respective
files in the upstream repository.
Diffstat (limited to 'gcc/testsuite/go.test/test/interface')
-rw-r--r-- | gcc/testsuite/go.test/test/interface/bigdata.go | 75 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/convert.go | 147 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/convert1.go | 25 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/convert2.go | 25 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/embed.go | 82 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/embed0.go | 29 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/embed1.go | 45 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/explicit.go | 75 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/fail.go | 26 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/fake.go | 97 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/pointer.go | 36 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/receiver.go | 121 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/receiver1.go | 58 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/recursive.go | 21 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/returntype.go | 25 | ||||
-rw-r--r-- | gcc/testsuite/go.test/test/interface/struct.go | 156 |
16 files changed, 1043 insertions, 0 deletions
diff --git a/gcc/testsuite/go.test/test/interface/bigdata.go b/gcc/testsuite/go.test/test/interface/bigdata.go new file mode 100644 index 000000000..44f6ab127 --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/bigdata.go @@ -0,0 +1,75 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// check that big vs small, pointer vs not +// interface methods work. + +package main + +type I interface { M() int64 } + +type BigPtr struct { a, b, c, d int64 } +func (z *BigPtr) M() int64 { return z.a+z.b+z.c+z.d } + +type SmallPtr struct { a int32 } +func (z *SmallPtr) M() int64 { return int64(z.a) } + +type IntPtr int32 +func (z *IntPtr) M() int64 { return int64(*z) } + +var bad bool + +func test(name string, i I) { + m := i.M() + if m != 12345 { + println(name, m) + bad = true + } +} + +func ptrs() { + var bigptr BigPtr = BigPtr{ 10000, 2000, 300, 45 } + var smallptr SmallPtr = SmallPtr{ 12345 } + var intptr IntPtr = 12345 + +// test("bigptr", bigptr) + test("&bigptr", &bigptr) +// test("smallptr", smallptr) + test("&smallptr", &smallptr) +// test("intptr", intptr) + test("&intptr", &intptr) +} + +type Big struct { a, b, c, d int64 } +func (z Big) M() int64 { return z.a+z.b+z.c+z.d } + +type Small struct { a int32 } +func (z Small) M() int64 { return int64(z.a) } + +type Int int32 +func (z Int) M() int64 { return int64(z) } + +func nonptrs() { + var big Big = Big{ 10000, 2000, 300, 45 } + var small Small = Small{ 12345 } + var int Int = 12345 + + test("big", big) + test("&big", &big) + test("small", small) + test("&small", &small) + test("int", int) + test("&int", &int) +} + +func main() { + ptrs() + nonptrs() + + if bad { + println("BUG: interface4") + } +} diff --git a/gcc/testsuite/go.test/test/interface/convert.go b/gcc/testsuite/go.test/test/interface/convert.go new file mode 100644 index 000000000..7f429f703 --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/convert.go @@ -0,0 +1,147 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Check uses of all the different interface +// conversion runtime functions. + +package main + +type Stringer interface { + String() string +} +type StringLengther interface { + String() string + Length() int +} +type Empty interface{} + +type T string + +func (t T) String() string { + return string(t) +} +func (t T) Length() int { + return len(t) +} + +type U string + +func (u U) String() string { + return string(u) +} + +var t = T("hello") +var u = U("goodbye") +var e Empty +var s Stringer = t +var sl StringLengther = t +var i int +var ok bool + +func hello(s string) { + if s != "hello" { + println("not hello: ", s) + panic("fail") + } +} + +func five(i int) { + if i != 5 { + println("not 5: ", i) + panic("fail") + } +} + +func true(ok bool) { + if !ok { + panic("not true") + } +} + +func false(ok bool) { + if ok { + panic("not false") + } +} + +func main() { + // T2I + s = t + hello(s.String()) + + // I2T + t = s.(T) + hello(t.String()) + + // T2E + e = t + + // E2T + t = e.(T) + hello(t.String()) + + // T2I again + sl = t + hello(sl.String()) + five(sl.Length()) + + // I2I static + s = sl + hello(s.String()) + + // I2I dynamic + sl = s.(StringLengther) + hello(sl.String()) + five(sl.Length()) + + // I2E (and E2T) + e = s + hello(e.(T).String()) + + // E2I + s = e.(Stringer) + hello(s.String()) + + // I2T2 true + t, ok = s.(T) + true(ok) + hello(t.String()) + + // I2T2 false + _, ok = s.(U) + false(ok) + + // I2I2 true + sl, ok = s.(StringLengther) + true(ok) + hello(sl.String()) + five(sl.Length()) + + // I2I2 false (and T2I) + s = u + sl, ok = s.(StringLengther) + false(ok) + + // E2T2 true + t, ok = e.(T) + true(ok) + hello(t.String()) + + // E2T2 false + i, ok = e.(int) + false(ok) + + // E2I2 true + sl, ok = e.(StringLengther) + true(ok) + hello(sl.String()) + five(sl.Length()) + + // E2I2 false (and T2E) + e = u + sl, ok = e.(StringLengther) + false(ok) +} diff --git a/gcc/testsuite/go.test/test/interface/convert1.go b/gcc/testsuite/go.test/test/interface/convert1.go new file mode 100644 index 000000000..658b1a92f --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/convert1.go @@ -0,0 +1,25 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Check that static interface conversion of +// interface value nil succeeds. + +package main + +type R interface { R() } +type RW interface { R(); W() } + +var e interface {} +var r R +var rw RW + +func main() { + r = r + r = rw + e = r + e = rw + rw = rw +} diff --git a/gcc/testsuite/go.test/test/interface/convert2.go b/gcc/testsuite/go.test/test/interface/convert2.go new file mode 100644 index 000000000..658b1a92f --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/convert2.go @@ -0,0 +1,25 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Check that static interface conversion of +// interface value nil succeeds. + +package main + +type R interface { R() } +type RW interface { R(); W() } + +var e interface {} +var r R +var rw RW + +func main() { + r = r + r = rw + e = r + e = rw + rw = rw +} diff --git a/gcc/testsuite/go.test/test/interface/embed.go b/gcc/testsuite/go.test/test/interface/embed.go new file mode 100644 index 000000000..4a702398c --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/embed.go @@ -0,0 +1,82 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Check methods derived from embedded interface and *interface values. + +package main + +import "os" + +const Value = 1e12 + +type Inter interface { M() int64 } + +type T int64 +func (t T) M() int64 { return int64(t) } +var t = T(Value) +var pt = &t +var ti Inter = t +var pti = &ti + +type S struct { Inter } +var s = S{ ti } +var ps = &s + +type SP struct { *Inter } +var sp = SP{ &ti } +var psp = &sp + +var i Inter +var pi = &i + +var ok = true + +func check(s string, v int64) { + if v != Value { + println(s, v) + ok = false + } +} + +func main() { + check("t.M()", t.M()) + check("pt.M()", pt.M()) + check("ti.M()", ti.M()) + check("pti.M()", pti.M()) + check("s.M()", s.M()) + check("ps.M()", ps.M()) + check("sp.M()", sp.M()) + check("psp.M()", psp.M()) + + i = t + check("i = t; i.M()", i.M()) + check("i = t; pi.M()", pi.M()) + + i = pt + check("i = pt; i.M()", i.M()) + check("i = pt; pi.M()", pi.M()) + + i = s + check("i = s; i.M()", i.M()) + check("i = s; pi.M()", pi.M()) + + i = ps + check("i = ps; i.M()", i.M()) + check("i = ps; pi.M()", pi.M()) + + i = sp + check("i = sp; i.M()", i.M()) + check("i = sp; pi.M()", pi.M()) + + i = psp + check("i = psp; i.M()", i.M()) + check("i = psp; pi.M()", pi.M()) + + if !ok { + println("BUG: interface10") + os.Exit(1) + } +} diff --git a/gcc/testsuite/go.test/test/interface/embed0.go b/gcc/testsuite/go.test/test/interface/embed0.go new file mode 100644 index 000000000..bbd81e760 --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/embed0.go @@ -0,0 +1,29 @@ +// true # used by embed1.go + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Check that embedded interface types can have local methods. + +package p + +type T int +func (t T) m() {} + +type I interface { m() } +type J interface { I } + +func main() { + var i I + var j J + var t T + i = t + j = t + _ = i + _ = j + i = j + _ = i + j = i + _ = j +} diff --git a/gcc/testsuite/go.test/test/interface/embed1.go b/gcc/testsuite/go.test/test/interface/embed1.go new file mode 100644 index 000000000..24e50471f --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/embed1.go @@ -0,0 +1,45 @@ +// $G $D/embed0.go && $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Check that embedded interface types can have local methods. + +package main + +import "./embed0" + +type T int +func (t T) m() {} + +type I interface { m() } +type J interface { I } + +type PI interface { p.I } +type PJ interface { p.J } + +func main() { + var i I + var j J + var t T + i = t + j = t + _ = i + _ = j + i = j + _ = i + j = i + _ = j + var pi PI + var pj PJ + var pt p.T + pi = pt + pj = pt + _ = pi + _ = pj + pi = pj + _ = pi + pj = pi + _ = pj +} diff --git a/gcc/testsuite/go.test/test/interface/explicit.go b/gcc/testsuite/go.test/test/interface/explicit.go new file mode 100644 index 000000000..b6a582fff --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/explicit.go @@ -0,0 +1,75 @@ +// errchk $G -e $D/$F.go + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Static error messages about interface conversions. + +package main + +type T struct { + a int +} + +var t *T + +type I interface { + M() +} + +var i I + +type I2 interface { + M() + N() +} + +var i2 I2 + +type E interface{} + +var e E + +func main() { + e = t // ok + t = e // ERROR "need explicit|need type assertion" + + // neither of these can work, + // because i has an extra method + // that t does not, so i cannot contain a t. + i = t // ERROR "incompatible|missing M method" + t = i // ERROR "incompatible|need type assertion" + + i = i2 // ok + i2 = i // ERROR "incompatible|missing N method" + + i = I(i2) // ok + i2 = I2(i) // ERROR "invalid|missing N method" + + e = E(t) // ok + t = T(e) // ERROR "need explicit|need type assertion|incompatible" +} + +type M interface { + M() +} + +var m M + +var _ = m.(int) // ERROR "impossible type assertion" + +type Int int + +func (Int) M(float64) {} + +var _ = m.(Int) // ERROR "impossible type assertion" + +var ii int +var jj Int + +var m1 M = ii // ERROR "incompatible|missing" +var m2 M = jj // ERROR "incompatible|wrong type for M method" + +var m3 = M(ii) // ERROR "invalid|missing" +var m4 = M(jj) // ERROR "invalid|wrong type for M method" diff --git a/gcc/testsuite/go.test/test/interface/fail.go b/gcc/testsuite/go.test/test/interface/fail.go new file mode 100644 index 000000000..3e741d3f9 --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/fail.go @@ -0,0 +1,26 @@ +// $G $D/$F.go && $L $F.$A && ! ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Check that interface conversion fails when method is missing. + +package main + +type I interface { + Foo() +} + +func main() { + var s *S + var i I + var e interface {} + e = s + i = e.(I) + _ = i +} + +// hide S down here to avoid static warning +type S struct { +} diff --git a/gcc/testsuite/go.test/test/interface/fake.go b/gcc/testsuite/go.test/test/interface/fake.go new file mode 100644 index 000000000..5cf3be052 --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/fake.go @@ -0,0 +1,97 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Interface comparisons using types hidden +// inside reflected-on structs. + +package main + +import "reflect" + +type T struct { + f float32 + g float32 + + s string + t string + + u uint32 + v uint32 + + w uint32 + x uint32 + + y uint32 + z uint32 +} + +func add(s, t string) string { + return s + t +} + +func assert(b bool) { + if !b { + panic("assert") + } +} + +func main() { + var x T + x.f = 1.0 + x.g = x.f + x.s = add("abc", "def") + x.t = add("abc", "def") + x.u = 1 + x.v = 2 + x.w = 1<<28 + x.x = 2<<28 + x.y = 0x12345678 + x.z = x.y + + // check mem and string + v := reflect.NewValue(x) + i := v.(*reflect.StructValue).Field(0) + j := v.(*reflect.StructValue).Field(1) + assert(i.Interface() == j.Interface()) + + s := v.(*reflect.StructValue).Field(2) + t := v.(*reflect.StructValue).Field(3) + assert(s.Interface() == t.Interface()) + + // make sure different values are different. + // make sure whole word is being compared, + // not just a single byte. + i = v.(*reflect.StructValue).Field(4) + j = v.(*reflect.StructValue).Field(5) + assert(i.Interface() != j.Interface()) + + i = v.(*reflect.StructValue).Field(6) + j = v.(*reflect.StructValue).Field(7) + assert(i.Interface() != j.Interface()) + + i = v.(*reflect.StructValue).Field(8) + j = v.(*reflect.StructValue).Field(9) + assert(i.Interface() == j.Interface()) +} + +/* +comparing uncomparable type float32 +throw: interface compare + +panic PC=0x28ceb8 [1] +throw+0x41 /Users/rsc/goX/src/runtime/runtime.c:54 + throw(0x3014a, 0x0) +ifaceeq+0x15c /Users/rsc/goX/src/runtime/iface.c:501 + ifaceeq(0x2aa7c0, 0x0, 0x0, 0x0, 0x2aa7c0, ...) +sys·ifaceeq+0x48 /Users/rsc/goX/src/runtime/iface.c:527 + sys·ifaceeq(0x2aa7c0, 0x0, 0x0, 0x0, 0x2aa7c0, ...) +main·main+0x190 /Users/rsc/goX/src/cmd/gc/x.go:10 + main·main() +mainstart+0xf /Users/rsc/goX/src/runtime/amd64/asm.s:53 + mainstart() +sys·Goexit /Users/rsc/goX/src/runtime/proc.c:124 + sys·Goexit() +*/ diff --git a/gcc/testsuite/go.test/test/interface/pointer.go b/gcc/testsuite/go.test/test/interface/pointer.go new file mode 100644 index 000000000..e628b558e --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/pointer.go @@ -0,0 +1,36 @@ +// errchk $G $D/$F.go + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Check that interface{M()} = *interface{M()} produces a compiler error. + +package main + +type Inst interface { + Next() *Inst +} + +type Regexp struct { + code []Inst + start Inst +} + +type Start struct { + foo *Inst +} + +func (start *Start) Next() *Inst { return nil } + + +func AddInst(Inst) *Inst { + print("ok in addinst\n") + return nil +} + +func main() { + print("call addinst\n") + var x Inst = AddInst(new(Start)) // ERROR "pointer to interface" + print("return from addinst\n") +} diff --git a/gcc/testsuite/go.test/test/interface/receiver.go b/gcc/testsuite/go.test/test/interface/receiver.go new file mode 100644 index 000000000..f53daf8da --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/receiver.go @@ -0,0 +1,121 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Implicit methods for embedded types. +// Mixed pointer and non-pointer receivers. + +package main + +type T int + +var nv, np int + +func (t T) V() { + if t != 42 { + panic(t) + } + nv++ +} + +func (t *T) P() { + if *t != 42 { + println(t, *t) + panic("fail") + } + np++ +} + +type V interface { + V() +} +type P interface { + P() + V() +} + +type S struct { + T +} + +type SP struct { + *T +} + +func main() { + var t T + var v V + var p P + + t = 42 + + t.P() + t.V() + + v = t + v.V() + + p = &t + p.P() + p.V() + + v = &t + v.V() + + // p = t // ERROR + var i interface{} = t + if _, ok := i.(P); ok { + println("dynamic i.(P) succeeded incorrectly") + panic("fail") + } + + // println("--struct--"); + var s S + s.T = 42 + s.P() + s.V() + + v = s + s.V() + + p = &s + p.P() + p.V() + + v = &s + v.V() + + // p = s // ERROR + var j interface{} = s + if _, ok := j.(P); ok { + println("dynamic j.(P) succeeded incorrectly") + panic("fail") + } + + // println("--struct pointer--"); + var sp SP + sp.T = &t + sp.P() + sp.V() + + v = sp + sp.V() + + p = &sp + p.P() + p.V() + + v = &sp + v.V() + + p = sp // not error + p.P() + p.V() + + if nv != 13 || np != 7 { + println("bad count", nv, np) + panic("fail") + } +} diff --git a/gcc/testsuite/go.test/test/interface/receiver1.go b/gcc/testsuite/go.test/test/interface/receiver1.go new file mode 100644 index 000000000..51312d000 --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/receiver1.go @@ -0,0 +1,58 @@ +// errchk $G $D/$F.go + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Error messages about missing implicit methods. + +package main + +type T int + +func (t T) V() +func (t *T) P() + +type V interface { + V() +} +type P interface { + P() + V() +} + +type S struct { + T +} +type SP struct { + *T +} + +func main() { + var t T + var v V + var p P + var s S + var sp SP + + v = t + p = t // ERROR "does not implement|requires a pointer" + _, _ = v, p + v = &t + p = &t + _, _ = v, p + + v = s + p = s // ERROR "does not implement|requires a pointer" + _, _ = v, p + v = &s + p = &s + _, _ = v, p + + v = sp + p = sp // no error! + _, _ = v, p + v = &sp + p = &sp + _, _ = v, p +} diff --git a/gcc/testsuite/go.test/test/interface/recursive.go b/gcc/testsuite/go.test/test/interface/recursive.go new file mode 100644 index 000000000..1eb56e976 --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/recursive.go @@ -0,0 +1,21 @@ +// $G $D/$F.go || echo BUG: should compile + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Check mutually recursive interfaces + +package main + +type I1 interface { + foo() I2 +} + +type I2 interface { + bar() I1 +} + +type T int +func (t T) foo() I2 { return t } +func (t T) bar() I1 { return t } diff --git a/gcc/testsuite/go.test/test/interface/returntype.go b/gcc/testsuite/go.test/test/interface/returntype.go new file mode 100644 index 000000000..c526b3b0e --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/returntype.go @@ -0,0 +1,25 @@ +// $G $D/$F.go && $L $F.$A && (! ./$A.out || echo BUG: should not succeed) + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Check methods with different return types. + +package main + +type S struct { a int } +type T struct { b string } + +func (s *S) Name() int8 { return 1 } +func (t *T) Name() int64 { return 64 } + +type I1 interface { Name() int8 } +type I2 interface { Name() int64 } + +func main() { + var i1 I1 + var s *S + i1 = s + print(i1.(I2).Name()) +} diff --git a/gcc/testsuite/go.test/test/interface/struct.go b/gcc/testsuite/go.test/test/interface/struct.go new file mode 100644 index 000000000..40b7f4f91 --- /dev/null +++ b/gcc/testsuite/go.test/test/interface/struct.go @@ -0,0 +1,156 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out || echo BUG interface6 + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Interface values containing structures. + +package main + +import "os" + +var fail int + +func check(b bool, msg string) { + if (!b) { + println("failure in", msg) + fail++ + } +} + +type I1 interface { Get() int; Put(int) } + +type S1 struct { i int } +func (p S1) Get() int { return p.i } +func (p S1) Put(i int) { p.i = i } + +func f1() { + s := S1{1} + var i I1 = s + i.Put(2) + check(i.Get() == 1, "f1 i") + check(s.i == 1, "f1 s") +} + +func f2() { + s := S1{1} + var i I1 = &s + i.Put(2) + check(i.Get() == 1, "f2 i") + check(s.i == 1, "f2 s") +} + +func f3() { + s := &S1{1} + var i I1 = s + i.Put(2) + check(i.Get() == 1, "f3 i") + check(s.i == 1, "f3 s") +} + +type S2 struct { i int } +func (p *S2) Get() int { return p.i } +func (p *S2) Put(i int) { p.i = i } + +// Disallowed by restriction of values going to pointer receivers +// func f4() { +// s := S2{1} +// var i I1 = s +// i.Put(2) +// check(i.Get() == 2, "f4 i") +// check(s.i == 1, "f4 s") +// } + +func f5() { + s := S2{1} + var i I1 = &s + i.Put(2) + check(i.Get() == 2, "f5 i") + check(s.i == 2, "f5 s") +} + +func f6() { + s := &S2{1} + var i I1 = s + i.Put(2) + check(i.Get() == 2, "f6 i") + check(s.i == 2, "f6 s") +} + +type I2 interface { Get() int64; Put(int64) } + +type S3 struct { i, j, k, l int64 } +func (p S3) Get() int64 { return p.l } +func (p S3) Put(i int64) { p.l = i } + +func f7() { + s := S3{1, 2, 3, 4} + var i I2 = s + i.Put(5) + check(i.Get() == 4, "f7 i") + check(s.l == 4, "f7 s") +} + +func f8() { + s := S3{1, 2, 3, 4} + var i I2 = &s + i.Put(5) + check(i.Get() == 4, "f8 i") + check(s.l == 4, "f8 s") +} + +func f9() { + s := &S3{1, 2, 3, 4} + var i I2 = s + i.Put(5) + check(i.Get() == 4, "f9 i") + check(s.l == 4, "f9 s") +} + +type S4 struct { i, j, k, l int64 } +func (p *S4) Get() int64 { return p.l } +func (p *S4) Put(i int64) { p.l = i } + +// Disallowed by restriction of values going to pointer receivers +// func f10() { +// s := S4{1, 2, 3, 4} +// var i I2 = s +// i.Put(5) +// check(i.Get() == 5, "f10 i") +// check(s.l == 4, "f10 s") +// } + +func f11() { + s := S4{1, 2, 3, 4} + var i I2 = &s + i.Put(5) + check(i.Get() == 5, "f11 i") + check(s.l == 5, "f11 s") +} + +func f12() { + s := &S4{1, 2, 3, 4} + var i I2 = s + i.Put(5) + check(i.Get() == 5, "f12 i") + check(s.l == 5, "f12 s") +} + +func main() { + f1() + f2() + f3() +// f4() + f5() + f6() + f7() + f8() + f9() +// f10() + f11() + f12() + if fail > 0 { + os.Exit(1) + } +} |