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

Commits:

23 changed files:

Changes:

  • changelog.d/T27137
    1
    +section: compiler
    
    2
    +issues: #28137
    
    3
    +mrs: !16899
    
    4
    +synopsis:
    
    5
    +  Refactor desugaring for pattern matching
    
    6
    +description:
    
    7
    +  Fix a missing warning for an overflowed literal in
    
    8
    +  pattern matching, when using ``-XStrict``.  We get the extra
    
    9
    +  warning, and a minor change in column numbers for lazy-pattern
    
    10
    +  warnings.

  • 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
    +  GHC was generating poor code for pattern matching that used bang-patterns

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

  • testsuite/tests/deSugar/should_compile/T27862d.stderr
    1
    +T27862d.hs:9:3: warning: [GHC-97441] [-Woverflowed-literals (in -Wdefault)]
    
    2
    +    Literal 300 is out of the Int8# range -128..127
    
    3
    +

  • testsuite/tests/deSugar/should_compile/T28137.hs
    1
    +{-# LANGUAGE Strict #-}  -- NB: Strict
    
    2
    +module T28137 where
    
    3
    +
    
    4
    +import Data.Word
    
    5
    +
    
    6
    +f :: Word8 -> Int
    
    7
    +f 300 = 1   -- Should trigger a warning
    
    8
    +f _   = 2

  • testsuite/tests/deSugar/should_compile/T28137.stderr
    1
    +T28137.hs:7: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/all.T
    ... ... @@ -121,3 +121,13 @@ 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, [''])
    
    132
    +test('T27862d', normal, compile, [''])
    
    133
    +test('T28137', normal, compile, [''])

  • 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
    +