Skip to content

Commit 03e6514

Browse files
committed
[Rust] Minor cloning update
1 parent ce4ecf9 commit 03e6514

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/Fable.Transforms/Rust/Fable2Rust.fs

+19-7
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,11 @@ module UsageTracking =
131131
let isBoxScoped ctx name =
132132
ctx.ScopedSymbols |> Map.tryFind name |> Option.map (fun s -> s.IsBox) |> Option.defaultValue false
133133

134-
let usageCount name usages=
134+
let usageCount name usages =
135135
Map.tryFind name usages |> Option.defaultValue 0
136136

137137
let hasMultipleUses (name: string) =
138138
Map.tryFind name >> Option.map (fun x -> x > 1) >> Option.defaultValue true
139-
//fun _ -> true
140139

141140
module TypeInfo =
142141

@@ -230,6 +229,14 @@ module TypeInfo =
230229
not (ent |> hasStructuralEquality)
231230
| _ -> false
232231

232+
let hasMutableFields (com: IRustCompiler) (ent: Fable.Entity) =
233+
if ent.IsFSharpUnion then
234+
ent.UnionCases |> Seq.exists (fun uci ->
235+
uci.UnionCaseFields |> List.exists (fun fi -> fi.IsMutable)
236+
)
237+
else
238+
ent.FSharpFields |> Seq.exists (fun fi -> fi.IsMutable)
239+
233240
let isEntityOfType (com: IRustCompiler) isTypeOf entNames (ent: Fable.Entity) =
234241
if Set.contains ent.FullName entNames then
235242
true // already checked, avoids circular checks
@@ -330,6 +337,7 @@ module TypeInfo =
330337
let isCopyableEntity com entNames (ent: Fable.Entity) =
331338
not (ent.IsInterface)
332339
&& ent.IsValueType
340+
&& not (hasMutableFields com ent)
333341
&& (isEntityOfType com isCopyableType entNames ent)
334342

335343
let isEquatableType (com: IRustCompiler) entNames typ =
@@ -448,6 +456,7 @@ module TypeInfo =
448456
| Fable.DelegateType _
449457
| Fable.Option _
450458
| Fable.List _
459+
| Fable.Array _
451460
-> true
452461

453462
| Fable.AnonymousRecordType _ -> true
@@ -463,10 +472,13 @@ module TypeInfo =
463472
| Fable.UnionField _
464473
| Fable.FieldGet _
465474
| Fable.ListHead _
466-
| Fable.ListTail _ -> true
475+
| Fable.ListTail _
476+
-> true
477+
467478
| Fable.TupleIndex _
468479
| Fable.ExprGet _
469-
| Fable.UnionTag _ -> false
480+
| Fable.UnionTag _
481+
-> false
470482
// TODO: Add isForced flag to distinguish between value accessed in pattern matching or not
471483
| Fable.IdentExpr _
472484
-> true
@@ -1292,7 +1304,7 @@ module Util =
12921304
if shouldBeRefCountWrapped com ctx typ |> Option.isSome
12931305
then makeAsRef expr
12941306
else
1295-
if isRefScoped ctx name
1307+
if isRefScoped ctx name || (isInRefType com typ)
12961308
then expr
12971309
else expr |> mkAddrOfExpr
12981310

@@ -1620,8 +1632,8 @@ module Util =
16201632
expr |> mkAddrOfExpr
16211633
elif Option.exists (isByRefType com) t && not varAttrs.IsRef then //implicit syntax
16221634
expr |> mkAddrOfExpr
1623-
elif shouldBeRefCountWrapped com ctx e.Type |> Option.isSome && not isOnlyReference then
1624-
expr |> makeClone
1635+
// elif shouldBeRefCountWrapped com ctx e.Type |> Option.isSome && not isOnlyReference then
1636+
// expr |> makeClone
16251637
elif isCloneableType com e.Type && not isOnlyReference then
16261638
expr |> makeClone // TODO: can this clone be removed somehow?
16271639
elif varAttrs.IsRef then

0 commit comments

Comments
 (0)