Skip to content

go/types: position of field Var S.T in type S struct{ p.T } coincides with p, not T #60372

@adonovan

Description

@adonovan

When go/types encounters an embedded (unnamed) field, it uses the Pos of the ast.Field syntax node as the Pos of the field types.Var object. If the field type is a qualified identifier p.T, this means that the source code at the position of the declaration of struct field T does not contain an identifier "T". Instead, the source at that position is the package identifier "p".

I realize these invariants are not as well formalized as we would like, but I argue this is a bug. It should use the Pos of the type name identifier in all cases.

The relevant source is in go/types/struct.go, Checker.structType:

func (check *Checker) structType(styp *Struct, e *ast.StructType) {
...
	for _, f := range list.List {
		typ = check.varType(f.Type)
		tag = check.tag(f.Tag)
		if len(f.Names) > 0 {
			// named fields
			...
		} else {
			// embedded field
			// spec: "An embedded type must be specified as a type name T or as a
			// pointer to a non-interface type name *T, and T itself may not be a
			// pointer type."
			pos := f.Type.Pos() // <--------------HERE

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions