Simon Peyton Jones pushed to branch wip/T27844 at Glasgow Haskell Compiler / GHC

Commits:

4 changed files:

Changes:

  • compiler/GHC/Tc/Solver/Equality.hs
    ... ... @@ -1824,7 +1824,7 @@ Ticket #10009, a very nasty example:
    1824 1824
         g :: forall a. (UnF (F a) ~ a) => a -> ()
    
    1825 1825
         g _ = f (undefined :: F a)
    
    1826 1826
     
    
    1827
    -For g we get [G]  g1 : UnF (F a) ~ a
    
    1827
    +For g we get [G] g1 : UnF (F a) ~ a
    
    1828 1828
                  [W] w1 : UnF (F beta) ~ beta
    
    1829 1829
                  [W] w2 : F a ~ F beta
    
    1830 1830
     
    
    ... ... @@ -2213,6 +2213,9 @@ canEqCanLHSFinish_try_unification ev eq_rel swapped lhs rhs
    2213 2213
                   -- We have a touchable unification variable on the left,
    
    2214 2214
                   -- and the top-shape check succeeded. These are both guaranteed
    
    2215 2215
                   -- by the fact that simpleUnifyCheck did not return SUC_CannotUnify.
    
    2216
    +              --
    
    2217
    +              -- Next, do a fully `checkTyEqRhs` check to see if we can unify
    
    2218
    +              -- See Note [checkTyEqRhs] in GHC.Tc.Utils.Unify
    
    2216 2219
                   do  { let flags = unifyingLHSMetaTyVar_TEFTask ev lhs_tv
    
    2217 2220
                       ; check_result <- wrapTcS (checkTyEqRhs flags rhs)
    
    2218 2221
                       ; traceTcS "canEqCanLHSFinish_try_unification" $
    
    ... ... @@ -2229,21 +2232,19 @@ canEqCanLHSFinish_try_unification ev eq_rel swapped lhs rhs
    2229 2232
       = finish_no_unify
    
    2230 2233
     
    
    2231 2234
       where
    
    2232
    -    -- We can't unify, but we can see if this equality can go in the inert set
    
    2233
    -    -- and be used to rewrite other constraints.
    
    2234
    -    finish_no_unify = canEqCanLHSFinish_no_unification ev eq_rel swapped lhs rhs
    
    2235
    -
    
    2236
    -    -- We can't unify, and this equality should not be used to rewrite
    
    2237
    -    -- other constraints (e.g. because it has an occurs check).
    
    2238
    -    -- So add it to the inert Irreds.
    
    2239
    ---    try_irred reason = tryIrredInstead reason ev eq_rel swapped lhs rhs
    
    2240
    -
    
    2241
    -    -- We can't unify as-is, and want to flip the equality around.
    
    2242
    -    -- Example: alpha ~ F tys, flip it around to become the canonical
    
    2243
    -    -- equality f tys ~ alpha.
    
    2244
    ---    swap_and_finish tv can_rhs = swapAndFinish ev eq_rel swapped (mkTyVarTy tv) can_rhs
    
    2235
    +    -- finish_no_unify: abandon the attempt to unify, and instead hand off to
    
    2236
    +    -- canEqCanLHSFinish_no_unification
    
    2237
    +    -- First, though, if we have (tv ~ F tys)
    
    2238
    +    --        swap it over so we get (F tys ~ tv), with `F tys` on the LHS
    
    2239
    +    -- See Note [Orienting TyVarLHS/TyFamLHS]
    
    2240
    +    finish_no_unify
    
    2241
    +      | TyVarLHS lhs_tv <- lhs
    
    2242
    +      , Just can_rhs <- canTyFamEqLHS_maybe rhs
    
    2243
    +      = canEqCanLHSFinish_no_unification ev eq_rel (flipSwap swapped) can_rhs (mkTyVarTy lhs_tv)
    
    2244
    +      | otherwise
    
    2245
    +      = canEqCanLHSFinish_no_unification ev eq_rel swapped lhs rhs
    
    2245 2246
     
    
    2246
    -    -- Phew! Finally!  We can unify; go ahead and do so.
    
    2247
    +    -- do_unification: Phew! Finally!  We can unify; go ahead and do so.
    
    2247 2248
         do_unification tv rhs_redn =
    
    2248 2249
           do { -- In the common case where rhs_redn is Refl, we don't need to rewrite
    
    2249 2250
                -- the evidence, even if swapped=IsSwapped.   Suppose the original was
    
    ... ... @@ -2280,39 +2281,41 @@ canEqCanLHSFinish_try_unification ev eq_rel swapped lhs rhs
    2280 2281
              ; return (Stop new_ev (text "Solved by unification")) }
    
    2281 2282
     
    
    2282 2283
     ---------------------------
    
    2283
    --- Unification is off the table
    
    2284 2284
     canEqCanLHSFinish_no_unification ev eq_rel swapped lhs rhs
    
    2285
    +  -- We can't unify, but we can see if this equality can go in the inert set
    
    2286
    +  -- and be used to rewrite other constraints.
    
    2287
    +  -- See Note [Equalities where unification fails]
    
    2285 2288
       = do { -- Do checkTypeEq to guarantee (TyEq:OC), (TyEq:F)
    
    2286 2289
              -- Must do the occurs check even on tyvar/tyvar equalities,
    
    2287 2290
              -- in case have  x ~ (y :: ..x...); this is #12593.
    
    2288 2291
            ; check_result <- checkTypeEq ev eq_rel lhs rhs
    
    2289
    -
    
    2290
    -       ; let lhs_ty = canEqLHSType lhs
    
    2291 2292
            ; case check_result of
    
    2292 2293
                 PuFail reason
    
    2293
    -
    
    2294
    -              -- If we had F a ~ G (F a), which gives an occurs check,
    
    2295
    -              -- then swap it to G (F a) ~ F a, which does not
    
    2296
    -              -- See Note [Orienting TyVarLHS/TyFamLHS]
    
    2297
    -              -- ToDo: fix me  ... also needed for a ~ G (F a)
    
    2298
    -              -- However `swap_for_size` above will orient it with (G (F a)) on
    
    2299
    -              -- the left anwyway.  `swap_for_rewriting` "wins", but that doesn't
    
    2300
    -              -- matter: in the occurs check case swap_for_rewriting will be moot.
    
    2301
    -              -- TL;DR: the next four lines of code are redundant
    
    2302
    -              -- I'm leaving them here in case they become relevant again
    
    2303
    -              | Just can_rhs <- canTyFamEqLHS_maybe rhs
    
    2304
    -              , reason `cterHasOnlyProblem` cteSolubleOccurs
    
    2305
    -              -> finishWith ev eq_rel (flipSwap swapped) can_rhs lhs_ty
    
    2306
    -
    
    2307 2294
                   | reason `cterHasOnlyProblems` do_not_prevent_rewriting
    
    2308
    -              -> finishWith ev eq_rel swapped lhs rhs
    
    2295
    +              ->   do { new_ev <- rewriteEqEvidenceSwapOnly ev eq_rel swapped lhs rhs
    
    2296
    +                      ; continueWith $ Right $
    
    2297
    +                        EqCt { eq_ev  = new_ev, eq_eq_rel = eq_rel
    
    2298
    +                             , eq_lhs = lhs , eq_rhs = rhs } }
    
    2309 2299
     
    
    2310 2300
                   | otherwise
    
    2311
    -              -> tryIrredInstead reason ev eq_rel swapped lhs rhs
    
    2301
    +              -> -- We can't unify, and this equality should not be used to rewrite
    
    2302
    +                 -- other constraints (e.g. because it has an occurs check).
    
    2303
    +                 -- So add it to the inert Irreds.
    
    2304
    +                 do { new_ev <- rewriteEqEvidenceSwapOnly ev eq_rel swapped lhs rhs
    
    2305
    +                    ; finishCanWithIrred (NonCanonicalReason reason) new_ev }
    
    2312 2306
     
    
    2313 2307
                 PuOK new_eqs rhs_redn
    
    2314
    -              -> do { emitWork new_eqs
    
    2308
    +              -> -- Claim: there should be no new_eqs
    
    2309
    +                 -- The non-unification check does non promotion nor type-family
    
    2310
    +                 -- flattening for Wanteds
    
    2311
    +                 assertPpr (isEmptyCts new_eqs)
    
    2312
    +                    (vcat [ text "new_eqs"  <+> ppr new_eqs
    
    2313
    +                          , text "rhs_redn" <+> ppr rhs_redn
    
    2314
    +                          , text "lhs"      <+> ppr lhs
    
    2315
    +                          , text "rhs"      <+> ppr rhs ]) $
    
    2316
    +                 do { emitWork new_eqs
    
    2315 2317
                         ; let new_holes = rewriterSetFromCts new_eqs
    
    2318
    +                          lhs_ty    = canEqLHSType lhs
    
    2316 2319
                               lhs_redn  = mkReflRedn (eqRelRole eq_rel) lhs_ty
    
    2317 2320
                         ; new_ev <- rewriteEqEvidence ev swapped lhs_redn rhs_redn new_holes
    
    2318 2321
     
    
    ... ... @@ -2336,45 +2339,7 @@ canEqCanLHSFinish_no_unification ev eq_rel swapped lhs rhs
    2336 2339
     do_not_prevent_rewriting :: CheckTyEqResult
    
    2337 2340
     do_not_prevent_rewriting = cteProblem cteSkolemEscape S.<>
    
    2338 2341
                                cteProblem cteConcrete
    
    2339
    -
    
    2340
    -----------------------
    
    2341
    -finishWith :: CtEvidence -> EqRel -> SwapFlag
    
    2342
    -           -> CanEqLHS -> TcType
    
    2343
    -           -> TcS (StopOrContinue (Either unused EqCt))
    
    2344
    -finishWith ev eq_rel swapped can_lhs rhs_ty
    
    2345
    -  = do { new_ev <- rewriteEqEvidenceSwapOnly ev eq_rel swapped can_lhs rhs_ty
    
    2346
    -       ; continueWith $ Right $
    
    2347
    -         EqCt { eq_ev  = new_ev, eq_eq_rel = eq_rel
    
    2348
    -              , eq_lhs = can_lhs , eq_rhs = rhs_ty } }
    
    2349
    -
    
    2350
    -{-
    
    2351
    -swapAndFinish :: CtEvidence -> EqRel -> SwapFlag
    
    2352
    -              -> TcType -> CanEqLHS      -- ty ~ F tys
    
    2353
    -              -> TcS (StopOrContinue (Either unused EqCt))
    
    2354
    --- We have an equality alpha ~ F tys, that we can't unify e.g because 'tys'
    
    2355
    --- mentions alpha, it would not be a canonical constraint as-is.
    
    2356
    --- We want to flip it to (F tys ~ a), whereupon it is canonical
    
    2357
    -swapAndFinish ev eq_rel swapped lhs_ty can_rhs
    
    2358
    -  = do { new_ev <- rewriteEqEvidenceSwapOnly ev eq_rel (flipSwap swapped) can_rhs lhs_ty
    
    2359
    -       ; continueWith $ Right $
    
    2360
    -         EqCt { eq_ev  = new_ev, eq_eq_rel = eq_rel
    
    2361
    -              , eq_lhs = can_rhs, eq_rhs = lhs_ty } }
    
    2362
    --}
    
    2363
    -
    
    2364 2342
     ----------------------
    
    2365
    -tryIrredInstead :: CheckTyEqResult -> CtEvidence
    
    2366
    -                -> EqRel -> SwapFlag -> CanEqLHS -> TcType
    
    2367
    -                -> TcS (StopOrContinue (Either IrredCt unused))
    
    2368
    --- We have a non-canonical equality
    
    2369
    --- We still swap it if 'swapped' says so, so that it is oriented
    
    2370
    --- in the direction that the error-reporting machinery
    
    2371
    --- expects it; e.g.  (m ~ t m) rather than (t m ~ m)
    
    2372
    --- This is not very important, and only affects error reporting.
    
    2373
    -tryIrredInstead reason ev eq_rel swapped lhs rhs
    
    2374
    -  = do { traceTcS "cantMakeCanonical" (ppr reason $$ ppr lhs $$ ppr rhs)
    
    2375
    -       ; new_ev <- rewriteEqEvidenceSwapOnly ev eq_rel swapped lhs rhs
    
    2376
    -       ; finishCanWithIrred (NonCanonicalReason reason) new_ev }
    
    2377
    -
    
    2378 2343
     finishCanWithIrred :: CtIrredReason -> CtEvidence
    
    2379 2344
                        -> TcS (StopOrContinue (Either IrredCt a))
    
    2380 2345
     finishCanWithIrred reason ev
    
    ... ... @@ -2394,8 +2359,26 @@ canEqReflexive ev eq_rel ty
    2394 2359
       = do { setEqIfWanted ev (mkReflCPH eq_rel ty)
    
    2395 2360
            ; stopWith ev "Solved by reflexivity" }
    
    2396 2361
     
    
    2397
    -{- Note [Equalities with heterogeneous kinds]
    
    2398
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2362
    +{- Note [Equalities where unification fails]
    
    2363
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2364
    +If we can't unify, but the equality is canonical, we still want to add it to the
    
    2365
    +inert set so that it can be used to rewrite other Wanteds.  (There are many
    
    2366
    +reasons by unification might not happen: the LHS is not a touchable unification
    
    2367
    +variable, it has rewriters, it's a Given etc.)
    
    2368
    +
    
    2369
    +* In this case we call `checkTypeEq`to see if it is canonical (which in turn
    
    2370
    +  calls `checkTypeEqRhs`).
    
    2371
    +
    
    2372
    +* Even if `checkTypeEq` returns `PuFail`, we may /still/ be able to add it to the inert
    
    2373
    +  set, depending on the reason for failure; examples are given in `do_not_prevent_rewriting`.
    
    2374
    +
    
    2375
    +* NB: we do /not/ try to take advantage of the case where `canEqCanLHSFinish_try_unification`
    
    2376
    +  has already called `checkTyEqRhs`; doing so caused #27844.   (The first call did fam-app
    
    2377
    +  breaking, which concealed a genuine occurs check.)  It's just too clever; better to do
    
    2378
    +  the simple thing.
    
    2379
    +
    
    2380
    +Note [Equalities with heterogeneous kinds]
    
    2381
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2399 2382
     What do we do when we have an equality
    
    2400 2383
     
    
    2401 2384
       (tv :: k1) ~ (rhs :: k2)
    
    ... ... @@ -3066,8 +3049,6 @@ inertsEqsCanDischarge inerts (EqCt { eq_lhs = lhs_w, eq_rhs = rhs_w
    3066 3049
     
    
    3067 3050
     inertsEqsCanDischarge _ _ = Nothing
    
    3068 3051
     
    
    3069
    -
    
    3070
    -
    
    3071 3052
     {- Note [Do not unify Givens]
    
    3072 3053
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    3073 3054
     Consider this GADT match
    
    ... ... @@ -3133,34 +3114,6 @@ Wrinkles:
    3133 3114
        we should kick out `co` so that we can now unify it, which might
    
    3134 3115
        unlock other stuff.  See `kickOutAfterFillingCoercionHole` in
    
    3135 3116
        GHC.Tc.Solver.Monad.
    
    3136
    -
    
    3137
    -Note [Solve by unification]
    
    3138
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    3139
    -If we solve
    
    3140
    -   alpha[n] ~ ty
    
    3141
    -by unification, there are two cases to consider
    
    3142
    -
    
    3143
    -* TouchableSameLevel: if the ambient level is 'n', then
    
    3144
    -  we can simply update alpha := ty, and do nothing else
    
    3145
    -
    
    3146
    -* TouchableOuterLevel free_metas n: if the ambient level is greater than
    
    3147
    -  'n' (the level of alpha), in addition to setting alpha := ty we must
    
    3148
    -  do two other things:
    
    3149
    -
    
    3150
    -  1. Promote all the free meta-vars of 'ty' to level n.  After all,
    
    3151
    -     alpha[n] is at level n, and so if we set, say,
    
    3152
    -          alpha[n] := Maybe beta[m],
    
    3153
    -     we must ensure that when unifying beta we do skolem-escape checks
    
    3154
    -     etc relevant to level n.  Simple way to do that: promote beta to
    
    3155
    -     level n.
    
    3156
    -
    
    3157
    -  2. Set the Unification Level Flag to record that a level-n unification has
    
    3158
    -     taken place. See Note [WhatUnifications] in GHC.Tc.Utils.Unify
    
    3159
    -
    
    3160
    -NB: TouchableSameLevel is just an optimisation for TouchableOuterLevel. Promotion
    
    3161
    -would be a no-op, and setting the unification flag unnecessarily would just
    
    3162
    -make the solver iterate more often.  (We don't need to iterate when unifying
    
    3163
    -at the ambient level because of the kick-out mechanism.)
    
    3164 3117
     -}
    
    3165 3118
     
    
    3166 3119
     
    

  • compiler/GHC/Tc/Solver/Monad.hs
    ... ... @@ -2333,7 +2333,7 @@ checkTypeEq ev eq_rel lhs rhs =
    2333 2333
         given_flags :: TyEqFlags TcM (TcTyVar, TcType)
    
    2334 2334
         given_flags = wanted_flags { tef_fam_app = mkTEFA_Break ev eq_rel BreakGiven }
    
    2335 2335
             -- TEFA_Break used for: [G] a ~ Maybe (F a)
    
    2336
    -        --                   or [W] F a ~ Maybe (F a)
    
    2336
    +        --                   or [G] F a ~ Maybe (F a)
    
    2337 2337
     
    
    2338 2338
         -- occ_prob: see Note [Occurs check and representational equality]
    
    2339 2339
         occ_prob = case eq_rel of
    

  • compiler/GHC/Tc/Utils/Unify.hs
    ... ... @@ -4203,7 +4203,7 @@ famAppArgFlags flags = case flags of
    4203 4203
     The key function `checkTyEqRhs ty_eq_flags rhs` is called on the
    
    4204 4204
     RHS of a type equality
    
    4205 4205
            lhs ~ rhs
    
    4206
    -and checks to see if `rhs` satisfies, or can be made to satisfy,
    
    4206
    +and checks to see if `rhs` satisfies, /or can be made to satisfy/,
    
    4207 4207
     invariants described by `ty_eq_flags`.  It can succeded or fail; in
    
    4208 4208
     the latter case it returns a `CheckTyEqResult` that describes why it
    
    4209 4209
     failed.
    
    ... ... @@ -4224,6 +4224,15 @@ Notably, it can check for things like:
    4224 4224
       * Concreteness error
    
    4225 4225
           e.g. alpha[conc] ~ r[sk]
    
    4226 4226
     
    
    4227
    +Note "or can be made to satisfy the preconditions"!  `checkTyEqRhs` may transform
    
    4228
    +the RHS by
    
    4229
    +
    
    4230
    +  * Turning a type-family application into a type variable, perhaps to avoid
    
    4231
    +    an occurs check.  See Note Note [Family applications in canonical constraints]
    
    4232
    +
    
    4233
    +  * Promoting any nested unification variables, in order to satisfy the
    
    4234
    +    level invariant. See Note [Promotion and level-checking]
    
    4235
    +
    
    4227 4236
     Its specific behaviour is governed by the `TyEqFlags` that are passed
    
    4228 4237
     to it; see Note [TyEqFlags].
    
    4229 4238
     
    

  • testsuite/tests/indexed-types/should_fail/T9662.stderr
    1 1
     T9662.hs:49:8: error: [GHC-25897]
    
    2
    -    • Couldn't match type ‘n’ with ‘Int’
    
    2
    +    • Couldn't match type ‘m’ with ‘Int’
    
    3 3
           Expected: Exp (((sh :. k) :. m) :. n)
    
    4 4
                     -> Exp (((sh :. m) :. n) :. k)
    
    5 5
             Actual: Exp
    
    6 6
                       (Tuple (((Atom a0 :. Atom Int) :. Atom Int) :. Atom Int))
    
    7 7
                     -> Exp
    
    8 8
                          (Plain (((Unlifted (Atom a0) :. Exp Int) :. Exp Int) :. Exp Int))
    
    9
    -      ‘n’ is a rigid type variable bound by
    
    9
    +      ‘m’ is a rigid type variable bound by
    
    10 10
             the type signature for:
    
    11 11
               test :: forall sh k m n.
    
    12 12
                       Shape (((sh :. k) :. m) :. n) -> Shape (((sh :. m) :. n) :. k)