Skip to content

go/types: API allows instantiation of instantiated type #72978

Closed

Description

@griesemer

Consider: https://go.dev/play/p/SiV_jaPuzNr
Re-Instantiation of the instantiated type of x in the code works unexpectedly.
Re-Instantiation of the instantiated type of f in the code fails as expected.

This seems inconsistent. Either we permit both or neither.

@findleyr for input

Activity

  1. added
    BugReportIssues describing a possible bug in the Go implementation.
    on Mar 20, 2025
  2. added theissue type on Mar 20, 2025
  3. added this to the Go1.25 milestone on Mar 20, 2025
  4. findleyr commented on Mar 21, 2025

    @findleyr
    Contributor

    This is just a bug: there are no type params to substitute in the double-instantiated instance. The underlying is the same as the first instance.
    https://go.dev/play/p/6WDvAzt01tY

    It seems we are just missing a check that there are no type arguments.

  5. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Mar 21, 2025
  6. KirtanSoni commented on Mar 23, 2025

    @KirtanSoni

    May I propose a PR or a Plan on this?

  7. obeyda commented on Mar 27, 2025

    @obeyda
    Contributor

    Hi,
    It looks like the double instantiation check was removed in CL 333589 when the instantiation logic was moved from subst.go to instantiate.go.

    Current state:

    switch orig := orig.(type) {
    case *Named:
    res = check.newNamedInstance(pos, orig, targs, expanding) // substituted lazily

    I suggest to fix this by reintroducing the check in instantiate.go. For example:

     case *Named:
    +    // Verify type parameter count for named types
    +    tparams := orig.TypeParams()
    +    if !check.validateTArgLen(pos, orig.obj.Name(), tparams.Len(), len(targs)) {
    +        return Typ[Invalid]
    +    }
    +    if tparams.Len() == 0 {
    +        // No type parameters to substitute (orig is not generic or already instantiated)
    +        return orig  // nothing to do
    +    }
         res = check.newNamedInstance(pos, orig, targs, expanding) // substituted lazily
    

    This should restore the original behavior and prevent double instantiation. Happy to work on a CL if this approach looks acceptable.

  8. gopherbot commented on Apr 16, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/666115 mentions this issue: go/types, types2: add missing generic type test to Checker.Instantiate

  9. gopherbot commented on Apr 17, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/666435 mentions this issue: go/types, types2: add tests for duplicate instantiation

  10. modified the milestones: Go1.25, Go1.26 on Aug 12, 2025
  11. modified the milestones: Go1.26, Go1.27 on Jan 14, 2026
  12. gopherbot commented on Jun 12, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/790040 mentions this issue: go/types, types2: add missing generic test to Checker.Instantiate

  13. added a commit that references this issue on Jun 12, 2026
    2e2e974
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugReportIssues describing a possible bug in the Go implementation.NeedsFixThe path to resolution is known, but the work has not been done.

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions