Skip to content

Allow Addr# at top level in source Haskell

Motivation

GHC currently disallows Addr# at the top level in source Haskell. However, Addr# is allowed at the top level in Core, and when compiling with optimizations, GHC aggressively floats all Addr# literals to the top level. This source-haskell restriction can be mildly annoying. It requires users to wrap top-level literals in a Ptr data constructor. Although GHC's optimizations prevent the data constructor allocations from actually happening, it is an unnecessary burden for users.

Proposal

Relax the restriction on Addr# literals at the top level. Currently, such literals are rejected by dsTopLHsBinds (via isUnliftedHsBind). The check could be relaxed to allow top-level Addr# when the RHS is an Addr# literal. For example:

idAddr :: Addr# -> Addr#
idAddr x = x

-- litA would be accepted by GHC
litA :: Addr#
litA = "foo"#

-- litB would be rejected by GHC
litA :: Addr#
litA = idAddr "foo"#
Edited by Andrew Martin
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information