Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FORK] Add runtime type check for function return types #1209

Merged
merged 6 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/cp/orders.pact
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

)

(defun with-order-status:object{order} (order-id status)
(defun with-order-status (order-id status)
"Check that order status is correct, returning details"
(let ((o (read orders order-id)))
(enforce (= (at 'status o) status)
Expand Down
9 changes: 8 additions & 1 deletion src/Pact/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,14 @@ functionApp fnName funTy mod_ as fnBody docs ai = do
let body = instantiate (resolveArg ai args') fnBody
fname = asString fnName
fa = FunApp ai fname mod_ Defun (funTypes fty) docs
guardRecursion fname mod_ $ appCall fa ai args' $ fmap (gas,) $ reduceBody body

returnVal <- guardRecursion fname mod_ $ appCall fa ai args' $ fmap (gas,) $ reduceBody body

unlessExecutionFlagSet FlagDisablePact47 $
typecheckTerm ai (_ftReturn fty) returnVal

return returnVal


-- | Evaluate a dynamic ref to either a fully-reduced value from a 'TConst'
-- or a module member 'Def' for applying.
Expand Down
24 changes: 24 additions & 0 deletions tests/pact/tc.repl
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,27 @@
"rtc object lists"
1
(rtc-object-list (let ((a {'a:1}) (b {'a: 2})) [a b])))

;; A module with incorrect annotations for a function's return type.
;; Runtime typechecking for function return types was added in
;; pact-4.7.
(module tc-test-function-return-types g
(defcap g () true)
(defun f:string () (+ 1 2))
)

(expect-failure
"evaluating a function whose type signature conflits with its body should fail"
(tc-test-function-return-types.f)
)

(expect-failure
"composing an ill-typed function with another function should not evaluate"
(tc-test-function-return-types.g (tc-test-function-return-types.f 1))
)

(env-exec-config ["DisablePact47"])
(expect
"evaluatingn ill-typed function should succeed with pact-4.7 features disabled"
imalsogreg marked this conversation as resolved.
Show resolved Hide resolved
3
(tc-test-function-return-types.f))