Repository navigation
Add a refactoring between option and struct partial active patterns - #20545
xperiandri wants to merge 8 commits into
Conversation
✅ Release notes checked
|
|
I appreciate all these PRs, they're bringing F# (VS) tooling closer to what C#'s had for ages, but I feel like you're just creating an ever-increasing mountain of work to review. I get anxious with every new PR notification, and it's not even my job to review them. Maybe take a break for a couple of weeks, making sure the existing PRs get over the line, and see where things stand afterwards? |
This comment has been minimized.
This comment has been minimized.
T-Gro
left a comment
There was a problem hiding this comment.
🤖🕵️ 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.
Covered casesConverts
Not offered
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Identifier rewriting mishandles escaped names, and unqualified or unresolved StructAttribute handling can corrupt valid code or metadata.
Review effort: Balanced
Findings: 1
Open (2)
What changed in this PR
Adds an IDE refactoring that converts partial active patterns between option and struct voption returns.
Changes:
- Implements syntax detection, semantic validation, and source rewriting.
- Adds comprehensive refactoring tests and project registration.
- Adds resources, localization placeholders, and release notes.
| File | Description |
|---|---|
vsintegration/src/FSharp.Editor/Refactor/ConvertActivePatternReturn.fs |
Implements the refactoring. |
vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj |
Includes the provider. |
vsintegration/src/FSharp.Editor/FSharp.Editor.resx |
Adds action titles. |
vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf |
Adds Czech resources. |
vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf |
Adds German resources. |
vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf |
Adds Spanish resources. |
vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf |
Adds French resources. |
vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf |
Adds Italian resources. |
vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf |
Adds Japanese resources. |
vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf |
Adds Korean resources. |
vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf |
Adds Polish resources. |
vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf |
Adds Portuguese resources. |
vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf |
Adds Russian resources. |
vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf |
Adds Turkish resources. |
vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf |
Adds Simplified Chinese resources. |
vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf |
Adds Traditional Chinese resources. |
vsintegration/tests/FSharp.Editor.Tests/Refactors/ConvertActivePatternReturnTests.fs |
Tests conversion and eligibility. |
vsintegration/tests/FSharp.Editor.Tests/FSharp.Editor.Tests.fsproj |
Includes the tests. |
docs/release-notes/.VisualStudio/18.vNext.md |
Documents the feature. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
| let private renamed (sourceText: SourceText) (isStruct: bool) (ident: Ident) = | ||
| let start = (spanOf sourceText ident.idRange).Start | ||
| let prefix = if Char.IsLower ident.idText[0] then "v" else "Value" | ||
|
|
||
| if isStruct then | ||
| TextChange(TextSpan(start, prefix.Length), "") | ||
| else | ||
| TextChange(TextSpan(start, 0), prefix) |
There was a problem hiding this comment.
Fixed in d6b9fff: a case or annotation name is now replaced as a whole identifier with the computed name, so ValueSome written between double backticks becomes Some. New cases with escaped identifiers in Option-returning active pattern converts to a struct one and Struct active pattern converts to an option-returning one.
| | Some target, Some name -> | ||
| String.Equals(target.idText, "return", StringComparison.Ordinal) | ||
| && (String.Equals(name.idText, "Struct", StringComparison.Ordinal) | ||
| || String.Equals(name.idText, "StructAttribute", StringComparison.Ordinal)) |
There was a problem hiding this comment.
Fixed in d6b9fff: the Struct return attribute is removed only when it resolves to Microsoft.FSharp.Core.StructAttribute; an attribute of another type with that name stays in place. In the other direction the conversion is not offered when StructAttribute would not resolve to the FSharp.Core one where [<return: Struct>] is inserted. Tests: a user-defined StructAttribute case in Struct active pattern converts to an option-returning one, and a new No action case.
abonie
left a comment
There was a problem hiding this comment.
🤖🕵️ AI review — verify independently.
| renamed sourceText target.IsStruct case | ||
|
|
||
| match target.Annotation with | ||
| | Annotation.Head head -> renamed sourceText target.IsStruct head |
There was a problem hiding this comment.
🤖🕵️ Renaming the annotation drops the alias’s payload mapping: int option here means int list option, but becomes int voption and fails FS0001. Resolve the annotation’s meaning before rewriting its head.
type option<'T> = 'T list Microsoft.FSharp.Core.option
let (|P|_|) x : int option = Some [x]
printfn "%A" (match 1 with P xs -> xs | _ -> [])There was a problem hiding this comment.
Fixed in d6b9fff: the head of the return annotation now has to resolve to the FSharp.Core option/voption type, like the cases. Your example is a new No action case.
399c658 to
fd60b83
Compare
abonie
left a comment
There was a problem hiding this comment.
🤖🕵️ AI review — verify independently.
| if isStruct then | ||
| TextChange(TextSpan(start, prefix.Length), "") | ||
| else | ||
| TextChange(TextSpan(start, 0), prefix) |
There was a problem hiding this comment.
🤖🕵️ Verified source case rewritten to a shadowed destination: conversion changes this program’s output from 1 to 11, while both versions compile. Check or qualify the generated ValueSome binding, not just the source Some.
let ValueSome x = ValueSome (x + 10)
let (|P|_|) x = Some x
printfn "%d" (match 1 with P x -> x | _ -> -1)There was a problem hiding this comment.
Fixed in d6b9fff: each case the conversion would write has to resolve to the FSharp.Core one at the place it is written (IsRelativeNameResolvableFromSymbol); otherwise the conversion is not offered. Your example is a new No action case, together with its mirror: let Some x = ... above a struct pattern.
| let changedDocument = | ||
| cancellableTask { | ||
| let changes = ActivePatternReturnConversion.changes sourceText target | ||
| return document.WithText(sourceText.WithChanges changes) |
There was a problem hiding this comment.
🤖🕵️ Definition-only edit breaks a direct function consumer: converting the pattern leaves result expecting option and produces FS0001. Match sites are not its only consumers; handle expression uses or suppress unsafe conversion.
let (|P|_|) x = Some x
let result: int option = (|P|_|) 1
printfn "%A" resultThere was a problem hiding this comment.
Fixed in d6b9fff: the conversion is not offered when the file uses the pattern as a function value. Your example is a new No action case. Uses in other files are not searched.
fd60b83 to
03e83a2
Compare
| let listSpan = spanOf sourceText list.Range | ||
| let line = sourceText.Lines.GetLineFromPosition listSpan.Start | ||
| let before = sourceText.ToString(TextSpan.FromBounds(line.Start, listSpan.Start)) | ||
| let after = sourceText.ToString(TextSpan.FromBounds(listSpan.End, line.End)) |
There was a problem hiding this comment.
🤖 🕵️ [P2] Applying the conversion to a multiline return attribute throws ArgumentOutOfRangeException instead of producing the option-returning pattern:
[<
return: Struct
>]
let (|P|_|) x = if x > 0 then ValueSome x else ValueNoneThis input typechecks and passes the source-symbol guard. Reproduced with both LF and CRLF: listSpan.End is on the closing line, but line.End belongs to the opening line, so TextSpan.FromBounds(listSpan.End, line.End) receives reversed bounds. Use the attribute list's ending line for the trailing-whitespace span and the end of the standalone block when removing the whole list.
There was a problem hiding this comment.
Fixed in d6b9fff: the trailing text is now read from the attribute list's last line, and a list that stands alone is removed through that line. Your example is a new case in Struct active pattern converts to an option-returning one.
03e83a2 to
9fe6cbe
Compare
T-Gro
left a comment
There was a problem hiding this comment.
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 16 new code comment lines and 1524 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.
71ea216 to
ca8b0c9
Compare
|
Pushed b1b3056: the 15 implementation comment lines are gone, so the PR adds one comment line, the license header (16 comment lines and 1524 characters before), and its added lines in total go from 1062 to 1047. The names carry what the doc comments said: |
T-Gro
left a comment
There was a problem hiding this comment.
🤖🕵️ AI review — verify independently.
| | Some attribute -> resolvesAt target.Keyword.Range attribute | ||
| | None -> false) | ||
|
|
||
| casesResolve && attributeResolves |
There was a problem hiding this comment.
🤖🕵️ Generated annotation binds to a local alias and produces FS0001.
type voption<'T> = 'T list Microsoft.FSharp.Core.voption
let (|P|_|) (x: int) : int option = Some xThere was a problem hiding this comment.
Fixed in 128da1b: no conversion is offered when a type definition in the file has the name the conversion would write: voption here, option in the other direction. New No action cases for both directions.
There was a problem hiding this comment.
🤖🕵️ Imported alias still permits a conversion that produces FS0001 in the isolated module check.
// Aliases.fs
module Aliases
module Value =
type voption<'T> = 'T list Microsoft.FSharp.Core.voption
// Consumer.fsx
#load "Aliases.fs"
open Aliases.Value
let (|P|_|) (x: int) : int option = Some xPort of the dotnet#20545 compaction (feature/active-pattern-struct-refactoring ca8b0c9..b1b3056) to all-fixes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Port of the dotnet#20545 compaction (feature/active-pattern-struct-refactoring ca8b0c9..b1b3056) to all-fixes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Port of the dotnet#20545 compaction (feature/active-pattern-struct-refactoring ca8b0c9..b1b3056) to all-fixes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Port of the dotnet#20545 compaction (feature/active-pattern-struct-refactoring ca8b0c9..b1b3056) to all-fixes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Port of the dotnet#20545 compaction (feature/active-pattern-struct-refactoring ca8b0c9..b1b3056) to all-fixes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
b1b3056 to
842013b
Compare
…patterns A single-case partial active pattern whose results are all Some/None (or all ValueSome/ValueNone) switches between the two: the result cases and the return type annotation are renamed, and [<return: Struct>] is added or removed. Match sites need no change. Local active patterns cannot carry attributes, so there the value option return alone makes them struct, which needs F# 9. 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>
After the parse-only check, type-check the file and require every result case to resolve to the FSharp.Core Some/None or ValueSome/ValueNone, and every raising function to the FSharp.Core one. A shadowed Some no longer gets renamed and changes what the program computes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… its edits inside their tokens The conversion is no longer offered when - the return annotation is not the FSharp.Core `option`/`voption`: with `type option<'T> = 'T list Microsoft.FSharp.Core.option` in scope, renaming the head of `int option` changed what the annotation meant; - a case it would write is shadowed where it writes it: `let ValueSome x = ...` above the pattern made the converted pattern call that function; - `StructAttribute` is shadowed where `[<return: Struct>]` would be inserted; - the file uses the pattern as a function value, `(|P|_|) 1`, whose callers see the return type change. Other files are not searched. A `Struct` return attribute that resolves to another type than the FSharp.Core one is left in place: a value option makes the pattern struct by itself, and that attribute is not the marker. A case or annotation name is now replaced as a whole identifier, so ``ValueSome`` in backticks becomes `Some` instead of losing five characters after the first backtick. Removing an attribute list that spans several lines no longer throws: the trailing text is read from the list's last line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Delete the fifteen implementation comment lines (the doc comments of the private helpers and one inline remark) and let the names say what they said: - isOfFSharpCoreEntity, with the entity lookup that sees through abbreviations and reaches the entity of a value or union case extracted as entityFullNameOf - existingNamesAreFSharpCore and writtenNamesAreFSharpCore tell the names the target has from the names the conversion writes - withoutNonFSharpCoreStructAttribute, structnessOfName, toggleValuePrefix, isUsedAsFunctionValue, tryOptionCasesOfOneKind and isOnBindingHeader carry what the remaining doc comments said Behaviour is unchanged. 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>
… name it would write The conversion renames the annotation `option` to `voption` (and back). A type with that name declared in the file, for example `type voption<'T> = 'T list voption`, hides the FSharp.Core one at the annotation, so the converted pattern no longer type checks. No conversion is offered when a type definition in the file has the written name. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
842013b to
128da1b
Compare
128da1b to
567865e
Compare
| | Annotation.Absent | ||
| | Annotation.Unsupported -> true | ||
|
|
||
| let isUsedAsFunctionValue |
There was a problem hiding this comment.
🤖🕵️ Public AP used as a function value elsewhere breaks (FS0001) and its compiled signature changes
// Lib.fs
module Lib
let (|P|_|) x = if x > 0 then Some x else None
// Use.fs
open Lib
let r : int option = (|P|_|) 1

Match sites don't change, so only the definition is edited. A local active pattern can't carry the attribute, so it converts to struct only on F# 9 or later. Not offered when a result isn't built from
Some/None(orValueSome/ValueNone), or when the file has a paired.fsi.🤖 Generated with Claude Code