Skip to content

cmd/compile: invalidly inferred type for interface that doesn't use type parameter #60377

Closed

Description

@griesemer

In the following code:

type T[P any] interface {
	m()
}

func g[P any](T[P]) {}

func _() {
	var x T[int]
	g(x)         // here we infer P == int, but in fact any type of P would be ok
	g[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.

Activity

  1. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on May 23, 2023
  2. added this to the Go1.21 milestone on May 23, 2023
  3. self-assigned this
    on May 23, 2023
  4. changed the title [-]cmd/compile: [/-] [+]cmd/compile: invalidly inferred type for interface that doesn't use type parameter[/+] on May 23, 2023
  5. jimmyfrasche commented on May 23, 2023

    @jimmyfrasche
    Member

    I'm not sure I follow.

    In my mind

    • unifying P = int is correct because that's what x is
    • g[string](x) should be an error because string ≠ int

    That P is not currently used by T does not seem relevant. Say you allow g[string] but then in a later version T is updated to

    type T[P any] interface {
    	m() P
    }

    now g[string] is an error and the correct inference would be int but the only thing that changed was an implementation detail

  6. griesemer commented on May 23, 2023

    @griesemer
    ContributorAuthor

    Consider this case:

    type T1[P any] interface {
    	m()
    }
    
    type T2[P any] interface {
    	m()
    }
    
    func g[P any](T1[P]) {}
    
    func _() {
    	var x T2[int]
    	g(x)         // here we infer P == int, but in fact any type of P would be ok
    	g[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.

  7. jimmyfrasche commented on May 23, 2023

    @jimmyfrasche
    Member

    I see what you are saying. The interfaces-as-constraints are identical to each other and interface { m() } so P does not matter for the constraint.

    However, it seems like the parameterization of the interface-as-type should still be part of its identity.

  8. atdiar commented on May 23, 2023

    @atdiar

    @jimmyfrasche that's why it's inference which would fail.

    But not instantiation.

  9. jimmyfrasche commented on May 23, 2023

    @jimmyfrasche
    Member

    I expect it to behave like this, since the interface(s) aren't used as constraints and are just ordinary types:

    type T[P any] struct{}
    
    func g[P any](T[P]) {}
    
    func _() {
    	var x T[int]
    	g(x)         // here we infer P == int
    	g[string](x) // here we fail to set P == string as P = int
    }

    https://go.dev/play/p/0Wj38UCOijw

  10. griesemer commented on May 23, 2023

    @griesemer
    ContributorAuthor

    @jimmyfrasche But the rules for interface assignment are quite different from non-interface assignment. But here is a counter-example:

    type T[P any] interface{ m() }
    
    func g[P any](func(T[P])) {}
    
    func _() {
    	var f func(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.

  11. modified the milestones: Go1.21, Go1.22 on May 23, 2023
  12. added
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on May 23, 2023
  13. removed
    NeedsFixThe path to resolution is known, but the work has not been done.
    on May 23, 2023
  14. 20 remaining items

  15. gopherbot commented on May 27, 2023

    @gopherbot
    Contributor

    Change https://go.dev/cl/498895 mentions this issue: go/types, types2: use exact unification for component types

  16. hajimehoshi commented on Aug 18, 2023

    @hajimehoshi
    Member

    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?

    type T[P any] 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).

    What do you think?

  17. atdiar commented on Aug 18, 2023

    @atdiar

    @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.

  18. hajimehoshi commented on Aug 18, 2023

    @hajimehoshi
    Member

    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]T somewhere.

  19. atdiar commented on Aug 18, 2023

    @atdiar

    @hajimehoshi in general to express units.

    type Distance[Unit any] float64
    type Miles struct{}
    type Meters struct{} 
    
    type DistanceMiles = Distance[Miles] 
    type DistanceMeters = Distance[Meters] 

    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.

  20. hajimehoshi commented on Aug 18, 2023

    @hajimehoshi
    Member

    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.

  21. added a commit that references this issue on Apr 29, 2024
    1dd24d8
  22. locked and limited conversation to collaborators on Aug 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions