Source file src/internal/types/testdata/check/lookup2.go
1 // Copyright 2024 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 p 6 7 import ( 8 "go/ast" 9 "math/big" 10 ) 11 12 // case sel pkg have message (examples for general lookup) 13 // --------------------------------------------------------------------------------------------------------- 14 // ok x.Foo == Foo 15 // misspelled x.Foo == FoO type X has no field or method Foo, but does have field FoO 16 // misspelled x.Foo == foo type X has no field or method Foo, but does have field foo 17 // misspelled x.Foo == foO type X has no field or method Foo, but does have field foO 18 // 19 // misspelled x.foo == Foo type X has no field or method foo, but does have field Foo 20 // misspelled x.foo == FoO type X has no field or method foo, but does have field FoO 21 // ok x.foo == foo 22 // misspelled x.foo == foO type X has no field or method foo, but does have field foO 23 // 24 // ok x.Foo != Foo 25 // misspelled x.Foo != FoO type X has no field or method Foo, but does have field FoO 26 // unexported x.Foo != foo type X has no field or method Foo, but does have unexported field foo 27 // missing x.Foo != foO type X has no field or method Foo 28 // 29 // misspelled x.foo != Foo type X has no field or method foo, but does have field Foo 30 // missing x.foo != FoO type X has no field or method foo 31 // inaccessible x.foo != foo cannot refer to unexported field foo 32 // missing x.foo != foO type X has no field or method foo 33 34 type S struct { 35 Foo1 int 36 FoO2 int 37 foo3 int 38 foO4 int 39 } 40 41 func _() { 42 var x S 43 _ = x.Foo1 // OK 44 _ = x.Foo2 // ERROR "x.Foo2 undefined (type S has no field or method Foo2, but does have field FoO2)" 45 _ = x.Foo3 // ERROR "x.Foo3 undefined (type S has no field or method Foo3, but does have field foo3)" 46 _ = x.Foo4 // ERROR "x.Foo4 undefined (type S has no field or method Foo4, but does have field foO4)" 47 48 _ = x.foo1 // ERROR "x.foo1 undefined (type S has no field or method foo1, but does have field Foo1)" 49 _ = x.foo2 // ERROR "x.foo2 undefined (type S has no field or method foo2, but does have field FoO2)" 50 _ = x.foo3 // OK 51 _ = x.foo4 // ERROR "x.foo4 undefined (type S has no field or method foo4, but does have field foO4)" 52 } 53 54 func _() { 55 _ = S{Foo1: 0} // OK 56 _ = S{Foo2 /* ERROR "unknown field Foo2 in struct literal of type S, but does have FoO2" */ : 0} 57 _ = S{Foo3 /* ERROR "unknown field Foo3 in struct literal of type S, but does have foo3" */ : 0} 58 _ = S{Foo4 /* ERROR "unknown field Foo4 in struct literal of type S, but does have foO4" */ : 0} 59 60 _ = S{foo1 /* ERROR "unknown field foo1 in struct literal of type S, but does have Foo1" */ : 0} 61 _ = S{foo2 /* ERROR "unknown field foo2 in struct literal of type S, but does have FoO2" */ : 0} 62 _ = S{foo3: 0} // OK 63 _ = S{foo4 /* ERROR "unknown field foo4 in struct literal of type S, but does have foO4" */ : 0} 64 } 65 66 // The following tests follow the same pattern as above but operate on an imported type instead of S. 67 // Currently our testing framework doesn't make it easy to define an imported package for testing, so 68 // instead we use the big.Float and ast.File types as they provide a suitable mix of exported and un- 69 // exported fields and methods. 70 71 func _() { 72 var x *big.Float 73 _ = x.Neg // OK 74 _ = x.NeG // ERROR "x.NeG undefined (type *big.Float has no field or method NeG, but does have method Neg)" 75 _ = x.Form // ERROR "x.Form undefined (type *big.Float has no field or method Form, but does have unexported field form)" 76 _ = x.ForM // ERROR "x.ForM undefined (type *big.Float has no field or method ForM)" 77 78 _ = x.abs // ERROR "x.abs undefined (type *big.Float has no field or method abs, but does have method Abs)" 79 _ = x.abS // ERROR "x.abS undefined (type *big.Float has no field or method abS)" 80 _ = x.form // ERROR "x.form undefined (cannot refer to unexported field form)" 81 _ = x.forM // ERROR "x.forM undefined (type *big.Float has no field or method forM)" 82 } 83 84 func _() { 85 _ = ast.File{Name: nil} // OK 86 _ = ast.File{NamE /* ERROR "unknown field NamE in struct literal of type ast.File, but does have Name" */ : nil} 87 _ = big.Float{Form /* ERROR "unknown field Form in struct literal of type big.Float, but does have unexported form" */ : 0} 88 _ = big.Float{ForM /* ERROR "unknown field ForM in struct literal of type big.Float" */ : 0} 89 90 _ = ast.File{name /* ERROR "unknown field name in struct literal of type ast.File, but does have Name" */ : nil} 91 _ = ast.File{namE /* ERROR "unknown field namE in struct literal of type ast.File" */ : nil} 92 _ = big.Float{form /* ERROR "cannot refer to unexported field form in struct literal of type big.Float" */ : 0} 93 _ = big.Float{forM /* ERROR "unknown field forM in struct literal of type big.Float" */ : 0} 94 } 95