Skip to content

Commit 69cd2fe

Browse files
xperiandriclaude
andcommitted
Withhold function extraction that would mutate a copy of a mutable struct
A by-value parameter of a struct type is a copy, so s.Inc() on it left the caller's mutable s unchanged. When a mutable struct local that would become a parameter is the receiver of a member call, a field or indexer access or an address-of in the selection, no function extraction is offered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 17f695a commit 69cd2fe

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎vsintegration/src/FSharp.Editor/Refactor/ExtractFunction.fs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,28 @@ module private FunctionExtraction =
203203
parameters
204204
|> List.exists (fun parameter -> parameter.Uses |> List.exists (fun m -> assigned.Contains m.Start))
205205

206+
let mutatesStructCopy (expr: SynExpr) (parameters: Parameter list) =
207+
let receivers =
208+
(HashSet<pos>(), [ SyntaxNode.SynExpr expr ])
209+
||> SyntaxNodes.fold (fun positions _ node ->
210+
match node with
211+
| SyntaxNode.SynExpr(SynExpr.LongIdent(longDotId = SynLongIdent(id = head :: _ :: _)))
212+
| SyntaxNode.SynExpr(SynExpr.LongIdentSet(longDotId = SynLongIdent(id = head :: _ :: _)))
213+
| SyntaxNode.SynExpr(SynExpr.DotGet(expr = SynExpr.Ident head))
214+
| SyntaxNode.SynExpr(SynExpr.DotSet(targetExpr = SynExpr.Ident head))
215+
| SyntaxNode.SynExpr(SynExpr.DotIndexedGet(objectExpr = SynExpr.Ident head))
216+
| SyntaxNode.SynExpr(SynExpr.DotIndexedSet(objectExpr = SynExpr.Ident head))
217+
| SyntaxNode.SynExpr(SynExpr.AddressOf(expr = SynExpr.Ident head)) -> positions.Add head.idRange.Start |> ignore
218+
| _ -> ()
219+
220+
positions)
221+
222+
parameters
223+
|> List.exists (fun parameter ->
224+
parameter.Value.IsMutable
225+
&& parameter.Value.FullTypeSafe |> Option.exists isStruct
226+
&& parameter.Uses |> List.exists (fun m -> receivers.Contains m.Start))
227+
206228
let rec private identifierArguments (argument: SynExpr) =
207229
match argument with
208230
| SynExpr.Ident ident -> [ ident ]
@@ -416,6 +438,7 @@ module private FunctionExtraction =
416438
| ValueSome captures when
417439
not (assignsCapturedValue target.Expr captures.Parameters)
418440
&& not (mutableCapturedByClosure extraction.CheckResults declaration captures.Parameters)
441+
&& not (mutatesStructCopy target.Expr captures.Parameters)
419442
->
420443
let annotate =
421444
annotationOf setting (positionsNeedingType target.Expr captures.MethodNameEnds)

‎vsintegration/tests/FSharp.Editor.Tests/Refactors/ExtractFunctionTests.fs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,21 @@ let f () =
336336
""",
337337
"(bump (); n)",
338338
"")>]
339+
[<InlineData("Mutable struct local the selection calls a member on",
340+
"""
341+
[<Struct>]
342+
type S =
343+
val mutable X: int
344+
new(x) = { X = x }
345+
member self.Inc() = self.X <- self.X + 1
346+
347+
let f () =
348+
let mutable s = S(1)
349+
s.Inc()
350+
s.X
351+
""",
352+
"s.Inc()",
353+
"")>]
339354
[<InlineData("Mutable local read at once becomes a parameter",
340355
"""
341356
let f () =

0 commit comments

Comments
 (0)