summaryrefslogtreecommitdiffstats
path: root/test/fixedbugs/issue46304.go
blob: b8ecfc93a5976c026c655843b92fb18926df2ad3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// run

// Copyright 2021 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.

// This testcase caused a crash when the register ABI was in effect,
// on amd64 (problem with register allocation).

package main

type Op struct {
	tag   string
	_x    []string
	_q    [20]uint64
	plist []P
}

type P struct {
	tag string
	_x  [10]uint64
	b   bool
}

type M int

//go:noinline
func (w *M) walkP(p *P) *P {
	np := &P{}
	*np = *p
	np.tag += "new"
	return np
}

func (w *M) walkOp(op *Op) *Op {
	if op == nil {
		return nil
	}

	orig := op
	cloned := false
	clone := func() {
		if !cloned {
			cloned = true
			op = &Op{}
			*op = *orig
		}
	}

	pCloned := false
	for i := range op.plist {
		if s := w.walkP(&op.plist[i]); s != &op.plist[i] {
			if !pCloned {
				pCloned = true
				clone()
				op.plist = make([]P, len(orig.plist))
				copy(op.plist, orig.plist)
			}
			op.plist[i] = *s
		}
	}

	return op
}

func main() {
	var ww M
	w := &ww
	p1 := P{tag: "a"}
	p1._x[1] = 9
	o := Op{tag: "old", plist: []P{p1}}
	no := w.walkOp(&o)
	if no.plist[0].tag != "anew" {
		panic("bad")
	}
}