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

use ImplicitZero instead of Return unit #773

Merged
merged 2 commits into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions src/fsharp/TypeChecker.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7512,12 +7512,17 @@ and TcComputationExpression cenv env overallTy mWhole interpExpr builderTy tpenv
| None ->
// This only occurs in final position in a sequence
match comp with
// "do! expr;" in final position is treated as { let! () = expr in return () }
// "do! expr;" in final position is treated as { let! () = expr in return () } when Return is provided or as { let! () = expr in zero } otherwise
| SynExpr.DoBang(rhsExpr,m) ->
let mUnit = rhsExpr.Range
let rhsExpr = mkSourceExpr rhsExpr
if isQuery then error(Error(FSComp.SR.tcBindMayNotBeUsedInQueries(),m))
trans true q varSpace (SynExpr.LetOrUseBang(NoSequencePointAtDoBinding, false, false, SynPat.Const(SynConst.Unit, mUnit), rhsExpr, SynExpr.YieldOrReturn((false,true), SynExpr.Const(SynConst.Unit,m), m),m)) translatedCtxt
let bodyExpr =
if isNil (TryFindIntrinsicOrExtensionMethInfo cenv env m ad "Return" builderTy) then
SynExpr.ImplicitZero m
else
SynExpr.YieldOrReturn((false,true), SynExpr.Const(SynConst.Unit, m), m)
trans true q varSpace (SynExpr.LetOrUseBang(NoSequencePointAtDoBinding, false, false, SynPat.Const(SynConst.Unit, mUnit), rhsExpr, bodyExpr, m)) translatedCtxt
// "expr;" in final position is treated as { expr; zero }
// Suppress the sequence point on the "zero"
| _ ->
Expand Down
43 changes: 43 additions & 0 deletions tests/fsharp/core/comprehensions/test.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,49 @@ module EnumPatternWithFunkyTypes_FSharp_1_0_13904 =
// This is allowed - 'a is known to be "bool"
let s = seq { for i in T true -> i }


module SideEffectListMonad =
type SideEffectListWithReturnBuilder(onReturn, onZero) =
member b.Bind(x:unit,f) :list<'b> = f()
member b.Combine(x:list<'a>,y:list<'a>) :list<'a> = List.append x y
member b.Delay(f:unit->list<'a>) :list<'a> = f()
member b.Return _ :list<'a> = onReturn(); []
member b.Zero() :list<'a> = onZero(); []
member b.Yield(x:'a) :list<'a> = [x]

let sideEffectListWithReturn onReturn onZero = SideEffectListWithReturnBuilder(onReturn, onZero)

type SideEffectListWithZeroBuilder(onZero) =
member b.Bind(x:unit,f) :list<'b> = f()
member b.Combine(x:list<'a>,y:list<'a>) :list<'a> = List.append x y
member b.Delay(f:unit->list<'a>) :list<'a> = f()
member b.Zero() :list<'a> = onZero(); []
member b.Yield(x:'a) :list<'a> = [x]

let sideEffectListWithZero onZero = SideEffectListWithZeroBuilder(onZero)

module SideEffectListTests =
#if Portable
let printfn s = printfn "%s" s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you copy/paste from near tests (good), but maybe this is good opportunity to cleanup this duplication, adding the printfn function at top level

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@enricosada maybe, or better to get rid of the root cause. I mean why isn't it simply working for Portable too?

#endif

let x0a : list<int> * int * int =
let calledReturn = ref 0
let onReturn () = calledReturn := !calledReturn + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the use of ref instead of mutable ( f# 4) here it's beacuse this need to run as f# 3.1 test? /cc @dsyme

let calledZero = ref 0
let onZero () = calledZero := !calledZero + 1
sideEffectListWithReturn onReturn onZero { yield 1
do! printfn "hello" }, !calledReturn, !calledZero
test "x0a" (x0a = ([1], 1, 0))

let x0b : list<int> * int =
let calledZero = ref 0
let onZero () = calledZero := !calledZero + 1
sideEffectListWithZero onZero { yield 1
do! printfn "hello" }, !calledZero
test "x0b" (x0b = ([1], 1))


let aa =
if !failures then (stdout.WriteLine "Test Failed"; exit 1)
else (stdout.WriteLine "Test Passed";
Expand Down