Skip to content

x/tools/gopls: shallow export data produces non-canonical forwarded fields/methods #60819

Closed
@findleyr

Description

@findleyr

Discovered by way of #60676, our shallow importer loses the canonical identity of forwarded fields:

package foo

type Foo struct { F int }
---
package bar

type Bar Foo
---
package baz

var _ = Bar{ F: 2 }

In this example, when we import bar.Bar, we build its underlying. However, since we have not imported foo in the same importer, we do not get the same exact type for struct { F int }, because it doesn't exist in the type index. Therefore, the importer creates a new field.

I believe this is only a problem for forwarded fields and forwarded interface methods. It is not a problem for concrete methods or embedded interfaces.

I am going to make a quick-and-dirty fix for references (using position in our objectPath algorithm 😵 ...), but we really want to avoid duplication of these objects.

A longer term solution could be:

  • keep track of the current decl when exporting
  • when exporting a field or interface method whose package differs from the current decl's package, instead of encoding the field itself, encode its package and objectpath
  • in the importer, use objectpath.Object to look up the corresponding canonical field

Unfortunately, this doesn't work for instantiated fields:

type Bar Foo[int]

In this case, there's nothing we can do to create a correct field. We'd want to set its origin, but there is no types.Var.SetOrigin method (or constructor), and no way to know which instance produced a given field :(

CC @adonovan @griesemer @mdempsky

Activity

added this to the gopls/v0.13.0 milestone on Jun 15, 2023
added
ToolsThis label describes issues relating to any tools in the x/tools repository.
goplsIssues related to the Go language server, gopls.
on Jun 15, 2023
gopherbot

gopherbot commented on Jun 19, 2023

@gopherbot
Contributor

Change https://go.dev/cl/503438 mentions this issue: internal/gcimporter: supporting encoding objects from different packages

added a commit that references this issue on Feb 16, 2024
89d9fae
locked and limited conversation to collaborators on Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @gopherbot@findleyr

        Issue actions

          x/tools/gopls: shallow export data produces non-canonical forwarded fields/methods · Issue #60819 · golang/go