github.com/JimmyHuang454/JLS-go@v0.0.0-20230831150107-90d536585ba0/internal/types/testdata/check/methodsets.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package methodsets 6 7 type T0 struct {} 8 9 func (T0) v0() {} 10 func (*T0) p0() {} 11 12 type T1 struct {} // like T0 with different method names 13 14 func (T1) v1() {} 15 func (*T1) p1() {} 16 17 type T2 interface { 18 v2() 19 p2() 20 } 21 22 type T3 struct { 23 T0 24 *T1 25 T2 26 } 27 28 // Method expressions 29 func _() { 30 var ( 31 _ func(T0) = T0.v0 32 _ = T0.p0 /* ERROR invalid method expression T0\.p0 \(needs pointer receiver \(\*T0\)\.p0\) */ 33 34 _ func (*T0) = (*T0).v0 35 _ func (*T0) = (*T0).p0 36 37 // T1 is like T0 38 39 _ func(T2) = T2.v2 40 _ func(T2) = T2.p2 41 42 _ func(T3) = T3.v0 43 _ func(T3) = T3.p0 /* ERROR invalid method expression T3\.p0 \(needs pointer receiver \(\*T3\)\.p0\) */ 44 _ func(T3) = T3.v1 45 _ func(T3) = T3.p1 46 _ func(T3) = T3.v2 47 _ func(T3) = T3.p2 48 49 _ func(*T3) = (*T3).v0 50 _ func(*T3) = (*T3).p0 51 _ func(*T3) = (*T3).v1 52 _ func(*T3) = (*T3).p1 53 _ func(*T3) = (*T3).v2 54 _ func(*T3) = (*T3).p2 55 ) 56 } 57 58 // Method values with addressable receivers 59 func _() { 60 var ( 61 v0 T0 62 _ func() = v0.v0 63 _ func() = v0.p0 64 ) 65 66 var ( 67 p0 *T0 68 _ func() = p0.v0 69 _ func() = p0.p0 70 ) 71 72 // T1 is like T0 73 74 var ( 75 v2 T2 76 _ func() = v2.v2 77 _ func() = v2.p2 78 ) 79 80 var ( 81 v4 T3 82 _ func() = v4.v0 83 _ func() = v4.p0 84 _ func() = v4.v1 85 _ func() = v4.p1 86 _ func() = v4.v2 87 _ func() = v4.p2 88 ) 89 90 var ( 91 p4 *T3 92 _ func() = p4.v0 93 _ func() = p4.p0 94 _ func() = p4.v1 95 _ func() = p4.p1 96 _ func() = p4.v2 97 _ func() = p4.p2 98 ) 99 } 100 101 // Method calls with addressable receivers 102 func _() { 103 var v0 T0 104 v0.v0() 105 v0.p0() 106 107 var p0 *T0 108 p0.v0() 109 p0.p0() 110 111 // T1 is like T0 112 113 var v2 T2 114 v2.v2() 115 v2.p2() 116 117 var v4 T3 118 v4.v0() 119 v4.p0() 120 v4.v1() 121 v4.p1() 122 v4.v2() 123 v4.p2() 124 125 var p4 *T3 126 p4.v0() 127 p4.p0() 128 p4.v1() 129 p4.p1() 130 p4.v2() 131 p4.p2() 132 } 133 134 // Method values with value receivers 135 func _() { 136 var ( 137 _ func() = T0{}.v0 138 _ func() = T0{}.p0 /* ERROR "cannot call pointer method p0 on T0" */ 139 140 _ func() = (&T0{}).v0 141 _ func() = (&T0{}).p0 142 143 // T1 is like T0 144 145 // no values for T2 146 147 _ func() = T3{}.v0 148 _ func() = T3{}.p0 /* ERROR "cannot call pointer method p0 on T3" */ 149 _ func() = T3{}.v1 150 _ func() = T3{}.p1 151 _ func() = T3{}.v2 152 _ func() = T3{}.p2 153 154 _ func() = (&T3{}).v0 155 _ func() = (&T3{}).p0 156 _ func() = (&T3{}).v1 157 _ func() = (&T3{}).p1 158 _ func() = (&T3{}).v2 159 _ func() = (&T3{}).p2 160 ) 161 } 162 163 // Method calls with value receivers 164 func _() { 165 T0{}.v0() 166 T0{}.p0 /* ERROR "cannot call pointer method p0 on T0" */ () 167 168 (&T0{}).v0() 169 (&T0{}).p0() 170 171 // T1 is like T0 172 173 // no values for T2 174 175 T3{}.v0() 176 T3{}.p0 /* ERROR "cannot call pointer method p0 on T3" */ () 177 T3{}.v1() 178 T3{}.p1() 179 T3{}.v2() 180 T3{}.p2() 181 182 (&T3{}).v0() 183 (&T3{}).p0() 184 (&T3{}).v1() 185 (&T3{}).p1() 186 (&T3{}).v2() 187 (&T3{}).p2() 188 } 189 190 // *T has no methods if T is an interface type 191 func issue5918() { 192 var ( 193 err error 194 _ = err.Error() 195 _ func() string = err.Error 196 _ func(error) string = error.Error 197 198 perr = &err 199 _ = perr.Error /* ERROR "type \*error is pointer to interface, not interface" */ () 200 _ func() string = perr.Error /* ERROR "type \*error is pointer to interface, not interface" */ 201 _ func(*error) string = (*error).Error /* ERROR "type \*error is pointer to interface, not interface" */ 202 ) 203 204 type T *interface{ m() int } 205 var ( 206 x T 207 _ = (*x).m() 208 _ = (*x).m 209 210 _ = x.m /* ERROR "type T is pointer to interface, not interface" */ () 211 _ = x.m /* ERROR "type T is pointer to interface, not interface" */ 212 _ = T.m /* ERROR "type T is pointer to interface, not interface" */ 213 ) 214 }