Skip to content

Tweak error messages for narrowly-kinded assoc default decls

Ryan Scott requested to merge RyanGlScott/ghc:wip/T13971 into master

This program, from #13971 (closed), currently has a rather confusing error message:

class C a where
  type T a :: k
  type T a = Int
    • Kind mis-match on LHS of default declaration for ‘T’
    • In the default type instance declaration for ‘T’
      In the class declaration for ‘C’

It's not at all obvious why GHC is complaining about the LHS until you realize that the default, when printed with -fprint-explicit-kinds, is actually type T @{k} @* a = Int. That is to say, the kind of a is being instantiated to Type, whereas it ought to be a kind variable. The primary thrust of this patch is to weak the error message to make this connection more obvious:

    • Illegal argument ‘*’ in:
        ‘type T @{k} @* a = Int’
        The arguments to ‘T’ must all be type variables
    • In the default type instance declaration for ‘T’
      In the class declaration for ‘C’

Along the way, I performed some code cleanup suggested by @rae in #13971 (closed) (comment 191287). Before, we were creating a substitution from the default declaration's type variables to the type family tycon's type variables by way of tcMatchTys. But this is overkill, since we already know (from the aforementioned validity checking) that all the arguments in a default declaration must be type variables anyway. Therefore, creating the substitution is as simple as using zipTvSubst. I took the opportunity to perform this refactoring while I was in town.

Fixes #13971 (closed).

Merge request reports