From bdb0a1abfcf30bf05ef6e981179826b02e4539e5 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 24 Jan 2024 13:09:55 -0800 Subject: [PATCH] go/types, types2: in struct processing, set name position in types2 As a consequence, the positions needed by the Checker.structType internal helper functions add and addInvalid are always the positions of the provided identifiers, and we can leave away the extra position arguments. Change-Id: Iddc275c83d3781261476b8e1903050e0a049957c Reviewed-on: https://go-review.googlesource.com/c/go/+/558316 Run-TryBot: Robert Griesemer Reviewed-by: Robert Findley Auto-Submit: Robert Griesemer TryBot-Result: Gopher Robot Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/types2/struct.go | 15 ++++++++------- src/go/types/struct.go | 13 +++++++------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/cmd/compile/internal/types2/struct.go b/src/cmd/compile/internal/types2/struct.go index 9e46b349a3..212e9e17fb 100644 --- a/src/cmd/compile/internal/types2/struct.go +++ b/src/cmd/compile/internal/types2/struct.go @@ -80,7 +80,7 @@ func (check *Checker) structType(styp *Struct, e *syntax.StructType) { // current field typ and tag var typ Type var tag string - add := func(ident *syntax.Name, embedded bool, pos syntax.Pos) { + add := func(ident *syntax.Name, embedded bool) { if tag != "" && tags == nil { tags = make([]string, len(fields)) } @@ -88,6 +88,7 @@ func (check *Checker) structType(styp *Struct, e *syntax.StructType) { tags = append(tags, tag) } + pos := ident.Pos() name := ident.Value fld := NewField(pos, check.pkg, name, typ, embedded) // spec: "Within a struct, non-blank field names must be unique." @@ -101,10 +102,10 @@ func (check *Checker) structType(styp *Struct, e *syntax.StructType) { // fields with errors; this keeps the number of struct fields in sync // with the source as long as the fields are _ or have different names // (go.dev/issue/25627). - addInvalid := func(ident *syntax.Name, pos syntax.Pos) { + addInvalid := func(ident *syntax.Name) { typ = Typ[Invalid] tag = "" - add(ident, true, pos) + add(ident, true) } var prev syntax.Expr @@ -121,7 +122,7 @@ func (check *Checker) structType(styp *Struct, e *syntax.StructType) { } if f.Name != nil { // named field - add(f.Name, false, f.Name.Pos()) + add(f.Name, false) } else { // embedded field // spec: "An embedded type must be specified as a type name T or as a @@ -131,11 +132,11 @@ func (check *Checker) structType(styp *Struct, e *syntax.StructType) { name := embeddedFieldIdent(f.Type) if name == nil { check.errorf(pos, InvalidSyntaxTree, "invalid embedded field type %s", f.Type) - name = &syntax.Name{Value: "_"} // TODO(gri) need to set position to pos - addInvalid(name, pos) + name = syntax.NewName(pos, "_") + addInvalid(name) continue } - add(name, true, name.Pos()) // struct{p.T} field has position of T + add(name, true) // struct{p.T} field has position of T // Because we have a name, typ must be of the form T or *T, where T is the name // of a (named or alias) type, and t (= deref(typ)) must be the type of T. diff --git a/src/go/types/struct.go b/src/go/types/struct.go index 935a549530..0c86654315 100644 --- a/src/go/types/struct.go +++ b/src/go/types/struct.go @@ -82,7 +82,7 @@ func (check *Checker) structType(styp *Struct, e *ast.StructType) { // current field typ and tag var typ Type var tag string - add := func(ident *ast.Ident, embedded bool, pos token.Pos) { + add := func(ident *ast.Ident, embedded bool) { if tag != "" && tags == nil { tags = make([]string, len(fields)) } @@ -90,6 +90,7 @@ func (check *Checker) structType(styp *Struct, e *ast.StructType) { tags = append(tags, tag) } + pos := ident.Pos() name := ident.Name fld := NewField(pos, check.pkg, name, typ, embedded) // spec: "Within a struct, non-blank field names must be unique." @@ -103,10 +104,10 @@ func (check *Checker) structType(styp *Struct, e *ast.StructType) { // fields with errors; this keeps the number of struct fields in sync // with the source as long as the fields are _ or have different names // (go.dev/issue/25627). - addInvalid := func(ident *ast.Ident, pos token.Pos) { + addInvalid := func(ident *ast.Ident) { typ = Typ[Invalid] tag = "" - add(ident, true, pos) + add(ident, true) } for _, f := range list.List { @@ -115,7 +116,7 @@ func (check *Checker) structType(styp *Struct, e *ast.StructType) { if len(f.Names) > 0 { // named fields for _, name := range f.Names { - add(name, false, name.Pos()) + add(name, false) } } else { // embedded field @@ -128,10 +129,10 @@ func (check *Checker) structType(styp *Struct, e *ast.StructType) { check.errorf(f.Type, InvalidSyntaxTree, "embedded field type %s has no name", f.Type) name = ast.NewIdent("_") name.NamePos = pos - addInvalid(name, pos) + addInvalid(name) continue } - add(name, true, name.Pos()) // struct{p.T} field has position of T + add(name, true) // struct{p.T} field has position of T // Because we have a name, typ must be of the form T or *T, where T is the name // of a (named or alias) type, and t (= deref(typ)) must be the type of T.