@@ -3041,18 +3041,52 @@ module Util =
30413041 let transformCurry ( com : IRustCompiler ) ( ctx : Context ) arity ( expr : Fable.Expr ) : Rust.Expr =
30423042 com.TransformExpr( ctx, Replacements.Api.curryExprAtRuntime com arity expr)
30433043
3044+ let tryGetCurriedApplyArgAndReturnTypes typ =
3045+ match typ with
3046+ | Fable.LambdaType( argType, returnType) -> Some( argType, returnType)
3047+ | Fable.DelegateType([ argType ], returnType) -> Some( argType, returnType)
3048+ | _ -> None
3049+
3050+ let isErasedUnitClosureType typ =
3051+ match typ with
3052+ | Fable.LambdaType( Fable.Unit, _)
3053+ | Fable.DelegateType([ Fable.Unit ], _) -> true
3054+ | _ -> false
3055+
30443056 let transformCurriedApply ( com : IRustCompiler ) ctx r typ calleeExpr args =
30453057 match ctx.TailCallOpportunity with
30463058 | Some tc when tc.IsRecursiveRef( calleeExpr) && List.length tc.Args = List.length args ->
30473059 optimizeTailCall com ctx r tc args
30483060 | _ ->
30493061 let callee = transformCallee com ctx calleeExpr
30503062
3051- ( callee, args)
3052- ||> List.fold ( fun expr arg ->
3053- let args = FSharp2Fable.Util.dropUnitCallArg com [ arg ] [] None
3054- callFunction com ctx r expr args
3063+ (( callee, Some calleeExpr, calleeExpr.Type), args)
3064+ ||> List.fold ( fun ( expr , currentExpr , currentType ) arg ->
3065+ let expectedArgType , nextType =
3066+ match tryGetCurriedApplyArgAndReturnTypes currentType with
3067+ | Some( argType, returnType) -> argType, returnType
3068+ | None -> arg.Type, typ
3069+
3070+ if arg.Type = Fable.Unit then
3071+ let appliedExpr =
3072+ match currentExpr with
3073+ | Some( Fable.IdentExpr ident) when isFuncScoped ctx ident.Name -> mkCallExpr expr []
3074+ | _ -> makeLibCall com ctx None " Native" " applyUnit" [ expr |> makeClone ]
3075+
3076+ appliedExpr, None, nextType
3077+ else
3078+ let argExpr =
3079+ transformCallArgs com ctx [ arg ] [ expectedArgType ] [] |> List.exactlyOne
3080+
3081+ let argExpr =
3082+ if isErasedUnitClosureType expectedArgType then
3083+ makeLibCall com ctx None " Native" " eraseUnitArg" [ argExpr |> makeClone ]
3084+ else
3085+ argExpr
3086+
3087+ mkCallExpr expr [ argExpr ], None, nextType
30553088 )
3089+ |> fun ( expr , _ , _ ) -> expr
30563090
30573091 let makeUnionCasePat unionCaseName fields =
30583092 if List.isEmpty fields then
0 commit comments