Andreas Klebinger pushed to branch wip/andreask/fold_tag_to_enum at Glasgow Haskell Compiler / GHC

Commits:

3 changed files:

Changes:

  • compiler/GHC/Core/Opt/ConstantFold.hs
    ... ... @@ -69,7 +69,6 @@ import GHC.Cmm.MachOp ( FMASign(..) )
    69 69
     import GHC.Cmm.Type ( Width(..) )
    
    70 70
     
    
    71 71
     import GHC.Data.FastString
    
    72
    -import GHC.Data.Maybe      ( orElse )
    
    73 72
     
    
    74 73
     import GHC.Utils.Outputable
    
    75 74
     import GHC.Utils.Misc
    
    ... ... @@ -2008,9 +2007,13 @@ tagToEnumRule = do
    2008 2007
         Just (tycon, tc_args) | isEnumerationTyCon tycon -> do
    
    2009 2008
           let tag = fromInteger i
    
    2010 2009
               correct_tag dc = (dataConTagZ dc) == tag
    
    2011
    -      (dc:rest) <- return $ filter correct_tag (tyConDataCons_maybe tycon `orElse` [])
    
    2012
    -      massert (null rest)
    
    2013
    -      return $ mkTyApps (Var (dataConWorkId dc)) tc_args
    
    2010
    +      Just dataCons <- pure $ tyConDataCons_maybe tycon
    
    2011
    +      case filter correct_tag dataCons of
    
    2012
    +        (dc:rest) -> do
    
    2013
    +          massert (null rest)
    
    2014
    +          pure $ mkTyApps (Var (dataConWorkId dc)) tc_args
    
    2015
    +        -- Literal is out of range, e.g. tagToEnum @Bool #4
    
    2016
    +        [] -> pure $ mkImpossibleExpr ty "tagToEnum: Argument out of range"
    
    2014 2017
     
    
    2015 2018
         -- See Note [tagToEnum#]
    
    2016 2019
         _ -> warnPprTrace True "tagToEnum# on non-enumeration type" (ppr ty) $
    

  • testsuite/tests/simplCore/should_compile/T25976.hs
    1
    +{-# LANGUAGE MagicHash #-}
    
    2
    +
    
    3
    +module T25976 where
    
    4
    +
    
    5
    +import GHC.PrimOps (tagToEnum#)
    
    6
    +
    
    7
    +-- Spoiler - it's all dead code since tagToEnum 3# is undefined
    
    8
    +main = case (tagToEnum# 4# :: Bool) of
    
    9
    +  True -> print "Dead Code"
    
    10
    +  False -> print "Dead Code"
    \ No newline at end of file

  • testsuite/tests/simplCore/should_compile/all.T
    ... ... @@ -535,3 +535,4 @@ test('T25197', [req_th, extra_files(["T25197_TH.hs"]), only_ways(['optasm'])], m
    535 535
     test('T25389', normal, compile, ['-O -ddump-simpl -dsuppress-uniques -dno-typeable-binds'])
    
    536 536
     test('T24359a', normal, compile, ['-O -ddump-rules'])
    
    537 537
     test('T25713', [grep_errmsg('W:::')], compile, ['-O -ddump-simpl'])
    
    538
    +test('T25976', [grep_errmsg('Dead Code')], compile, ['-O -ddump-simpl -dsuppress-uniques -dno-typeable-binds'])