-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
go/types, types2: disable field accesses through type parameters for now #51576
Comments
Change https://go.dev/cl/391135 mentions this issue: |
Change https://go.dev/cl/391137 mentions this issue: |
Change https://go.dev/cl/391274 mentions this issue: |
Change https://go.dev/cl/391279 mentions this issue: |
Document field access limitations for type parameters. Also, remove some superfluous "currently" uses - the time context is clear from the introductory paragraph of the respective section. For golang/go#51576. For golang/go#47694. Change-Id: If4c12f95f024894a9efb949dda9341d8ab0dc77e Reviewed-on: https://go-review.googlesource.com/c/website/+/391279 Reviewed-by: DO NOT USE <iant@google.com> Trust: Dmitri Shuralyov <dmitshur@google.com>
For #51576. Change-Id: I43f72c3fb618e724d46360a70ab9f8abc3d63273 Reviewed-on: https://go-review.googlesource.com/c/go/+/391137 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Change https://go.dev/cl/391355 mentions this issue: |
By 'disable this feature', do you mean that it won't be possible to access the fields of a type parameter at all? So this example would no longer be valid: https://go.dev/play/p/bJ1QGu1MU38?v=gotip |
@bitfield That is correct, your example doesn't work anymore (at least for Go 1.18). You can always use a method on the struct and the constraint. (As an aside, I have yet to see a convincing example for why this mechanism is crucial; especially because methods permit a work-around.) There are simply too many unanswered questions around this mechanism for which we don't have solid answers yet. We don't want to be locked in through the backward compatibility guarantee if we make a mistake here. Note that the rules around field access are among the most subtle and complicated in the language spec and the implementation, even though for a user it may look "simple". Thanks. |
That's fine, thanks—I just wanted to clarify exactly what this change implies. I don't have a convincing use case for this either, but I'm looking at it from a writer's/teacher's point of view. It seems to be quite natural for people to try this quite early on when experimenting with constraints, and I need to be able to explain to them why this apparently straightforward thing isn't allowed. It looks easy from the programmer's side, if you see what I mean. "I know my parameter is a struct with these exact fields, why can't I access them?" No writer likes to have to say "Here's something you can't do!" But I think it's going to be important to get out ahead of those inevitable questions. |
…ugh type parameters This is a feature that is not understood well enough and may have subtle repercussions impacting future changes. Disable for Go 1.18. The actual change is trivial: disable a branch through a flag. The remaining changes are adjustments to tests. Fixes #51576. Change-Id: Ib77b038b846711a808315a8889b3904e72367bce Reviewed-on: https://go-review.googlesource.com/c/go/+/391135 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> (cherry picked from commit b8248fa) Reviewed-on: https://go-review.googlesource.com/c/go/+/391355 Trust: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Document field access limitations for type parameters. Also, remove some superfluous "currently" uses - the time context is clear from the introductory paragraph of the respective section. For golang/go#51576. For golang/go#47694. Change-Id: If4c12f95f024894a9efb949dda9341d8ab0dc77e Reviewed-on: https://go-review.googlesource.com/c/website/+/391279 Reviewed-by: DO NOT USE <iant@google.com> Trust: Dmitri Shuralyov <dmitshur@google.com>
Field accesses through type parameters have been implemented and work, but are currently restricted to type parameters that have a core type.
This code
is currently accepted (as expected) because we don't determine that the type set for
P
is empty (the intersection of the single typestruct{ f int }
and the set of types that have methodf() int
is empty). We could change the embedded type element to~struct{ f int }
but in that case we cannot instantiateP
because it's not possible to have a struct type with a field and a method with the same name.This code
is also accepted (as expected). Here it is possible to instantiate
P
because the fieldf
and the methodf
are at different depths.Note that in pre-1.18 Go, field and method names for a struct must be different (at the same level of embedding). It is not clear what the rules should be here.
These are just some of the questions that are brought up by this mechanism.
The current implementation could be described in the spec as follows:
It's not obviously clear if the generalization beyond core types (
x.f
is valid if it's valid for all types in a type set) will not lead to subtle surprises in behavior. We don't know if we should have restrictions with respect to method and field names that are the same. And so on.In summary, while we do have an implementation, we don't fully understand the repercussions of this feature and how it would affect its generalization. At the same time we can always use methods as field accessors so there is a reasonable work-around.
The prudent decision for Go 1.18 is to disable this feature until we understand it well. We don't want to be in a situation where we have to support a potentially flawed mechanism for backward-compatibility.
The text was updated successfully, but these errors were encountered: