|
| 1 | +// errorcheck -0 -m |
| 2 | + |
| 3 | +// Copyright 2025 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 | + |
| 7 | +package escape |
| 8 | + |
| 9 | +import ( |
| 10 | + "crypto/sha256" |
| 11 | + "encoding" |
| 12 | + "hash" |
| 13 | + "io" |
| 14 | +) |
| 15 | + |
| 16 | +type M interface{ M() } |
| 17 | + |
| 18 | +type A interface{ A() } |
| 19 | + |
| 20 | +type C interface{ C() } |
| 21 | + |
| 22 | +type Impl struct{} |
| 23 | + |
| 24 | +func (*Impl) M() {} // ERROR "can inline" |
| 25 | + |
| 26 | +func (*Impl) A() {} // ERROR "can inline" |
| 27 | + |
| 28 | +type CImpl struct{} |
| 29 | + |
| 30 | +func (CImpl) C() {} // ERROR "can inline" |
| 31 | + |
| 32 | +func t() { |
| 33 | + var a M = &Impl{} // ERROR "&Impl{} does not escape" |
| 34 | + |
| 35 | + a.(M).M() // ERROR "devirtualizing a.\(M\).M" "inlining call" |
| 36 | + a.(A).A() // ERROR "devirtualizing a.\(A\).A" "inlining call" |
| 37 | + a.(*Impl).M() // ERROR "inlining call" |
| 38 | + a.(*Impl).A() // ERROR "inlining call" |
| 39 | + |
| 40 | + v := a.(M) |
| 41 | + v.M() // ERROR "devirtualizing v.M" "inlining call" |
| 42 | + v.(A).A() // ERROR "devirtualizing v.\(A\).A" "inlining call" |
| 43 | + v.(*Impl).A() // ERROR "inlining call" |
| 44 | + v.(*Impl).M() // ERROR "inlining call" |
| 45 | + |
| 46 | + v2 := a.(A) |
| 47 | + v2.A() // ERROR "devirtualizing v2.A" "inlining call" |
| 48 | + v2.(M).M() // ERROR "devirtualizing v2.\(M\).M" "inlining call" |
| 49 | + v2.(*Impl).A() // ERROR "inlining call" |
| 50 | + v2.(*Impl).M() // ERROR "inlining call" |
| 51 | + |
| 52 | + a.(M).(A).A() // ERROR "devirtualizing a.\(M\).\(A\).A" "inlining call" |
| 53 | + a.(A).(M).M() // ERROR "devirtualizing a.\(A\).\(M\).M" "inlining call" |
| 54 | + |
| 55 | + a.(M).(A).(*Impl).A() // ERROR "inlining call" |
| 56 | + a.(A).(M).(*Impl).M() // ERROR "inlining call" |
| 57 | + |
| 58 | + { |
| 59 | + var a C = &CImpl{} // ERROR "does not escape" |
| 60 | + a.(any).(C).C() // ERROR "devirtualizing" "inlining" |
| 61 | + a.(any).(*CImpl).C() // ERROR "inlining" |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// TODO: these type assertions could also be devirtualized. |
| 66 | +func t2() { |
| 67 | + { |
| 68 | + var a M = &Impl{} // ERROR "&Impl{} escapes to heap" |
| 69 | + if v, ok := a.(M); ok { |
| 70 | + v.M() |
| 71 | + } |
| 72 | + } |
| 73 | + { |
| 74 | + var a M = &Impl{} // ERROR "&Impl{} escapes to heap" |
| 75 | + if v, ok := a.(A); ok { |
| 76 | + v.A() |
| 77 | + } |
| 78 | + } |
| 79 | + { |
| 80 | + var a M = &Impl{} // ERROR "&Impl{} escapes to heap" |
| 81 | + v, ok := a.(M) |
| 82 | + if ok { |
| 83 | + v.M() |
| 84 | + } |
| 85 | + } |
| 86 | + { |
| 87 | + var a M = &Impl{} // ERROR "&Impl{} escapes to heap" |
| 88 | + v, ok := a.(A) |
| 89 | + if ok { |
| 90 | + v.A() |
| 91 | + } |
| 92 | + } |
| 93 | + { |
| 94 | + var a M = &Impl{} // ERROR "does not escape" |
| 95 | + v, ok := a.(*Impl) |
| 96 | + if ok { |
| 97 | + v.A() // ERROR "inlining" |
| 98 | + v.M() // ERROR "inlining" |
| 99 | + } |
| 100 | + } |
| 101 | + { |
| 102 | + var a M = &Impl{} // ERROR "&Impl{} escapes to heap" |
| 103 | + v, _ := a.(M) |
| 104 | + v.M() |
| 105 | + } |
| 106 | + { |
| 107 | + var a M = &Impl{} // ERROR "&Impl{} escapes to heap" |
| 108 | + v, _ := a.(A) |
| 109 | + v.A() |
| 110 | + } |
| 111 | + { |
| 112 | + var a M = &Impl{} // ERROR "does not escape" |
| 113 | + v, _ := a.(*Impl) |
| 114 | + v.A() // ERROR "inlining" |
| 115 | + v.M() // ERROR "inlining" |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +//go:noinline |
| 120 | +func testInvalidAsserts() { |
| 121 | + { |
| 122 | + var a M = &Impl{} // ERROR "escapes" |
| 123 | + a.(C).C() // this will panic |
| 124 | + a.(any).(C).C() // this will panic |
| 125 | + } |
| 126 | + { |
| 127 | + var a C = &CImpl{} // ERROR "escapes" |
| 128 | + a.(M).M() // this will panic |
| 129 | + a.(any).(M).M() // this will panic |
| 130 | + } |
| 131 | + { |
| 132 | + var a C = &CImpl{} // ERROR "does not escape" |
| 133 | + |
| 134 | + // this will panic |
| 135 | + a.(M).(*Impl).M() // ERROR "inlining" |
| 136 | + |
| 137 | + // this will panic |
| 138 | + a.(any).(M).(*Impl).M() // ERROR "inlining" |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func testSha256() { |
| 143 | + h := sha256.New() // ERROR "inlining call" "does not escape" |
| 144 | + h.Write(nil) // ERROR "devirtualizing" |
| 145 | + h.(io.Writer).Write(nil) // ERROR "devirtualizing" |
| 146 | + h.(hash.Hash).Write(nil) // ERROR "devirtualizing" |
| 147 | + h.(encoding.BinaryUnmarshaler).UnmarshalBinary(nil) // ERROR "devirtualizing" |
| 148 | + |
| 149 | + h2 := sha256.New() // ERROR "escapes" "inlining call" |
| 150 | + h2.(M).M() // this will panic |
| 151 | +} |
0 commit comments