Skip to content

Commit a965318

Browse files
adonovangopherbot
authored andcommitted
go/types: set correct Pos for T in struct{p.T}
Previously, the field Var for T created for struct{p.T} would use the Pos of the ast.Field, which coincides with p. This change makes it use the Pos of T. Errors about the field type are still reported at the position of the ast.Field (e.g. *p.T) not the field T. Fixes #60372 Change-Id: I06000874f2018d47159493626da3d16e6716f4c8 Reviewed-on: https://go-review.googlesource.com/c/go/+/497882 Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Alan Donovan <[email protected]>
1 parent 326df69 commit a965318

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

src/cmd/compile/internal/types2/struct.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ func (check *Checker) structType(styp *Struct, e *syntax.StructType) {
127127
// spec: "An embedded type must be specified as a type name T or as a
128128
// pointer to a non-interface type name *T, and T itself may not be a
129129
// pointer type."
130-
pos := syntax.StartPos(f.Type)
130+
pos := syntax.StartPos(f.Type) // position of type, for errors
131131
name := embeddedFieldIdent(f.Type)
132132
if name == nil {
133133
check.errorf(pos, InvalidSyntaxTree, "invalid embedded field type %s", f.Type)
134134
name = &syntax.Name{Value: "_"} // TODO(gri) need to set position to pos
135135
addInvalid(name, pos)
136136
continue
137137
}
138-
add(name, true, pos)
138+
add(name, true, name.Pos()) // struct{p.T} field has position of T
139139

140140
// Because we have a name, typ must be of the form T or *T, where T is the name
141141
// of a (named or alias) type, and t (= deref(typ)) must be the type of T.

src/go/types/struct.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (check *Checker) structType(styp *Struct, e *ast.StructType) {
122122
// spec: "An embedded type must be specified as a type name T or as a
123123
// pointer to a non-interface type name *T, and T itself may not be a
124124
// pointer type."
125-
pos := f.Type.Pos()
125+
pos := f.Type.Pos() // position of type, for errors
126126
name := embeddedFieldIdent(f.Type)
127127
if name == nil {
128128
check.errorf(f.Type, InvalidSyntaxTree, "embedded field type %s has no name", f.Type)
@@ -131,7 +131,7 @@ func (check *Checker) structType(styp *Struct, e *ast.StructType) {
131131
addInvalid(name, pos)
132132
continue
133133
}
134-
add(name, true, pos)
134+
add(name, true, name.Pos()) // struct{p.T} field has position of T
135135

136136
// Because we have a name, typ must be of the form T or *T, where T is the name
137137
// of a (named or alias) type, and t (= deref(typ)) must be the type of T.

src/internal/types/testdata/check/decls3.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ func _() {
9999
// unsafe.Pointers are treated like regular pointers when embedded
100100
type T2 struct {
101101
unsafe /* ERROR "cannot be unsafe.Pointer" */ .Pointer
102-
*/* ERROR "cannot be unsafe.Pointer" */ /* ERROR "Pointer redeclared" */ unsafe.Pointer
102+
*/* ERROR "cannot be unsafe.Pointer" */ unsafe.Pointer /* ERROR "Pointer redeclared" */
103103
UP /* ERROR "cannot be unsafe.Pointer" */
104-
* /* ERROR "cannot be unsafe.Pointer" */ /* ERROR "UP redeclared" */ UP
104+
* /* ERROR "cannot be unsafe.Pointer" */ UP /* ERROR "UP redeclared" */
105105
}
106106
}
107107

src/internal/types/testdata/examples/types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ type _ struct {
152152
List[int]
153153

154154
int8 /* ERROR "int8 redeclared" */
155-
* /* ERROR "int16 redeclared" */ int16
155+
*int16 /* ERROR "int16 redeclared" */
156156
List /* ERROR "List redeclared" */ [int]
157157
}
158158

@@ -166,17 +166,17 @@ type _ struct {
166166
// func _[T interface{ m(); ~int }]() {
167167
// type L T
168168
// var x L
169-
//
169+
//
170170
// // m is not defined on L (it is not "inherited" from
171171
// // its underlying type).
172172
// x.m /* ERROR "x.m undefined" */ ()
173-
//
173+
//
174174
// // But the properties of T, such that as that it supports
175175
// // the operations of the types given by its type bound,
176176
// // are also the properties of L.
177177
// x++
178178
// _ = x - x
179-
//
179+
//
180180
// // On the other hand, if we define a local alias for T,
181181
// // that alias stands for T as expected.
182182
// type A = T

0 commit comments

Comments
 (0)