Skolem escape in static forms is broken
Here is some code in GHC.Tc.Gen.Expr
```
tcExpr (HsStatic fvs expr) res_ty
= do { res_ty <- expTypeToType res_ty
; (co, (p_ty, expr_ty)) <- matchExpectedAppTy res_ty
; (expr', lie) <- captureConstraints $
addErrCtxt (StaticFormCtxt expr) $
tcCheckPolyExprNC expr expr_ty
-- Check that the free variables of the static form are closed.
-- It's OK to use nonDetEltsUniqSet here as the only side effects of
-- checkClosedInStaticForm are error messages.
; mapM_ checkClosedInStaticForm $ nonDetEltsUniqSet fvs
-- Require the type of the argument to be Typeable.
; typeableClass <- tcLookupClass typeableClassName
; typeable_ev <- emitWantedEvVar StaticOrigin $
mkTyConApp (classTyCon typeableClass)
[liftedTypeKind, expr_ty]
-- Insert the constraints of the static form in a global list for later
-- validation. See #13499 for an explanation of why this really isn't the
-- right thing to do: the enclosing skolems aren't in scope any more!
-- Static forms really aren't well worked out yet.
; emitStaticConstraints lie
```
Note that last comment!
## Why it has become important
This problem bit me when working on !14474 and its new mechanism for reporting unifications.
When typechecking `typecheck/should_compile/StaticPtrTypeFamily` we get this set of constraints to solve
```
simplifyTop {
wanted = WC {wc_simple =
[W] hole{co_aOK} {0}
:: F t_aOz[tau:1] ~# t_aOE[tau:1] (CNonCanonical)
wc_impl =
Implic {
TcLevel = 1
Skolems = a_aOs[sk:1]
Given-eqs = MaybeGivenEqs
Status = Unsolved
Given =
$dTypeable_aOt :: Typeable a_aOs[sk:1]
$dTypeable_aOu :: Typeable (F a_aOs[sk:1])
Wanted =
WC {wc_simple =
[W] $dTypeable_aOL {0} :: Typeable a_aOw[tau:1] (CNonCanonical)
[W] $dIsStatic_aOR {0} :: IsStatic StaticPtr (CNonCanonical)
[W] hole{co_aOU} {0}
:: F a_aOs[sk:1] ~# t_aOE[tau:1] (CNonCanonical)}
Binds = EvBindsVar<aOV>
need = ENS{ens_dms = {} ens_fvs = {}}
need_implic = ENS{ens_dms = {} ens_fvs = {}}
the type signature for:
caller :: forall a.
(Typeable a, Typeable (F a)) =>
a -> F a -> () }}
```
Look at `cOK`. We are at ambient level 0 but it mentions a level-1 unification variable.
This is wrong: constraints should never mention variables at deeper level than ambient. And indeed #13499 identified precisely this issue.
This is currently causing an assertion failure in `canEqCanLHSHetero` my MR !14474, for the above test, but it's not the fault of !14474; it's the fault of this bug.
Once fixed, I should re-instate the assertion in `canEqCanLHSHetero`.
## What do to?
My plan is this:
* Emit an implication in-place (not thrown up to the top level in a separate bag)
* Give it a special `SkolemInfoAnon`, say `StaticFormSkol`
* In `nestImplicTcS`, when walking inside an implication for `StaticFormSkol` simply abandon all Givens and solve in a fresh inert set.
issue