Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: _ methods should not be added to an interface's method set #42964

Closed
griesemer opened this issue Dec 3, 2020 · 3 comments
Closed
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@griesemer
Copy link
Contributor

The spec does not permit blank (_) method names for interfaces:

The name of each explicitly specified method must be unique and not blank.

The compiler complains about such methods but still adds them to the method set, as evidenced by this program

type T interface {
	_()
}

type I struct{}

var _ T = I{}

The compiler complains that the I doesn't implement T because of the missing _ method, hence the method must be present in T. Interestingly, the modified program

type T interface {
	_()
}

type I struct{}

func (I) _() {}

var _ T = I{}

where I has a _ method, still results in the same error. This is due to the fact that _ methods cannot be found in I as they are never declared.

Furthermore, given the program

type A interface {
	_()
}

type B interface {
	A
	_()
}

the compiler as before reports errors about the _ methods being invalid but otherwise is quiet. Yet, the following version

type A interface {
	_()
}

type B interface {
	A
	_(int)
}

reports the error duplicate method _ because now the two _ methods have different signatures. While this is consistent with what we'd expect from any other method, it seems confusing.

For contrast, go/types never adds the _ methods which results in fewer errors.

See also $GOROOT/test/interface/explicit.go (at end) where there's an explicit test that nothing implements an interface with a _ method. Since it's not possible to declare such interfaces in the first place, it may be simpler to just ignore _ methods when encountering them in interface declarations, and then this last test would pass w/o error, and the above examples would only produce errors when the _ methods are declared.

I'd like to see compelling evidence for keeping the existing behavior.

cc: @mdempsky

@griesemer griesemer added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Dec 3, 2020
@griesemer griesemer added this to the Unplanned milestone Dec 3, 2020
@mdempsky
Copy link
Member

mdempsky commented Dec 3, 2020

I'd noticed and thought about the same thing recently. I'm happy to change cmd/compile's behavior here to match go/types. I see no reason to complain about not implementing a blank method either.

@gopherbot
Copy link

Change https://golang.org/cl/275294 mentions this issue: [dev.regabi] cmd/compile: silence errors about missing blank methods

gopherbot pushed a commit that referenced this issue Dec 7, 2020
If an interface contains a blank method, that's already an error. No
need for useless follow-up error messages about not implementing them.

Fixes #42964.

Change-Id: I5bf53a8f27d75d4c86c61588c5e2e3e95563d320
Reviewed-on: https://go-review.googlesource.com/c/go/+/275294
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
@griesemer
Copy link
Contributor Author

This was fixed with https://golang.org/cl/275294 in the dev.regabi branch.

@golang golang locked and limited conversation to collaborators Dec 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

3 participants