Skip to content

Commit

Permalink
Make sure caseCon only works on matching data constructors
Browse files Browse the repository at this point in the history
Fixes #2376
  • Loading branch information
leonschoorl committed Jan 27, 2023
1 parent b0e00cc commit dd094b3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clash-lib/src/Clash/Normalize/Transformations/Case.hs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ caseCon' ctx@(TransformContext is0 _) e@(Case subj ty alts) = do
_ -> changed (TyApp (Prim NP.undefined) ty)
where
-- Check whether the pattern matches the data constructor
equalCon (DataPat dcPat _ _) = dcTag dc == dcTag dcPat
equalCon (DataPat dcPat _ _) = dcUniq dc == dcUniq dcPat
equalCon _ = False

-- Decide whether the applied arguments of the data constructor should
Expand Down
1 change: 1 addition & 0 deletions tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ runClashTest = defaultMain $ clashTestRoot
, runTest "T2342A" def{hdlSim=[]}
, runTest "T2342B" def{hdlSim=[]}
, runTest "T2360" def{hdlSim=[],clashFlags=["-fclash-force-undefined=0"]}
, runTest "T2376_unsafeCoerce_Dict" def{hdlTargets=[VHDL],hdlSim=[]}
] <>
if compiledWith == Cabal then
-- This tests fails without environment files present, which are only
Expand Down
37 changes: 37 additions & 0 deletions tests/shouldwork/Issues/T2376_unsafeCoerce_Dict.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module T2376_unsafeCoerce_Dict where

import Clash.Prelude
import Data.Constraint
import Data.Proxy
import Unsafe.Coerce

data T depth = T (BitVector depth) deriving (Generic)

instance (1 <= CLog 2 depth, KnownNat depth) => NFDataX (T depth)

-- | if (2 <= n) holds, then (1 <= CLog 2 n) also holds.
oneLeCLog2n :: forall n . (2 <= n) => Proxy n -> Dict (1 <= CLog 2 n)
oneLeCLog2n Proxy = unsafeCoerce (Dict :: Dict ())

f ::
forall dom depth.
( HiddenClockResetEnable dom
, KnownNat depth
, 2 <= depth ) =>
Proxy depth ->
Signal dom Bool ->
Signal dom Bool
f Proxy =
case oneLeCLog2n (Proxy @depth) of
Dict -> mealy go (T 0)

where
go :: T depth -> Bool -> (T depth, Bool)
go (T n) True = (T (n + 1), False)
go (T n) False = (T (n - 1), True)
{-# NOINLINE f #-}

topEntity clk rst ena =
withClockResetEnable clk rst ena $
f @System @2 Proxy
{-# NOINLINE topEntity #-}

0 comments on commit dd094b3

Please sign in to comment.