Repository navigation
Offer the open that brings an extension member into scope - #20670
xperiandri wants to merge 8 commits into
Conversation
✅ Release notes checked
Warning No PR link found in some release notes, please consider adding it.
|
Covered casesOffered
Not offered
|
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Generic applicability, cancellation, and public API compatibility issues remain unresolved.
Review effort: Balanced
Findings: 1
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. |
| let extendsAny (extensibleNames: HashSet<string> voption) (extension: FSharpMemberOrFunctionOrValue) = | ||
| match extensibleNames, tryGetExtendedDefinitionName extension with | ||
| | ValueSome names, Some extended -> names.Contains extended | ||
| | _ -> true |
There was a problem hiding this comment.
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.
0441d67 to
81bbdfa
Compare
81bbdfa to
515955c
Compare
515955c to
591f67a
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 92 new code comment lines and 7842 new code comment chars.
GetCodeFixesAsyncinAddOpenCodeFixProvider.fs:442-487reaches at least 3 logical nesting levels.- Tests would benefit from deduplicated setup or code-as-test reuse:
AddOpenOnTopOnTests.fs:171-195andAddOpenOnTopOffTests.fs:150-174copy the same attribute-on-header case, whileAddOpenOnTopOnTests.fs:627-681repeats the same module around four open declarations.
|
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 ( The The attribute-header case exists once, in |
T-Gro
left a comment
There was a problem hiding this comment.
🤖🕵️ AI review — verify independently.
| | Some thisParameter when thisParameter.Type.IsGenericParameter -> | ||
| thisParameter.Type.GenericParameter.Constraints | ||
| |> Seq.forall (fun typeConstraint -> | ||
| not typeConstraint.IsCoercesToConstraint |
There was a problem hiding this comment.
🤖🕵️ Incompatible constrained extension is offered; applying the open changes FS0039 to FS0193.
let value = "text"
value.Constrained() // member requires 'T : structThere was a problem hiding this comment.
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.
There was a problem hiding this comment.
🤖🕵️ 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()There was a problem hiding this comment.
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.
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>
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>
Applies what the pull request gained after 9d7c8dc. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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>
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>
Applies what the pull request gained after 9d7c8dc. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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>
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>
Applies what the pull request gained after 9d7c8dc. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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>
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>
Applies what the pull request gained after 9d7c8dc. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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>
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>
Applies what the pull request gained after 9d7c8dc. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…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>
630001a to
c30ae8a
Compare
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>
c30ae8a to
3312c20
Compare
T-Gro
left a comment
There was a problem hiding this comment.
🤖🕵️ AI review — verify independently.
|
|
||
| definition.IsEnum | ||
| || (definition.IsValueType | ||
| && definition.GenericParameters.Count = 0 |
There was a problem hiding this comment.
🤖🕵️ 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()

Stacked on #20501.
For
xs.Where(fun x -> x > 1)withoutopen System.Linqthe 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 offeredopen typeof its class, because that does not make it available by name.🤖 Generated with Claude Code