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

Commits:

18 changed files:

Changes:

  • changelog.d/T27862
    1
    +section: compiler
    
    2
    +issues: #27862
    
    3
    +mrs: !16899
    
    4
    +synopsis:
    
    5
    +  Improve desugaring for pattern matching involving bang patterns.
    
    6
    +description:
    
    7
    +  We were generating poor code for pattern matching that used bang-patterns.
    
    8
    +  There should be no user-visible changes except more efficient code.

  • compiler/GHC/HsToCore/Match.hs
    ... ... @@ -23,7 +23,7 @@ where
    23 23
     import GHC.Prelude
    
    24 24
     import GHC.Platform
    
    25 25
     
    
    26
    -import {-#SOURCE#-} GHC.HsToCore.Expr (dsExpr)
    
    26
    +import {-#SOURCE#-} GHC.HsToCore.Expr (dsLExpr)
    
    27 27
     
    
    28 28
     import GHC.Types.Basic
    
    29 29
     
    
    ... ... @@ -32,6 +32,9 @@ import GHC.Hs
    32 32
     import GHC.Hs.Syn.Type
    
    33 33
     import GHC.Tc.Types.Evidence
    
    34 34
     import GHC.Tc.Utils.Monad
    
    35
    +
    
    36
    +import GHC.Rename.Utils (wrapGenSpan)
    
    37
    +
    
    35 38
     import GHC.HsToCore.Pmc
    
    36 39
     import GHC.HsToCore.Pmc.Utils
    
    37 40
     import GHC.HsToCore.Types ( LdiNablas )
    
    ... ... @@ -164,6 +167,57 @@ applying ``the mixture rule'' (SLPJ, p.~88) [which really {\em
    164 167
     un}mixes the equations], producing a list of equation-info
    
    165 168
     blocks, each block having as its first column patterns compatible with each other.
    
    166 169
     
    
    170
    +
    
    171
    +Note [Grouping equations]
    
    172
    +~~~~~~~~~~~~~~~~~~~~~~~~~
    
    173
    +Given a bunch of equations, `groupEquations` makes groups of consecutive equations,
    
    174
    +in which the first patern of each member of a group shares the same `PatGroup`
    
    175
    +category.
    
    176
    +   groupEquations :: [EquationInfoNE] -> [NonEmpty (PatGroup, EquationInfoNE)]
    
    177
    +Generally, in each NonEmpty (PatGroup, EquationInfoNE) element of the result list,
    
    178
    +all the PatGroup's will be the same (e.g. all PgCon or all PgAny). But see (GE1)
    
    179
    +for an exception.
    
    180
    +
    
    181
    +We will match each group in turn, with failure falling through into the
    
    182
    +next group.  For example
    
    183
    +   f x           0 = ...  -- (E1)
    
    184
    +   f !y          1 = ...  -- (E2)
    
    185
    +   f y           2 = ...  -- (E3)
    
    186
    +   f (Just True) _ = ...  -- (E4)
    
    187
    +   f Nothing     _ = ...  -- (E5)
    
    188
    +   f x           4 = ...  -- (E6)
    
    189
    +   f _           5 = ...  -- (E7)
    
    190
    +
    
    191
    +The initial groups are
    
    192
    +   [E1]
    
    193
    +   [E2,E3, .. E7]
    
    194
    +because the bang dominates all the the equations that follow; see (GE1) below.   Once we
    
    195
    +get inside the bang we'll again do a `groupEquations` and get groups
    
    196
    +   [E2,E3]   -- PgAny
    
    197
    +   [E4,E5]   -- PgCon
    
    198
    +   [E6,E7]   -- PgAny
    
    199
    +
    
    200
    +Wrinkles
    
    201
    +
    
    202
    +(GE1) A bang pattern captures all the equations that follow.  For example, given
    
    203
    +    g !x   (p,True)  = ...(E1)...
    
    204
    +    g x    (p,False) = ...(E2)...
    
    205
    +    g True v         = ...(E3)...
    
    206
    +  we want to get the (p,True) and (p,False) patterns in a single group so we can share
    
    207
    +  the matching on the pair, and we can't do that if we make two groups for the top
    
    208
    +  level match.  See #27862.  So we want these groups:
    
    209
    +
    
    210
    +       Group 1:   !x (p,True)  -> ..E1..
    
    211
    +                  x  (p,False) -> ..E2..
    
    212
    +
    
    213
    +       Group 2:   True v       -> ..E3..
    
    214
    +
    
    215
    +  A consequence is that in a group with a `PgBang` category, such as Group 1 above,
    
    216
    +  the second and subsequent equations may not be headed with a bang-pattern; indeed
    
    217
    +  their PatGroup classifiers may differ from each other.  Hence in `matchBangs`, the
    
    218
    +  function `getBangPat` is not surprised to see a non-bang pattern.
    
    219
    +
    
    220
    +(GE2) Overloaded literals: see Note [Grouping overloaded literal patterns]
    
    167 221
     -}
    
    168 222
     
    
    169 223
     match :: [MatchId]        -- ^ Variables rep\'ing the exprs we\'re matching with
    
    ... ... @@ -176,17 +230,25 @@ match [] ty eqns = maybe (assertPprPanic (ppr ty)) combineEqnRhss $ nonEmpty eqn
    176 230
     match (v:vs) ty eqns    -- Eqns can be empty, but each equation is nonempty
    
    177 231
       = assertPpr (all (isInternalName . idName . matchId) vars) (ppr vars) $
    
    178 232
         do  { dflags <- getDynFlags
    
    233
    +
    
    234
    +        -- Tidy the first pattern, to remove unnecessary diversity,
    
    235
    +        -- generating auxiliary bindings if necessary
    
    236
    +        -- See Note [Tidying patterns]
    
    179 237
             ; let platform = targetPlatform dflags
    
    180
    -                -- Tidy the first pattern, generating
    
    181
    -                -- auxiliary bindings if necessary
    
    182 238
             ; (aux_binds, tidy_eqns) <- mapAndUnzipM (tidyEqnInfo v) eqns
    
    183
    -                -- Group the equations and match each group in turn
    
    184
    -        ; let grouped = groupEquations platform tidy_eqns
    
    185 239
     
    
    186
    -         -- print the view patterns that are commoned up to help debug
    
    187
    -        ; whenDOptM Opt_D_dump_view_pattern_commoning (debug grouped)
    
    240
    +        -- Group the equations and match each group in turn
    
    241
    +        -- See Note [Grouping equations]
    
    242
    +        ; let grouped ::  [NonEmpty (PatGroup, EquationInfoNE)]
    
    243
    +              grouped = groupEquations platform tidy_eqns
    
    244
    +
    
    245
    +         -- Print the view patterns that are commoned up to help debug
    
    246
    +        ; whenDOptM Opt_D_dump_view_pattern_commoning (debugViewPats grouped)
    
    188 247
     
    
    248
    +        -- Match each group separately
    
    189 249
             ; match_results <- match_groups grouped
    
    250
    +
    
    251
    +        -- And combine the results
    
    190 252
             ; return $ foldr (.) id aux_binds <$>
    
    191 253
                 foldr1 combineMatchResults match_results
    
    192 254
             }
    
    ... ... @@ -197,10 +259,11 @@ match (v:vs) ty eqns -- Eqns can be empty, but each equation is nonempty
    197 259
         dropGroup = fmap snd
    
    198 260
     
    
    199 261
         match_groups :: [NonEmpty (PatGroup,EquationInfoNE)] -> DsM (NonEmpty (MatchResult CoreExpr))
    
    200
    -    match_groups [] = matchEmpty v ty
    
    262
    +    match_groups []     = matchEmpty v ty
    
    201 263
         match_groups (g:gs) = mapM match_group $ g :| gs
    
    202 264
     
    
    203 265
         match_group :: NonEmpty (PatGroup,EquationInfoNE) -> DsM (MatchResult CoreExpr)
    
    266
    +    -- See Note [Grouping equations]
    
    204 267
         match_group eqns@((group,_) :| _)
    
    205 268
             = case group of
    
    206 269
                 PgCon {}  -> matchConFamily  vars ty (ne $ subGroupUniq [(c,e) | (PgCon c, e) <- eqns'])
    
    ... ... @@ -218,19 +281,23 @@ match (v:vs) ty eqns -- Eqns can be empty, but each equation is nonempty
    218 281
                   Just nel -> nel
    
    219 282
                   Nothing -> pprPanic "match match_group" $ text "Empty result should be impossible since input was non-empty"
    
    220 283
     
    
    221
    -    -- FIXME: we should also warn about view patterns that should be
    
    222
    -    -- commoned up but are not
    
    223 284
     
    
    224
    -    -- print some stuff to see what's getting grouped
    
    225
    -    -- use -dppr-debug to see the resolution of overloaded literals
    
    226
    -    debug eqns =
    
    227
    -        let gs = map (\group -> foldr (\ (p,_) -> \acc ->
    
    228
    -                                           case p of PgView e _ -> e:acc
    
    229
    -                                                     _ -> acc) [] group) eqns
    
    230
    -            maybeWarn [] = return ()
    
    231
    -            maybeWarn l  = diagnosticDs (DsAggregatedViewExpressions l)
    
    232
    -        in
    
    233
    -          maybeWarn $ filter (not . null) gs
    
    285
    +debugViewPats :: [NonEmpty (PatGroup, EquationInfoNE)] -> DsM ()
    
    286
    +-- print some stuff to see what's getting grouped
    
    287
    +-- use -dppr-debug to see the resolution of overloaded literals
    
    288
    +--
    
    289
    +-- FIXME: we should also warn about view patterns that should be
    
    290
    +-- commoned up but are not
    
    291
    +debugViewPats eqns
    
    292
    +  | null gs   = return ()
    
    293
    +  | otherwise = diagnosticDs (DsAggregatedViewExpressions gs)
    
    294
    +  where
    
    295
    +    gs :: [[LHsExpr GhcTc]]
    
    296
    +    gs = mapMaybe gather_views eqns
    
    297
    +
    
    298
    +    gather_views :: NonEmpty (PatGroup, EquationInfoNE) -> Maybe [LHsExpr GhcTc]
    
    299
    +    gather_views ((PgView e _, _) :| vs) = Just (e : [ e' | (PgView e' _, _) <- vs ])
    
    300
    +    gather_views _                       = Nothing
    
    234 301
     
    
    235 302
     matchEmpty :: MatchId -> Type -> DsM (NonEmpty (MatchResult CoreExpr))
    
    236 303
     -- See Note [Empty case alternatives]
    
    ... ... @@ -246,6 +313,7 @@ matchVariables :: NonEmpty MatchId -> Type -> NonEmpty EquationInfoNE -> DsM (Ma
    246 313
     matchVariables (_ :| vars) ty eqns = match vars ty $ NE.toList $ shiftEqns eqns
    
    247 314
     
    
    248 315
     matchBangs :: NonEmpty MatchId -> Type -> NonEmpty EquationInfoNE -> DsM (MatchResult CoreExpr)
    
    316
    +-- See (GE1) in Note [Grouping equations]
    
    249 317
     matchBangs (var :| vars) ty eqns
    
    250 318
       = do  { match_result <- match (var:vars) ty $ NE.toList $
    
    251 319
                 decomposeFirstPat getBangPat <$> eqns
    
    ... ... @@ -275,37 +343,44 @@ matchView (var :| vars) ty eqns@(eqn1 :| _)
    275 343
              -- to figure out the type of the fresh variable
    
    276 344
              let TcViewPat viewExpr pat = firstPat eqn1
    
    277 345
              -- do the rest of the compilation
    
    278
    -        ; let pat_ty' = hsPatType pat
    
    346
    +        ; let pat_ty' = hsLPatType pat
    
    279 347
             ; var' <- newUniqueId (matchId var) (matchIdMult var) pat_ty'
    
    280 348
             ; match_result <- match (mkMatchId var' : vars) ty $ NE.toList $
    
    281 349
                 decomposeFirstPat getViewPat <$> eqns
    
    282 350
              -- compile the view expressions
    
    283
    -        ; viewExpr' <- dsExpr viewExpr
    
    351
    +        ; viewExpr' <- dsLExpr viewExpr
    
    284 352
             ; return (mkViewMatchResult var'
    
    285 353
                         (mkCoreApp viewExpr' (matchIdExpr var))
    
    286 354
                         match_result) }
    
    287 355
     
    
    288 356
     -- decompose the first pattern and leave the rest alone
    
    289
    -decomposeFirstPat :: (Pat GhcTc -> Pat GhcTc) -> EquationInfoNE -> EquationInfoNE
    
    290
    -decomposeFirstPat extract eqn@(EqnMatch { eqn_pat = pat }) = eqn{eqn_pat = fmap extract pat}
    
    357
    +decomposeFirstPat :: (LPat GhcTc -> LPat GhcTc) -> EquationInfoNE -> EquationInfoNE
    
    358
    +decomposeFirstPat extract eqn@(EqnMatch { eqn_pat = pat }) = eqn { eqn_pat = extract pat }
    
    291 359
     decomposeFirstPat _ (EqnDone {}) = panic "decomposeFirstPat"
    
    292 360
     
    
    293
    -getCoPat, getBangPat, getViewPat :: Pat GhcTc -> Pat GhcTc
    
    294
    -getCoPat (XPat (CoPat _ pat _)) = pat
    
    295
    -getCoPat _                   = panic "getCoPat"
    
    296
    -getBangPat (BangPat _ pat  ) = unLoc pat
    
    297
    -getBangPat _                 = panic "getBangPat"
    
    298
    -getViewPat (TcViewPat _ pat) = pat
    
    299
    -getViewPat _                 = panic "getViewPat"
    
    361
    +getCoPat :: LPat GhcTc -> LPat GhcTc
    
    362
    +getCoPat (L loc (XPat (CoPat _ pat _))) = L loc pat
    
    363
    +getCoPat _ = panic "getCoPat"
    
    364
    +
    
    365
    +getViewPat :: LPat GhcTc -> LPat GhcTc
    
    366
    +getViewPat (L _ (TcViewPat _ pat)) = pat
    
    367
    +getViewPat _                       = panic "getViewPat"
    
    368
    +
    
    369
    +getBangPat :: LPat GhcTc -> LPat GhcTc
    
    370
    +-- See (GE1) in Note [Grouping equations] for why it's expected
    
    371
    +-- that `getBangPat` may be passed a non-bang pattern.
    
    372
    +getBangPat (L _ (BangPat _ pat)) = pat
    
    373
    +getBangPat pat                   = pat
    
    374
    +
    
    300 375
     
    
    301 376
     -- | Use this pattern synonym to match on a 'ViewPat'.
    
    302 377
     --
    
    303 378
     -- N.B.: View patterns can occur inside HsExpansions.
    
    304
    -pattern TcViewPat :: HsExpr GhcTc -> Pat GhcTc -> Pat GhcTc
    
    379
    +pattern TcViewPat :: LHsExpr GhcTc -> LPat GhcTc -> Pat GhcTc
    
    305 380
     pattern TcViewPat viewExpr pat <- (getTcViewPat -> (viewExpr, pat))
    
    306 381
     
    
    307
    -getTcViewPat :: Pat GhcTc -> (HsExpr GhcTc, Pat GhcTc)
    
    308
    -getTcViewPat (ViewPat _ viewLExpr pat)  = (unLoc viewLExpr, unLoc pat)
    
    382
    +getTcViewPat :: Pat GhcTc -> (LHsExpr GhcTc, LPat GhcTc)
    
    383
    +getTcViewPat (ViewPat _ viewLExpr pat)  = (viewLExpr, pat)
    
    309 384
     getTcViewPat (XPat (ExpansionPat  _ p)) = getTcViewPat p
    
    310 385
     getTcViewPat p = pprPanic "getTcViewPat" (ppr p)
    
    311 386
     
    
    ... ... @@ -334,41 +409,48 @@ See also the "lifted case" discussion in Note [Case elimination] in GHC.Core.Opt
    334 409
     *                                                                      *
    
    335 410
     ************************************************************************
    
    336 411
     
    
    337
    -Tidy up the leftmost pattern in an @EquationInfo@, given the variable @v@
    
    338
    -which will be scrutinised.
    
    339
    -
    
    340
    -This makes desugaring the pattern match simpler by transforming some of
    
    341
    -the patterns to simpler forms. (Tuples to Constructor Patterns)
    
    412
    +Note [Tidying patterns]
    
    413
    +~~~~~~~~~~~~~~~~~~~~~~~
    
    414
    +`tidyEqninfo` tidies up the leftmost pattern in an `EquationInfo`, given the
    
    415
    +variable `v` which will be scrutinised. This makes desugaring the pattern match
    
    416
    +simpler by transforming some of the patterns to simpler forms. (Tuples to
    
    417
    +Constructor Patterns)
    
    342 418
     
    
    343 419
     Among other things in the resulting Pattern:
    
    344 420
     * Variables and irrefutable(lazy) patterns are replaced by Wildcards
    
    345 421
     * As patterns are replaced by the patterns they wrap.
    
    346 422
     
    
    347 423
     The bindings created by the above patterns are put into the returned wrapper
    
    348
    -instead.
    
    349
    -
    
    350
    -This means a definition of the form:
    
    424
    +instead.  This means a definition of the form:
    
    351 425
       f x = rhs
    
    352 426
     when called with v get's desugared to the equivalent of:
    
    353 427
       let x = v
    
    354 428
       in
    
    355 429
       f _ = rhs
    
    356 430
     
    
    357
    -The same principle holds for as patterns (@) and
    
    358
    -irrefutable/lazy patterns (~).
    
    359
    -In the case of irrefutable patterns the irrefutable pattern is pushed into
    
    360
    -the binding.
    
    431
    +The same principle holds for as patterns (@) and irrefutable/lazy patterns (~).  In
    
    432
    +the case of irrefutable patterns the irrefutable pattern is pushed into the binding.
    
    361 433
     
    
    362
    -Pattern Constructors which only represent syntactic sugar are converted into
    
    363
    -their desugared representation.
    
    364
    -This usually means converting them to Constructor patterns but for some
    
    365
    -depends on enabled extensions. (Eg OverloadedLists)
    
    434
    +Pattern Constructors which only represent syntactic sugar are converted into their
    
    435
    +desugared representation.  This usually means converting them to Constructor
    
    436
    +patterns but for some depends on enabled extensions. (Eg OverloadedLists)
    
    366 437
     
    
    367 438
     GHC also tries to convert overloaded Literals into regular ones.
    
    368 439
     
    
    369 440
     The result of this tidying is that the column of patterns will include
    
    370 441
     only these which can be assigned a PatternGroup (see patGroup).
    
    371 442
     
    
    443
    +(TP1) We may call `tidyPat` on the same pattern more than once, principally because
    
    444
    +   of the treatment of bang-patterns; see (GE1) in Note [Grouping equations].
    
    445
    +   So (slightly hackily) we wrap tidied patterns in GeneratedSrcSpan to signal this.
    
    446
    +   See `wrapGenSpan` calls in `tidyLPat`.
    
    447
    +
    
    448
    +(TP2) Tidying is a convenient place to warn about overflowed literal values. We
    
    449
    +  can't do it in the typechecker because the types are not settled.  See the
    
    450
    +  `tidyPat1` equations for `LitPat`, `NPat`, `NPlusKPat`.  But we only want to
    
    451
    +  want /once/; hence, for these cases:
    
    452
    +    (a) the `unlessInGenCode` check, in turn relying on (TP1).
    
    453
    +    (b) wrap the result with `wrapGenSpan`
    
    372 454
     -}
    
    373 455
     
    
    374 456
     tidyEqnInfo :: MatchId -> EquationInfo
    
    ... ... @@ -383,44 +465,40 @@ tidyEqnInfo :: MatchId -> EquationInfo
    383 465
     
    
    384 466
     tidyEqnInfo _ eqn@(EqnDone {}) = return (idDsWrapper, eqn)
    
    385 467
     
    
    386
    -tidyEqnInfo v eqn@(EqnMatch { eqn_pat = p@(L loc _) }) = do
    
    387
    -  (wrap, pat') <- tidyLPat v p
    
    388
    -  return (wrap, eqn{eqn_pat = L loc pat' })
    
    389
    -
    
    390
    -tidyLPat :: MatchId -> LPat GhcTc -> DsM (DsWrapper, Pat GhcTc)
    
    391
    -tidyLPat v (L loc pat) = putSrcSpanDsA loc (tidy1 v pat)
    
    468
    +tidyEqnInfo v eqn@(EqnMatch { eqn_pat = pat })
    
    469
    +  = do { (wrap, pat') <- tidyLPat v pat
    
    470
    +       ; return (wrap, eqn{ eqn_pat = pat' }) }
    
    392 471
     
    
    393
    -tidy1 :: MatchId             -- The scrutinee
    
    394
    -      -> Pat GhcTc           -- The pattern against which it is to be matched
    
    395
    -      -> DsM (DsWrapper,     -- Extra bindings to do before the match
    
    396
    -              Pat GhcTc)     -- Equivalent pattern
    
    472
    +tidyLPat :: MatchId     -- The scrutinee
    
    473
    +         -> LPat GhcTc  -- The pattern against which it is to be matched
    
    474
    +         -> DsM (DsWrapper,     -- Extra bindings to do before the match
    
    475
    +                 LPat GhcTc)    -- Equivalent pattern
    
    397 476
     
    
    398 477
     -------------------------------------------------------
    
    399
    ---      (pat', mr') = tidy1 v pat mr
    
    478
    +--      (pat', mr') = tidyLPat v pat mr
    
    400 479
     -- tidies the *outer level only* of pat, giving pat'
    
    401 480
     -- It eliminates many pattern forms (as-patterns, variable patterns,
    
    402 481
     -- list patterns, etc) and returns any created bindings in the wrapper.
    
    403 482
     
    
    404
    -tidy1 v (ParPat _ pat)        = tidyLPat v pat
    
    405
    -tidy1 v (SigPat _ pat _)      = tidyLPat v pat
    
    406
    -tidy1 _ (WildPat ty)          = return (idDsWrapper, WildPat ty)
    
    407
    -tidy1 v (BangPat _ (L l p))   = putSrcSpanDsA l $ tidy_bang_pat v l p
    
    408
    -tidy1 v (ModifiedPat _ _ pat) = tidyLPat v pat
    
    483
    +tidyLPat v (L _ (ParPat _ pat))        = tidyLPat v pat  -- Drop parens
    
    484
    +tidyLPat v (L _ (SigPat _ pat _))      = tidyLPat v pat  -- Drop signatures
    
    485
    +tidyLPat v (L _ (ModifiedPat _ _ pat)) = tidyLPat v pat  -- Drop modifiers
    
    486
    +
    
    487
    +tidyLPat v (L _ (BangPat _ p))         = tidy_bang_pat v p
    
    409 488
     
    
    410 489
             -- case v of { x -> mr[] }
    
    411 490
             -- = case v of { _ -> let x=v in mr[] }
    
    412
    -tidy1 v (VarPat _ (L _ var))
    
    413
    -  = return (bindMatchId var v, WildPat (idType var))
    
    491
    +tidyLPat v (L loc (VarPat _ (L _ var)))
    
    492
    +  = return (bindMatchId var v, L loc (WildPat (idType var)))
    
    414 493
     
    
    415 494
             -- case v of { x@p -> mr[] }
    
    416 495
             -- = case v of { p -> let x=v in mr[] }
    
    417
    -tidy1 v (AsPat _ (L _ var) pat)
    
    418
    -
    
    496
    +tidyLPat v (L _ (AsPat _ (L _ var) pat))
    
    419 497
       = do  { (wrap, pat') <- tidyLPat v pat
    
    420 498
             ; return (bindMatchId var v . wrap, pat') }
    
    421 499
     
    
    422 500
     {- now, here we handle lazy patterns:
    
    423
    -    tidy1 v ~p bs = (v, v1 = case v of p -> v1 :
    
    501
    +    tidyLPat v ~p bs = (v, v1 = case v of p -> v1 :
    
    424 502
                             v2 = case v of p -> v2 : ... : bs )
    
    425 503
     
    
    426 504
         where the v_i's are the binders in the pattern.
    
    ... ... @@ -430,29 +508,35 @@ tidy1 v (AsPat _ (L _ var) pat)
    430 508
         The case expr for v_i is just: match [v] [(p, [], \ x -> Var v_i)] any_expr
    
    431 509
     -}
    
    432 510
     
    
    433
    -tidy1 v (LazyPat _ pat)
    
    511
    +tidyLPat v (L loc (LazyPat _ lpat))
    
    434 512
         -- This is a convenient place to check for unlifted types under a lazy pattern.
    
    435 513
         -- Doing this check during type-checking is unsatisfactory because we may
    
    436 514
         -- not fully know the zonked types yet. We sure do here.
    
    437
    -  = putSrcSpanDs (getLocA pat) $
    
    438
    -    do  { let unlifted_bndrs = filter (isUnliftedType . idType) (collectPatBinders CollNoDictBinders pat)
    
    439
    -            -- NB: the binders can't be representation-polymorphic, so we're OK to call isUnliftedType
    
    515
    +  = putSrcSpanDsA loc $
    
    516
    +        -- This putSrcSpan sets the location for
    
    517
    +        -- (a) the `DsLazyPatCantBindVarsOfUnliftedType` warning
    
    518
    +        -- (b) pattern-match overlap warnings inside `mkSelectorBinds`
    
    519
    +        --     (which ultimately calls `pmcPatBind`)
    
    520
    +    do  { let unlifted_bndrs = filter (isUnliftedType . idType) $
    
    521
    +                               collectPatBinders CollNoDictBinders lpat
    
    522
    +            -- NB: the binders can't be representation-polymorphic,
    
    523
    +            --     so we're OK to call isUnliftedType
    
    440 524
             ; unless (null unlifted_bndrs) $
    
    441 525
               diagnosticDs (DsLazyPatCantBindVarsOfUnliftedType unlifted_bndrs)
    
    442 526
     
    
    443
    -        ; (_,sel_prs) <- mkSelectorBinds [] pat LazyPatCtx (matchIdExpr v)
    
    527
    +        ; (_,sel_prs) <- mkSelectorBinds [] lpat LazyPatCtx (matchIdExpr v)
    
    444 528
             ; let sel_binds =  [NonRec b rhs | (b,rhs) <- sel_prs]
    
    445
    -        ; return (mkCoreLets sel_binds, WildPat (matchIdType v)) }
    
    529
    +        ; return (mkCoreLets sel_binds, L loc (WildPat (matchIdType v))) }
    
    446 530
     
    
    447
    -tidy1 _ (ListPat ty pats)
    
    448
    -  = return (idDsWrapper, unLoc list_ConPat)
    
    531
    +tidyLPat _ (L _ (ListPat ty pats))
    
    532
    +  = return (idDsWrapper, list_ConPat)
    
    449 533
       where
    
    450 534
         list_ConPat = foldr (\ x y -> mkPrefixConPat consDataCon [x, y] [ty])
    
    451 535
                             (mkNilPat ty)
    
    452 536
                             pats
    
    453 537
     
    
    454
    -tidy1 _ (TuplePat tys pats boxity)
    
    455
    -  = return (idDsWrapper, unLoc tuple_ConPat)
    
    538
    +tidyLPat _ (L _ (TuplePat tys pats boxity))
    
    539
    +  = return (idDsWrapper, tuple_ConPat)
    
    456 540
       where
    
    457 541
         arity = length pats
    
    458 542
         tuple_ConPat = mkPrefixConPat (tupleDataCon boxity arity) pats tys'
    
    ... ... @@ -461,45 +545,47 @@ tidy1 _ (TuplePat tys pats boxity)
    461 545
                  Boxed   -> tys
    
    462 546
                -- See Note [Unboxed tuple RuntimeRep vars] in TyCon
    
    463 547
     
    
    464
    -tidy1 _ (SumPat tys pat alt arity)
    
    465
    -  = return (idDsWrapper, unLoc sum_ConPat)
    
    548
    +tidyLPat _ (L _ (SumPat tys pat alt arity))
    
    549
    +  = return (idDsWrapper, sum_ConPat)
    
    466 550
       where
    
    467 551
         sum_ConPat = mkPrefixConPat (sumDataCon alt arity) [pat] (map getRuntimeRep tys ++ tys)
    
    468 552
                      -- See Note [Unboxed tuple RuntimeRep vars] in TyCon
    
    469 553
     
    
    470 554
     -- LitPats: we *might* be able to replace these w/ a simpler form
    
    471
    -tidy1 _ (LitPat _ lit)
    
    472
    -  = do { in_gen <- inGeneratedCodeDs;
    
    473
    -         unless in_gen $
    
    474
    -           warnAboutOverflowedLit lit
    
    475
    -       ; return (idDsWrapper, tidyLitPat lit) }
    
    555
    +tidyLPat _ (L loc (LitPat _ lit))
    
    556
    +  = do { unlessInGenCode loc $ warnAboutOverflowedLit lit
    
    557
    +            -- Overflow warnings: see (TP2) in Note [Tidying patterns]
    
    558
    +       ; return (idDsWrapper, wrapGenSpan $ tidyLitPat lit) }
    
    476 559
     
    
    477 560
     -- NPats: we *might* be able to replace these w/ a simpler form
    
    478
    -tidy1 _ (NPat ty (L _ lit@OverLit { ol_val = v }) mb_neg eq)
    
    479
    -  = do { in_gen <- inGeneratedCodeDs;
    
    480
    -         unless in_gen $
    
    481
    -           let lit' | Just _ <- mb_neg = lit{ ol_val = negateOverLitVal v }
    
    482
    -                    | otherwise = lit
    
    483
    -           in warnAboutOverflowedOverLit lit'
    
    484
    -       ; return (idDsWrapper, tidyNPat lit mb_neg eq ty) }
    
    561
    +tidyLPat _ (L loc (NPat ty (L _ lit@OverLit { ol_val = v }) mb_neg eq))
    
    562
    +  = do { unlessInGenCode loc $ warnAboutOverflowedOverLit lit'
    
    563
    +            -- Overflow warnings: see (TP2) in Note [Tidying patterns]
    
    564
    +       ; return (idDsWrapper, wrapGenSpan $ tidyNPat lit mb_neg eq ty) }
    
    565
    +  where
    
    566
    +    lit' | Just _ <- mb_neg = lit{ ol_val = negateOverLitVal v }
    
    567
    +         | otherwise        = lit
    
    485 568
     
    
    486 569
     -- NPlusKPat: we may want to warn about the literals
    
    487
    -tidy1 _ n@(NPlusKPat _ _ (L _ lit1) lit2 _ _)
    
    488
    -  = do { in_gen <- inGeneratedCodeDs;
    
    489
    -         unless in_gen $ do
    
    490
    -           warnAboutOverflowedOverLit lit1
    
    491
    -           warnAboutOverflowedOverLit lit2
    
    492
    -       ; return (idDsWrapper, n) }
    
    493
    -
    
    494
    -tidy1 _ (OrPat ty lpats)
    
    570
    +tidyLPat _ (L loc n@(NPlusKPat _ _ (L _ lit1) lit2 _ _))
    
    571
    +  = do { unlessInGenCode loc $
    
    572
    +         do { warnAboutOverflowedOverLit lit1
    
    573
    +            ; warnAboutOverflowedOverLit lit2 }
    
    574
    +            -- Overflow warnings: see (TP2) in Note [Tidying patterns]
    
    575
    +       ; return (idDsWrapper, wrapGenSpan n) }
    
    576
    +
    
    577
    +tidyLPat _ (L loc (OrPat ty lpats))
    
    495 578
       -- See Note [Implementation of OrPatterns]. We desugar
    
    496 579
       --   (1; 2; 3)
    
    497 580
       -- to
    
    498 581
       --   ((\case 1 -> True; 2 -> True; 3 -> True; _ -> False) -> True)
    
    499
    -  = return (idDsWrapper, ViewPat ty (noLocA (HsLam noAnn LamCase mg)) (mkPrefixConPat trueDataCon [] []))
    
    582
    +  = return ( idDsWrapper
    
    583
    +           , L loc $ ViewPat ty (noLocA (HsLam noAnn LamCase mg))
    
    584
    +                                (mkPrefixConPat trueDataCon [] []) )
    
    500 585
       where
    
    501 586
         mg :: MatchGroup GhcTc (LHsExpr GhcTc)
    
    502
    -    mg = MG mgtc (noLocA (map match_true (NE.toList lpats) ++ [match_false (noLocA $ WildPat ty)]))
    
    587
    +    mg = MG mgtc (noLocA (map match_true (NE.toList lpats)
    
    588
    +                          ++ [match_false (noLocA $ WildPat ty)]))
    
    503 589
         mgtc = MatchGroupTc
    
    504 590
            { mg_arg_tys = [tymult ty]
    
    505 591
            , mg_res_ty = boolTy
    
    ... ... @@ -516,50 +602,59 @@ tidy1 _ (OrPat ty lpats)
    516 602
     
    
    517 603
         hs_var :: Var -> LHsExpr GhcTc
    
    518 604
         hs_var v = noLocA $ mkHsVar (noLocA v)
    
    605
    +
    
    519 606
         single_grhs :: LHsExpr GhcTc -> GRHSs GhcTc (LHsExpr GhcTc)
    
    520
    -    single_grhs e = GRHSs emptyComments [noLocA $ GRHS noAnn [] e] (noLocA $ EmptyLocalBinds noExtField)
    
    607
    +    single_grhs e = GRHSs emptyComments [noLocA $ GRHS noAnn [] e]
    
    608
    +                          (noLocA $ EmptyLocalBinds noExtField)
    
    521 609
     
    
    522 610
     -- Everything else goes through unchanged...
    
    523
    -tidy1 _ non_interesting_pat
    
    611
    +tidyLPat _ non_interesting_pat
    
    524 612
       = return (idDsWrapper, non_interesting_pat)
    
    525 613
     
    
    526 614
     --------------------
    
    527
    -tidy_bang_pat :: MatchId -> SrcSpanAnnA -> Pat GhcTc
    
    528
    -              -> DsM (DsWrapper, Pat GhcTc)
    
    529
    -
    
    530
    --- Discard par/sig under a bang
    
    531
    -tidy_bang_pat v _ (ParPat _ (L l p))   = putSrcSpanDsA l $ tidy_bang_pat v l p
    
    532
    -tidy_bang_pat v _ (SigPat _ (L l p) _) = putSrcSpanDsA l $ tidy_bang_pat v l p
    
    533
    -
    
    534
    --- Push the bang-pattern inwards, in the hope that
    
    535
    --- it may disappear next time
    
    536
    -tidy_bang_pat v l (AsPat x v' p)
    
    537
    -  = tidy1 v (AsPat x v' (L l (BangPat noExtField p)))
    
    538
    -tidy_bang_pat v l (XPat (CoPat w p t))
    
    539
    -  = tidy1 v (XPat $ CoPat w (BangPat noExtField (L l p)) t)
    
    540
    -tidy_bang_pat v l (OrPat x (p:|ps)) -- push bang into first pat alt
    
    541
    -  = tidy1 v (OrPat x (L l (BangPat noExtField p) :| ps))
    
    615
    +tidy_bang_pat :: MatchId -> LPat GhcTc
    
    616
    +              -> DsM (DsWrapper, LPat GhcTc)
    
    617
    +
    
    618
    +-- Push the bang-pattern inwards, in the hope that it may disappear next time
    
    619
    +--   AsPat  !(x@p)     -->  x@(!p)
    
    620
    +--   CoPat  !(p |> co) -->  (!p) |> co
    
    621
    +--   OrPat  !(p || q)  -->  (!p || !q)
    
    622
    +tidy_bang_pat v (L l (AsPat x v' p))
    
    623
    +  = tidyLPat v (L l (AsPat x v' (L l (BangPat noExtField p))))
    
    624
    +tidy_bang_pat v (L l (XPat (CoPat w p t)))
    
    625
    +  = tidyLPat v (L l (XPat $ CoPat w (BangPat noExtField (L l p)) t))
    
    626
    +tidy_bang_pat v (L l (OrPat x (p:|ps))) -- push bang into first pat alt
    
    627
    +  = tidyLPat v (L l (OrPat x (L l (BangPat noExtField p) :| ps)))
    
    628
    +
    
    629
    +-- Discard par/sig/bang under a bang
    
    630
    +--    !(p)      -->   !p
    
    631
    +--    !(p::ty)  -->   !p
    
    632
    +--    !(!p)     -->   !p
    
    633
    +tidy_bang_pat v (L _ (ParPat _ lp))   = tidy_bang_pat v lp
    
    634
    +tidy_bang_pat v (L _ (SigPat _ lp _)) = tidy_bang_pat v lp
    
    635
    +tidy_bang_pat v (L _ (BangPat _ lp))  = tidy_bang_pat v lp
    
    542 636
     
    
    543 637
     -- Discard bang around strict pattern
    
    544
    -tidy_bang_pat v _ p@(LitPat {})    = tidy1 v p
    
    545
    -tidy_bang_pat v _ p@(ListPat {})   = tidy1 v p
    
    546
    -tidy_bang_pat v _ p@(TuplePat {})  = tidy1 v p
    
    547
    -tidy_bang_pat v _ p@(SumPat {})    = tidy1 v p
    
    638
    +--    e.g.  !(Just p)  -->  Just p
    
    639
    +tidy_bang_pat v lp@(L _ (LitPat {}))   = tidyLPat v lp
    
    640
    +tidy_bang_pat v lp@(L _ (ListPat {}))  = tidyLPat v lp
    
    641
    +tidy_bang_pat v lp@(L _ (TuplePat {})) = tidyLPat v lp
    
    642
    +tidy_bang_pat v lp@(L _ (SumPat {}))   = tidyLPat v lp
    
    548 643
     
    
    549 644
     -- Data/newtype constructors
    
    550
    -tidy_bang_pat v l p@(ConPat { pat_con = L _ (RealDataCon dc)
    
    551
    -                            , pat_args = args
    
    552
    -                            , pat_con_ext = ConPatTc
    
    553
    -                              { cpt_arg_tys = arg_tys
    
    554
    -                              }
    
    555
    -                            })
    
    645
    +tidy_bang_pat v lp@(L l p@(ConPat { pat_con = L _ (RealDataCon dc)
    
    646
    +                                  , pat_args = args
    
    647
    +                                  , pat_con_ext = ConPatTc
    
    648
    +                                      { cpt_arg_tys = arg_tys
    
    649
    +                                  } }))
    
    556 650
       -- Newtypes: push bang inwards (#9844)
    
    557
    -  =
    
    558
    -    if isNewTyCon (dataConTyCon dc)
    
    559
    -      then tidy1 v (p { pat_args = push_bang_into_newtype_arg l (scaledThing ty) args })
    
    560
    -      else tidy1 v p  -- Data types: discard the bang
    
    561
    -    where
    
    562
    -      (ty:_) = dataConInstArgTys dc arg_tys
    
    651
    +  | isNewTyCon (dataConTyCon dc)
    
    652
    +  , (ty:_) <- dataConInstArgTys dc arg_tys
    
    653
    +  = tidyLPat v (L l (p { pat_args = push_bang_into_newtype_arg l (scaledThing ty) args }))
    
    654
    +
    
    655
    +  -- Discard bang around non-newtype data constructors
    
    656
    +  | otherwise
    
    657
    +  = tidyLPat v lp
    
    563 658
     
    
    564 659
     -------------------
    
    565 660
     -- Default case, leave the bang there:
    
    ... ... @@ -576,7 +671,7 @@ tidy_bang_pat v l p@(ConPat { pat_con = L _ (RealDataCon dc)
    576 671
     --
    
    577 672
     -- NB: SigPatIn, ConPatIn should not happen
    
    578 673
     
    
    579
    -tidy_bang_pat _ l p = return (idDsWrapper, BangPat noExtField (L l p))
    
    674
    +tidy_bang_pat _ lp = return (idDsWrapper, wrapGenSpan $ BangPat noExtField lp)
    
    580 675
     
    
    581 676
     -------------------
    
    582 677
     push_bang_into_newtype_arg :: SrcSpanAnnA
    
    ... ... @@ -1033,14 +1128,25 @@ for overloaded strings.
    1033 1128
     groupEquations :: Platform -> [EquationInfoNE] -> [NonEmpty (PatGroup, EquationInfoNE)]
    
    1034 1129
     -- If the result is of form [g1, g2, g3],
    
    1035 1130
     -- (a) all the (pg,eq) pairs in g1 have the same pg
    
    1131
    +--     with the exception documented in (GE1) of Note [Grouping equations]
    
    1036 1132
     -- (b) none of the gi are empty
    
    1037 1133
     -- The ordering of equations is unchanged
    
    1038 1134
     groupEquations platform eqns
    
    1039
    -  = NE.groupBy same_gp $ [(patGroup platform (firstPat eqn), eqn) | eqn <- eqns]
    
    1040
    -  -- comprehension on NonEmpty
    
    1135
    +  = group_eqns $ map add_gp eqns
    
    1041 1136
       where
    
    1042
    -    same_gp :: (PatGroup,EquationInfo) -> (PatGroup,EquationInfo) -> Bool
    
    1043
    -    (pg1,_) `same_gp` (pg2,_) = pg1 `sameGroup` pg2
    
    1137
    +    add_gp :: EquationInfoNE -> (PatGroup, EquationInfoNE)
    
    1138
    +    add_gp eqn = (patGroup platform (firstPat eqn), eqn)
    
    1139
    +
    
    1140
    +    group_eqns :: [(PatGroup,EquationInfoNE)] -> [NonEmpty (PatGroup, EquationInfoNE)]
    
    1141
    +    -- See Note [Grouping equations]
    
    1142
    +    group_eqns [] = []
    
    1143
    +    group_eqns ((PgBang,e) : pges) = [ (PgBang,e) :| pges ]  -- See (GE1)
    
    1144
    +    group_eqns ((pg1,e)    : pges) = ((pg1,e) :| pges1) : group_eqns pges2
    
    1145
    +      where
    
    1146
    +        (pges1, pges2) = span same_gp pges
    
    1147
    +
    
    1148
    +        same_gp :: (PatGroup,EquationInfo) -> Bool
    
    1149
    +        same_gp (pg2,_) = pg1 `sameGroup` pg2
    
    1044 1150
     
    
    1045 1151
     -- TODO Make subGroup1 using a NonEmptyMap
    
    1046 1152
     subGroup :: (m -> [NonEmpty EquationInfo]) -- Map.elems
    

  • compiler/GHC/HsToCore/Match/Literal.hs
    ... ... @@ -581,8 +581,9 @@ tidyNPat (OverLit (OverLitTc False _ ty) val) mb_neg _eq outer_ty
    581 581
     
    
    582 582
         mk_con_pat :: DataCon -> HsLit GhcTc -> Pat GhcTc
    
    583 583
         mk_con_pat con lit
    
    584
    -      -- Mark the generated literal to avoid duplicate -Woverflowed-literals.
    
    585 584
           = unLoc (mkPrefixConPat con [wrapGenSpan $ LitPat noExtField lit] [])
    
    585
    +        -- wrapGenSpan: mark the literal in a generated SrcSpan to avoid duplicate
    
    586
    +        -- -Woverflowed-literals. See (TP2) in Note [Tidying patterns]
    
    586 587
     
    
    587 588
         mb_int_lit :: Maybe Integer
    
    588 589
         mb_int_lit = case (mb_neg, val) of
    

  • compiler/GHC/HsToCore/Monad.hs
    ... ... @@ -16,7 +16,7 @@ module GHC.HsToCore.Monad (
    16 16
             newSysLocalMDs, newSysLocalsMDs, newFailLocalMDs,
    
    17 17
             newUniqueId, newPredVarDs, newStaticId,
    
    18 18
             getSrcSpanDs, inGeneratedCodeDs, putSrcSpanDs, putSrcSpanDsA,
    
    19
    -        mkNamePprCtxDs,
    
    19
    +        unlessInGenCode, mkNamePprCtxDs,
    
    20 20
             newUnique,
    
    21 21
             UniqSupply, newUniqueSupply,
    
    22 22
             getGhcModeDs, dsGetFamInstEnvs, dsGetGlobalRdrEnv,
    
    ... ... @@ -151,7 +151,7 @@ data EquationInfo
    151 151
       = EqnMatch  { eqn_pat :: LPat GhcTc
    
    152 152
                     -- ^ The first pattern of the equation
    
    153 153
                     --
    
    154
    -                -- NB: The location info is used to determine whether the
    
    154
    +                -- NB: The location info is used only to determine whether the
    
    155 155
                     -- pattern is generated or not.
    
    156 156
                     -- This helps us avoid warnings on patterns that GHC elaborated.
    
    157 157
                     --
    
    ... ... @@ -681,6 +681,13 @@ getSrcSpanDs = do { env <- getLclEnv
    681 681
     inGeneratedCodeDs :: DsM Bool
    
    682 682
     inGeneratedCodeDs = dsl_in_generated_code <$> getLclEnv
    
    683 683
     
    
    684
    +unlessInGenCode :: EpAnn ann -> DsM () -> DsM ()
    
    685
    +unlessInGenCode ann do_warn
    
    686
    +  | isGeneratedSrcSpan span = return ()
    
    687
    +  | otherwise               = putSrcSpanDs span $ do_warn
    
    688
    +  where
    
    689
    +    span = locA ann
    
    690
    +
    
    684 691
     putSrcSpanDs :: SrcSpan -> DsM a -> DsM a
    
    685 692
     putSrcSpanDs (RealSrcSpan real_span _) thing_inside
    
    686 693
       = updLclEnv (\ env -> env {dsl_loc = real_span, dsl_in_generated_code = False}) thing_inside
    

  • compiler/GHC/HsToCore/Pmc.hs
    ... ... @@ -143,10 +143,8 @@ pmcGRHSs hs_ctxt guards@(GRHSs _ grhss _) =
    143 143
         let combined_loc = foldl1 combineSrcSpans (NE.map getLocA grhss)
    
    144 144
             ctxt = DsMatchContext hs_ctxt combined_loc
    
    145 145
         matches  <- dontDoPmc $ desugarGRHSs combined_loc empty guards
    
    146
    -    tracePm "pmcGRHSs" (hang (vcat [ppr ctxt
    
    147
    -                                  , text "Guards:"])
    
    148
    -                                  2
    
    149
    -                                  (pprGRHSs hs_ctxt guards $$ ppr missing))
    
    146
    +    tracePm "pmcGRHSs" (hang (vcat [ppr ctxt, text "Guards:"])
    
    147
    +                           2 (pprGRHSs hs_ctxt guards $$ ppr missing))
    
    150 148
         result <- unCA (checkGRHSs matches) missing
    
    151 149
         tracePm "}: " (ppr (cr_uncov result))
    
    152 150
         formatReportWarnings ReportGRHSs ctxt [] result
    

  • testsuite/tests/ado/T22483.stderr
    1
    -
    
    2 1
     T22483.hs:1:1: warning: [GHC-38417] [-Wmissing-signatures (in -Wall)]
    
    3 2
         Top-level binding with no type signature: main :: IO ()
    
    4 3
     
    
    5
    -T22483.hs:4:4: warning: [GHC-62161] [-Wincomplete-uni-patterns (in -Wall)]
    
    4
    +T22483.hs:4:3: warning: [GHC-62161] [-Wincomplete-uni-patterns (in -Wall)]
    
    6 5
         Pattern match(es) are non-exhaustive
    
    7 6
         In an irrefutable pattern:
    
    8 7
             Patterns of type ‘Maybe ()’ not matched: Nothing
    
    8
    +

  • testsuite/tests/deSugar/should_compile/T27862.hs
    1
    +{-# LANGUAGE BangPatterns #-}
    
    2
    +module T27862 (allBangs, firstBang, lastLazy) where
    
    3
    +
    
    4
    +data Axis = Axis !Int !Int
    
    5
    +
    
    6
    +-- The axes of extent other than 1, their strides made positive, and
    
    7
    +-- the offset moved by each negative stride. The three functions are
    
    8
    +-- the same, except for the bangs on 'off'.
    
    9
    +
    
    10
    +{- With GHC 10.0
    
    11
    +* allBangs has a bang on all three equations.
    
    12
    +  The flag reports the bangs on equations 2 and 3.
    
    13
    +
    
    14
    +* firstBang keeps only the bang that the flag does not report. Its
    
    15
    +  worker matches both lists two times: the second match is in a join point fail, and
    
    16
    +  the first match jumps to it. In the second match, GHC does not know that n is
    
    17
    +  evaluated. Thus it builds the strict-field constructor Axis x n inside case n
    
    18
    +  of I#, and in STG this is an updatable thunk, one for each axis with an extent
    
    19
    +  other than 1 and a non-negative stride. allBangs allocates no thunk.
    
    20
    +
    
    21
    +* lastLazy removes only the bang on equation 3. Its worker is better than
    
    22
    +  the worker of allBangs. allBangs keeps a join point fail _ = (# axes, ww #),
    
    23
    +  and its two [] alternatives jump to it. In lastLazy, the two [] alternatives
    
    24
    +  return (# axes, ww #) directly.
    
    25
    +-}
    
    26
    +
    
    27
    +allBangs :: [Axis] -> Int -> [Int] -> [Int] -> ([Axis], Int)
    
    28
    +allBangs axes !off (_ : sts) (1 : ns) = allBangs axes off sts ns
    
    29
    +allBangs axes !off (st : sts) (n : ns)
    
    30
    +  | st < 0 = allBangs (Axis (negate st) n : axes) (off + (n - 1) * st) sts ns
    
    31
    +  | otherwise = allBangs (Axis st n : axes) off sts ns
    
    32
    +allBangs axes !off _ _ = (axes, off)
    
    33
    +
    
    34
    +-- Both bangs that the flag reports are removed.
    
    35
    +firstBang :: [Axis] -> Int -> [Int] -> [Int] -> ([Axis], Int)
    
    36
    +firstBang axes !off (_ : sts) (1 : ns) = firstBang axes off sts ns
    
    37
    +firstBang axes off (st : sts) (n : ns)
    
    38
    +  | st < 0 = firstBang (Axis (negate st) n : axes) (off + (n - 1) * st) sts ns
    
    39
    +  | otherwise = firstBang (Axis st n : axes) off sts ns
    
    40
    +firstBang axes off _ _ = (axes, off)
    
    41
    +
    
    42
    +-- Only the last bang that the flag reports is removed.
    
    43
    +lastLazy :: [Axis] -> Int -> [Int] -> [Int] -> ([Axis], Int)
    
    44
    +lastLazy axes !off (_ : sts) (1 : ns) = lastLazy axes off sts ns
    
    45
    +lastLazy axes !off (st : sts) (n : ns)
    
    46
    +  | st < 0 = lastLazy (Axis (negate st) n : axes) (off + (n - 1) * st) sts ns
    
    47
    +  | otherwise = lastLazy (Axis st n : axes) off sts ns
    
    48
    +lastLazy axes off _ _ = (axes, off)

  • testsuite/tests/deSugar/should_compile/T27862.stderr
    1
    +firstBang = lastLazy
    
    2
    +allBangs = lastLazy

  • testsuite/tests/deSugar/should_compile/T27862a.hs
    1
    +{-# LANGUAGE BangPatterns #-}
    
    2
    +{-# OPTIONS_GHC -Woverflowed-literals #-}
    
    3
    +
    
    4
    +-- We expect only /one/ warning
    
    5
    +
    
    6
    +module T27862a where
    
    7
    +
    
    8
    +import Data.Word
    
    9
    +
    
    10
    +f :: Word8 -> Int
    
    11
    +f !x | even x = 0
    
    12
    +f 300 = 1
    
    13
    +f _   = 2

  • testsuite/tests/deSugar/should_compile/T27862a.stderr
    1
    +T27862a.hs:12:3: warning: [GHC-97441] [-Woverflowed-literals (in -Wdefault)]
    
    2
    +    Literal 300 is out of the Word8 range 0..255
    
    3
    +

  • testsuite/tests/deSugar/should_compile/T27862b.hs
    1
    +{-# LANGUAGE BangPatterns #-}
    
    2
    +{-# OPTIONS_GHC -Woverflowed-literals #-}
    
    3
    +
    
    4
    +module T27862b where
    
    5
    +
    
    6
    +import Data.Word
    
    7
    +
    
    8
    +f :: Word8 -> Int
    
    9
    +f !300 = 1
    
    10
    +f _   = 2

  • testsuite/tests/deSugar/should_compile/T27862b.stderr
    1
    +T27862b.hs:9:4: warning: [GHC-97441] [-Woverflowed-literals (in -Wdefault)]
    
    2
    +    Literal 300 is out of the Word8 range 0..255
    
    3
    +

  • testsuite/tests/deSugar/should_compile/T27862c.hs
    1
    +{-# LANGUAGE BangPatterns, MagicHash #-}
    
    2
    +
    
    3
    +module T27862c where
    
    4
    +
    
    5
    +import GHC.Exts
    
    6
    +
    
    7
    +g :: Int -> Int
    
    8
    +g !(~(I# x)) = 0

  • testsuite/tests/deSugar/should_compile/T27862c.stderr
    1
    +T27862c.hs:8:5: error: [GHC-17879]
    
    2
    +    A lazy (~) pattern cannot bind variables of unlifted type.
    
    3
    +    Unlifted variables:
    
    4
    +      x :: Int#
    
    5
    +

  • testsuite/tests/deSugar/should_compile/all.T
    ... ... @@ -121,3 +121,11 @@ test('T27383',
    121 121
          [collect_compiler_stats('bytes allocated', 10)],
    
    122 122
          compile,
    
    123 123
          ['-Wincomplete-patterns'])
    
    124
    +
    
    125
    +# T27862.  Expecting to see
    
    126
    +#   allBangs = lastLazy, and firstBang = lastLazy,
    
    127
    +# so they have identical code
    
    128
    +test('T27862', [grep_errmsg(' lastLazy')], compile, ['-O -ddump-simpl -dsuppress-uniques -dno-typeable-binds'])
    
    129
    +test('T27862a', normal, compile, [''])
    
    130
    +test('T27862b', normal, compile, [''])
    
    131
    +test('T27862c', normal, compile_fail, [''])

  • testsuite/tests/deSugar/should_run/dsrun008.stderr
    1 1
     dsrun008: Uncaught exception ghc-internal:GHC.Internal.Control.Exception.Base.PatternMatchFail:
    
    2 2
     
    
    3
    -dsrun008.hs:2:32-36: Non-exhaustive patterns in (2, x)
    
    3
    +dsrun008.hs:2:31-36: Non-exhaustive patterns in (2, x)
    
    4 4
     
    
    5 5
     
    
    6 6
     HasCallStack backtrace:
    
    7
    -  throw, called at libraries/ghc-internal/src/GHC/Internal/Control/Exception/Base.hs:434:30 in ghc-internal:GHC.Internal.Control.Exception.Base
    
    7
    +  throw, called at libraries/ghc-internal/src/GHC/Internal/Control/Exception/Base.hs:438:30 in ghc-internal:GHC.Internal.Control.Exception.Base
    
    8 8
     

  • testsuite/tests/pmcheck/should_compile/T24234.stderr
    1
    -
    
    2
    -T24234.hs:6:6: warning: [GHC-62161] [-Wincomplete-uni-patterns (in -Wall)]
    
    1
    +T24234.hs:6:5: warning: [GHC-62161] [-Wincomplete-uni-patterns (in -Wall)]
    
    3 2
         Pattern match(es) are non-exhaustive
    
    4 3
         In an irrefutable pattern: Patterns of type ‘[()]’ not matched: []
    
    5 4
     
    
    6 5
     T24234.hs:7:1: warning: [GHC-53633] [-Woverlapping-patterns (in -Wdefault)]
    
    7 6
         Pattern match is redundant
    
    8 7
         In an equation for ‘foo’: foo _ = ...
    
    8
    +

  • testsuite/tests/typecheck/should_fail/tcfail203a.stderr
    1
    -
    
    2
    -tcfail203a.hs:10:17: error: [GHC-17879]
    
    1
    +tcfail203a.hs:10:16: error: [GHC-17879]
    
    3 2
         A lazy (~) pattern cannot bind variables of unlifted type.
    
    4 3
         Unlifted variables:
    
    5 4
           x :: Int#
    
    5
    +