@@ -34,6 +34,7 @@ type TypegenContext = {
3434type ScopedVarAttrs = {
3535 IsArm: bool
3636 IsRef: bool
37+ IsBox: bool
3738 HasMultipleUses: bool
3839}
3940
@@ -116,6 +117,9 @@ module UsageTracking =
116117 let isRefScoped ctx name =
117118 ctx.ScopedSymbols |> Map.tryFind name |> Option.map ( fun s -> s.IsRef) |> Option.defaultValue false
118119
120+ let isBoxScoped ctx name =
121+ ctx.ScopedSymbols |> Map.tryFind name |> Option.map ( fun s -> s.IsBox) |> Option.defaultValue false
122+
119123 let hasMultipleUses ( name : string ) =
120124 Map.tryFind name >> Option.map ( fun x -> x > 1 ) >> Option.defaultValue false
121125 //fun _ -> true
@@ -875,8 +879,10 @@ module Util =
875879 elif ident.IsMutable then
876880 expr |> mutableGet
877881 else
878- if isRefScoped ctx ident.Name && ( ctx.Typegen.TakingOwnership)
879- then makeClone expr // mkDerefExpr expr |> mkParenExpr
882+ if isBoxScoped ctx ident.Name
883+ then expr |> makeRcValue
884+ elif isRefScoped ctx ident.Name && ctx.Typegen.TakingOwnership
885+ then expr |> makeClone // |> mkDerefExpr |> mkParenExpr
880886 else expr
881887
882888 let transformIdentSet com ctx r ( ident : Fable.Ident ) ( value : Rust.Expr ) =
@@ -1537,6 +1543,7 @@ module Util =
15371543 |> Option.defaultValue {
15381544 IsArm = false
15391545 IsRef = false
1546+ IsBox = false
15401547 HasMultipleUses = true }
15411548 let isOnlyReference =
15421549 if varAttrs.IsRef then false
@@ -1749,7 +1756,7 @@ module Util =
17491756
17501757 let transformCallee ( com : IRustCompiler ) ctx calleeExpr =
17511758 let ctx = { ctx with Typegen = { ctx.Typegen with TakingOwnership = false } }
1752- com.TransformAsExpr ( ctx, calleeExpr)
1759+ transformExprMaybeIdentExpr com ctx calleeExpr
17531760
17541761 let isModuleMember ( com : IRustCompiler ) ( callInfo : Fable.CallInfo ) =
17551762 match callInfo.CallMemberInfo with
@@ -2051,6 +2058,7 @@ module Util =
20512058 let scopedVarAttrs = {
20522059 IsArm = false
20532060 IsRef = false
2061+ IsBox = false
20542062 HasMultipleUses = hasMultipleUses ident.Name usages
20552063 }
20562064 let ctxNext = { ctx with ScopedSymbols = ctx.ScopedSymbols |> Map.add ident.Name scopedVarAttrs }
@@ -2061,13 +2069,12 @@ module Util =
20612069 let ctx , letStmtsRev =
20622070 (( ctx, []), bindings)
20632071 ||> List.fold ( fun ( ctx , lst ) ( ident : Fable.Ident , expr ) ->
2064- let ( stmt , ctxNext ) =
2072+ let stmt , ctxNext =
20652073 match expr with
20662074 | Function ( args, body, _ name) ->
2067- let name = Some( ident.Name)
2068- let isCapturing = hasCapturedNames com ctx name args body
2075+ let isCapturing = hasCapturedNames com ctx ident.Name args body
20692076 if isCapturing then makeLetStmt com ctx usages ident expr
2070- else transformInnerFunction com ctx name args body
2077+ else transformInnerFunction com ctx ident.Name args body
20712078 | _ ->
20722079 makeLetStmt com ctx usages ident expr
20732080 ( ctxNext, stmt:: lst) )
@@ -2261,21 +2268,18 @@ module Util =
22612268 let body =
22622269 //com.TransformAsExpr(ctx, bodyExpr)
22632270 let usages = calcIdentUsages bodyExpr
2264-
2271+ let getScope name =
2272+ name, { IsArm = true
2273+ IsRef = true
2274+ IsBox = false
2275+ HasMultipleUses = hasMultipleUses name usages }
22652276 let symbolsAndNames =
22662277 let fromIdents =
22672278 idents
2268- |> List.map ( fun id ->
2269- id.Name, { IsArm = true
2270- IsRef = true
2271- HasMultipleUses = hasMultipleUses id.Name usages })
2272-
2279+ |> List.map ( fun id -> getScope id.Name)
22732280 let fromExtra =
22742281 extraVals
2275- |> List.map ( fun ( name , friendlyName , t ) ->
2276- friendlyName, { IsArm = true
2277- IsRef = true
2278- HasMultipleUses = hasMultipleUses friendlyName usages })
2282+ |> List.map ( fun ( _name , friendlyName , _t ) -> getScope friendlyName)
22792283 fromIdents @ fromExtra
22802284 let scopedSymbolsNext =
22812285 Helpers.Map.merge ctx.ScopedSymbols ( symbolsAndNames |> Map.ofList)
@@ -2401,7 +2405,8 @@ module Util =
24012405 let transformDecisionTreeSuccessAsExpr ( com : IRustCompiler ) ( ctx : Context ) targetIndex boundValues =
24022406 let bindings , target = getDecisionTargetAndBindValues com ctx targetIndex boundValues
24032407 match bindings with
2404- | [] -> com.TransformAsExpr( ctx, target)
2408+ | [] ->
2409+ transformExprMaybeUnwrapRef com ctx target
24052410 | bindings ->
24062411 let target = List.rev bindings |> List.fold ( fun e ( i , v ) -> Fable.Let( i, v, e)) target
24072412 com.TransformAsExpr( ctx, target)
@@ -2772,8 +2777,8 @@ module Util =
27722777 let allNames = name |> Option.fold ( fun xs x -> x :: xs) ( argNames @ fixedNames)
27732778 allNames |> Set.ofList
27742779
2775- let hasCapturedNames com ctx ( name : string option ) ( args : Fable.Ident list ) ( body : Fable.Expr ) =
2776- let ignoredNames = HashSet( getIgnoredNames name args)
2780+ let hasCapturedNames com ctx ( name : string ) ( args : Fable.Ident list ) ( body : Fable.Expr ) =
2781+ let ignoredNames = HashSet( getIgnoredNames ( Some name) args)
27772782 let isClosedOver expr =
27782783 isClosedOverIdent com ctx ignoredNames expr |> Option.isSome
27792784 FableTransforms.deepExists isClosedOver body
@@ -2798,6 +2803,7 @@ module Util =
27982803 let scopedVarAttrs = {
27992804 IsArm = false
28002805 IsRef = true
2806+ IsBox = false
28012807 HasMultipleUses = hasMultipleUses arg.Name usages
28022808 }
28032809 acc |> Map.add arg.Name scopedVarAttrs)
@@ -2948,9 +2954,9 @@ module Util =
29482954 | _ -> None)
29492955 |> mkGenerics
29502956
2951- let transformInnerFunction com ctx ( name : string option ) ( args : Fable.Ident list ) ( body : Fable.Expr ) =
2957+ let transformInnerFunction com ctx ( name : string ) ( args : Fable.Ident list ) ( body : Fable.Expr ) =
29522958 let fnDecl , fnBody , fnGenParams =
2953- transformFunction com ctx name args body
2959+ transformFunction com ctx ( Some name) args body
29542960 let fnBodyBlock =
29552961 if body.Type = Fable.Unit
29562962 then mkSemiBlock fnBody
@@ -2959,9 +2965,15 @@ module Util =
29592965 let generics = makeGenerics fnGenParams
29602966 let kind = mkFnKind header fnDecl generics ( Some fnBodyBlock)
29612967 let attrs = []
2962- let name = name |> Option.defaultValue " __"
29632968 let fnItem = mkFnItem attrs name kind |> mkNonPublicItem
2964- mkItemStmt fnItem, ctx
2969+ let scopedVarAttrs = {
2970+ IsArm = false
2971+ IsRef = false
2972+ IsBox = true
2973+ HasMultipleUses = true
2974+ }
2975+ let ctxNext = { ctx with ScopedSymbols = ctx.ScopedSymbols |> Map.add name scopedVarAttrs }
2976+ mkItemStmt fnItem, ctxNext
29652977
29662978 let transformModuleFunction ( com : IRustCompiler ) ctx ( decl : Fable.MemberDecl ) =
29672979 let name = splitLast decl.Name
0 commit comments