Skip to content

Offer the open that brings an extension member into scope - #20670

Open
xperiandri wants to merge 8 commits into
dotnet:mainfrom
xperiandri:fix/add-open-extension-members
Open

xperiandri wants to merge 8 commits into
dotnet:mainfrom
xperiandri:fix/add-open-extension-members

Conversation

@xperiandri

Copy link
Copy Markdown
Contributor

Stacked on #20501.

For xs.Where(fun x -> x > 1) without open System.Linq the lightbulb offered nothing, and F# extension members were never offered at all. When the name is used as a member of a value or a type, the fix now offers the namespace of a C#-style extension method, or the module of an F# extension member together with what opens it when the module is [<AutoOpen>]. When the receiver's type is known, only extensions that apply to it are offered. A C#-style extension method written as a bare name is no longer offered open type of its class, because that does not make it available by name.

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings October 1, 2026 00:46
@xperiandri
xperiandri requested a review from a team as a code owner October 1, 2026 00:46
@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

✅ Release notes checked


✅ Found changes and release notes in following paths:

Warning

No PR link found in some release notes, please consider adding it.

Change path Release notes path Description
`src/Compiler` docs/release-notes/.FSharp.Compiler.Service/11.0.200.md No current pull request URL (#20670) found, please consider adding it
`vsintegration/src` docs/release-notes/.VisualStudio/18.vNext.md

@xperiandri

Copy link
Copy Markdown
Contributor Author

Covered cases

Offered

  • xs.Where(...) on an int list → open System.Linq, and no qualification
  • [ 1; 2; 3 ].Where(...) → every namespace with a Where extension (System.Data, System.Linq), since the type of an expression receiver is not known
  • numbers.AsSpan() on an int[] → open System, though the overload the assembly content keeps extends string
  • greeting.Shout for an F# extension property on string → open Extensions, placed after that module
  • s.Shout() from [<AutoOpen>] module StringExtensions in namespace Lib → open Lib and open Lib.StringExtensions
  • System.String.Shout "hi" for a static F# extension member → open Extensions
  • two modules with a Shout extension, one for string and one for int → only the one for string

Not offered

  • an F# extension member of string on an int
  • a module that requires qualified access
  • open type System.Linq.Enumerable for Where(xs, ...) written as a bare name

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Generic applicability, cancellation, and public API compatibility issues remain unresolved.

Review effort: Balanced
Findings: 1 High severity · 2 Medium severity

Open (3)
What changed in this PR

Extends the Add Open code fix to discover applicable C# and F# extension members, building on the open-placement and multi-suggestion work from #20501.

Changes:

  • Adds extension-member discovery and receiver filtering.
  • Supports multiple ordered open/qualification fixes and open type.
  • Consolidates open insertion logic and expands regression coverage.
File Description
vsintegration/​tests/​FSharp.Editor.Tests/​FSharp.Editor.Tests.fsproj Includes extension-member tests.
vsintegration/​tests/​FSharp.Editor.Tests/​CodeFixes/​AddOpenOnTopOnTests.fs Expands top-level placement tests.
vsintegration/​tests/​FSharp.Editor.Tests/​CodeFixes/​AddOpenOnTopOffTests.fs Expands nearest-scope placement tests.
vsintegration/​tests/​FSharp.Editor.Tests/​CodeFixes/​AddOpenForExtensionMembersTests.fs Covers C# and F# extensions.
vsintegration/​src/​FSharp.Editor/​Common/​RoslynHelpers.fs Centralizes open text changes.
vsintegration/​src/​FSharp.Editor/​CodeFixes/​AddOpenCodeFixProvider.fs Implements discovery and multiple fixes.
tests/​FSharp.Compiler.Service.Tests/​OpenDeclarationInsertionTests.fs Tests syntax-based insertion positions.
tests/​FSharp.Compiler.Service.Tests/​FSharp.Compiler.Service.SurfaceArea.netstandard20.bsl Records the FCS API change.
src/​Compiler/​Service/​ServiceParsedInputOps.fsi Exposes insertion metadata.
src/​Compiler/​Service/​ServiceParsedInputOps.fs Refines insertion and namespace analysis.
docs/​release-notes/​.VisualStudio/​18.vNext.md Documents editor behavior.
docs/​release-notes/​.FSharp.Compiler.Service/​11.0.200.md Documents FCS changes.

Comment on lines +191 to +194
let extendsAny (extensibleNames: HashSet<string> voption) (extension: FSharpMemberOrFunctionOrValue) =
match extensibleNames, tryGetExtendedDefinitionName extension with
| ValueSome names, Some extended -> names.Contains extended
| _ -> true

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 eda4f40: the receiver's types are kept with their type arguments, a C#-style extension method applies only when those match the type of its first parameter, and a type parameter there has to have its coercion constraints met. Covered by Offers a C# extension method only for the type arguments it extends and Offers a C# extension method on a type parameter only for a type that meets its constraints.

@T-Gro T-Gro added the AI-reviewed PR reviewed by AI review council label Oct 1, 2026
@T-Gro
T-Gro self-requested a review October 1, 2026 13:32
@xperiandri
xperiandri force-pushed the fix/add-open-extension-members branch from 0441d67 to 81bbdfa Compare October 2, 2026 16:03

@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 81bbdfa; LGTM

@xperiandri
xperiandri force-pushed the fix/add-open-extension-members branch from 81bbdfa to 515955c Compare October 8, 2026 01:11

@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 515955c; LGTM

@xperiandri
xperiandri force-pushed the fix/add-open-extension-members branch from 515955c to 591f67a 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 92 new code comment lines and 7842 new code comment chars.
  • GetCodeFixesAsync in AddOpenCodeFixProvider.fs:442-487 reaches at least 3 logical nesting levels.
  • Tests would benefit from deduplicated setup or code-as-test reuse: AddOpenOnTopOnTests.fs:171-195 and AddOpenOnTopOffTests.fs:150-174 copy the same attribute-on-header case, while AddOpenOnTopOnTests.fs:627-681 repeats the same module around four open declarations.

@github-project-automation github-project-automation Bot moved this from New to In Progress in F# Compiler and Tooling Oct 13, 2026
@xperiandri

Copy link
Copy Markdown
Contributor Author

Compacted in 7f44dba (the merge of the compacted #20501), 876d706 (provider) and 32a3ec1 (tests); behaviour unchanged. Added comment lines went from 92 to 11 (116 to 9 when trailing ones count; 5 without the 6 public API doc lines, two of them license headers of new test files; 8662 to 1181 characters) and added lines from 1561 to 995, with the comments on the receiver, the extension types and the open targets replaced by names (memberReceiverOf, receiverTypesInclude, extendsOneOf, getModuleOpenTargets).

The candidates block of GetCodeFixesAsync that the review points at (lines 442-487) nested a lambda, a lambda, a match, a loop and three ifs around a second loop, eight levels; it is now getNameCandidates over nameIdentsOf and getExtensionOpens, and the member reaches three levels (lambda, lambda, match). The new helpers stay at three at most, extendsOneOf is split into one function per kind of extension, and getSuggestionsAsCodeFixes keeps the shape it has in #20501.

The attribute-header case exists once, in AddOpenOnTopSharedTests.fs (from #20500), and the four-open WriteLine case is one table row (from #20501). AddOpenForExtensionMembersTests.fs went from 248 to 187 lines and from 12 test methods to 4 (two theories over named scenarios and two facts) over the same 14 cases. The tests are the same 65 AddOpen cases before and after (65/65, also on an Optimize=true build; FCS OpenDeclarationInsertion 23 and AssemblyContent 5 on net472 and SurfaceArea unchanged, completion tests as before apart from the known fsiShouldTriggerCompletionInFsxFile failure), and the one comment about caching per module stays because only the code shape cannot say it.

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

| Some thisParameter when thisParameter.Type.IsGenericParameter ->
thisParameter.Type.GenericParameter.Constraints
|> Seq.forall (fun typeConstraint ->
not typeConstraint.IsCoercesToConstraint

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.

🤖🕵️ Incompatible constrained extension is offered; applying the open changes FS0039 to FS0193.

let value = "text"
value.Constrained() // member requires 'T : struct

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 97c15d1 and fafda99: for an extension on a type parameter the receiver also has to meet the kind constraints (struct, class, new(), enum, delegate, unmanaged) before the open is offered, not only the types the parameter must coerce to. "text".Constrained() with 'T : struct no longer gets an open. Covered by the struct, class, new(), enum and delegate rows of Offers the opens of the extension members that apply.

@T-Gro T-Gro Oct 30, 2026 •

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.

🤖🕵️ Incompatible unmanaged receiver still passes the filter. open Lib: FS0039 → FS0193.

module Lib =
    open System.Runtime.CompilerServices
    [<Extension>]
    type Extensions =
        [<Extension>]
        static member Constrained<'T when 'T : unmanaged>(x: 'T) = x

[<Struct>]
type Managed = { Text: string }

module App =
    let useIt (x: Managed) = x.Constrained()

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 3312c20: a receiver counts as unmanaged only when it is a primitive number, bool or char, an enum, or a non-generic struct whose instance fields are all unmanaged. A struct with a reference-type field no longer gets the open. New rows in Offers the opens of the extension members that apply: a struct of numbers extends, a struct with a string field does not, a number extends, a string does not.

xperiandri added a commit to xperiandri/fsharp that referenced this pull request Oct 19, 2026
Port of the dotnet#20501 compaction (fix/add-open-type 6682313..db6709e) to
all-fixes, without the merge of dotnet#20500's head (the previous commit):

- Say in names what the open suggestions comment on
- Run the open type and every-candidate cases as theories

The comments on the suggestion caps, the System-first order, the qualifier span,
the entity lookup, the open versus open type decision and the operator filter
are gone. The provider keeps the extension member code that all-fixes has from
dotnet#20670; it is the one dotnet#20670's merge of this head resolved to
(642c00c..7f44dba). The release note bullets of dotnet#20501 were shortened
with the previous commit, as the stack's merge of dotnet#20500 shortens them.

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#20670 compaction (fix/add-open-extension-members
642c00c..32a3ec1) to all-fixes. The part of that range which merges the
compacted dotnet#20501 head came with the previous two commits; this one carries what
is specific to the extension member code fix:

- Find the extension opens in flat helpers named for what they find
- Run the extension member cases as theories

The release note bullet of dotnet#20670 is the shortened one.

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
Applies what the pull request gained after 9d7c8dc.

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#20501 compaction (fix/add-open-type 6682313..db6709e) to
all-fixes, without the merge of dotnet#20500's head (the previous commit):

- Say in names what the open suggestions comment on
- Run the open type and every-candidate cases as theories

The comments on the suggestion caps, the System-first order, the qualifier span,
the entity lookup, the open versus open type decision and the operator filter
are gone. The provider keeps the extension member code that all-fixes has from
dotnet#20670; it is the one dotnet#20670's merge of this head resolved to
(642c00c..7f44dba). The release note bullets of dotnet#20501 were shortened
with the previous commit, as the stack's merge of dotnet#20500 shortens them.

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#20670 compaction (fix/add-open-extension-members
642c00c..32a3ec1) to all-fixes. The part of that range which merges the
compacted dotnet#20501 head came with the previous two commits; this one carries what
is specific to the extension member code fix:

- Find the extension opens in flat helpers named for what they find
- Run the extension member cases as theories

The release note bullet of dotnet#20670 is the shortened one.

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
Applies what the pull request gained after 9d7c8dc.

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#20501 compaction (fix/add-open-type 6682313..db6709e) to
all-fixes, without the merge of dotnet#20500's head (the previous commit):

- Say in names what the open suggestions comment on
- Run the open type and every-candidate cases as theories

The comments on the suggestion caps, the System-first order, the qualifier span,
the entity lookup, the open versus open type decision and the operator filter
are gone. The provider keeps the extension member code that all-fixes has from
dotnet#20670; it is the one dotnet#20670's merge of this head resolved to
(642c00c..7f44dba). The release note bullets of dotnet#20501 were shortened
with the previous commit, as the stack's merge of dotnet#20500 shortens them.

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#20670 compaction (fix/add-open-extension-members
642c00c..32a3ec1) to all-fixes. The part of that range which merges the
compacted dotnet#20501 head came with the previous two commits; this one carries what
is specific to the extension member code fix:

- Find the extension opens in flat helpers named for what they find
- Run the extension member cases as theories

The release note bullet of dotnet#20670 is the shortened one.

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
Applies what the pull request gained after 9d7c8dc.

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#20501 compaction (fix/add-open-type 6682313..db6709e) to
all-fixes, without the merge of dotnet#20500's head (the previous commit):

- Say in names what the open suggestions comment on
- Run the open type and every-candidate cases as theories

The comments on the suggestion caps, the System-first order, the qualifier span,
the entity lookup, the open versus open type decision and the operator filter
are gone. The provider keeps the extension member code that all-fixes has from
dotnet#20670; it is the one dotnet#20670's merge of this head resolved to
(642c00c..7f44dba). The release note bullets of dotnet#20501 were shortened
with the previous commit, as the stack's merge of dotnet#20500 shortens them.

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#20670 compaction (fix/add-open-extension-members
642c00c..32a3ec1) to all-fixes. The part of that range which merges the
compacted dotnet#20501 head came with the previous two commits; this one carries what
is specific to the extension member code fix:

- Find the extension opens in flat helpers named for what they find
- Run the extension member cases as theories

The release note bullet of dotnet#20670 is the shortened one.

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
Applies what the pull request gained after 9d7c8dc.

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#20501 compaction (fix/add-open-type 6682313..db6709e) to
all-fixes, without the merge of dotnet#20500's head (the previous commit):

- Say in names what the open suggestions comment on
- Run the open type and every-candidate cases as theories

The comments on the suggestion caps, the System-first order, the qualifier span,
the entity lookup, the open versus open type decision and the operator filter
are gone. The provider keeps the extension member code that all-fixes has from
dotnet#20670; it is the one dotnet#20670's merge of this head resolved to
(642c00c..7f44dba). The release note bullets of dotnet#20501 were shortened
with the previous commit, as the stack's merge of dotnet#20500 shortens them.

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#20670 compaction (fix/add-open-extension-members
642c00c..32a3ec1) to all-fixes. The part of that range which merges the
compacted dotnet#20501 head came with the previous two commits; this one carries what
is specific to the extension member code fix:

- Find the extension opens in flat helpers named for what they find
- Run the extension member cases as theories

The release note bullet of dotnet#20670 is the shortened one.

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
Applies what the pull request gained after 9d7c8dc.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xperiandri
xperiandri requested a review from T-Gro October 29, 2026 22:09
xperiandri added a commit to xperiandri/fsharp that referenced this pull request Nov 1, 2026
…dotnet#20670

all-fixes carried an earlier form of these PRs. They have since changed in
what the compiler service exposes, where the open goes and how the tests read:

- The public records stay as on main. The editor gets the position of the use
  and the namespace ident count from the internal TryFindInsertionContextAt and
  FindNearestPointToInsertOpenDeclarationAt.
- The open stays out of a block comment that runs on from a module or
  namespace header.
- A nested module declared on one line with an attribute is entered only for a
  use in its body.
- A generic C# extension method is matched to its receiver by the kind
  constraints as well (struct, class, new(), enum, delegate, unmanaged).
- The provider keeps the candidate of a name in one record, and the
  active-pattern tests look the fix up by its message.

The release notes list each of the four editor notes and the compiler service
note once, and drop the NamespaceIdentCount note. The surface-area baseline is
main's again for InsertionContextEntity.

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 b078a64; LGTM

@xperiandri
xperiandri force-pushed the fix/add-open-extension-members branch from 630001a to c30ae8a Compare November 5, 2026 19:52
xperiandri and others added 8 commits November 8, 2026 03:04
The "open namespace" code fix and the completion that adds an open both
decided where the declaration goes by recognizing a module header in the
text of a line — `line.StartsWith "module" && not (line.EndsWith "=")` —
followed by arithmetic that assumed a blank line under that header. Every
shape the spelling missed put the open where it does not compile:
`[<AutoOpen>] module Ns.Name` written on one line read as an implicit
module and took the open above the module declaration, and a header with
no blank line under it took the open below the first declaration, leaving
the identifier as unresolved as before.

InsertionContext.Pos now means one thing for every ScopeKind — the first
line inside the scope — taken from the leading keyword the trivia records,
the name it introduces and, for a nested module, the `=`. Attributes and
doc comments sit above that line by construction, so no spelling has to be
recognized. AdjustInsertionPoint keeps only the cosmetic step of stepping
over the blank line under a header, and the editor's two insertion paths
now share one implementation, which also follows the file's own line
endings instead of the host's.

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

Main's active-pattern tests encoded two spacings that the code fix and the
completion each had: the code fix left no blank line between an adjacent
header and the open, the completion left none between a header and an open
placed under a blank line. Both now go through one change builder, so the
open always gets a blank line above it and below it, and
InsertionContext.Pos names the first line inside a namespace instead of its
header line.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…e body is unresolved

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A block comment that starts on the header line after the first declaration
has begun belongs to that declaration. Moving the end of the header past it
put the open after the use.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…open cannot reach

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…for an unmanaged receiver

Any struct was taken for unmanaged, so a struct with a reference-type field got an open that left the call failing with FS0193.
A receiver now counts as unmanaged when it is a primitive number, bool or char, an enum, or a non-generic struct whose
instance fields are all unmanaged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xperiandri
xperiandri force-pushed the fix/add-open-extension-members branch from c30ae8a to 3312c20 Compare November 8, 2026 03:13

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


definition.IsEnum
|| (definition.IsValueType
&& definition.GenericParameters.Count = 0

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.

🤖🕵️ Valid Cell<int> loses open Lib. Adding the open compiles.

[<Struct>]
type Cell<'T> = { Value: 'T }

module Lib =
    open System.Runtime.CompilerServices
    [<Extension>]
    type Extensions =
        [<Extension>]
        static member Constrained<'T when 'T : unmanaged>(value: 'T) = value

module App =
    let useIt (x: Cell<int>) = x.Constrained()

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

AI-reviewed PR reviewed by AI review council

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

3 participants