Skip to content

cmd/compile: wrong logic for pkg derivation of unnamed type #21696

@rsc

Description

@rsc

Happened to be working in cmd/compile/internal/gc/reflect.go and saw:

		// Unnamed type. Grab the package from the first field, if any.
		for _, f := range t.Fields().Slice() {
			if f.Embedded != 0 {
				continue
			}
			pkg = f.Sym.Pkg
			break
		}

I don't believe this is completely correct. There is no guarantee that the first non-embedded field will have a package. The commit message refers to

struct { i int }

which the code handles correctly, but I think the code still mishandles

struct { X int; i int }

because it will look at X, which has no Pkg, instead of at i, which does. It should probably continue the loop until it finds something with a pkg.

/cc @crawshaw @griesemer @mdempsky

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions