Fix #14579 by defining tyConAppNeedsKindSig, and using it
GeneralizedNewtypeDeriving and DerivingVia often generate code
with excessive amounts of kind signatures, which is a bit
unfortunate. Thankfully, there's a separate part of GHC that has
already tackled the problem of calculating exactly when generated
code needs explicit kind signatures (and when they don't)—the
reify_tc_app function from TcSplice. reify_tc_app uses a
heuristic defined in terms of the injective_vars_of_binder function,
which works quite well in practice. (In fact, Haddock uses the same
heuristic, which is why this patch updates the Haddock submodule—
more on this in a bit.)
This exact same heuristic could be used to lop off some redundant
kind signatures from the generated code in GND/DerivingVia, so this
patch does exactly that. Specifically:
-
I define a
tyConAppNeedsKindSigfunction inTyCoRepwhich implements the heuristic previously inlined intoreify_tc_app. I explain how this heuristic works inNote [When does a tycon application need an explicit kind signature?]inTyCoRep(which was previouslyNote [Kind annotations on TyConApps]inTcSplice).We now use
tyConAppNeedsKindSigin GND/DerivingVia–generated code to determine if generated tycon applications require explicit kind signatures or not. (I also use this function in Haddock, which is why the submodule is bumped.) -
I extend
injective_vars_of_binderwith aBoolargument which controls whether specified arguments contribute towards injective positions or not (seeNote [When does a tycon application need an explicit kind signature?]for an explanation of what "injective position" means). Note that if one religiously uses visible kind applications, this is true! Correspondingly, I change GND/DerivingViato generate code that always instantiates specified arguments with visible kind applications.