Skip to content

x/tools/gopls: type constraint discrepancy between go compiler and gopls #61609

Description

@marekscholle

Working on Go project In VSCode, I noticed gopls reports a problem which is ok for the compiler.

I extracted a minimal example as follows:

Suppose we have a project with packages foo (file foo.go) and bar (file bar.go).

The package foo exports type constraint A:

// foo.go
package foo

type A comparable

The package bar consumes the constraint A as

// bar.go
package bar

// import "foo"

func F[T foo.A](a T, b T) bool {
	return a == b
}

Since the constraint A "inherits" from comparable, the expression a == b is well defined: T is constrained by A which is comparable, hence == is available for values of type T. The compiler is happy with the code.

But VSCode reports a == b as the problem:

invalid operation: a == b (incomparable types in type set)

In editor,
image

Two observations:

  • if the constraint type A comparable is defined in the package bar, it's ok for gopls – it's an cross-package problem
  • if the constraint is defined as type A = comparable ie. as typedef (not "newtype") in package foo, it's also ok

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions