[commit: ghc] ghc-8.2: OptCoercion: Ensure that TyConApps match in arity (ca76ae0)

git at git.haskell.org git at git.haskell.org
Tue May 16 01:12:20 UTC 2017


Repository : ssh://git@git.haskell.org/ghc

On branch  : ghc-8.2
Link       : http://ghc.haskell.org/trac/ghc/changeset/ca76ae071d9ffb4f36fc4b59ef9fa3bf3dfe2b8a/ghc

>---------------------------------------------------------------

commit ca76ae071d9ffb4f36fc4b59ef9fa3bf3dfe2b8a
Author: Ben Gamari <bgamari.foss at gmail.com>
Date:   Mon May 8 17:40:50 2017 -0400

    OptCoercion: Ensure that TyConApps match in arity
    
    Previously OptCoercion would potentially change the type of UnivCo
    coercions of the shape,
    ```
    co :: TyCon arg1 ... argN ~ TyCon arg1' ... argN'
    ```
    where the arities of the left and right applications differ. In this
    case we
    would try to zip the two argument lists, meaning that one would get
    truncated.
    
    One would think this could never happen since it implies we are
    applying the
    same TyCon to two different numbers of arguments. However, it does
    arise in the
    case of applications of the `Any` tycon, which arises from the
    typechecker (in
    `Data.Typeable.Internal`) where we end up with an `UnsafeCo`,
    ```
    co :: Any (Any -> Any) Any ~ Any (Any -> Any)
    ```
    
    Test Plan: Validate
    
    Reviewers: simonpj, austin, goldfire
    
    Reviewed By: simonpj
    
    Subscribers: rwbarton, thomie
    
    GHC Trac Issues: #13658
    
    Differential Revision: https://phabricator.haskell.org/D3545
    
    (cherry picked from commit 87ff5d4f0f812bad118600df0156f980b91191c5)


>---------------------------------------------------------------

ca76ae071d9ffb4f36fc4b59ef9fa3bf3dfe2b8a
 compiler/types/OptCoercion.hs | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/compiler/types/OptCoercion.hs b/compiler/types/OptCoercion.hs
index 7f96754..2e6c00e 100644
--- a/compiler/types/OptCoercion.hs
+++ b/compiler/types/OptCoercion.hs
@@ -364,6 +364,20 @@ opt_phantom env sym co
   where
     Pair ty1 ty2 = coercionKind co
 
+{- Note [Differing kinds]
+   ~~~~~~~~~~~~~~~~~~~~~~
+The two types may not have the same kind (although that would be very unusual).
+But even if they have the same kind, and the same type constructor, the number
+of arguments in a `CoTyConApp` can differ. Consider
+
+  Any :: forall k. k
+
+  Any * Int                      :: *
+  Any (*->*) Maybe Int  :: *
+
+Hence the need to compare argument lengths; see Trac #13658
+ -}
+
 opt_univ :: LiftingContext -> SymFlag -> UnivCoProvenance -> Role
          -> Type -> Type -> Coercion
 opt_univ env sym (PhantomProv h) _r ty1 ty2
@@ -378,6 +392,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 -- see Note [Differing kinds]
       -- 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



More information about the ghc-commits mailing list