Skip to content

Fix #78006: include type arguments in instantiated type cycle errors#78009

Open
9chait9 wants to merge 1 commit intogolang:masterfrom
9chait9:fix/issue-78006
Open

Fix #78006: include type arguments in instantiated type cycle errors#78009
9chait9 wants to merge 1 commit intogolang:masterfrom
9chait9:fix/issue-78006

Conversation

@9chait9
Copy link

@9chait9 9chait9 commented Mar 7, 2026

More Context

This PR addresses issue #78006 by enhancing the clarity of cycle error messages for instantiated generic types in the Go compiler. Previously, when a declaration cycle involved a generic type instantiated with specific arguments (e.g., MyType[int]), the compiler's error message would only display the generic type's base name ("MyType"), omitting the crucial type arguments. This made it challenging to identify the exact recursive type causing the error.

The core problem lies in the pathString function and the local name helper within src/cmd/compile/internal/types2/decl.go. These functions, responsible for constructing cycle error messages, did not leverage the full type-formatting capabilities available in src/cmd/compile/internal/types2/typestring.go. Consequently, when processing an Object representing an instantiated generic TypeName, obj.Name() only returned the base name, ignoring the associated type arguments. This PR rectifies this by integrating the detailed type representation from typestring.go into the error reporting in decl.go.

Test Plan

To verify this fix, the following test cases should be considered:

Test Case 1: Simple Instantiated Generic Type Cycle

  • Scenario: Create a Go program with a declaration cycle involving a straightforward instantiated generic type.
    • Example:
      package p
      type A[T any] B[T]
      type B[T any] A[T]
      var _ A[int]
  • Expected Result: The compiler error message for the cycle should explicitly include the type arguments, e.g., "A[int] refers to B[int] -> A[int]".

Test Case 2: Complex Instantiated Generic Type Cycle

  • Scenario: Introduce a more intricate declaration cycle involving multiple instantiated generic types or nested generic types.
    • Example:
      package p
      type C[X any] D[X, int]
      type D[Y, Z any] C[Y]
      var _ C[string]
  • Expected Result: The compiler error message should accurately reflect all instantiated generic types with their specific arguments throughout the cycle path.

Test Case 3: Non-Generic Type Cycle (No Change Expected)

  • Scenario: Create a Go program with a declaration cycle that does not involve any generic types.
    • Example:
      package p
      type E F
      type F E
      var _ E
  • Expected Result: The compiler error message should remain unchanged, confirming that this PR does not inadvertently affect error reporting for non-generic cycles.

Test Case 4: Cycle Involving Imported Generic Types

  • Scenario: Define a generic type and a cycle involving it in one package. In a separate package, import and instantiate this generic type, forming a cycle.
  • Expected Result: The error message should correctly qualify the imported generic type with its package name and include its type arguments.

Fixes #78006

Summary

This PR addresses issue #78006 by ensuring that cycle error messages for instantiated generic types include their type arguments. Previously, these error messages would only show the generic type's name, making it difficult to pinpoint the exact recursive type causing the error. By including the type arguments, the error messages become more informative and actionable.

Changes

  • src/cmd/compile/internal/types2/decl.go:
    • Added a new helper function objTypeName that formats Objects, specifically TypeName objects that are instances of generic types, to include their type arguments (e.g., Type[Arg1, Arg2]).
    • Modified pathString to utilize objTypeName for formatting objects in the cycle path.
    • Modified the name helper function within cycleError to also use objTypeName when constructing error messages for declaration cycles.

@gopherbot
Copy link
Contributor

This PR (HEAD: 89839d1) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/752660.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/752660.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Gopher Robot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.


Please don’t reply on this GitHub thread. Visit golang.org/cl/752660.
After addressing review feedback, remember to publish your drafts!

@9chait9 9chait9 marked this pull request as ready for review March 7, 2026 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants