[GHC] #13658: Assertion failure on HEAD: "optCoercion changed types!"

#13658: Assertion failure on HEAD: "optCoercion changed types!"
-------------------------------------+-------------------------------------
Reporter: bgamari | Owner: (none)
Type: bug | Status: new
Priority: high | Milestone: 8.2.1
Component: Compiler | Version: 8.3
Keywords: | Operating System: Unknown/Multiple
Architecture: | Type of failure: None/Unknown
Unknown/Multiple |
Test Case: | Blocked By:
Blocking: | Related Tickets:
Differential Rev(s): | Wiki Page:
-------------------------------------+-------------------------------------
I am seeing the following assertion failure on my `wip/libdw-prof` branch,
{{{
ghc-stage1: panic! (the 'impossible' happened)
(GHC version 8.3.20170507 for x86_64-unknown-linux):
ASSERT failed!
optCoercion changed types!
in_co:
(base:Data.Typeable.Internal.TypeRep{tc 35W}
(UnsafeCo nominal ghc-prim:GHC.Types.Any{(w) tc 35K} (ghc-
prim:GHC.Types.Any{(w) tc 35K}
-> ghc-
prim:GHC.Types.Any{(w) tc 35K}))
(UnsafeCo nominal (ghc-prim:GHC.Types.Any{(w) tc 35K}
ghc-prim:GHC.Types.Any{(w) tc 35K}) ghc-
prim:GHC.Types.Any{(w) tc 35K}))_R
in_ty1:
base:Data.Typeable.Internal.TypeRep{tc 35W}
(ghc-prim:GHC.Types.Any{(w) tc 35K}
ghc-prim:GHC.Types.Any{(w) tc 35K})
in_ty2:
base:Data.Typeable.Internal.TypeRep{tc 35W}
ghc-prim:GHC.Types.Any{(w) tc 35K}
out_co:
(base:Data.Typeable.Internal.TypeRep{tc 35W}
(UnsafeCo nominal ghc-prim:GHC.Types.Any{(w) tc 35K} (ghc-
prim:GHC.Types.Any{(w) tc 35K}
-> ghc-
prim:GHC.Types.Any{(w) tc 35K}))

#13658: Assertion failure on HEAD: "optCoercion changed types!" -------------------------------------+------------------------------------- Reporter: bgamari | Owner: (none) Type: bug | Status: new Priority: high | Milestone: 8.2.1 Component: Compiler | Version: 8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by slyfox): I've added a bit of debug into ASSERT. The complain is about type mismatch between in_ty1, out_ty1: {{{ in_ty1: base:Data.Typeable.Internal.TypeRep{tc 35W} (ghc-prim:GHC.Types.Any{(w) tc 35K} ghc-prim:GHC.Types.Any{(w) tc 35K}) out_ty1: base:Data.Typeable.Internal.TypeRep{tc 35W} ghc-prim:GHC.Types.Any{(w) tc 35K} }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13658#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13658: Assertion failure on HEAD: "optCoercion changed types!" -------------------------------------+------------------------------------- Reporter: bgamari | Owner: (none) Type: bug | Status: new Priority: high | Milestone: 8.2.1 Component: Compiler | Version: 8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by slyfox): The same error is also reproducible on a ghc-HEAD with this tiny '''mk/build.mk''': {{{ SRC_HC_OPTS += -g -dcore-lint -DDEBUG }}} I'll try to extract something smaller. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13658#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13658: Assertion failure on HEAD: "optCoercion changed types!" -------------------------------------+------------------------------------- Reporter: bgamari | Owner: (none) Type: bug | Status: new Priority: high | Milestone: 8.2.1 Component: Compiler | Version: 8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by slyfox): This should be self-contained example. You'll need a debugging compiler to build it. I'm not sure I've preserved kind structure of original module as core-lint detects all sorts of problems in this program. But the error is still the same. I was not able to remove foldl/foldr: the error goes away. {{{#!hs -- Bug.hs: {-# LANGUAGE TypeInType #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE RankNTypes #-} {- # OPTIONS_GHC -Werror #-} {-# OPTIONS_GHC -g -O2 #-} module Bug (bug) where import Unsafe.Coerce (unsafeCoerce) undefined :: a undefined = undefined prelude_id :: a -> a prelude_id x = x prelude_foldl :: forall a b. (b -> a -> b) -> b -> [a] -> b prelude_foldl k z0 xs = prelude_foldr (\v fn -> (\z -> fn (k z v))) prelude_id xs z0 prelude_foldr :: (a -> b -> b) -> b -> [a] -> b prelude_foldr f z = go where go [] = z go (y:ys) = y `f` go ys data TypeRep (a :: k) where TrTyCon :: TypeRep (a :: k) TrApp :: forall k1 k2 (a :: k1 -> k2) (b :: k1). TypeRep (a b) data SomeTypeRep where SomeTypeRep :: forall k (a :: k). TypeRep a -> SomeTypeRep mkTrApp :: forall k1 k2 (a :: k1 -> k2) (b :: k1). TypeRep (a :: k1 -> k2) -> TypeRep (a b) mkTrApp TrTyCon = undefined mkTrApp TrApp = undefined bug :: [a] -> SomeTypeRep bug args = prelude_foldl applyTy tycon_app args where tycon_app = SomeTypeRep TrTyCon applyTy :: SomeTypeRep -> a -> SomeTypeRep applyTy (SomeTypeRep acc) _unused = SomeTypeRep (mkTrApp (unsafeCoerce acc)) }}} {{{ $ ../"inplace/bin/ghc-stage1" -hisuf hi -osuf o -hcsuf hc -static -H32m -O -g -dcore-lint -DDEBUG -Wall -this-unit-id base-4.10.0.0 -hide- all-packages -i.. -i../libraries/base/. -i../libraries/base/dist- install/build -I../libraries/base/dist-install/build -i../libraries/base /dist-install/build/./autogen -I../libraries/base/dist- install/build/./autogen -Ilibraries/base/include -optP- DOPTIMISE_INTEGER_GCD_LCM -optP-include -optP../libraries/base/dist- install/build/./autogen/cabal_macros.h -package-id rts -package-id ghc- prim-0.5.0.0 -package-id integer-gmp-1.0.0.1 -this-unit-id base -XHaskell2010 -O2 -no-user-package-db -rtsopts -Wno-trustworthy-safe -Wno-deprecated-flags -Wnoncanonical-monad-instances -odir libraries/base/dist-install/build -hidir libraries/base/dist-install/build -stubdir libraries/base/dist-install/build -split-sections -dynamic-too -c Bug.hs -fforce-recomp ghc-stage1: panic! (the 'impossible' happened) (GHC version 8.3.20170506 for x86_64-unknown-linux): ASSERT failed! optCoercion changed types! in_co: (TypeRep (UnsafeCo nominal Any (Any -> Any)) (UnsafeCo nominal (Any Any) Any))_R in_ty1: TypeRep (Any Any) in_ty2: TypeRep Any out_co: (TypeRep (UnsafeCo nominal Any (Any -> Any)) <Any>_N)_R out_ty1: TypeRep Any out_ty2: TypeRep Any subst: [TCvSubst In scope: InScope {wild_00 wild_Xu y_a1t ys_a1u a_ayL $krep_aAd $krep_aAe $krep_aAf $krep_aAg $krep_aAh $krep_aAi $krep_aAj $krep_aAk $krep_aAl $krep_aAm undefined $tcTypeRep $tcSomeTypeRep $tc'TrTyCon $tc'TrApp $trModule $tc'SomeTypeRep bug $trModule_sB4 $trModule_sB5 $trModule_sB6 $trModule_sB7 $tcTypeRep_sB8 $tcTypeRep_sB9 $krep_sBa $krep_sBb $tc'TrTyCon_sBc $tc'TrTyCon_sBd $krep_sBe $krep_sBf $tc'TrApp_sBg $tc'TrApp_sBh $tcSomeTypeRep_sBi $tcSomeTypeRep_sBj $tc'SomeTypeRep_sBk $tc'SomeTypeRep_sBl poly_go_sBz lvl_sBA $spoly_go_sCu sc_sCv sc_sCw $spoly_go_sCx} Type env: [] Co env: []] Call stack: CallStack (from HasCallStack): prettyCurrentCallStack, called at compiler/utils/Outputable.hs:1134:58 in ghc:Outputable callStackDoc, called at compiler/utils/Outputable.hs:1189:22 in ghc:Outputable assertPprPanic, called at compiler/types/OptCoercion.hs:96:188 in ghc:OptCoercion 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/utils/Outputable.hs:1187:5 in ghc:Outputable assertPprPanic, called at compiler/types/OptCoercion.hs:96:188 in ghc:OptCoercion }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13658#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13658: Assertion failure on HEAD: "optCoercion changed types!" -------------------------------------+------------------------------------- Reporter: bgamari | Owner: (none) Type: bug | Status: new Priority: high | Milestone: 8.2.1 Component: Compiler | Version: 8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by bgamari): The problem here appears to be that the `TyConAppCo` case of `OptCoercion.opt_univ` fires, {{{#!hs opt_univ env sym prov role oty1 oty2 | Just (tc1, tys1) <- splitTyConApp_maybe oty1 , Just (tc2, tys2) <- splitTyConApp_maybe oty2 , tc1 == tc2 = pprTrace "opt_univ(tyconapp)" (ppr tc1 <+> ppr tys1 $$ ppr tc2 <+> ppr tys2) $ let roles = tyConRolesX role tc1 arg_cos = zipWith3 (mkUnivCo prov) roles tys1 tys2 arg_cos' = zipWith (opt_co4 env sym False) roles arg_cos in mkTyConAppCo role tc1 arg_cos' }}} This emits, {{{ opt_univ(tyconapp) Any [Any -> Any, Any] Any [Any -> Any] }}} for the problematic coercion. The issue here is that we end up dropping the second argument from `tys1` due to the `zipWith`. I suspect we want to add a guard to this equation asserting that `tys1` and `tys2` are of equal lengths. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13658#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13658: Assertion failure on HEAD: "optCoercion changed types!" -------------------------------------+------------------------------------- Reporter: bgamari | Owner: (none) Type: bug | Status: new Priority: high | Milestone: 8.2.1 Component: Compiler | Version: 8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by slyfox): Slightly shorter example without foldl: {{{#!hs {-# LANGUAGE TypeInType #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE BangPatterns #-} {- # OPTIONS_GHC -Werror #-} {-# OPTIONS_GHC -g -O2 #-} module Bug (bug) where -- import GHC.Base (seq) import Unsafe.Coerce (unsafeCoerce) undefined :: a undefined = undefined data TypeRep (a :: k) where TrTyCon :: TypeRep (a :: k) TrApp :: forall k1 k2 (a :: k1 -> k2) (b :: k1). TypeRep (a b) data SomeTypeRep where SomeTypeRep :: forall k (a :: k). TypeRep a -> SomeTypeRep mkTrApp :: forall k1 k2 (a :: k1 -> k2) (b :: k1). TypeRep (a :: k1 -> k2) -> TypeRep (a b) mkTrApp TrTyCon = undefined mkTrApp TrApp = undefined bug :: SomeTypeRep -- bug = f x -- this works bug = f (f x) where x = SomeTypeRep TrTyCon f :: SomeTypeRep -> SomeTypeRep f (SomeTypeRep acc) = SomeTypeRep (mkTrApp (unsafeCoerce acc)) }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13658#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13658: Assertion failure on HEAD: "optCoercion changed types!" -------------------------------------+------------------------------------- Reporter: bgamari | Owner: (none) Type: bug | Status: new Priority: high | Milestone: 8.2.1 Component: Compiler | Version: 8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by bgamari): Indeed this appears to resolve the issue, {{{#!patch diff --git a/compiler/types/OptCoercion.hs b/compiler/types/OptCoercion.hs index b1aa646a97..03e1d6cb5f 100644 --- a/compiler/types/OptCoercion.hs +++ b/compiler/types/OptCoercion.hs @@ -378,6 +378,7 @@ opt_univ env sym prov role oty1 oty2 | Just (tc1, tys1) <- splitTyConApp_maybe oty1 , Just (tc2, tys2) <- splitTyConApp_maybe oty2 , tc1 == tc2 + , equalLength tys1 tys2 -- NB: prov must not be the two interesting ones (ProofIrrel & Phantom); -- Phantom is already taken care of, and ProofIrrel doesn't relate tyconapps = let roles = tyConRolesX role tc1 }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13658#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13658: Assertion failure on HEAD: "optCoercion changed types!" -------------------------------------+------------------------------------- Reporter: bgamari | Owner: (none) Type: bug | Status: patch Priority: high | Milestone: 8.2.1 Component: Compiler | Version: 8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D3545 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * status: new => patch * differential: => Phab:D3545 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13658#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13658: Assertion failure on HEAD: "optCoercion changed types!"
-------------------------------------+-------------------------------------
Reporter: bgamari | Owner: (none)
Type: bug | Status: patch
Priority: high | Milestone: 8.2.1
Component: Compiler | Version: 8.3
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture:
| Unknown/Multiple
Type of failure: None/Unknown | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s): Phab:D3545
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by Ben Gamari

#13658: Assertion failure on HEAD: "optCoercion changed types!" -------------------------------------+------------------------------------- Reporter: bgamari | Owner: (none) Type: bug | Status: merge Priority: high | Milestone: 8.2.1 Component: Compiler | Version: 8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D3545 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * status: patch => merge -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13658#comment:9 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13658: Assertion failure on HEAD: "optCoercion changed types!" -------------------------------------+------------------------------------- Reporter: bgamari | Owner: (none) Type: bug | Status: merge Priority: high | Milestone: 8.2.1 Component: Compiler | Version: 8.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: | simplCore/should_compile/T13658 Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D3545 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * testcase: => simplCore/should_compile/T13658 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13658#comment:10 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13658: Assertion failure on HEAD: "optCoercion changed types!" -------------------------------------+------------------------------------- Reporter: bgamari | Owner: (none) Type: bug | Status: closed Priority: high | Milestone: 8.2.1 Component: Compiler | Version: 8.3 Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: | simplCore/should_compile/T13658 Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D3545 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * status: merge => closed * resolution: => fixed Comment: Merged with ca76ae071d9ffb4f36fc4b59ef9fa3bf3dfe2b8a. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13658#comment:11 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC