Skip to content

Commit ea4d877

Browse files
Sanity check types in 'piResultTys'/'piResultTyMaybe'
1 parent 1c5c939 commit ea4d877

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
{-# LANGUAGE CPP #-}
1010
{-# LANGUAGE NamedFieldPuns #-}
1111
{-# LANGUAGE OverloadedStrings #-}
12+
{-# LANGUAGE QuasiQuotes #-}
1213
{-# LANGUAGE TemplateHaskell #-}
1314

1415
module Clash.Core.Util where
@@ -23,6 +24,7 @@ import Data.List
2324
import Data.List.Extra (nubOrd)
2425
import Data.Maybe
2526
(fromJust, isJust, mapMaybe, catMaybes)
27+
import qualified Data.String.Interpolate as I
2628
import qualified Data.Text as T
2729
import Data.Text.Prettyprint.Doc (line)
2830
#if !MIN_VERSION_base(4,11,0)
@@ -39,7 +41,7 @@ import Clash.Core.Name
3941
import Clash.Core.Pretty (ppr, showPpr)
4042
import Clash.Core.Subst
4143
(extendTvSubst, mkSubst, mkTvSubst, substTy, substTyWith,
42-
substTyInVar, extendTvSubstList)
44+
substTyInVar, extendTvSubstList, aeqType)
4345
import Clash.Core.Term
4446
(LetBinding, Pat (..), PrimInfo (..), Term (..), Alt, WorkInfo (..),
4547
TickInfo (..), collectArgs)
@@ -327,7 +329,8 @@ applyTypeToArgs e m opTy args = go opTy args
327329
-- Do not iterate 'piResultTy', because it's inefficient to substitute one
328330
-- variable at a time; instead use 'piResultTys'
329331
piResultTy
330-
:: TyConMap
332+
:: HasCallStack
333+
=> TyConMap
331334
-> Type
332335
-> Type
333336
-> Type
@@ -340,15 +343,26 @@ piResultTy m ty arg = case piResultTyMaybe m ty arg of
340343
-- Do not iterate 'piResultTyMaybe', because it's inefficient to substitute one
341344
-- variable at a time; instead use 'piResultTys'
342345
piResultTyMaybe
343-
:: TyConMap
346+
:: HasCallStack
347+
=> TyConMap
344348
-> Type
345349
-> Type
346350
-> Maybe Type
347351
piResultTyMaybe m ty arg
348352
| Just ty' <- coreView1 m ty
349353
= piResultTyMaybe m ty' arg
350-
| FunTy _ res <- tyView ty
351-
= Just res
354+
| FunTy a res <- tyView ty
355+
= if debugIsOn && not (aeqType a arg) then error [I.i|
356+
Unexpected application. A function with type:
357+
358+
#{showPpr ty}
359+
360+
Got applied to an argument of type:
361+
362+
#{showPpr arg}
363+
|]
364+
else
365+
Just res
352366
| ForAllTy tv res <- ty
353367
= let emptySubst = mkSubst (mkInScopeSet (tyFVsOfTypes [arg,res]))
354368
in Just (substTy (extendTvSubst emptySubst tv arg) res)
@@ -379,16 +393,27 @@ piResultTyMaybe m ty arg
379393
-- For efficiency reasons, when there are no foralls, we simply drop arrows from
380394
-- a function type/kind.
381395
piResultTys
382-
:: TyConMap
396+
:: HasCallStack
397+
=> TyConMap
383398
-> Type
384399
-> [Type]
385400
-> Type
386401
piResultTys _ ty [] = ty
387402
piResultTys m ty origArgs@(arg:args)
388403
| Just ty' <- coreView1 m ty
389404
= piResultTys m ty' origArgs
390-
| FunTy _ res <- tyView ty
391-
= piResultTys m res args
405+
| FunTy a res <- tyView ty
406+
= if debugIsOn && not (aeqType a arg) then error [I.i|
407+
Unexpected application. A function with type:
408+
409+
#{showPpr ty}
410+
411+
Got applied to an argument of type:
412+
413+
#{showPpr arg}
414+
|]
415+
else
416+
piResultTys m res args
392417
| ForAllTy tv res <- ty
393418
= go (extendVarEnv tv arg emptyVarEnv) res args
394419
| otherwise

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ applyDebug lvl _transformations name exprOld hasChanged exprNew =
220220
, "substitution."
221221
])
222222

223-
traceIf (lvl >= DebugAll && (beforeTy `aeqType` afterTy))
223+
traceIf (lvl >= DebugApplied && (not (beforeTy `aeqType` afterTy)))
224224
( concat [ $(curLoc)
225225
, "Error when applying rewrite ", name
226226
, " to:\n" , before

0 commit comments

Comments
 (0)