summaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/gc/testdata/slice.go
blob: a02e4a442a6e1155583be4758fc29e098f3dc019 (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
// run

// This test makes sure that t.s = t.s[0:x] doesn't write
// either the slice pointer or the capacity.
// See issue #14855.

package main

import "fmt"

const N = 1000000

type T struct {
	s []int
}

func main() {
	done := make(chan struct{})
	a := make([]int, N+10)

	t := &T{a}

	go func() {
		for i := 0; i < N; i++ {
			t.s = t.s[1:9]
		}
		done <- struct{}{}
	}()
	go func() {
		for i := 0; i < N; i++ {
			t.s = t.s[0:8] // should only write len
		}
		done <- struct{}{}
	}()
	<-done
	<-done

	ok := true
	if cap(t.s) != cap(a)-N {
		fmt.Printf("wanted cap=%d, got %d\n", cap(a)-N, cap(t.s))
		ok = false
	}
	if &t.s[0] != &a[N] {
		fmt.Printf("wanted ptr=%p, got %p\n", &a[N], &t.s[0])
		ok = false
	}
	if !ok {
		panic("bad")
	}
}