1
0
mirror of https://github.com/golang/go synced 2024-11-17 12:54:47 -07:00

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 <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2024-01-24 13:09:55 -08:00 committed by Gopher Robot
parent 5c7c24ce82
commit bdb0a1abfc
2 changed files with 15 additions and 13 deletions

View File

@ -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.

View File

@ -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.