Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 5,251
    • Issues 5,251
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 577
    • Merge requests 577
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell CompilerGlasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #19522
Closed
Open
Issue created Mar 10, 2021 by Ryan Scott@RyanGlScottMaintainer

Levity monomorphic instantiation of unsafeCoerce# is rejected as being levity polymorphic in 9.0+

While experimenting with unsafeCoerce# recently, I tried the following:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE TypeApplications #-}
module Bug where

import GHC.Exts
import Unsafe.Coerce

f :: Int -> Int
f x = unsafeCoerce# @LiftedRep @LiftedRep @Int @Int x

Surprisingly, GHC 9.0 and later reject this:

$ /opt/ghc/9.0.1/bin/ghc Bug.hs
[1 of 1] Compiling Bug              ( Bug.hs, Bug.o )

Bug.hs:10:7: error:
    Cannot use function with levity-polymorphic arguments:
      unsafeCoerce# :: forall (q :: RuntimeRep) (r :: RuntimeRep)
                              (a :: TYPE q) (b :: TYPE r).
                       a -> b
    (Note that levity-polymorphic primops such as 'coerce' and unboxed tuples
    are eta-expanded internally because they must occur fully saturated.
    Use -fprint-typechecker-elaboration to display the full expression.)
    Levity-polymorphic arguments: a :: TYPE q
   |
10 | f x = unsafeCoerce# @LiftedRep @LiftedRep @Int @Int x
   |       ^^^^^^^^^^^^^

The error message confuses me, since nothing about this is levity polymorphic.

Note that this only occurs if visible type applications are used. If I remove them:

f :: Int -> Int
f x = unsafeCoerce# x

Then GHC accepts it.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking