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

[Rust] Fixed recursive lambda captured idents cloning #3565

Merged
merged 1 commit into from
Oct 24, 2023
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
4 changes: 4 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

#### Rust

* Fixed recursive lambda captured idents cloning (by @ncave)

## 4.4.0 - 2023-10-24

### Changed
Expand Down
13 changes: 10 additions & 3 deletions src/Fable.Transforms/Rust/Fable2Rust.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3269,7 +3269,6 @@ module Util =
makeLibCall com ctx None "Native" ("fix" + argCount) fixCallArgs
else
fnBody
let closureExpr = mkClosureExpr true fnDecl fnBody
let cloneStmts =
// clone captured idents (in move closures)
// skip non-local idents (e.g. module let bindings)
Expand All @@ -3283,8 +3282,16 @@ module Util =
letExpr |> mkSemiStmt)
|> Seq.toList
let closureExpr =
if List.isEmpty cloneStmts then closureExpr
else mkStmtBlockExpr (cloneStmts @ [closureExpr |> mkExprStmt])
if List.isEmpty cloneStmts then
mkClosureExpr true fnDecl fnBody
else
let fnBody =
// additional captured idents cloning for recursive lambdas
if isRecursive && not isTailRec then
mkStmtBlockExpr (cloneStmts @ [fnBody |> mkExprStmt])
else fnBody
let closureExpr = mkClosureExpr true fnDecl fnBody
mkStmtBlockExpr (cloneStmts @ [closureExpr |> mkExprStmt])
let funcWrap = getLibraryImportName com ctx "Native" ("Func" + argCount)
makeCall [funcWrap; "new"] None [closureExpr]

Expand Down
Loading