summaryrefslogtreecommitdiffstats
path: root/test/fixedbugs/issue8606b.go
blob: 448ea566f0bea0ad66b7fc387fa1eb4589380008 (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
// run

// Copyright 2020 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 is an optimization check. We want to make sure that we compare
// string lengths, and other scalar fields, before checking string
// contents.  There's no way to verify this in the language, and
// codegen tests in test/codegen can't really detect ordering
// optimizations like this. Instead, we generate invalid strings with
// bad backing store pointers but nonzero length, so we can check that
// the backing store never gets compared.
//
// We use two different bad strings so that pointer comparisons of
// backing store pointers fail.

package main

import (
	"fmt"
	"reflect"
	"unsafe"
)

func bad1() string {
	s := "foo"
	(*reflect.StringHeader)(unsafe.Pointer(&s)).Data = 1 // write bad value to data ptr
	return s
}
func bad2() string {
	s := "foo"
	(*reflect.StringHeader)(unsafe.Pointer(&s)).Data = 2 // write bad value to data ptr
	return s
}

type SI struct {
	s string
	i int
}

type SS struct {
	s string
	t string
}

func main() {
	for _, test := range []struct {
		a, b interface{}
	}{
		{SI{s: bad1(), i: 1}, SI{s: bad2(), i: 2}},
		{SS{s: bad1(), t: "a"}, SS{s: bad2(), t: "aa"}},
		{SS{s: "a", t: bad1()}, SS{s: "b", t: bad2()}},
		// This one would panic because the length of both strings match, and we check
		// the body of the bad strings before the body of the good strings.
		//{SS{s: bad1(), t: "a"}, SS{s: bad2(), t: "b"}},
	} {
		if test.a == test.b {
			panic(fmt.Sprintf("values %#v and %#v should not be equal", test.a, test.b))
		}
	}

}