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

x/tools/gopls: completion gives unneeded generic type instantiation snippet #51783

Open
muirdm opened this issue Mar 18, 2022 · 2 comments
Open
Labels
gopls/completion Issues related to auto-completion in gopls. gopls/generics Issues related to gopls' support for generics gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@muirdm
Copy link

muirdm commented Mar 18, 2022

func foo[T float64 | int](a T) T {
	return a
}

func main() {
	var _ int = fo<>
}

Completing to "foo" at <> inserts "int(foo[T float64|int](a T))" (ignore the extra type conversion), but most likely the type argument can be inferred so gopls should offer just "foo(a int)" instead.

@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Mar 18, 2022
@gopherbot gopherbot added this to the Unreleased milestone Mar 18, 2022
@muirdm
Copy link
Author

muirdm commented Mar 18, 2022

Would this work: if there is one type param and it is the func’s return type and one of the variants is assignable to the expected type (int in this case), then presume type inference will work and omit the instantiating snippet? Is there any programmatic interface to check type param inference?

I guess it is more question of "will type inference work for this function call", and if "yes" then don't give the type param snippet. If go/types can't tell us that, then I feel like some simple heuristics can cover common cases. For example, if all the type params are referenced in the param list, then type inference will work (is that correct?).

@suzmue suzmue modified the milestones: Unreleased, gopls/on-deck Mar 18, 2022
@muirdm
Copy link
Author

muirdm commented Mar 26, 2022

@findleyr Can we hook in to type param inference to determine a priori if a generic function's type params will/might be inferable after the normal params are filled in? I know sometimes it depends on the arguments, but my feeling is that type param inference works most of the time if it can work at all for a given function.

In a similar vein, the heuristic I suggested above would not work in all cases, but I think it would be an improvement (i.e. if type inference will "probably" work, don't give the type param snippet).

@findleyr findleyr added gopls/completion Issues related to auto-completion in gopls. gopls/generics Issues related to gopls' support for generics labels May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/completion Issues related to auto-completion in gopls. gopls/generics Issues related to gopls' support for generics gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants