Skip to content

Commit 7fa6d34

Browse files
xperiandriclaude
andcommitted
Don't offer open type for a type inside a generic type
A generic type cannot be opened without type arguments, and the name the suggestion is built from does not carry them: for `Dictionary.KeyCollection` the fix offered `open type System.Collections.Generic.Dictionary`, which is FS0033. The qualifications are still offered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 51bd3c2 commit 7fa6d34

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

‎docs/release-notes/.VisualStudio/18.vNext.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Go To Definition no longer blocks the UI thread with a bare `Task.Wait`: the synchronous `IFSharpGoToDefinitionService` call now waits through the cancellable threaded-wait dialog, and the editor's `TaskCompletionSource` bridges run their continuations on the thread pool instead of inline on whichever thread finished the check, so repeated F12 on a large solution no longer starves semantic classification and other main-thread work. ([PR #20482](https://github.com/dotnet/fsharp/pull/20482))
1010
* Peek Definition on an F# symbol whose definition lives in metadata no longer deadlocks Visual Studio. Peek holds the main thread in `JoinableTaskFactory.Run` without pumping messages while it asks the language service for the definition, and generating the metadata document needs that same thread; Peek now stops at definitions that already have a document, and Go To Definition, which owns the wait it makes, still opens the generated one. ([PR #20503](https://github.com/dotnet/fsharp/pull/20503))
1111
* The "open namespace" code fix and the completion that adds an `open` now place it from the syntax tree rather than by recognizing a `module` line by its text, so it no longer lands above the file's own module declaration (`[<AutoOpen>] module Ns.Name` written on one line) or below the first declaration when no blank line follows the header. Both paths now share one implementation and follow the file's existing line endings. ([PR #20500](https://github.com/dotnet/fsharp/pull/20500))
12-
* The "open namespace" code fix offers `open type System.Console` where it used to offer `open System.Console`. A plain `open` only reaches namespaces and F# modules, so for a type nested in a type (`System.Net.WebRequestMethods.File`) or a static member of one (`System.Console.WriteLine`) every suggestion it made was code that does not compile — `FS0039: The namespace 'Console' is not defined`. ([PR #20501](https://github.com/dotnet/fsharp/pull/20501))
12+
* The "open namespace" code fix offers `open type System.Console` where it used to offer `open System.Console`. A plain `open` only reaches namespaces and F# modules, so for a type nested in a type (`System.Net.WebRequestMethods.File`) or a static member of one (`System.Console.WriteLine`) every suggestion it made was code that does not compile — `FS0039: The namespace 'Console' is not defined`. A type inside a generic type is not offered to open, since `open type` would need type arguments the name does not carry. ([PR #20501](https://github.com/dotnet/fsharp/pull/20501))
1313
* The "open namespace" code fix lists every namespace and type a name can be resolved from instead of one arbitrary suggestion. It computed them all along and then kept whichever one the assembly crawler happened to reach first, so an unresolved `File` was answered with `System.Net.WebRequestMethods` and `System.IO` was never offered at all. As in Roslyn's add-import fix, the list is capped — five opens, three qualifications — and ordered with `System` first. ([PR #20501](https://github.com/dotnet/fsharp/pull/20501))
1414
* Improve Find All References performance by throttling parallel typechecks. ([PR #20128](https://github.com/dotnet/fsharp/pull/20128))
1515
* Fixed Rename incorrectly renaming `get` and `set` keywords for properties with explicit accessors. ([Issue #18270](https://github.com/dotnet/fsharp/issues/18270), [PR #19252](https://github.com/dotnet/fsharp/pull/19252))

‎vsintegration/src/FSharp.Editor/CodeFixes/AddOpenCodeFixProvider.fs‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,16 @@ type internal AddOpenCodeFixProvider [<ImportingConstructor>] (assemblyContentPr
7777
| _ -> ValueNone
7878

7979
// A plain `open` reaches namespaces and F# modules. When the name is opened deeper than that - a
80-
// type nested in a type, or a static member of one - the type itself has to be opened.
81-
let openDeclaration (entity: InsertionContextEntity) (symbol: AssemblySymbol) ns =
80+
// type nested in a type, or a static member of one - the type itself has to be opened, and a generic
81+
// one cannot be: the type arguments `open type` needs are not part of the name.
82+
let tryGetOpenDeclaration (entity: InsertionContextEntity) (symbol: AssemblySymbol) ns =
8283
match tryFindEntityNamedBy entity.NamespaceIdentCount symbol.CleanedIdents.Length symbol.Symbol with
83-
| ValueSome opened when not opened.IsFSharpModule -> $"open type {ns}"
84-
| _ -> $"open {ns}"
84+
| ValueSome opened when not opened.IsFSharpModule ->
85+
if opened.GenericParameters.Count > 0 then
86+
ValueNone
87+
else
88+
ValueSome $"open type {ns}"
89+
| _ -> ValueSome $"open {ns}"
8590

8691
let getSuggestionsAsCodeFixes
8792
(firstIdentSpan: TextSpan)
@@ -90,9 +95,12 @@ type internal AddOpenCodeFixProvider [<ImportingConstructor>] (assemblyContentPr
9095
=
9196
seq {
9297
candidates
93-
|> Seq.choose (fun (entity, ctx, symbol) ->
94-
entity.Namespace
95-
|> Option.map (fun ns -> openDeclaration entity symbol ns, entity.FullDisplayName, ctx))
98+
|> Seq.chooseV (fun (entity, ctx, symbol) ->
99+
match entity.Namespace with
100+
| Some ns ->
101+
tryGetOpenDeclaration entity symbol ns
102+
|> ValueOption.map (fun declaration -> declaration, entity.FullDisplayName, ctx)
103+
| None -> ValueNone)
96104
|> Seq.groupBy (fun (declaration, _, _) -> declaration)
97105
|> Seq.map (fun (declaration, xs) ->
98106
declaration,

‎vsintegration/tests/FSharp.Editor.Tests/CodeFixes/AddOpenOnTopOnTests.fs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,18 @@ let write () = WriteLine "hi"
680680

681681
Assert.Equal<TestCodeFix list>(expected, actual)
682682

683+
[<Fact>] // NEGATIVE: `open type` needs the type arguments of a generic type, and the name does not carry them
684+
let ``Doesn't offer to open a generic type`` () =
685+
let code =
686+
"""module Module1
687+
688+
let keys (collection: KeyCollection) = collection
689+
"""
690+
691+
let actual = openFixes code Auto
692+
693+
Assert.Equal<TestCodeFix list>([], actual)
694+
683695
[<Fact>] // NEGATIVE: a type sitting directly in a namespace is reached by a plain open
684696
let ``Fixes FS0039 with a plain open for a type in a namespace`` () =
685697
let code =

0 commit comments

Comments
 (0)