3
// Copyright 2013 The Go Authors. All rights reserved.
4
// Use of this source code is governed by a BSD-style
5
// license that can be found in the LICENSE file.
9
// Concrete types implementing M method.
10
// Smaller than a word, word-sized, larger than a word.
11
// Value and pointer receivers.
13
type Tinter interface {
14
M(int, byte) (byte, int)
19
func (v Tsmallv) M(x int, b byte) (byte, int) { return b, x+int(v) }
23
func (p *Tsmallp) M(x int, b byte) (byte, int) { return b, x+int(*p) }
27
func (v Twordv) M(x int, b byte) (byte, int) { return b, x+int(v) }
31
func (p *Twordp) M(x int, b byte) (byte, int) { return b, x+int(*p) }
35
func (v Tbigv) M(x int, b byte) (byte, int) { return b, x+int(v[0])+int(v[1]) }
39
func (p *Tbigp) M(x int, b byte) (byte, int) { return b, x+int(p[0])+int(p[1]) }
41
// Again, with an unexported method.
45
func (v tsmallv) m(x int, b byte) (byte, int) { return b, x+int(v) }
49
func (p *tsmallp) m(x int, b byte) (byte, int) { return b, x+int(*p) }
53
func (v twordv) m(x int, b byte) (byte, int) { return b, x+int(v) }
57
func (p *twordp) m(x int, b byte) (byte, int) { return b, x+int(*p) }
61
func (v tbigv) m(x int, b byte) (byte, int) { return b, x+int(v[0])+int(v[1]) }
65
func (p *tbigp) m(x int, b byte) (byte, int) { return b, x+int(p[0])+int(p[1]) }
67
type tinter interface {
68
m(int, byte) (byte, int)
71
// Embedding via pointer.
88
func (t4 T4) M(x int, b byte) (byte, int) { return b, x+40 }
92
func CheckI(name string, i Tinter, inc int) {
94
if b != 99 || x != 1000+inc {
96
print(name, ".M(1000, 99) = ", b, ", ", x, " want 99, ", 1000+inc, "\n")
99
CheckF("(i="+name+")", i.M, inc)
102
func CheckF(name string, f func(int, byte) (byte, int), inc int) {
104
if b != 99 || x != 1000+inc {
106
print(name, "(1000, 99) = ", b, ", ", x, " want 99, ", 1000+inc, "\n")
110
func checkI(name string, i tinter, inc int) {
111
b, x := i.m(1000, 99)
112
if b != 99 || x != 1000+inc {
114
print(name, ".m(1000, 99) = ", b, ", ", x, " want 99, ", 1000+inc, "\n")
117
checkF("(i="+name+")", i.m, inc)
120
func checkF(name string, f func(int, byte) (byte, int), inc int) {
122
if b != 99 || x != 1000+inc {
124
print(name, "(1000, 99) = ", b, ", ", x, " want 99, ", 1000+inc, "\n")
128
func shouldPanic(f func()) {
130
if recover() == nil {
131
panic("not panicking")
137
func shouldNotPanic(f func()) {
144
CheckF("sv.M", sv.M, 1)
145
CheckF("(&sv).M", (&sv).M, 1)
147
CheckI("psv", psv, 1)
148
CheckF("psv.M", psv.M, 1)
149
CheckF("(*psv).M", (*psv).M, 1)
152
CheckI("&sp", &sp, 2)
153
CheckF("sp.M", sp.M, 2)
154
CheckF("(&sp).M", (&sp).M, 2)
156
CheckI("psp", psp, 2)
157
CheckF("psp.M", psp.M, 2)
158
CheckF("(*psp).M", (*psp).M, 2)
162
CheckF("wv.M", wv.M, 3)
163
CheckF("(&wv).M", (&wv).M, 3)
165
CheckI("pwv", pwv, 3)
166
CheckF("pwv.M", pwv.M, 3)
167
CheckF("(*pwv).M", (*pwv).M, 3)
170
CheckI("&wp", &wp, 4)
171
CheckF("wp.M", wp.M, 4)
172
CheckF("(&wp).M", (&wp).M, 4)
174
CheckI("pwp", pwp, 4)
175
CheckF("pwp.M", pwp.M, 4)
176
CheckF("(*pwp).M", (*pwp).M, 4)
178
bv := Tbigv([2]uintptr{5, 6})
181
CheckF("bv.M", bv.M, 11)
182
CheckF("(&bv).M", (&bv).M, 11)
183
CheckI("pbv", pbv, 11)
184
CheckF("pbv.M", pbv.M, 11)
185
CheckF("(*pbv).M", (*pbv).M, 11)
187
bp := Tbigp([2]uintptr{7,8})
188
CheckI("&bp", &bp, 15)
189
CheckF("bp.M", bp.M, 15)
190
CheckF("(&bp).M", (&bp).M, 15)
192
CheckI("pbp", pbp, 15)
193
CheckF("pbp.M", pbp.M, 15)
194
CheckF("(*pbp).M", (*pbp).M, 15)
197
checkI("_sv", _sv, 1)
198
checkF("_sv.m", _sv.m, 1)
199
checkF("(&_sv).m", (&_sv).m, 1)
201
checkI("_psv", _psv, 1)
202
checkF("_psv.m", _psv.m, 1)
203
checkF("(*_psv).m", (*_psv).m, 1)
206
checkI("&_sp", &_sp, 2)
207
checkF("_sp.m", _sp.m, 2)
208
checkF("(&_sp).m", (&_sp).m, 2)
210
checkI("_psp", _psp, 2)
211
checkF("_psp.m", _psp.m, 2)
212
checkF("(*_psp).m", (*_psp).m, 2)
215
checkI("_wv", _wv, 3)
216
checkF("_wv.m", _wv.m, 3)
217
checkF("(&_wv).m", (&_wv).m, 3)
219
checkI("_pwv", _pwv, 3)
220
checkF("_pwv.m", _pwv.m, 3)
221
checkF("(*_pwv).m", (*_pwv).m, 3)
224
checkI("&_wp", &_wp, 4)
225
checkF("_wp.m", _wp.m, 4)
226
checkF("(&_wp).m", (&_wp).m, 4)
228
checkI("_pwp", _pwp, 4)
229
checkF("_pwp.m", _pwp.m, 4)
230
checkF("(*_pwp).m", (*_pwp).m, 4)
232
_bv := tbigv([2]uintptr{5, 6})
234
checkI("_bv", _bv, 11)
235
checkF("_bv.m", _bv.m, 11)
236
checkF("(&_bv).m", (&_bv).m, 11)
237
checkI("_pbv", _pbv, 11)
238
checkF("_pbv.m", _pbv.m, 11)
239
checkF("(*_pbv).m", (*_pbv).m, 11)
241
_bp := tbigp([2]uintptr{7,8})
242
checkI("&_bp", &_bp, 15)
243
checkF("_bp.m", _bp.m, 15)
244
checkF("(&_bp).m", (&_bp).m, 15)
246
checkI("_pbp", _pbp, 15)
247
checkF("_pbp.m", _pbp.m, 15)
248
checkF("(*_pbp).m", (*_pbp).m, 15)
255
CheckI("&t4", &t4, 40)
257
CheckI("&t3", &t3, 40)
259
CheckI("&t2", &t2, 40)
261
CheckI("&t1", &t1, 40)
263
// x.M panics if x is an interface type and is nil,
264
// or if x.M expands to (*x).M where x is nil,
265
// or if x.M expands to x.y.z.w.M where something
266
// along the evaluation of x.y.z.w is nil.
267
var f func(int, byte) (byte, int)
268
shouldPanic(func() { psv = nil; f = psv.M })
269
shouldPanic(func() { pwv = nil; f = pwv.M })
270
shouldPanic(func() { pbv = nil; f = pbv.M })
271
shouldPanic(func() { var i Tinter; f = i.M })
272
shouldPanic(func() { _psv = nil; f = _psv.m })
273
shouldPanic(func() { _pwv = nil; f = _pwv.m })
274
shouldPanic(func() { _pbv = nil; f = _pbv.m })
275
shouldPanic(func() { var _i tinter; f = _i.m })
276
shouldPanic(func() { var t1 T1; f = t1.M })
277
shouldPanic(func() { var t2 T2; f = t2.M })
278
shouldPanic(func() { var t3 *T3; f = t3.M })
279
shouldPanic(func() { var t3 T3; f = t3.M })
282
panic("something set f")
285
// x.M does not panic if x is a nil pointer and
286
// M is a method with a pointer receiver.
287
shouldNotPanic(func() { psp = nil; f = psp.M })
288
shouldNotPanic(func() { pwp = nil; f = pwp.M })
289
shouldNotPanic(func() { pbp = nil; f = pbp.M })
290
shouldNotPanic(func() { _psp = nil; f = _psp.m })
291
shouldNotPanic(func() { _pwp = nil; f = _pwp.m })
292
shouldNotPanic(func() { _pbp = nil; f = _pbp.m })
293
shouldNotPanic(func() { var t4 T4; f = t4.M })
295
panic("nothing set f")