Skip to content

cmd/compile: no need to infer type arguments for blank type parameters with any constraint #50547

Closed

Description

@griesemer
package p

func f[_ any]() {}

func _() {
        f()
}

produces the error

cannot infer _

but blank type parameters can't be used and thus it doesn't matter what their corresponding argument is if the constraint is any (any type will satisfy that constraint). If the constraint is not any we should still infer the type argument so we can verify constraint satisfaction.

Maybe we shouldn't report an error in this case. Or maybe this is so esoteric that is doesn't matter. Filing this issue so we document decision (and perhaps implement it).

cc: @ianlancetaylor @findleyr for input

Activity

  1. added
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on Jan 11, 2022
  2. added this to the Go1.19 milestone on Jan 11, 2022
  3. self-assigned this
    on Jan 11, 2022
  4. findleyr commented on Jan 11, 2022

    @findleyr
    Contributor

    In my opinion we shouldn't do anything special for this case, i.e. we should continue to report an error. It doesn't really matter that the type parameter is blank here -- all that really matters is that the function type does not depend on the type parameter.
    We could equally well support the following:

    func f[A, B any](a A) A { return a }
    
    var g = f(0)

    Choosing to omit this error for blank type parameters seems somewhat arbitrary. In your proposal, would we support the example here if B were _? If so, that's rather more complex as we'd have to short-circuit inference.

  5. bcmills commented on Jan 11, 2022

    @bcmills
    Contributor

    If we omitted the error for the blank-parameter case, then it would be impossible to change the name of the parameter from _ to any other name — even though that kind of renaming should otherwise be completely compatible.

  6. griesemer commented on Jan 11, 2022

    @griesemer
    ContributorAuthor

    The arguments against not inferring (i.e., for not doing anything special) in this case are convincing. Let's not make any changes. Closing.

  7. locked and limited conversation to collaborators on Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.TypeInferenceIssue is related to generic type inferencegenericsIssue is related to generics

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions