1// run 2 3// Copyright 2018 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. 6 7package main 8 9//go:noinline 10func f(p, q *struct{}) bool { 11 return *p == *q 12} 13 14type T struct { 15 x struct{} 16 y int 17} 18 19//go:noinline 20func g(p, q *T) bool { 21 return p.x == q.x 22} 23 24//go:noinline 25func h(p, q func() struct{}) bool { 26 return p() == q() 27} 28 29func fi(p, q *struct{}) bool { 30 return *p == *q 31} 32 33func gi(p, q *T) bool { 34 return p.x == q.x 35} 36 37func hi(p, q func() struct{}) bool { 38 return p() == q() 39} 40 41func main() { 42 shouldPanic(func() { f(nil, nil) }) 43 shouldPanic(func() { g(nil, nil) }) 44 shouldPanic(func() { h(nil, nil) }) 45 shouldPanic(func() { fi(nil, nil) }) 46 shouldPanic(func() { gi(nil, nil) }) 47 shouldPanic(func() { hi(nil, nil) }) 48 n := 0 49 inc := func() struct{} { 50 n++ 51 return struct{}{} 52 } 53 h(inc, inc) 54 if n != 2 { 55 panic("inc not called") 56 } 57 hi(inc, inc) 58 if n != 4 { 59 panic("inc not called") 60 } 61} 62 63func shouldPanic(x func()) { 64 defer func() { 65 if recover() == nil { 66 panic("did not panic") 67 } 68 }() 69 x() 70} 71