Skip to content

Commit 2c5bda2

Browse files
authored
Fix a shadowing bug in generating recursive types. (IntersectMBO#3934)
* Remove a workaround for a fixed bug * Insert more pedantic checks * Fix a bug relating to reusing a variable name in two binders This turns out to be benign so long as you rename, but we might as well not introduce extra pointless shadowing if we don't have to. * Rename before typechecking when doing paranoid checks * Fix tests with uniques in
1 parent f6853e2 commit 2c5bda2

File tree

6 files changed

+13
-14
lines changed

6 files changed

+13
-14
lines changed

plutus-core/plutus-core/stdlib/PlutusCore/StdLib/Type.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,10 +744,12 @@ makeRecursiveType1
744744
makeRecursiveType1 ann dataName argVar patBody1 = do
745745
let varName = _tyVarDeclName argVar
746746
varKind = _tyVarDeclKind argVar
747+
varName' <- freshenTyName varName
748+
let
747749
recKind = KindArrow ann varKind $ Type ann
748750
pat1 = TyLam ann dataName recKind $ TyLam ann varName varKind patBody1
749751
-- recType = \(v :: k) -> ifix (\(dataName :: k -> *) (v :: k) -> patBody1) v
750-
recType = TyLam ann varName varKind . TyIFix ann pat1 $ TyVar ann varName
752+
recType = TyLam ann varName' varKind . TyIFix ann pat1 $ TyVar ann varName'
751753
wrap args = case args of
752754
[arg] -> iWrap ann pat1 arg
753755
_ -> throw . IndicesLengthsMismatchException 1 (length args) $ dataName

plutus-core/plutus-ir/src/PlutusIR/Compiler.hs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ typeCheckTerm t = do
141141
check :: Compiling m e uni fun b => Term TyName Name uni fun (Provenance b) -> m ()
142142
check arg = do
143143
shouldCheck <- view (ccOpts . coPedantic)
144-
if shouldCheck then typeCheckTerm arg else pure ()
144+
-- the typechecker requires global uniqueness, so rename here
145+
if shouldCheck then typeCheckTerm =<< PLC.rename arg else pure ()
145146

146147
-- | The 1st half of the PIR compiler pipeline up to floating/merging the lets.
147148
-- We stop momentarily here to give a chance to the tx-plugin
@@ -185,25 +186,21 @@ compileReadableToPlc =
185186
>=> through check
186187
>=> (<$ logVerbose " !!! compileLets RecTerms")
187188
>=> Let.compileLets Let.RecTerms
188-
-- TODO: add 'check' steps from here on. Can't do this since we seem to generate a wrong type
189-
-- somewhere in the recursive binding compilation step
190189
>=> through check
191190
-- We introduce some non-recursive let bindings while eliminating recursive let-bindings, so we
192191
-- can eliminate any of them which are unused here.
193-
>=> (<$ logVerbose " !!! rename")
194-
>=> PLC.rename
195-
>=> through check
196-
-- NOTE: There was a bug in renamer handling non-rec terms, so we need to
197-
-- rename again.
198-
-- https://jira.iohk.io/browse/SCP-2156
199192
>=> (<$ logVerbose " !!! removeDeadBindings")
200193
>=> DeadCode.removeDeadBindings
194+
>=> through check
201195
>=> (<$ logVerbose " !!! simplifyTerm")
202196
>=> simplifyTerm
197+
>=> through check
203198
>=> (<$ logVerbose " !!! compileLets Types")
204199
>=> Let.compileLets Let.Types
200+
>=> through check
205201
>=> (<$ logVerbose " !!! compileLets NonRecTerms")
206202
>=> Let.compileLets Let.NonRecTerms
203+
>=> through check
207204
>=> (<$ logVerbose " !!! lowerTerm")
208205
>=> lowerTerm
209206

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(delay (lam x_138 x_138))
1+
(delay (lam x_85 x_85))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(delay (lam case_True_365 (lam case_False_366 case_False_366)))
1+
(delay (lam case_True_228 (lam case_False_229 case_False_229)))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(fun (con integer) (all a_48 (type) (fun a_48 a_48)))
1+
(fun (con integer) (all a_49 (type) (fun a_49 a_49)))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[ Forest_11 (all a_42 (type) (fun a_42 a_42)) ]
1+
[ Forest_11 (all a_44 (type) (fun a_44 a_44)) ]

0 commit comments

Comments
 (0)