You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: invalidly inferred type for interface that doesn't use type parameter #60377
typeT[Pany] interface {
m()
}
funcg[Pany](T[P]) {}
func_() {
varxT[int]
g(x) // here we infer P == int, but in fact any type of P would be okg[string](x) // here we set P == string
}
we infer the type int for P of g. But in fact any type would make this code work. Type inference should not succeed in this case.
typeT1[Pany] interface {
m()
}
typeT2[Pany] interface {
m()
}
funcg[Pany](T1[P]) {}
func_() {
varxT2[int]
g(x) // here we infer P == int, but in fact any type of P would be okg[string](x) // here we set P == string
}
Now we get an error for g(x) but g[string](x) is ok. That seems inconsistent. We should not be able to infer a type for P in the first case.
I expect it to behave like this, since the interface(s) aren't used as constraints and are just ordinary types:
typeT[Pany] struct{}
funcg[Pany](T[P]) {}
func_() {
varxT[int]
g(x) // here we infer P == intg[string](x) // here we fail to set P == string as P = int
}
@jimmyfrasche But the rules for interface assignment are quite different from non-interface assignment. But here is a counter-example:
typeT[Pany] interface{ m() }
funcg[Pany](func(T[P])) {}
func_() {
varffunc(T[int])
g(f)
g[int](f)
g[string](f) // ERROR cannot use f (variable of type func(T[int])) as func(T[string]) value in argument to g[string]
}
Because the interface is a component type, now it must match exactly.
This should be too late, and I am sorry if this was already discussed, but wouldn't it be possible to forbide unused type parameters by the Go compiler? In other words, couldn't Go compiler forbid the following code?
typeT[Pany] interface {
m()
}
I realized that unused type parameters tend to cause counterintuitive implicit conversions (https://go.dev/play/p/VLb031yF1z6, inspired by #61903). Also, unused parameters are unused then these should be useless. (If we really want to have a unused type parameter, we can add a member like [0]T somewhere).
@hajimehoshi then someone will want this (probably with some good reason), asking: "Where are my phantom types?"
:o)
(phantom type is the usual name of this feature)
Edit: now that I think of it, since you only want to disable it for interface types, I think it will depend on how the question of interface identity is solved.
I see, so if they need to use phantom types, I was wondering if it would be possible to 'convince' the Go compiler by adding a member like [0]T somewhere.
Edit: now that I think of it, since you only want to disable it for interface types, I think it will depend on how the question of interface identity is solved.
Good point. Probably what I want to solve is counterintuitve assignment to an interface type. For non-interface types, such a counterintuitive assignment should not happen as far as I understand. So, forbidding an unused parameter for an interface type might be enough, beside consistency.
Activity
[-]cmd/compile: [/-][+]cmd/compile: invalidly inferred type for interface that doesn't use type parameter[/+]jimmyfrasche commentedon May 23, 2023
I'm not sure I follow.
In my mind
P=intis correct because that's whatxisg[string](x)should be an error because string ≠ intThat
Pis not currently used byTdoes not seem relevant. Say you allowg[string]but then in a later versionTis updated tonow
g[string]is an error and the correct inference would beintbut the only thing that changed was an implementation detailgriesemer commentedon May 23, 2023
Consider this case:
Now we get an error for
g(x)butg[string](x)is ok. That seems inconsistent. We should not be able to infer a type forPin the first case.jimmyfrasche commentedon May 23, 2023
I see what you are saying. The interfaces-as-constraints are identical to each other and
interface { m() }soPdoes not matter for the constraint.However, it seems like the parameterization of the interface-as-type should still be part of its identity.
atdiar commentedon May 23, 2023
@jimmyfrasche that's why it's inference which would fail.
But not instantiation.
jimmyfrasche commentedon May 23, 2023
I expect it to behave like this, since the interface(s) aren't used as constraints and are just ordinary types:
https://go.dev/play/p/0Wj38UCOijw
griesemer commentedon May 23, 2023
@jimmyfrasche But the rules for interface assignment are quite different from non-interface assignment. But here is a counter-example:
Because the interface is a component type, now it must match exactly.
20 remaining items
gopherbot commentedon May 27, 2023
Change https://go.dev/cl/498895 mentions this issue:
go/types, types2: use exact unification for component typesgo/types, types2: use exact unification for component types
hajimehoshi commentedon Aug 18, 2023
This should be too late, and I am sorry if this was already discussed, but wouldn't it be possible to forbide unused type parameters by the Go compiler? In other words, couldn't Go compiler forbid the following code?
I realized that unused type parameters tend to cause counterintuitive implicit conversions (https://go.dev/play/p/VLb031yF1z6, inspired by #61903). Also, unused parameters are unused then these should be useless. (If we really want to have a unused type parameter, we can add a member like
[0]Tsomewhere).What do you think?
atdiar commentedon Aug 18, 2023
@hajimehoshi then someone will want this (probably with some good reason), asking: "Where are my phantom types?"
:o)
(phantom type is the usual name of this feature)
Edit: now that I think of it, since you only want to disable it for interface types, I think it will depend on how the question of interface identity is solved.
hajimehoshi commentedon Aug 18, 2023
OK so my question is when phantom types are useful.
EDIT: https://doc.rust-lang.org/rust-by-example/generics/phantom.html
I see, so if they need to use phantom types, I was wondering if it would be possible to 'convince' the Go compiler by adding a member like
[0]Tsomewhere.atdiar commentedon Aug 18, 2023
@hajimehoshi in general to express units.
I'd think.
But your question is more interesting and I responded a bit too fast, since you are especially targeting the case of interface types.
I'm curious as well to see what people think.
hajimehoshi commentedon Aug 18, 2023
Good point. Probably what I want to solve is counterintuitve assignment to an interface type. For non-interface types, such a counterintuitive assignment should not happen as far as I understand. So, forbidding an unused parameter for an interface type might be enough, beside consistency.
go/types, types2: don't infer type argument for unused parameter in i…