-
Notifications
You must be signed in to change notification settings - Fork 18k
go/types: errors when declaring methods with type alias receivers #23042
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
Comments
The alias proposal at https://github.com/golang/proposal/blob/master/design/18130-type-alias.md specifically permits declaring methods using either type.
|
I think the original intent for alias type names was that this should work. Since cmd/compile accepts it, I would say this is a go/types bug (and perhaps the spec could be clearer). |
@mdempsky Regarding the spec's prose: "The type denoted by T is called the receiver base type; it must not be a pointer or interface type and it must be defined in the same package as the method." |
@griesemer I suppose these sentences are where my confusion really come from:
The "possibly using parentheses" phrasing suggests to me the "form" we're talking about is syntactic, not structural. And so "T" appears to refer to the literal type name appearing in the method declaration's receiver parameter type. |
Change https://golang.org/cl/83996 mentions this issue: |
cmd/compile accepts this package:
but go/types rejects it with:
Notably, if you move the type alias and method declaration above the variable declarations, go/types accepts it.
It's worth noting that gccgo actually crashes on the method declaration with
It makes me wonder a little if we should just require that methods be directly associated with the actual receiver type. Rationale:
The spec says "The type denoted by T is called the receiver base type; it must not be a pointer or interface type and it must be defined in the same package as the method." It links to "defined type", which is disjoint from type aliases, so arguably the spec doesn't allow methods declared on type aliases anyway.
Since we can only declare methods on types declared in the same package anyway, there's not much to be gained by allowed method declarations on type aliases.
/cc @griesemer @ianlancetaylor
The text was updated successfully, but these errors were encountered: