@@ -29,17 +29,21 @@ func (ps structParsers) parse(p *Parser, expr ast.Expr, owner typeinfo.Type) (*P
2929 }
3030
3131 if len (path .StructField ) == 0 {
32- panic (1 )
32+ panic ("unreachable: struct field must be at least one" )
3333 }
3434 if len (path .StructField ) == 1 {
3535 return nil , codefmt .Errorf (p , expr , "cannot use %t itself as a field" , owner )
3636 }
3737 return path , nil
3838}
3939
40- func (ps structParsers ) walk (p * Parser , expr ast.Expr , owner typeinfo.Type ) (* Path , error ) {
40+ func (ps structParsers ) walk (p * Parser , expr ast.Expr , owner typeinfo.Type ) (* Path , error ) { // nolint:gocyclo
4141 expr = ast .Unparen (expr )
4242
43+ if ! owner .Deref ().IsNamed () {
44+ return nil , codefmt .Errorf (p , expr , "field of anonymous struct is not supported" )
45+ }
46+
4347 // Unwrap convgen.Field~ function calls
4448 // e.g., convgen.FieldGetter(T{}.GetField) -> T{}.GetField
4549 if call , ok := expr .(* ast.CallExpr ); ok {
@@ -51,51 +55,55 @@ func (ps structParsers) walk(p *Parser, expr ast.Expr, owner typeinfo.Type) (*Pa
5155
5256 switch x := expr .(type ) {
5357 case * ast.CompositeLit :
58+ err := codefmt .Errorf (p , expr , "field should belong to %t{}; got %c" , owner , expr )
59+
5460 // Expression: T{}
5561 if len (x .Elts ) != 0 {
5662 // T{...}
57- return nil , codefmt . Errorf ( p , expr , "field should belong to %t{} with empty braces; got %c" , owner , expr )
63+ return nil , err
5864 }
5965
6066 id , ok := ast .Unparen (x .Type ).(* ast.Ident )
6167 if ! ok {
6268 // struct{}{}
63- return nil , codefmt . Errorf ( p , expr , "field cannot belong to anonymous struct" )
69+ return nil , err
6470 }
6571
6672 t := typeinfo .TypeOf (p .Pkg ().TypesInfo .TypeOf (id ))
6773 if ! t .Identical (owner .Deref ()) {
6874 // U{} where U != T
69- return nil , codefmt . Errorf ( p , expr , "field should belong to %t{}; got %c" , owner , expr )
75+ return nil , err
7076 }
7177
7278 obj := p .Pkg ().TypesInfo .ObjectOf (id )
7379 return & Path {StructField : []types.Object {obj }, Pos : id .Pos ()}, nil
7480
7581 case * ast.CallExpr :
82+ err := codefmt .Errorf (p , expr , "field should belong to (*%t)(nil); got %c" , owner , expr )
83+
7684 // Expression: (*T)(nil)
7785 if len (x .Args ) != 1 || ! p .IsNil (x .Args [0 ]) {
7886 // (nil) expected but got (), (nonil), (...)
79- return nil , codefmt . Errorf ( p , expr , "field should belong to (*%t)(nil); got %c" , owner , expr )
87+ return nil , err
8088 }
8189
8290 fun := ast .Unparen (x .Fun )
8391 star , ok := fun .(* ast.StarExpr )
8492 if ! ok {
8593 // f(nil) where f != *T
86- return nil , codefmt . Errorf ( p , expr , "field should belong to (*%t)(nil); got %c" , owner , expr )
94+ return nil , err
8795 }
8896
8997 id , ok := ast .Unparen (star .X ).(* ast.Ident )
9098 if ! ok {
9199 // (*struct{})(nil)
92- return nil , codefmt . Errorf ( p , expr , "field cannot belong to anonymous struct" )
100+ return nil , err
93101 }
94102
95103 t := typeinfo .TypeOf (p .Pkg ().TypesInfo .TypeOf (id ))
96104 if ! t .Identical (owner .Deref ()) {
97105 // (*U)(nil) where U != T
98- return nil , codefmt . Errorf ( p , expr , "field should belong to (*%t)(nil); got %c" , owner , expr )
106+ return nil , err
99107 }
100108
101109 obj := p .Pkg ().TypesInfo .ObjectOf (id )
0 commit comments