Check for representation polymorphism in the typechecker using Concrete#
This patch moves the representation polymorphism checks from the desugarer to the typechecker, by introducing a magic
Concrete# predicate, and emitting
Concrete# Wanted constraints to rule out representation polymorphism where it isn't allowed. A broad outline in laid out in the wiki page FixedRuntimeRep.
This only implements phase 1 of the plan outlined on the wiki page: we don't allow
Concrete# constraints to be rewritten (only zonked), so this doesn't add support for type families in
Fixes these tickets:
Here's a list of the representation polymorphism checks that I have come across which already exist in the compiler; I'm tracking which of these have been handled with the
Tuple constructors (boxed and unboxed):
Unboxed sum constructors:
- Static pointers: no special check needed.
- Explicit lists: no special check needed (these are never rebindable syntax).
Arithmetic sequences: no special check needed (
enumFromTo/... aren't rebindable).
- List comprehensions: no special checks needed (no rebindable syntax).
Monad comprehensions: similar treatment as do notation, but in
usingmust have a type of the form
(a -> b) -> [a] -> [[a]], even with
OverloadedListsenabled. This means no check is needed.
Various arrow expressions: the arrow syntax functions are handled in
GHC.Tc.Utils.Instantiate.tcSyntaxName, with other checks peppered throughout
GHC.Tc.Gen.Arrow. It's been quite difficult to write tests which actually run into these checks, but I've added
RepPolyArrowCmd. The error messages are substantially improved: the old errors would print internal names like
ds_d1D0instead of the actual syntax name (e.g.
dsHsWrapper: handled in
mkWpFun(not sure which module to put
mkWpFunin right now).
dsSyntaxExpr: I think I don't want to insert checks here, although it is easily doable.
Unboxed tuple patterns:
PatBind: the other checks are sufficient. Test
selectConMatchVars: the other checks are sufficient.
CoPat: no special check needed, as the other checks in patterns prevent representation-polymorphism from sneaking through.
- Variable patterns:
Typeclass method types: the check in
GHC.Tc.TyCl.checkValidClassseems to be redundant, as representation polymorphism should already be prevented by
Data constructor declarations: the existing check in
Pattern synonym declarations: the existing check in
GHC.Tc.Gen.Sig.tcPatSynSigcovers arguments, and needs to be augmented with a check for the scrutinee type. Tests:
RULES: these caused a few issues, until I realised that we must never quantify over
FixedRuntimeRepconstraints in RULES (and also because of a bug that Richard introduced in
- Typeclass method types: the check in
Investigate performance regressions in T12545, T14683, T16577, T5631, T5642: the regressions seemed to be largely due to extra zonking that was needed with
FixedRuntimeRep#checks; these zonks are no longer needed with
Adjust the mechanism to avoid requiring that the kind of the type being checked must immediately be of the form
TYPE rep, allow this observation to be deferred.
Ensure that Ed Kmett's
unboxedlibrary can be compiled (provided one adds some
Concreteinstances in signature files to satisfy the representation-polymorphism checker).
FixedRuntimeRepconstraints to be deferred: test
Emit a custom error message when
FixedRuntimeRepconstraints go unsolved, instead of "No instance for FixedRuntimeRep rep".
Don't allow user-written
FixedRuntimeRepinstances in backpack signature files, same as for
- Remove the checks in the desugarer & zonker that are rendered obsolete.
Write Note [The FixedRuntimeRep mechanism] in
GHC.Tc.Instance.Classto briefly explain how
Update Note [Representation polymorphism checking] in
GHC.HsToCore.Monad: rewritten and moved to
GHC.Tc.Types.Originto go above
Update Note [Checking for representation-polymorphic functions]: rewritten and moved to
GHC.Tc.Gen.Appwhere the check now happens.
Update Note [Representation-polymorphic Ids] in
GHC.Types.Id: deleted the note, as it is now subsumed by Note [Checking for representation-polymorphic functions].
Update Note [Desugaring representation-polymorphic applications] in
GHC.HsToCore.Utils: deleted, as all of that complexity is now gone.
- Update the GHC 9.4 release notes.