| 1 |
// Copyright 2020 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package issue38068 |
| 6 |
|
| 7 |
// A type with a couple of inlinable, non-pointer-receiver methods |
| 8 |
// that have params and local variables. |
| 9 |
type A struct { |
| 10 |
s string |
| 11 |
next *A |
| 12 |
prev *A |
| 13 |
} |
| 14 |
|
| 15 |
// Inlinable, value-received method with locals and parms. |
| 16 |
func (a A) double(x string, y int) string { |
| 17 |
if y == 191 { |
| 18 |
a.s = "" |
| 19 |
} |
| 20 |
q := a.s + "a" |
| 21 |
r := a.s + "b" |
| 22 |
return q + r |
| 23 |
} |
| 24 |
|
| 25 |
// Inlinable, value-received method with locals and parms. |
| 26 |
func (a A) triple(x string, y int) string { |
| 27 |
q := a.s |
| 28 |
if y == 998877 { |
| 29 |
a.s = x |
| 30 |
} |
| 31 |
r := a.s + a.s |
| 32 |
return q + r |
| 33 |
} |
| 34 |
|
| 35 |
type methods struct { |
| 36 |
m1 func(a *A, x string, y int) string |
| 37 |
m2 func(a *A, x string, y int) string |
| 38 |
} |
| 39 |
|
| 40 |
// Now a function that makes references to the methods via pointers, |
| 41 |
// which should trigger the wrapper generation. |
| 42 |
func P(a *A, ms *methods) { |
| 43 |
if a != nil { |
| 44 |
defer func() { println("done") }() |
| 45 |
} |
| 46 |
println(ms.m1(a, "a", 2)) |
| 47 |
println(ms.m2(a, "b", 3)) |
| 48 |
} |
| 49 |
|
| 50 |
func G(x *A, n int) { |
| 51 |
if n <= 0 { |
| 52 |
println(n) |
| 53 |
return |
| 54 |
} |
| 55 |
// Address-taken local of type A, which will insure that the |
| 56 |
// compiler's writeType() routine will create a method wrapper. |
| 57 |
var a, b A |
| 58 |
a.next = x |
| 59 |
a.prev = &b |
| 60 |
x = &a |
| 61 |
G(x, n-2) |
| 62 |
} |
| 63 |
|
| 64 |
var M methods |
| 65 |
|
| 66 |
func F() { |
| 67 |
M.m1 = (*A).double |
| 68 |
M.m2 = (*A).triple |
| 69 |
G(nil, 100) |
| 70 |
} |