Skip to content

TransformListComp accepts invalid unboxed let bindings

Summary

let bindings with unboxed types in transformed list comprehensions desugar to invalid Core.

Steps to reproduce

{-# language MagicHash, TransformListComp #-}
module TLC where
import GHC.Exts

glum :: [Int] -> [Int]
glum xs = [I# p | I# x <- xs, let p = x +# 3#, then take 3]

This fails Core Lint. With optimizations enabled, it desugars like the following, but it's desugared just as invalidly when optimizations are disabled.

glum :: [Int] -> [Int]
[LclIdX]
glum
  = \ (xs_aw5 :: [Int]) ->
      build
        @Int
        (\ (@a_dye) (c_dyf :: Int -> a_dye -> a_dye) (n_dyg :: a_dye) ->
           GHC.Base.foldr
             @Int#
             @a_dye
             (\ (ds_dyv :: Int#) (ds_dyu :: a_dye) ->
                let {
                  p_axE :: Int#
                  [LclId]
                  p_axE = ds_dyv } in
                c_dyf ((\ (ds_dyw :: Int#) -> GHC.Types.I# ds_dyw) p_axE) ds_dyu)
             n_dyg
             ((\ (@a_axU) -> take @a_axU (GHC.Types.I# 3#))
                @Int#
                (build
                   @Int#
                   (\ (@a_dyh) (c_dyi :: Int# -> a_dyh -> a_dyh) (n_dyj :: a_dyh) ->
                      GHC.Base.foldr
                        @Int
                        @a_dyh
                        (\ (ds_dyl :: Int) (ds_dyk :: a_dyh) ->
                           case ds_dyl of wild_00 { I# x_axD ->
                           let {
                             p_axR :: Int#
                             [LclId]
                             p_axR = +# x_axD 3# } in
                           let {
                             p_axE :: Int#
                             [LclId]
                             p_axE = p_axR } in
                           c_dyi p_axE ds_dyk
                           })
                        n_dyj
                        xs_aw5))))
end Rec }

Note that one application each of foldr and build are at type Int#, which has the wrong kind.

Expected behavior

I expect this particular example to be rejected by the type checker. However, I would expect the following to continue to work:

loom :: [Int] -> [Int]
loom xs = [z | I# x <- xs, let p = x +# 3#, z <- [1 .. I# p], then take 3]

Note: I've included a "runtime crash" tag even though I haven't written an example that crashes because my informal testing strongly indicates invalid memory accesses that don't happen to segfault.

Environment

  • GHC version used:

Optional:

  • Operating System:
  • System Architecture:
Edited by David Feuer
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information