Refactor bindHsQTyVars and friends
authorSimon Peyton Jones <simonpj@microsoft.com>
Mon, 28 Aug 2017 13:20:02 +0000 (14:20 +0100)
committerSimon Peyton Jones <simonpj@microsoft.com>
Tue, 29 Aug 2017 08:37:01 +0000 (09:37 +0100)
commit0257dacf228024d0cc6ba247c707130637a25580
tree47809e6d47e6822d42d879b8b1dabd3bf96e1177
parent6f050d9caa2db8330f73f5ebeee986371e5aa56d
Refactor bindHsQTyVars and friends

This work was triggered by Trac #13738, which revealed to me that
the code RnTypes.bindHsQTyVars and bindLHsTyVarBndrs was a huge
tangled mess -- and outright wrong on occasion as the ticket showed.

The big problem was that bindLHsTyVarBndrs (which is invoked at every
HsForAll, including nested higher rank ones) was attempting to bind
implicit kind variables, which it has absolutely no busineess doing.
Imlicit kind quantification is done at the outside only, in fact
precisely where we have HsImplicitBndrs or LHsQTyVars (which also
has implicit binders).

Achieving this move was surprisingly hard, because more and more
barnacles had accreted aroud the original mistake.  It's much
much better now.

Summary of changes.  Almost all the action is in RnTypes.

* Implicit kind variables are bound only by
  - By bindHsQTyVars, which deals with LHsQTyVars
  - By rnImplicitBndrs, which deals with HsImplicitBndrs

* bindLHsTyVarBndrs, and bindLHsTyVarBndr are radically simplified.
  They simply does far less, and have lots their forest of
  incomprehensible accumulating parameters.  (To be fair, some of
  the code in bindLHsTyVarBndrs just moved to bindHsQTyVars, but
  in much more perspicuous form.)

* The code that checks if a variable appears in both a kind and
  a type (triggering RnTypes.mixedVarsErr) was bizarre.  E.g.
  we had this in RnTypes.extract_hs_tv_bndrs
       ; check_for_mixed_vars bndr_kvs acc_tvs
       ; check_for_mixed_vars bndr_kvs body_tvs
       ; check_for_mixed_vars body_tvs acc_kvs
       ; check_for_mixed_vars body_kvs acc_tvs
       ; check_for_mixed_vars locals body_kvs
  I cleaned all this up; now we check for mixed use at binding
  sites only.

* Checks for "Variable used as a kind before being bound", like
     data T (a :: k) k = rhs
  now just show up straightforwardly as "k is not in scope".
  See Note [Kind variable ordering]

* There are some knock-on simplifications in RnSource.
12 files changed:
compiler/rename/RnSource.hs
compiler/rename/RnTypes.hs
testsuite/tests/ghci/scripts/T7873.stderr
testsuite/tests/indexed-types/should_fail/SimpleFail6.stderr
testsuite/tests/polykinds/BadKindVar.hs [new file with mode: 0644]
testsuite/tests/polykinds/BadKindVar.stderr [new file with mode: 0644]
testsuite/tests/polykinds/T13738.hs [new file with mode: 0644]
testsuite/tests/polykinds/T13738.stderr [new file with mode: 0644]
testsuite/tests/polykinds/T7404.stderr
testsuite/tests/polykinds/all.T
testsuite/tests/rename/should_fail/T11592.stderr
testsuite/tests/typecheck/should_fail/T11963.stderr