Skip to content

Add a refactoring between reference and struct anonymous records - #20549

Open
xperiandri wants to merge 17 commits into
dotnet:mainfrom
xperiandri:feature/anonymous-record-struct-refactoring
Open

xperiandri wants to merge 17 commits into
dotnet:mainfrom
xperiandri:feature/anonymous-record-struct-refactoring

Conversation

@xperiandri

@xperiandri xperiandri commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #20548.

Offers Ctrl+. to switch between {| … |} and struct {| … |}, following the value the same way as the tuple refactoring. A copy-and-update {| r with … |} has its own form, so it converts independently of r.

🤖 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.

@xperiandri xperiandri changed the title Refactoring between reference and struct anonymous records Add a refactoring between reference and struct anonymous records Sep 15, 2026
@xperiandri

Copy link
Copy Markdown
Contributor Author

Covered cases

Converts

  • An anonymous record used nowhere else converts on its own, and back: printfn "%A" {| A = 1 |} → printfn "%A" struct {| A = 1 |}; a type argument list<{| A: int |}> → list<struct {| A: int |}>; a multi-line {| … |} → struct {| … |}
  • A value converts together with the annotations it flows into, and field access is left as is, both ways: let person = {| Name = "Ada"; Age = 36 |} + let copy: {| Name: string; Age: int |} = person + person.Age → struct {| Name = "Ada"; Age = 36 |} + let copy: struct {| Name: string; Age: int |} = person
  • A parameter annotation converts every argument passed to it, both literals and values, both ways: let greet (p: {| Name: string |}) + let ada = {| Name = "Ada" |} + greet ada; greet {| Name = "Bob" |} → let greet (p: struct {| Name: string |}) + let ada = struct {| Name = "Ada" |} + greet struct {| Name = "Bob" |}
  • A return type converts the result and the annotated values it is bound to, both ways: let origin () : {| X: int; Y: int |} = {| X = 0; Y = 0 |} + let start: {| X: int; Y: int |} = origin () → struct {| X: int; Y: int |} in all three places
  • A record field converts only its own values and the annotations that read it, both ways: { Info: {| Age: int |}; Tags: {| Count: int |} } + { Info = {| Age = 30 |}; Tags = {| Count = 0 |} } + let info: {| Age: int |} = person.Info → only Info becomes struct {| Age: int |}, struct {| Age = 30 |} and let info: struct {| Age: int |}; Tags is not changed
  • A copy-and-update expression converts separately from its source, both ways: the caret on let point = {| X = 1 |} gives struct {| X = 1 |} and leaves {| point with Y = 2 |} alone; the caret on with gives struct {| point with Y = 2 |} and leaves point alone
  • A value declared in a second file converts in that file: the caret on let name (p: {| Name: string |}) + name A.person in module B also rewrites let person = {| Name = "Ada" |} in A to struct {| Name = "Ada" |}; the result is checked as a new project and has no errors
  • The action titles name the target kind: Convert to struct anonymous record and Convert to reference anonymous record
  • Every result is re-type-checked and asserted to have no errors

Not offered

  • The caret is not on an anonymous record: let x = 1
  • A normal record: type Point = { X: int } + let point = { X = 1 }
  • An anonymous record inside a quotation: <@ {| A = 1 |} @>

@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.

🤖 🕵️ AI review — verify independently.

useSource
(List.item index exprs)
(SyntaxNode.SynExpr tuple :: SyntaxNode.SynExpr argument :: argumentPath)
| ValueSome(struct (argument, argumentPath)), ValueNone -> this.Retarget useSource argument argumentPath

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.

🤖 🕵️ Converting a also converts the independent b argument, producing FS0001 against its explicit reference-record type.

module M

let identity<'T> (x: 'T) = x
let a = identity {| A = 1 |}  // Invoke conversion on A = 1.
let b = identity<{| B: int |}> {| B = 2 |}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 3021452: propagation stops at a parameter, result or value whose declared type involves a generic type parameter, so converting a leaves b alone. Test: Generic parameter stops the conversion.

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.

🤖🕵️ New alias forwarding bypasses the fixed generic boundary and converts independent b, producing FS0001.

let identity<'T> (x: 'T) = x
let alias = identity
let a = alias {| A = 1 |} // Convert this record to struct.
let b: {| B: int |} = alias {| B = 2 |}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in d623b91: isGenericParameter reads the parameter type from the alias's own function type when the alias has no parameter list (let alias = identity), so converting a leaves b alone. Test: this input as a row of Generic parameter stops the conversion; it failed with FS0001 before.

match isStruct, toStruct with
| true, true
| false, false -> []
| false, true -> [ TextChange(TextSpan((spanOf sourceText m).Start, 0), "struct ") ]

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.

🤖 🕵️ Converting this valid adjacent application produces idstruct {| A = 1 |}, causing FS0039.

module M

let x = id{| A = 1 |}  // Invoke conversion on A = 1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 3021452: struct inserted before {| gets a leading space after an identifier character (by the lexer's categories) or a closing bracket, using the helper shared with tuples. Test: Struct keyword is separated from a name the record follows (id{| A = 1 |} and a name ending in U+0301).

@T-Gro T-Gro added the AI-reviewed PR reviewed by AI review council label Sep 18, 2026
@T-Gro
T-Gro self-requested a review September 18, 2026 15:25
@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.

xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
Ctrl+. inside an anonymous record expression or annotated type converts
it between {| ... |} and struct {| ... |}, following the value through
the solution; the propagation engine is shared with the tuple
refactoring. From dotnet#20549 (release note left out:
18.vNext.md has local edits).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
…ents with their calls

From dotnet#20548 and dotnet#20549.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
…et#20549) up to their pull request heads

Their calls to SymbolHelpers.getSymbolUses ask for RelatedSymbolUseKind.AllInCode, as the XML doc rename work on this branch requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
…annotated patterns

The conversion now also follows a tuple through
- a function in parentheses, `(sum) (1, 2)`;
- a name bound to a lambda, its parameters and its result;
- a function alias and a partial application, `let alias = make`;
- a named argument, `M(p = pair)`;
- an annotated tuple pattern, `((a, b): int * int)`, from the pattern, the
  annotation or the value;
- a value whose binding head is in parentheses, `let (pair: int * int) = (1, 2)`.

It is no longer offered where the result cannot compile or nothing would change:
- on the annotation of a member's only argument, which is its parameter list;
- on the argument of a union case whose only field is declared a tuple;
- on the argument of a method in parentheses, `(c.Add) (1, 2)`;
- on a tuple over several lines without parentheses, which cannot change in place.

`struct` is kept apart from type arguments the parenthesis follows: `f<int> struct (1, 2)`.

This is b7bc303 of dotnet#20548 as dotnet#20549 merged it (3abaa70) into the engine
both kinds share: `StructConversion.fs` and `StructPropagation.fs` take what
went into `TupleConversion.fs` and `TuplePropagation.fs` there. A member's
parameter list is told by the kind, so the annotation of a member's only
argument stays convertible for an anonymous record.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 23, 2026
Port of the dotnet#20549 compaction (feature/anonymous-record-struct-refactoring
4c7066a..5be006c) to all-fixes. The range includes the pull request's
merge of the compacted tuple refactoring (dotnet#20548), so this one commit covers
both pull requests: all-fixes has the generalized engine of dotnet#20549
(StructPropagation.fs, TupleConversion.fs as a kind of that engine) and no
TuplePropagation.fs, and the merge kept that layout.

- dotnet#20548: Compact the tuple refactoring sources
- dotnet#20548: Share the cross-file refactoring test setup
- dotnet#20549: Compact the shared struct conversion sources
- dotnet#20549: Share the cross-file test setup of the conversion tests

StructPropagation.fs reads the related uses once now (ForEachUse) and keeps the
RelatedSymbolUseKind.AllInCode argument that all-fixes needs there.

RefactorTestFramework.fs merges the pull request's helpers with those of the
Extract suites that the earlier commits of this series put there.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xperiandri
xperiandri force-pushed the feature/anonymous-record-struct-refactoring branch from 0fe03f4 to f82e448 Compare October 29, 2026 22:03
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 29, 2026
Ctrl+. inside an anonymous record expression or annotated type converts
it between {| ... |} and struct {| ... |}, following the value through
the solution; the propagation engine is shared with the tuple
refactoring. From dotnet#20549 (release note left out:
18.vNext.md has local edits).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 29, 2026
…ents with their calls

From dotnet#20548 and dotnet#20549.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 29, 2026
…et#20549) up to their pull request heads

Their calls to SymbolHelpers.getSymbolUses ask for RelatedSymbolUseKind.AllInCode, as the XML doc rename work on this branch requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 29, 2026
…annotated patterns

The conversion now also follows a tuple through
- a function in parentheses, `(sum) (1, 2)`;
- a name bound to a lambda, its parameters and its result;
- a function alias and a partial application, `let alias = make`;
- a named argument, `M(p = pair)`;
- an annotated tuple pattern, `((a, b): int * int)`, from the pattern, the
  annotation or the value;
- a value whose binding head is in parentheses, `let (pair: int * int) = (1, 2)`.

It is no longer offered where the result cannot compile or nothing would change:
- on the annotation of a member's only argument, which is its parameter list;
- on the argument of a union case whose only field is declared a tuple;
- on the argument of a method in parentheses, `(c.Add) (1, 2)`;
- on a tuple over several lines without parentheses, which cannot change in place.

`struct` is kept apart from type arguments the parenthesis follows: `f<int> struct (1, 2)`.

This is b7bc303 of dotnet#20548 as dotnet#20549 merged it (3abaa70) into the engine
both kinds share: `StructConversion.fs` and `StructPropagation.fs` take what
went into `TupleConversion.fs` and `TuplePropagation.fs` there. A member's
parameter list is told by the kind, so the annotation of a member's only
argument stays convertible for an anonymous record.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 29, 2026
Port of the dotnet#20549 compaction (feature/anonymous-record-struct-refactoring
4c7066a..5be006c) to all-fixes. The range includes the pull request's
merge of the compacted tuple refactoring (dotnet#20548), so this one commit covers
both pull requests: all-fixes has the generalized engine of dotnet#20549
(StructPropagation.fs, TupleConversion.fs as a kind of that engine) and no
TuplePropagation.fs, and the merge kept that layout.

- dotnet#20548: Compact the tuple refactoring sources
- dotnet#20548: Share the cross-file refactoring test setup
- dotnet#20549: Compact the shared struct conversion sources
- dotnet#20549: Share the cross-file test setup of the conversion tests

StructPropagation.fs reads the related uses once now (ForEachUse) and keeps the
RelatedSymbolUseKind.AllInCode argument that all-fixes needs there.

RefactorTestFramework.fs merges the pull request's helpers with those of the
Extract suites that the earlier commits of this series put there.

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

@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.

🤖 🕵️ Re-reviewed all recent changes as of f82e448; LGTM

xperiandri and others added 17 commits November 8, 2026 02:58
Ctrl+. on a tuple expression, pattern or annotated tuple type converts it
to the other kind and follows the value through the solution: annotations
of values, parameters, record fields and function results it flows
through, tuple patterns taking it apart, and the arguments and values
flowing into it. Uses that cannot be followed (fst, snd, generic
collections) are left for the compiler to report.

CreateWithCodeAndDependency now tells FCS about both files, so the second
file can be type-checked.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Adding the second document to a single-file solution left Find All
References unable to see either file, so a chain through a function's
call sites stopped at the first file. The synthetic project gives both
files to the checker the way AddReturnTypeTests and FindReferencesTests
set up theirs.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The synthetic project's checker reads the other file from disk, so
checking the refactored document saw the old definition; a struct tuple
pattern happens to accept a reference tuple, which hid that.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…ents with their calls

The only argument of a member or constructor is its parameter list, not
a tuple, so it is no longer offered. A tuple that is a whole curried
argument of a function or member now converts the matching argument at
every call, and `struct` no longer runs into a name the parenthesis
follows (`f(a, b)`).

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>
…y and match aliases

A parenthesized tuple applied to a method, a constructor or a union case of
several fields is an argument list however the call is spaced. Results flow
through try/finally and try/with, a match clause that names the value passes
it on, and removing `struct` from a type keeps a comment before its
parenthesis. `struct` is kept apart from a name ending in any identifier
character the lexer accepts, combining marks included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…annotated patterns

The conversion now also follows a tuple through
- a function in parentheses, `(sum) (1, 2)`;
- a name bound to a lambda, its parameters and its result;
- a function alias and a partial application, `let alias = make`;
- a named argument, `M(p = pair)`;
- an annotated tuple pattern, `((a, b): int * int)`, from the pattern, the
  annotation or the value;
- a value whose binding head is in parentheses, `let (pair: int * int) = (1, 2)`.

It is no longer offered where the result cannot compile or nothing would change:
- on the annotation of a member's only argument, which is its parameter list;
- on the argument of a union case whose only field is declared a tuple;
- on the argument of a method in parentheses, `(c.Add) (1, 2)`;
- on a tuple over several lines without parentheses, which cannot change in place.

`struct` is kept apart from type arguments the parenthesis follows: `f<int> struct (1, 2)`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Delete the comments the code says by itself and name what they described, share the edit code of tuple expressions and patterns, share the use scan of the propagation engine and keep every function at three nested levels or fewer.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Move the test helpers into RefactorTestFramework, let them add the module M wrapper and run the two-file refactoring with its re-check as a new project, and assert both directions of a conversion in one helper.

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>
A value bound by `(a, b) as alias` reaches the tuple pattern and the alias
alike: the conversion now follows the value into both sides of the as
pattern, from the value, from either side, and through a parameter, so
neither side keeps the old tuple kind (FS0001).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
An annotation such as `Pair` for `type Pair = int * int` is a contract the
conversion cannot rewrite, and converting around it gave FS0001. The checker
resolves the annotation; when it abbreviates a tuple the conversion fails.
The conversion now runs before the action is offered, so a failed
conversion offers no action instead of one that changes nothing.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The tuple and anonymous record conversions now share one engine. The branch carried copies of the commits of the tuple branch and of several commits of main, so its history is rebuilt as this one commit on top of the tuple branch. It holds the refactoring, the shared engine with the review fixes, and the compaction made since they were first written.
The shared engine converts a record bound through `as` on both sides and
withholds the conversion when the record passes an annotation that
abbreviates an anonymous record, as it does for tuples.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
`let alias = identity` has no parameter list of its own, so the generic
boundary missed its `'T` parameter and converting one call of the alias
converted every other, unrelated call (FS0001). The parameter type now
comes from the alias's function type when it has no parameter list.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
In `p <- q` the value of `q` flows into `p`. Converting either side
missed the assignment and left the other with the old kind (FS0001):
the conversion now follows the assignment from the variable to the
assigned value and back.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Converting `p: int * int` reaches an unparenthesized tuple over several
lines that cannot be rewritten in place. The action used to be offered
and then change nothing; since the conversion runs before the action is
offered, no action is offered.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xperiandri
xperiandri force-pushed the feature/anonymous-record-struct-refactoring branch from f82e448 to ede89e3 Compare November 8, 2026 03:29

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 AI-reviewed PR reviewed by AI review council 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