Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/types: NewSignatureType mutates its TypeParams, and panics unless they are new #67293

Open
adonovan opened this issue May 10, 2024 · 0 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented May 10, 2024

If a client of go/types uses NewSignatureType to create a variation on an existing type, it is an error to pass the TypeParams of the existing function, as bindTParams will update their index field, and panic if the field has already been set.

I think there are two separate issues here:

  1. NewSignatureType mutates its operands. It's not at all obvious that it does this; if this is unavoidable, it should be documented.
  2. If the same type parameters are re-used, their indices will be the same. In that case, bindTParams should probably just skip the update since it is unnecessary, rather than panic unnecessarily.

Offending client code:

		// Emit signature.
		//
		// Elide parameters after the third one.
		// WriteSignature is too complex to fork, so we replace
		// parameters 4+ with "invalid type", format,
		// then post-process the string.
		if sig.Params().Len() > 3 {
			sig = types.NewSignatureType(
				sig.Recv(),
				typesSeqToSlice[*types.TypeParam](sig.RecvTypeParams()),
				typesSeqToSlice[*types.TypeParam](sig.TypeParams()),
				types.NewTuple(append(
					typesSeqToSlice[*types.Var](sig.Params())[:3],
					types.NewVar(0, nil, "", types.Typ[types.Invalid]))...),
				sig.Results(),
				false) // any final ...T parameter is truncated
		}
		types.WriteSignature(&buf, sig, pkgRelative)

(Gopls uses NewSignatureType to truncate long parameter lists so that they can be formatted using TypeString or WriteSignature. If the machinery of TypeString provided more control over the traversal this would not be necessary.)

@gri @findleyr

@cherrymui cherrymui added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 10, 2024
@cherrymui cherrymui added this to the Backlog milestone May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

2 participants