Skip to content

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

Closed

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

Activity

  1. added this to the Go1.10 milestone on Aug 30, 2017
  2. mdempsky commented on Aug 30, 2017

    @mdempsky
    Contributor

    From what I can tell, the package we select there is just for a space optimization. Picking the wrong package means we waste space in the reflect type information tables (and thus should still be fixed), but I don't think it affects correctness at least.

  3. rsc commented on Aug 30, 2017

    @rsc
    ContributorAuthor

    @mdempsky the test in CL 27791, which introduced this code, is testing semantics observable via reflect and mentions #16616, so I don't think this is only about space usage.

  4. ianlancetaylor commented on Aug 30, 2017

    @ianlancetaylor
    Contributor

    It's not obvious to me that anything is wrong here. The f.Sym.Pkg field here is a compiler internal data structure. It's not the same as the reflect data. The fact that exported fields have no package in the reflect data is handled later, in dnameField.

  5. mdempsky commented on Aug 30, 2017

    @mdempsky
    Contributor

    @rsc fixedbugs/issue16616.go still passes after removing that loop though.

  6. gopherbot commented on Aug 30, 2017

    @gopherbot
    Contributor

    Change https://golang.org/cl/60410 mentions this issue: cmd/compile: fix and improve struct field reflect information

  7. locked and limited conversation to collaborators on Sep 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions