Skip to content
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
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [JS/TS] Fix invalid syntax emitted when negating negative literals (fix #4251) (by @MangelMaxime)
* [Rust] Fix negative counting in CallInfo.GenericArgs (by @ncave)
* [Rust] Fix inline bindings and captured idents tracking (by @ncave)
* [Rust] Fix `return!` in async computation expressions so inner async workflows are returned and awaited correctly (by @mizzle-mo)
* [JS/TS] Improve `Regex.Escape` and `Regex.Unescape` handling (by @MangelMaxime)
* [All] Fix allow plugins to target .NET6 target framework (by @MangelMaxime)
* [Python] Fix function references passed as arguments inside tail-call optimised functions gaining unnecessary default parameters for outer TCO variables they don't reference (fix #3877)
Expand Down
1 change: 1 addition & 0 deletions src/Fable.Compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [JS/TS] Fix invalid syntax emitted when negating negative literals (fix #4251) (by @MangelMaxime)
* [Rust] Fix negative counting in CallInfo.GenericArgs (by @ncave)
* [Rust] Fix inline bindings and captured idents tracking (by @ncave)
* [Rust] Fix `return!` in async computation expressions so inner async workflows are returned and awaited correctly (by @mizzle-mo)
* [JS/TS] Improve `Regex.Escape` and `Regex.Unescape` handling (by @MangelMaxime)
* [All] Fix allow plugins to target .NET6 target framework (by @MangelMaxime)
* [Python] Fix function references passed as arguments inside tail-call optimised functions gaining unnecessary default parameters for outer TCO variables they don't reference (fix #3877)
Expand Down
3 changes: 3 additions & 0 deletions src/Fable.Transforms/Rust/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3023,6 +3023,9 @@ let asyncBuilder (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Exp
| "Return", _, _ ->
Helper.LibCall(com, "AsyncBuilder", "r_return", t, args, i.SignatureArgTypes, ?loc = r)
|> Some
| "ReturnFrom", _, _ ->
Helper.LibCall(com, "AsyncBuilder", "return_from", t, args, i.SignatureArgTypes, ?loc = r)
|> Some
| "Zero", _, _ ->
Helper.LibCall(com, "AsyncBuilder", "zero", t, args, i.SignatureArgTypes, ?loc = r)
|> Some
Expand Down
4 changes: 4 additions & 0 deletions src/fable-library-rust/src/Async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ pub mod AsyncBuilder_ {
})
}

pub fn return_from<T: Send + Sync + 'static>(computation: Arc<Async<T>>) -> Arc<Async<T>> {
computation
}

pub fn zero<T: Send + Sync + 'static>() -> Arc<Async<()>> {
r_return(())
}
Expand Down
49 changes: 49 additions & 0 deletions tests/Rust/tests/src/AsyncTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,55 @@ let shouldConvertTaskToASyncAndEvalCorrectly () =
let t = task { return 1 } |> Async.AwaitTask
t |> Async.RunSynchronously |> equal 1

[<Fact>]
let ``return! should compile in async CE`` () =
let inner () = async { return 42 }
let outer () = async { return! inner () }
let result = Async.RunSynchronously (outer ())
result |> equal 42

[<Fact>]
let ``return! works in recursive async CE`` () =
let rec loop n = async {
if n <= 0 then return 0
else return! loop (n - 1)
}
let result = Async.RunSynchronously (loop 5)
result |> equal 0

[<Fact>]
let ``return! is transparent through multiple layers`` () =
// Verifies return_from is identity: chaining return! does not double-wrap the computation
// or alter the value in any way.
let inner () = async { return 7 }
let passthrough (comp: Async<int>) = async { return! comp }
let result =
inner ()
|> passthrough
|> passthrough
|> passthrough
|> Async.RunSynchronously
result |> equal 7

[<Fact>]
let ``return! propagates value from async built with bind`` () =
// Verifies that a computation produced via let! / return is correctly
// passed through return!, not just a literal return value.
let doubled x = async {
let! v = async { return x }
return v * 2
}
let outer () = async { return! doubled 21 }
Async.RunSynchronously (outer ()) |> equal 42

[<Fact>]
let ``return! propagates error Result from inner async`` () =
// Both Ok and Error variants must flow through return! unchanged.
let inner (result: Result<int, string>) = async { return result }
let outer (result: Result<int, string>) = async { return! inner result }
Async.RunSynchronously (outer (Ok 99)) |> equal (Ok 99)
Async.RunSynchronously (outer (Error "oops")) |> equal (Error "oops")

// [<Fact>]
// let shouldExecAsParallelStructurallyCorrect () =
// let t = Async.Parallel [
Expand Down
Loading