]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: in struct processing, set name position in types2
authorRobert Griesemer <gri@golang.org>
Wed, 24 Jan 2024 21:09:55 +0000 (13:09 -0800)
committerGopher Robot <gobot@golang.org>
Thu, 25 Jan 2024 23:27:39 +0000 (23:27 +0000)
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>
src/cmd/compile/internal/types2/struct.go
src/go/types/struct.go

index 9e46b349a3920d186beca11d5bbd8f2764ad63f8..212e9e17fbc523fed8e03d8d39af6595977deacb 100644 (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.
index 935a5495307c95ef0707aca160b43c01a198e19b..0c866543154fbeeddf71af05fa2117a2b950d5bc 100644 (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.