Skip to content

Commit

Permalink
fix issue #643 with wrong return type
Browse files Browse the repository at this point in the history
  • Loading branch information
daanx committed Jan 29, 2025
1 parent 6210e0e commit 1e962be
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/Compile/TypeCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ typeCheck flags defs coreImports program0
unreturn penv

-- checkCoreDefs "unreturn"
let borrowed = borrowedExtendICore (coreProgram{ Core.coreProgDefs = coreDefs }) (defsBorrowed defs)
coreDefs1 <- Core.getCoreDefs
let borrowed = borrowedExtendICore (coreProgram{ Core.coreProgDefs = coreDefs1 }) (defsBorrowed defs)
checkFBIP penv (platform flags) newtypes borrowed gamma

-- initial simplify
Expand Down
17 changes: 9 additions & 8 deletions src/Type/Infer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -710,18 +710,19 @@ inferExpr propagated expect (App (Var name _ nameRng) [(_,expr)] rng) | name ==
= do allowed <- isReturnAllowed
if (False && not allowed)
then infError rng (text "illegal expression context for a return statement")
else do (tp,eff,core) <- inferExpr propagated expect expr
mbTp <- lookupInfName nameReturn -- (unqualify nameReturn)
else do mbTp <- lookupInfName nameReturn -- (unqualify nameReturn)
case mbTp of
Nothing
-> do infError rng (text "illegal context for a return statement")
inferExpr propagated expect expr
Just (_,retTp)
-> do inferUnify (checkReturn rng) (getRange expr) retTp tp
resTp <- Op.freshStar
let typeReturn = typeFun [(nameNil,tp)] typeTotal resTp
addRangeInfo nameRng (RM.Id (newName "return") (RM.NIValue "expr" tp "" False) [] False)
return (resTp, eff, Core.App (Core.Var (Core.TName nameReturn typeReturn)
(Core.InfoExternal [(Default,"return #1")])) [core])
-> do (tp,eff,core) <- inferExpr (Just (retTp,nameRng)) expect expr
inferUnify (checkReturn rng) (getRange expr) retTp tp
resTp <- Op.freshStar
let typeReturn = typeFun [(nameNil,tp)] typeTotal resTp
addRangeInfo nameRng (RM.Id (newName "return") (RM.NIValue "expr" tp "" False) [] False)
return (resTp, eff, Core.App (Core.Var (Core.TName nameReturn typeReturn)
(Core.InfoExternal [(Default,"return #1")])) [core])
-- | Assign expression
inferExpr propagated expect (App assign@(Var name _ arng) [lhs@(_,lval),rhs@(_,rexpr)] rng) | name == nameAssign
= case lval of
Expand Down
19 changes: 19 additions & 0 deletions test/cgen/return6.kk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// issue #643
fun test(c: char): maybe<char>
val c1 =
match f(c)
Just(c1) ->
c1
Nothing ->
return Nothing // or `return test(c)`

Just(c1)

fun f(c: char): maybe<char>
val x = match Just(c)
Just(_cx) -> return Nothing
Nothing -> Just(c)
x

fun main()
()
Empty file added test/cgen/return6.kk.out
Empty file.

0 comments on commit 1e962be

Please sign in to comment.