Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix non-nested mutrec bindings
  • Loading branch information
dsyme committed Nov 15, 2021
commit db18cce2c44336e788f3bf42913bf2b35a7bb78d
22 changes: 17 additions & 5 deletions src/fsharp/IlxGen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7552,12 +7552,24 @@ and GenModuleDef cenv (cgbuf: CodeGenBuffer) qname lazyInitInfo eenv x =
GenExnDef cenv cgbuf.mgbuf eenvinner m tc
else
GenTypeDef cenv cgbuf.mgbuf lazyInitInfo eenvinner m tc
if mbinds |> List.forall (function ModuleOrNamespaceBinding.Binding _ -> true | _ -> false) then
let recBinds = mbinds |> List.choose (function ModuleOrNamespaceBinding.Binding recBind -> Some recBind | _ -> None)
GenLetRecBindings cenv cgbuf eenv (recBinds, m)
else
for mbind in mbinds do

// Generate chunks of non-nested bindings together to allow recursive fixups.
let mutable bindsRemaining = mbinds
while not bindsRemaining.IsEmpty do
match bindsRemaining with
| ModuleOrNamespaceBinding.Binding _ :: _ ->
let recBinds =
bindsRemaining
|> List.takeWhile (function ModuleOrNamespaceBinding.Binding _ -> true | _ -> false)
|> List.map (function ModuleOrNamespaceBinding.Binding recBind -> recBind | _ -> failwith "unexpected")
let otherBinds = mbinds |> List.skipWhile (function ModuleOrNamespaceBinding.Binding _ -> true | _ -> false)
GenLetRecBindings cenv cgbuf eenv (recBinds, m)
bindsRemaining <- otherBinds
| (ModuleOrNamespaceBinding.Module _ as mbind) :: rest ->
GenModuleBinding cenv cgbuf qname lazyInitInfo eenvinner m mbind
bindsRemaining <- rest
| [] -> failwith "unreachable"

eenvinner

| TMDefLet(bind, _) ->
Expand Down
62 changes: 62 additions & 0 deletions tests/fsharp/core/letrec/test.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,68 @@ module Test12384 =
test "cweewlwne5" two.Next.Value 1
test "cweewlwne6" two.Next.Next.Value 2

module Test12384b =
type Node =
{
Next: Node
Value: int
}

let rec one =
{
Next = two
Value = 1
}

and two =
{
Next = one
Value = 2
}
// Also test the case where the two recursive bindings occur with a nested module after
module M =
let f x = x + 1

printfn "%A" one
printfn "%A" two
test "cweewlwne1a" one.Value 1
test "cweewlwne2a" one.Next.Value 2
test "cweewlwne3a" one.Next.Next.Value 1
test "cweewlwne4a" two.Value 2
test "cweewlwne5a" two.Next.Value 1
test "cweewlwne6a" two.Next.Next.Value 2

module rec Test12384c =
type Node =
{
Next: Node
Value: int
}

let one =
{
Next = two
Value = 1
}

let two =
{
Next = one
Value = 2
}
// Also test the case where the two recursive bindings occur with a nested module after
module M =
let f x = x + 1

printfn "%A" one
printfn "%A" two
test "cweewlwne1b" one.Value 1
test "cweewlwne2b" one.Next.Value 2
test "cweewlwne3b" one.Next.Next.Value 1
test "cweewlwne4b" two.Value 2
test "cweewlwne5b" two.Next.Value 1
test "cweewlwne6b" two.Next.Next.Value 2

#if TESTS_AS_APP
let RUN() = !failures
#else
Expand Down