Opened 3 weeks ago

Last modified 4 days ago

#13929 new bug

GHC panic with levity polymorphism

Reported by: vagarenko Owned by:
Priority: high Milestone: 8.2.2
Component: Compiler Version: 8.2.1-rc2
Keywords: TypeInType, LevityPolymorphism Cc: goldfire, Iceland_jack
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

I'm using GHC version 8.2.0.20170507

This code fails to compile

{-# LANGUAGE MagicHash #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeInType #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE AllowAmbiguousTypes #-}

module Main where

import GHC.Exts
import Data.Kind
import GHC.Generics

class GUnbox (f :: Type -> Type) (r :: RuntimeRep) where
    type GUnboxed f r :: TYPE r
    gunbox :: f p -> GUnboxed f r

instance (GUnbox f rf, GUnbox g rg) => GUnbox (f :*: g) ('TupleRep '[rf, rg]) where
    type GUnboxed (f :*: g) ('TupleRep '[rf, rg]) = (# GUnboxed f rf, GUnboxed g rg #)
    -- if I remove implementation of `gunbox` it compiles successfully
    gunbox (x :*: y) = (# gunbox x, gunbox y #)

main :: IO ()
main = pure ()

with message:

[1 of 1] Compiling Main             ( Main.hs, .stack-work\dist\f42fcbca\build\Main.o )
ghc.EXE: panic! (the 'impossible' happened)
  (GHC version 8.2.0.20170507 for x86_64-unknown-mingw32):
        isUnliftedType
  GUnboxed g_a21y rg_a21z :: TYPE rg_a21z
  Call stack:
      CallStack (from HasCallStack):
        prettyCurrentCallStack, called at compiler\utils\Outputable.hs:1134:58 in ghc:Outputable
        callStackDoc, called at compiler\utils\Outputable.hs:1138:37 in ghc:Outputable
        pprPanic, called at compiler\types\Type.hs:1954:10 in ghc:Type

Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug

Change History (9)

comment:1 Changed 3 weeks ago by bgamari

Cc: goldfire added

Thanks for the bug report and nice reproducer! Adding goldfire to CC.

comment:2 Changed 3 weeks ago by Iceland_jack

Cc: Iceland_jack added

Fun example

comment:3 Changed 3 weeks ago by bgamari

Keywords: TypeInType added

comment:4 Changed 3 weeks ago by bgamari

For the record, a more complete callstack is,

ghc-stage2: panic! (the 'impossible' happened)
  (GHC version 8.2.0.20170704 for x86_64-unknown-linux):
	isUnliftedType
  GUnboxed g_a20x rg_a20y :: TYPE rg_a20y
  Call stack:
      CallStack (from HasCallStack):
        prettyCurrentCallStack, called at compiler/utils/Outputable.hs:1133:58 in ghc:Outputable
        callStackDoc, called at compiler/utils/Outputable.hs:1137:37 in ghc:Outputable
        pprPanic, called at compiler/types/Type.hs:1954:10 in ghc:Type
        isUnliftedType, called at compiler/coreSyn/CoreUtils.hs:487:27 in ghc:CoreUtils
        needsCaseBinding, called at compiler/coreSyn/MkCore.hs:162:10 in ghc:MkCore
        mk_val_app, called at compiler/coreSyn/MkCore.hs:146:42 in ghc:MkCore
        mkCoreApps, called at compiler/coreSyn/MkCore.hs:154:26 in ghc:MkCore
        mkCoreConApps, called at compiler/coreSyn/MkCore.hs:364:5 in ghc:MkCore
        mkCoreUbxTup, called at compiler/coreSyn/MkCore.hs:370:32 in ghc:MkCore
        mkCoreTupBoxity, called at compiler/deSugar/DsExpr.hs:382:19 in ghc:DsExpr

Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug

comment:5 Changed 3 weeks ago by bgamari

Keywords: LevityPolymorphism added

comment:6 Changed 3 weeks ago by RyanGlScott

Architecture: x86_64 (amd64)Unknown/Multiple
Operating System: WindowsUnknown/Multiple

comment:7 Changed 2 weeks ago by bgamari

To be clear, I don't think (although could be wrong) this program should compile; afterall, the type of gunbox requires that we represent a levity polymorphic result, which we cannot do. Hence, the real bug here is the panic instead of a proper type error.

comment:8 in reply to:  7 Changed 2 weeks ago by vagarenko

Replying to bgamari:

To be clear, I don't think (although could be wrong) this program should compile; afterall, the type of gunbox requires that we represent a levity polymorphic result, which we cannot do. Hence, the real bug here is the panic instead of a proper type error.

I think levity polymorphism paper says you can't have functions with levity-polymorphic parameters, levity-polymorphic results are OK.

This compiles fine:

instance (GUnbox f r) => GUnbox (M1 i t f) r where
    type GUnboxed (M1 i t f) r = GUnboxed f r
    gunbox (M1 x) = gunbox x

But unboxed sum also fails to compile:

instance (GUnbox f rf, GUnbox g rg) => GUnbox (f :+: g) ('SumRep '[rf, rg]) where
    type GUnboxed (f :+: g) ('SumRep '[rf, rg]) = (# GUnboxed f rf | GUnboxed g rg #)
    gunbox (L1 l) = (# gunbox l | #)
    gunbox (R1 r) = (# | gunbox r #)

with the same message.

comment:9 Changed 4 days ago by bgamari

Milestone: 8.2.18.2.2

I'm afraid this won't be fixed for 8.2.1.

Note: See TracTickets for help on using tickets.