Skip to content

Add a refactoring between ?x and [<Struct>] ?x optional parameters - #20546

Open
xperiandri wants to merge 9 commits into
dotnet:mainfrom
xperiandri:feature/optional-parameter-struct-refactoring
Open

xperiandri wants to merge 9 commits into
dotnet:mainfrom
xperiandri:feature/optional-parameter-struct-refactoring

Conversation

@xperiandri

@xperiandri xperiandri commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Offers Ctrl+. on a member's optional parameter to switch it between option and voption (F# 10). defaultArg, Option functions and match cases on it in the body, and ?x = value arguments at call sites across the solution, are rewritten to match.

Not offered when a use of the parameter would change type (passed on, Option.map), for overrides, abstract slots and constructors, or when the file has a paired .fsi.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

✅ Release notes checked


✅ Found changes and release notes in following paths:

Change path Release notes path Description
`vsintegration/src` docs/release-notes/.VisualStudio/18.vNext.md

@xperiandri
xperiandri marked this pull request as ready for review September 14, 2026 19:40
@xperiandri
xperiandri requested a review from a team as a code owner September 14, 2026 19:40
@github-actions github-actions Bot added the ⚠️ Affects-Design-Time Tooling check: PR touches type providers or dependency manager label Sep 14, 2026
@github-actions

This comment has been minimized.

@T-Gro T-Gro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖🕵️ Please shorten the description using this guidance. Focus on the problem and why the change is needed, in simplified technical English. Leave the implementation inventory to the Files tab and retain necessary caveats.

@github-project-automation github-project-automation Bot moved this from New to In Progress in F# Compiler and Tooling Sep 15, 2026
@xperiandri xperiandri changed the title Refactoring between option and struct optional member parameters Add a refactoring between ?x and [<Struct>] ?x optional parameters Sep 15, 2026
@xperiandri

Copy link
Copy Markdown
Contributor Author

Covered cases

Converts

  • Parameter and body: member _.Greet(name: string, ?greeting: string) with defaultArg greeting "Hello" → [<Struct>] ?greeting: string with defaultValueArg greeting "Hello"
  • Call sites in the same file: ?greeting = Some "Hey" → ?greeting = ValueSome "Hey", and ?greeting = g (with g: string option) → ?greeting = ValueOption.ofOption g; Greet("Ada") and greeting = "Hi" stay as they are
  • Converting that whole file back gives back the original exactly (ValueSome "Hey" → Some "Hey", ValueOption.ofOption g → g)
  • match on the parameter: | Some s -> s | None -> 1 → | ValueSome s -> s | ValueNone -> 1, both ways
  • Option module functions, called directly or through a pipe, in a static member: Option.isSome step && step.IsSome then step |> Option.defaultValue 0 → ValueOption.isSome step && step.IsSome then step |> ValueOption.defaultValue 0 (IsSome unchanged), both ways
  • Expression argument at a call site: ?step = (if true then step else None) → ?step = ValueOption.ofOption (if true then step else None), both ways
  • A voption passed to a struct parameter, when converting back: ?step = step (with step: int voption) → ?step = ValueOption.toOption step
  • Action titles: Use 'voption' for optional parameter / Use 'option' for optional parameter, exactly one action each
  • Each converted document type-checks without errors

Not offered

  • Parameter passed to a function the conversion does not handle: static member M(?x: int) = printfn "%A" x
  • Option module function that is not supported: Option.map string x
  • Override of an abstract slot: abstract M: ?x: int -> int with default _.M(?x) = defaultArg x 0
  • Caret on the member name (M() instead of the parameter
  • Parameter that is not optional: static member M(x: int option) = defaultArg x 0
  • Before F# 10 (--langversion:9.0): static member M(?x: int) = defaultArg x 0
  • File that has a signature file: static member M: ?x: int -> int

@T-Gro T-Gro added the vsintegration-only Changes only Visual Studio integration, plus optional docs, release notes, or tests label Sep 21, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@xperiandri
xperiandri force-pushed the feature/optional-parameter-struct-refactoring branch from fd24fc7 to 6080188 Compare October 10, 2026 22:28

@T-Gro T-Gro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff is still way too big - apply the NoBloat instructions and CodeCompaction skill.

Especially the massive code comments would benefit not just with drastic reduction, but ideally with well named functions/arguments/bindings/types making the intention clear.

If a well named piece of code just tells what the comment says - delete the comment.

  • This diff adds 22 new code comment lines and 2176 new code comment chars.
  • Comments bypass the type system and become stale maintenance state. Keep only durable public contracts; express implementation intent through names, functions, active patterns, discriminated unions, and types.

@xperiandri
xperiandri force-pushed the feature/optional-parameter-struct-refactoring branch from f176a68 to ed9ca77 Compare October 15, 2026 00:47
@xperiandri

Copy link
Copy Markdown
Contributor Author

Commits d40c554 (source) and c9643f6 (tests) apply the compaction: added comment lines 22 -> 1 and comment characters 2176 -> 123, the one left being the license header, with the explanations now carried by names such as resolvesOnlyToFSharpCore, arityOfOptionKindIndependentFunction, tryEnclosingMember and isUseInMemberBody. ComputeRefactoringsAsync went from eight nested levels of control flow to three (tryCreateAction and changedSolution are their own functions), and the repeated symbol lookup and case check became tryGetSymbolUse and isConvertibleCase. The tests went from 14 methods to 9 and from 568 to 399 lines with the same 36 cases (every input and expected result is kept, the module M header comes from one helper, and each theory row has a name), so the PR now adds 1238 lines instead of 1435. Left as is: tryParameter still folds the whole tree, since ParsedInput.tryPick stops at the caret with different edge rules and would change behaviour.

xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 15, 2026
… commenting them

The doc comments of ConvertOptionalParameterDefaultValue.fs move into names:
the Form cases FSharpOptional and DotNetOptional, fsharpCoreNameAt,
IdentsAndLast, tryAnnotatedParameter, hasOnlyOptionalAttributes,
tryShadowingLineSpan, whiteSpaceBeforeBody, passesMissingValueWhenOmitted,
isUseInMemberBody and projectAndDependentsSupportStructOptionals. Only the
license header stays.

ComputeRefactoringsAsync nested nine levels of control flow. The actions are
now built by actionsFor and the solution by changedSolution, so the provider
nests three. tryForm and tryParameter go from four levels to two through
tryDotNetOptional and searchResultAt.

Repeated pieces are merged: tryGetSymbolUse serves the provider and
isFSharpCoreSymbol, documentAction builds the two document actions,
Range.rangeContainsPos replaces hand-written position comparisons and
SingleIdent replaces isOperator.

tryGetSymbolUse, isInFSharpCore and isFSharpCoreSymbol are the same text as in
ConvertOptionalParameterStruct.fs of the optional parameter struct
refactoring (PR dotnet#20546); the two pull requests keep their own copies.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 19, 2026
Port of the dotnet#20546 compaction (feature/optional-parameter-struct-refactoring
ed9ca77..c9643f6) to all-fixes:

- Name the optional parameter conversion steps instead of commenting them
- Parameterize the optional parameter conversion tests

The refactoring's member lookup is the one call site of getSymbolUses; it keeps
the RelatedSymbolUseKind.AllInCode argument that all-fixes needs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 19, 2026
Port of the dotnet#20546 compaction (feature/optional-parameter-struct-refactoring
ed9ca77..c9643f6) to all-fixes:

- Name the optional parameter conversion steps instead of commenting them
- Parameterize the optional parameter conversion tests

The refactoring's member lookup is the one call site of getSymbolUses; it keeps
the RelatedSymbolUseKind.AllInCode argument that all-fixes needs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
Port of the dotnet#20546 compaction (feature/optional-parameter-struct-refactoring
ed9ca77..c9643f6) to all-fixes:

- Name the optional parameter conversion steps instead of commenting them
- Parameterize the optional parameter conversion tests

The refactoring's member lookup is the one call site of getSymbolUses; it keeps
the RelatedSymbolUseKind.AllInCode argument that all-fixes needs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
Port of the dotnet#20546 compaction (feature/optional-parameter-struct-refactoring
ed9ca77..c9643f6) to all-fixes:

- Name the optional parameter conversion steps instead of commenting them
- Parameterize the optional parameter conversion tests

The refactoring's member lookup is the one call site of getSymbolUses; it keeps
the RelatedSymbolUseKind.AllInCode argument that all-fixes needs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 29, 2026
Port of the dotnet#20546 compaction (feature/optional-parameter-struct-refactoring
ed9ca77..c9643f6) to all-fixes:

- Name the optional parameter conversion steps instead of commenting them
- Parameterize the optional parameter conversion tests

The refactoring's member lookup is the one call site of getSymbolUses; it keeps
the RelatedSymbolUseKind.AllInCode argument that all-fixes needs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xperiandri
xperiandri force-pushed the feature/optional-parameter-struct-refactoring branch from c9643f6 to 829ccb7 Compare October 30, 2026 01:02
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 30, 2026
… commenting them

The doc comments of ConvertOptionalParameterDefaultValue.fs move into names:
the Form cases FSharpOptional and DotNetOptional, fsharpCoreNameAt,
IdentsAndLast, tryAnnotatedParameter, hasOnlyOptionalAttributes,
tryShadowingLineSpan, whiteSpaceBeforeBody, passesMissingValueWhenOmitted,
isUseInMemberBody and projectAndDependentsSupportStructOptionals. Only the
license header stays.

ComputeRefactoringsAsync nested nine levels of control flow. The actions are
now built by actionsFor and the solution by changedSolution, so the provider
nests three. tryForm and tryParameter go from four levels to two through
tryDotNetOptional and searchResultAt.

Repeated pieces are merged: tryGetSymbolUse serves the provider and
isFSharpCoreSymbol, documentAction builds the two document actions,
Range.rangeContainsPos replaces hand-written position comparisons and
SingleIdent replaces isOperator.

tryGetSymbolUse, isInFSharpCore and isFSharpCoreSymbol are the same text as in
ConvertOptionalParameterStruct.fs of the optional parameter struct
refactoring (PR dotnet#20546); the two pull requests keep their own copies.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
xperiandri and others added 9 commits November 8, 2026 02:57
?x and [<Struct>] ?x give the member body an option or a value option. The
refactoring toggles the attribute, renames what keeps its type in the body
(defaultArg, Option functions that do not return an option, match cases on the
parameter) and rewrites ?x = value arguments at the call sites in the solution.
Bodies that pass the parameter on or map it are refused.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…e of its section

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Some/None, defaultArg and Option.* in the member body are renamed only when
they resolve to FSharp.Core; a shadowed name leaves the refactoring off. At
call sites, a value built with a shadowed Some goes through
ValueOption.ofOption instead of being renamed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review findings on the refactoring between `?x` and `[<Struct>] ?x`:

- The names the conversion writes (`ValueSome`, `ValueOption.isSome`,
  `defaultValueArg` and their option counterparts) are checked where they
  are written: when user code shadows one, the member body is not offered
  the conversion, a call site keeps its value behind a conversion function,
  and that function is written with its full FSharp.Core name.
- Converting to a value option needs F# 10 in every F# project that depends
  on the declaring one, because their call sites are rewritten too.
- `[<param: Struct>] ?x` is a value option parameter, as it is for the compiler.
- An option module function is renamed only when the parameter is its last
  argument, and `|>` only counts when it is the FSharp.Core operator.
- A match clause that binds the parameter to a new name is not converted.
- No action when another overload already has the converted signature.
- A call of an instance member with explicit type arguments is rewritten.
- Dropping `ValueOption.ofOption` at a call site keeps the comment after it
  and no longer overlaps the edits of a call nested in its argument.
- The solution change is computed anew for every invocation of the action,
  so an invocation that was cancelled does not cancel the next one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The doc comments of ConvertOptionalParameterStruct.fs move into names:
resolvesOnlyToFSharpCore, arityOfOptionKindIndependentFunction,
tryEnclosingMember, tryRenamesOfUse, tryRenamesOfEveryUse, calleeEndsAt,
isUseInMemberBody, otherParametersToOwnTypes, ownAsListedByEntity and
projectAndDependentsSupportValueOptions. Only the license header stays.

ComputeRefactoringsAsync nested eight levels of control flow. The action is
now built by tryCreateAction and the solution by changedSolution, so the
provider nests three.

Repeated pieces are merged: tryGetSymbolUse serves the provider and
isFSharpCoreSymbol, isConvertibleCase names the check two call sites spelled
out, Range.rangeContainsPos replaces a hand-written position comparison and
SingleIdent replaces isOperator.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The module header comes from one helper instead of being written into 45
strings. The six one-off conversion facts become named rows of the two
conversion theories, and every row of the three theories gets a name, so a
failure says which scenario it was. The caret is the first question mark of
the source unless a row needs another marker.

Every input and every expected result is kept: 36 cases before and after.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Replaying the branch onto main left some files short of the branch's own content; this restores it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xperiandri
xperiandri force-pushed the feature/optional-parameter-struct-refactoring branch from 829ccb7 to 5b9dec3 Compare November 8, 2026 03:13
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Nov 8, 2026
… commenting them

The doc comments of ConvertOptionalParameterDefaultValue.fs move into names:
the Form cases FSharpOptional and DotNetOptional, fsharpCoreNameAt,
IdentsAndLast, tryAnnotatedParameter, hasOnlyOptionalAttributes,
tryShadowingLineSpan, whiteSpaceBeforeBody, passesMissingValueWhenOmitted,
isUseInMemberBody and projectAndDependentsSupportStructOptionals. Only the
license header stays.

ComputeRefactoringsAsync nested nine levels of control flow. The actions are
now built by actionsFor and the solution by changedSolution, so the provider
nests three. tryForm and tryParameter go from four levels to two through
tryDotNetOptional and searchResultAt.

Repeated pieces are merged: tryGetSymbolUse serves the provider and
isFSharpCoreSymbol, documentAction builds the two document actions,
Range.rangeContainsPos replaces hand-written position comparisons and
SingleIdent replaces isOperator.

tryGetSymbolUse, isInFSharpCore and isFSharpCoreSymbol are the same text as in
ConvertOptionalParameterStruct.fs of the optional parameter struct
refactoring (PR dotnet#20546); the two pull requests keep their own copies.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xperiandri
xperiandri force-pushed the feature/optional-parameter-struct-refactoring branch from 5b9dec3 to f38250f Compare November 18, 2026 22:00
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Nov 18, 2026
… commenting them

The doc comments of ConvertOptionalParameterDefaultValue.fs move into names:
the Form cases FSharpOptional and DotNetOptional, fsharpCoreNameAt,
IdentsAndLast, tryAnnotatedParameter, hasOnlyOptionalAttributes,
tryShadowingLineSpan, whiteSpaceBeforeBody, passesMissingValueWhenOmitted,
isUseInMemberBody and projectAndDependentsSupportStructOptionals. Only the
license header stays.

ComputeRefactoringsAsync nested nine levels of control flow. The actions are
now built by actionsFor and the solution by changedSolution, so the provider
nests three. tryForm and tryParameter go from four levels to two through
tryDotNetOptional and searchResultAt.

Repeated pieces are merged: tryGetSymbolUse serves the provider and
isFSharpCoreSymbol, documentAction builds the two document actions,
Range.rangeContainsPos replaces hand-written position comparisons and
SingleIdent replaces isOperator.

tryGetSymbolUse, isInFSharpCore and isFSharpCoreSymbol are the same text as in
ConvertOptionalParameterStruct.fs of the optional parameter struct
refactoring (PR dotnet#20546); the two pull requests keep their own copies.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@T-Gro

T-Gro commented Nov 30, 2026

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
No pipelines are associated with this pull request.

@T-Gro T-Gro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖🕵️

let definitionChanges =
OptionalParameterConversion.definitionChanges sourceText parameter renames

CodeAction.Create(title, changedSolution document sourceText checkResults memberUse parameter definitionChanges, title))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖🕵️ Shared state machine: a canceled preview cancels apply.

let c = cancellableTask { let! ct = CancellableTask.getCancellationToken ()
                          do! Task.Delay(300, ct) }
try (c (new CancellationTokenSource(50)).Token).Wait() with _ -> ()  // preview
(c CancellationToken.None).Wait()  // apply: TaskCanceledException

@T-Gro

T-Gro commented Dec 3, 2026

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚠️ Affects-Build-Infra Tooling check: PR touches build infrastructure ⚠️ Affects-Design-Time Tooling check: PR touches type providers or dependency manager vsintegration-only Changes only Visual Studio integration, plus optional docs, release notes, or tests

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

4 participants