Skip to content

Commit e565d26

Browse files
committed
Use TH name to identify bottoming values
This way we can just refer to "'patError" regardless of whether it lives in: - Control.Exceptions.Base, or - GHC.Internal.Control.Exception.Base
1 parent d52447b commit e565d26

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
lines changed

clash-lib/src/Clash/Core/Util.hs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
module Clash.Core.Util where
2020

2121
import Control.Concurrent.Supply (Supply, freshId)
22+
import Control.Exception.Base (patError)
23+
#if MIN_VERSION_base(4,16,0)
24+
import GHC.Prim.Panic (absentError)
25+
#else
26+
import Control.Exception.Base (absentError)
27+
#endif
2228
import Control.Monad.Trans.Except (Except, throwE, runExcept)
2329
import Data.Bifunctor (first, second)
2430
import qualified Data.HashSet as HashSet
@@ -31,6 +37,9 @@ import Data.Maybe
3137
import qualified Data.Set as Set
3238
import qualified Data.Set.Lens as Lens
3339
import qualified Data.Text as T
40+
import Data.Text.Extra (showt)
41+
import GHC.Real
42+
(divZeroError, overflowError, ratioZeroDenominatorError, underflowError)
3443
import GHC.Stack (HasCallStack)
3544

3645
#if MIN_VERSION_ghc(9,0,0)
@@ -59,6 +68,9 @@ import Clash.Debug (traceIf)
5968
import Clash.Unique (fromGhcUnique)
6069
import Clash.Util
6170

71+
import {-# SOURCE #-} qualified Clash.Normalize.Primitives as Primitives
72+
import Clash.XException (errorX)
73+
6274
-- | Rebuild a let expression / let expressions by taking the SCCs of a list
6375
-- of bindings and remaking Let (NonRec ...) ... and Let (Rec ...) ...
6476
--
@@ -479,27 +491,23 @@ primUCo =
479491
}
480492

481493
undefinedPrims :: [T.Text]
482-
undefinedPrims =
483-
[ "Clash.Normalize.Primitives.undefined"
484-
, "Control.Exception.Base.absentError"
485-
, "Control.Exception.Base.patError"
486-
, "GHC.Err.error"
487-
, "GHC.Err.errorWithoutStackTrace"
488-
, "GHC.Err.undefined"
489-
, "GHC.Internal.Err.error"
490-
, "GHC.Internal.Err.errorWithoutStackTrace"
491-
, "GHC.Internal.Err.undefined"
492-
, "GHC.Prim.Panic.absentError"
493-
, "GHC.Real.divZeroError"
494-
, "GHC.Real.overflowError"
495-
, "GHC.Real.ratioZeroDenominatorError"
496-
, "GHC.Real.underflowError"
494+
undefinedPrims = fmap showt
495+
[ 'Primitives.undefined
496+
, 'patError
497+
, 'error
498+
, 'errorWithoutStackTrace
499+
, 'undefined
500+
, 'absentError
501+
, 'divZeroError
502+
, 'overflowError
503+
, 'ratioZeroDenominatorError
504+
, 'underflowError
497505
]
498506

499507
undefinedXPrims :: [T.Text]
500-
undefinedXPrims =
501-
[ "Clash.Normalize.Primitives.undefinedX"
502-
, "Clash.XException.errorX"
508+
undefinedXPrims = fmap showt
509+
[ 'Primitives.undefinedX
510+
, 'errorX
503511
]
504512

505513
substArgTys
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Clash.Normalize.Primitives where
2+
3+
import Clash.Core.Term (PrimInfo)
4+
5+
undefined :: PrimInfo
6+
undefinedX :: PrimInfo

clash-lib/src/Clash/Normalize/Transformations/Case.hs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ module Clash.Normalize.Transformations.Case
2626
, elimExistentials
2727
) where
2828

29+
import Control.Exception.Base (patError)
30+
#if MIN_VERSION_base(4,16,0)
31+
import GHC.Prim.Panic (absentError)
32+
#else
33+
import Control.Exception.Base (absentError)
34+
#endif
2935
import qualified Control.Lens as Lens
3036
import Control.Monad.State.Strict (evalState)
3137
import Data.Bifunctor (second)
@@ -80,6 +86,8 @@ import Clash.Rewrite.Util (changed, isFromInt, whnfRW)
8086
import Clash.Rewrite.WorkFree
8187
import Clash.Util (curLoc)
8288

89+
import Clash.XException (errorX)
90+
8391
-- | Move a Case-decomposition from the subject of a Case-decomposition to the
8492
-- alternatives
8593
caseCase :: HasCallStack => NormRewrite
@@ -266,27 +274,23 @@ caseCon' ctx@(TransformContext is0 _) e@(Case subj ty alts) = do
266274
-- WHNF of subject is _|_, in the form of `error`: that means that the
267275
-- entire case-expression is evaluates to _|_
268276
(Prim pInfo,repTy:_:callStack:msg:_,ticks)
269-
| primName pInfo `elem` ["GHC.Err.error"
270-
,"GHC.Internal.Err.error"] ->
277+
| primName pInfo == Text.showt 'error ->
271278
let e1 = mkApps (mkTicks (Prim pInfo) ticks)
272279
[repTy,Right ty,callStack,msg]
273280
in changed e1
274281
-- WHNF of subject is _|_, in the form of `absentError`: that means that
275282
-- the entire case-expression is evaluates to _|_
276283
(Prim pInfo,_:msgOrCallStack:_,ticks)
277-
| primName pInfo `elem` ["Control.Exception.Base.absentError"
278-
,"GHC.Prim.Panic.absentError"] ->
284+
| primName pInfo == Text.showt 'absentError ->
279285
let e1 = mkApps (mkTicks (Prim pInfo) ticks)
280286
[Right ty,msgOrCallStack]
281287
in changed e1
282288
-- WHNF of subject is _|_, in the form of `patError`, `undefined`, or
283289
-- `errorWithoutStackTrace`: that means the entire case-expression is _|_
284290
(Prim pInfo,repTy:_:msgOrCallStack:_,ticks)
285-
| primName pInfo `elem` ["Control.Exception.Base.patError"
286-
,"GHC.Err.undefined"
287-
,"GHC.Err.errorWithoutStackTrace"
288-
,"GHC.Internal.Err.undefined"
289-
,"GHC.Internal.Err.errorWithoutStackTrace"] ->
291+
| primName pInfo `elem` [ Text.showt 'patError
292+
, Text.showt 'undefined
293+
, Text.showt 'errorWithoutStackTrace] ->
290294
let e1 = mkApps (mkTicks (Prim pInfo) ticks)
291295
[repTy,Right ty,msgOrCallStack]
292296
in changed e1
@@ -300,7 +304,7 @@ caseCon' ctx@(TransformContext is0 _) e@(Case subj ty alts) = do
300304
-- WHNF of subject is _|_, in the form of `errorX`: that means that
301305
-- the entire case-expression is evaluates to _|_
302306
(Prim pInfo,_:callStack:msg:_,ticks)
303-
| primName pInfo == "Clash.XException.errorX"
307+
| primName pInfo == Text.showt 'errorX
304308
-> let e1 = mkApps (mkTicks (Prim pInfo) ticks) [Right ty,callStack,msg]
305309
in changed e1
306310
-- WHNF of subject is non of the above, so either a variable reference,

0 commit comments

Comments
 (0)