Refactor bad coercion checking in a few places
We do bad coercion checking in a few places in the compiler, but they all checked it differently:
- CoreToStg.coreToStgArgs: Disallowed lifted-to-unlifted, disallowed changing prim reps even when the sizes are the same.
- StgCmmExpr.cgCase: Checked primRepSlot equality. This disallowed Int to Int64 coercions on 64-bit systems (and Int to Int32 on 32-bit) even though those are fine.
- CoreLint: Only place where we do this right. Full rules are explained in Note [Bad unsafe coercion].
This patch implements the check explained in Note [Bad unsafe coercion] in CoreLint and uses it in CoreToStg.coreToStgArgs and StgCmmExpr.cgCase.
This fixes #16952 (closed) and unblocks !1381 (closed) (which fixes #16893 (closed)).
This is the most conservative and correct change I came up with that fixes #16952 (closed).
One remaining problem with coercion checking is that it's currently done in seemingly random places. What's special about CoreToStg.coreToStgArgs and StgCmmExpr.cgCase? My guess is that adding assertions to those places caught bugs before so we left assertions in those places. I think we should remove these assertions and do coercion checking in CoreLint and StgLint only (#17041).