Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge main to release/dev17.8 #15995

Merged
merged 6 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Dependencies>
<ProductDependencies>
<Dependency Name="Microsoft.SourceBuild.Intermediate.source-build-reference-packages" Version="8.0.0-alpha.1.23455.3">
<Dependency Name="Microsoft.SourceBuild.Intermediate.source-build-reference-packages" Version="9.0.0-alpha.1.23465.1">
<Uri>https://github.com/dotnet/source-build-reference-packages</Uri>
<Sha>75ec14a961f43446d952c64b5b3330df750db54f</Sha>
<Sha>eeb7f1b24a845eebf3e0885a4650b8df67741d4a</Sha>
<SourceBuild RepoName="source-build-reference-packages" ManagedOnly="true" />
</Dependency>
<Dependency Name="Microsoft.SourceBuild.Intermediate.msbuild" Version="17.7.0-preview-23217-02">
Expand All @@ -29,9 +29,9 @@
</Dependency>
</ProductDependencies>
<ToolsetDependencies>
<Dependency Name="Microsoft.DotNet.Arcade.Sdk" Version="8.0.0-beta.23461.2">
<Dependency Name="Microsoft.DotNet.Arcade.Sdk" Version="8.0.0-beta.23463.1">
<Uri>https://github.com/dotnet/arcade</Uri>
<Sha>4a908c64757841121e67fda7adaf776c93893163</Sha>
<Sha>1d451c32dda2314c721adbf8829e1c0cd4e681ff</Sha>
<SourceBuild RepoName="arcade" ManagedOnly="true" />
</Dependency>
<Dependency Name="Microsoft.DotNet.XliffTasks" Version="1.0.0-beta.23426.1" CoherentParentDependency="Microsoft.DotNet.Arcade.Sdk">
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"perl": "5.38.0.1"
},
"msbuild-sdks": {
"Microsoft.DotNet.Arcade.Sdk": "8.0.0-beta.23461.2",
"Microsoft.DotNet.Arcade.Sdk": "8.0.0-beta.23463.1",
"Microsoft.DotNet.Helix.Sdk": "8.0.0-beta.23255.2"
}
}
2 changes: 1 addition & 1 deletion src/Compiler/Service/IncrementalBuild.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ type IncrementalBuilderState with
HasSignature = hasSignature
Stamp = DateTime.MinValue
LogicalStamp = DateTime.MinValue
Notified = false
Notified = true
SyntaxTree = syntaxTree
BoundModel = model
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ open Xunit
open FSharp.Test.ProjectGeneration
open FSharp.Compiler.Text
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Diagnostics

open OpenTelemetry
open OpenTelemetry.Resources
open OpenTelemetry.Trace

#nowarn "57"

let makeTestProject () =
SyntheticProject.Create(
Expand Down Expand Up @@ -132,3 +139,30 @@ let ``Using getSource and notifications instead of filesystem`` () =
checkFile middle expectSignatureChanged
checkFile last expectSignatureChanged
}

[<Fact>]
let GetAllUsesOfAllSymbols() =
let traceProvider =
Sdk.CreateTracerProviderBuilder()
.AddSource("fsc")
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName="F#", serviceVersion = "1"))
.AddJaegerExporter()
.Build()

use _ = Activity.start "GetAllUsesOfAllSymbols" [ ]

let result =
async {
let project = makeTestProject()
let checker = ProjectWorkflowBuilder(project, useGetSource=true, useChangeNotifications = true).Checker
do! saveProject project false checker
let options = project.GetProjectOptions checker
let! checkProjectResults = checker.ParseAndCheckProject(options)
return checkProjectResults.GetAllUsesOfAllSymbols()
} |> Async.RunSynchronously


traceProvider.ForceFlush() |> ignore
traceProvider.Dispose()

Assert.Equal(80, result.Length)
2 changes: 1 addition & 1 deletion tests/FSharp.Test.Utilities/ProjectGeneration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ type ProjectWorkflowBuilder
defaultArg
checker
(FSharpChecker.Create(
keepAllBackgroundSymbolUses = false,
keepAllBackgroundSymbolUses = true,
enableBackgroundItemKeyStoreAndSemanticClassification = true,
enablePartialTypeChecking = true,
captureIdentifiersWhenParsing = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeFixes

open FSharp.Compiler.EditorServices
open FSharp.Compiler.Text
open FSharp.Compiler.Symbols

open CancellableTasks
Expand Down Expand Up @@ -43,10 +42,7 @@ type internal AddTypeAnnotationToObjectOfIndeterminateTypeFixProvider [<Importin
match lexerSymbolOpt with
| None -> return ValueNone
| Some lexerSymbol ->
let! sourceText = context.GetSourceTextAsync()
let textLine = sourceText.Lines.GetLineFromPosition position
let textLinePos = sourceText.Lines.GetLinePosition position
let fcsTextLineNumber = Line.fromZ textLinePos.Line
let! fcsTextLineNumber, textLine = context.GetLineNumberAndText position

let! _, checkFileResults =
document.GetFSharpParseAndCheckResultsAsync(nameof AddTypeAnnotationToObjectOfIndeterminateTypeFixProvider)
Expand All @@ -62,6 +58,7 @@ type internal AddTypeAnnotationToObjectOfIndeterminateTypeFixProvider [<Importin

match decl with
| FindDeclResult.DeclFound declRange when declRange.FileName = document.FilePath ->
let! sourceText = context.GetSourceTextAsync()
let declSpan = RoslynHelpers.FSharpRangeToTextSpan(sourceText, declRange)
let declTextLine = sourceText.Lines.GetLineFromPosition declSpan.Start

Expand Down
47 changes: 22 additions & 25 deletions vsintegration/src/FSharp.Editor/CodeFixes/CodeFixHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Microsoft.VisualStudio.FSharp.Editor

open System
open System.Threading
open System.Collections.Immutable
open System.Diagnostics

Expand All @@ -20,13 +19,6 @@ open FSharp.Compiler.Text

open CancellableTasks

module internal MutableCodeFixHelper =
let getLineNumberAndText (sourceText: SourceText) position =
let textLine = sourceText.Lines.GetLineFromPosition position
let textLinePos = sourceText.Lines.GetLinePosition position
let fcsTextLineNumber = Line.fromZ textLinePos.Line
fcsTextLineNumber, textLine.ToString()

module internal UnusedCodeFixHelper =
let getUnusedSymbol textSpan (document: Document) (sourceText: SourceText) codeFixName =
let ident = sourceText.ToString textSpan
Expand Down Expand Up @@ -80,34 +72,30 @@ module internal CodeFixHelpers =

TelemetryReporter.ReportSingleEvent(TelemetryEvents.CodefixActivated, props)

let createTextChangeCodeFix (name: string, title: string, context: CodeFixContext, changes: TextChange seq) =
let createTextChangeCodeFix (codeFix, context: CodeFixContext) =
CodeAction.Create(
title,
(fun (cancellationToken: CancellationToken) ->
backgroundTask {
let! sourceText = context.Document.GetTextAsync(cancellationToken)
let doc = context.Document.WithText(sourceText.WithChanges(changes))
reportCodeFixTelemetry context.Diagnostics context.Document name [||]
codeFix.Message,
(fun cancellationToken ->
cancellableTask {
let! sourceText = context.Document.GetTextAsync cancellationToken
let doc = context.Document.WithText(sourceText.WithChanges(codeFix.Changes))
reportCodeFixTelemetry context.Diagnostics context.Document codeFix.Name [||]
return doc
}),
name
}
|> CancellableTask.start cancellationToken),
codeFix.Name
)

[<AutoOpen>]
module internal CodeFixExtensions =
type CodeFixContext with

member ctx.RegisterFsharpFix(staticName, title, changes, ?diagnostics) =
let codeAction =
CodeFixHelpers.createTextChangeCodeFix (staticName, title, ctx, changes)

let diag = diagnostics |> Option.defaultValue ctx.Diagnostics
ctx.RegisterCodeFix(codeAction, diag)

member ctx.RegisterFsharpFix(codeFix: IFSharpCodeFixProvider) =
cancellableTask {
match! codeFix.GetCodeFixIfAppliesAsync ctx with
| ValueSome codeFix -> ctx.RegisterFsharpFix(codeFix.Name, codeFix.Message, codeFix.Changes)
| ValueSome codeFix ->
let codeAction = CodeFixHelpers.createTextChangeCodeFix (codeFix, ctx)
ctx.RegisterCodeFix(codeAction, ctx.Diagnostics)
| ValueNone -> ()
}
|> CancellableTask.startAsTask ctx.CancellationToken
Expand All @@ -130,6 +118,15 @@ module internal CodeFixExtensions =
return RoslynHelpers.TextSpanToFSharpRange(ctx.Document.FilePath, ctx.Span, sourceText)
}

member ctx.GetLineNumberAndText position =
cancellableTask {
let! sourceText = ctx.GetSourceTextAsync()
let textLine = sourceText.Lines.GetLineFromPosition position
let textLinePos = sourceText.Lines.GetLinePosition position
let fcsTextLineNumber = Line.fromZ textLinePos.Line
return fcsTextLineNumber, textLine.ToString()
}

// This cannot be an extension on the code fix context
// because the underlying GetFixAllProvider method doesn't take the context in.
#nowarn "3511" // state machine not statically compilable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,35 @@ namespace Microsoft.VisualStudio.FSharp.Editor

open System.Composition
open System.Collections.Immutable
open System.Threading.Tasks

open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeFixes
open FSharp.Compiler.Diagnostics

open CancellableTasks

[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = CodeFix.FixIndexerAccess); Shared>]
type internal LegacyFixAddDotToIndexerAccessCodeFixProvider() =
inherit CodeFixProvider()

static let title = CompilerDiagnostics.GetErrorMessage FSharpDiagnosticKind.AddIndexerDot

override _.FixableDiagnosticIds = ImmutableArray.Create("FS3217")
override _.FixableDiagnosticIds = ImmutableArray.Create "FS3217"

override _.RegisterCodeFixesAsync context : Task =
async {
let! sourceText = context.Document.GetTextAsync() |> Async.AwaitTask
override this.RegisterCodeFixesAsync context = context.RegisterFsharpFix this

context.Diagnostics
|> Seq.iter (fun diagnostic ->
interface IFSharpCodeFixProvider with
member _.GetCodeFixIfAppliesAsync context =
cancellableTask {
let! sourceText = context.GetSourceTextAsync()

let span, replacement =
try
let mutable span = context.Span

let notStartOfBracket (span: TextSpan) =
let t = sourceText.GetSubText(TextSpan(span.Start, span.Length + 1))
t.[t.Length - 1] <> '['
t[t.Length - 1] <> '['

// skip all braces and blanks until we find [
while span.End < sourceText.Length && notStartOfBracket span do
Expand All @@ -41,12 +42,11 @@ type internal LegacyFixAddDotToIndexerAccessCodeFixProvider() =
with _ ->
context.Span, sourceText.GetSubText(context.Span).ToString()

do
context.RegisterFsharpFix(
CodeFix.FixIndexerAccess,
title,
[| TextChange(span, replacement.TrimEnd() + ".") |],
ImmutableArray.Create(diagnostic)
))
}
|> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)
return
ValueSome
{
Name = CodeFix.FixIndexerAccess
Message = title
Changes = [ TextChange(span, replacement.TrimEnd() + ".") ]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ type internal MakeDeclarationMutableCodeFixProvider [<ImportingConstructor>] ()
| Some lexerSymbol ->
let! sourceText = context.GetSourceTextAsync()

let fcsTextLineNumber, textLine =
MutableCodeFixHelper.getLineNumberAndText sourceText position
let! fcsTextLineNumber, textLine = context.GetLineNumberAndText position

let! parseFileResults, checkFileResults =
document.GetFSharpParseAndCheckResultsAsync(nameof MakeDeclarationMutableCodeFixProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ open Microsoft.CodeAnalysis.CodeFixes
open FSharp.Compiler.Diagnostics
open FSharp.Compiler.EditorServices
open FSharp.Compiler.Syntax
open FSharp.Compiler.Text

open CancellableTasks

Expand All @@ -38,9 +37,7 @@ type internal ReplaceWithSuggestionCodeFixProvider [<ImportingConstructor>] () =
let! unresolvedIdentifierText = context.GetSquigglyTextAsync()
let pos = context.Span.End
let caretLinePos = sourceText.Lines.GetLinePosition(pos)
let caretLine = sourceText.Lines.GetLineFromPosition(pos)
let fcsCaretLineNumber = Line.fromZ caretLinePos.Line
let lineText = caretLine.ToString()
let! fcsCaretLineNumber, lineText = context.GetLineNumberAndText pos

let partialName =
QuickParse.GetPartialLongNameEx(lineText, caretLinePos.Character - 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ type internal UseMutationWhenValueIsMutableCodeFixProvider [<ImportingConstructo

let adjustedPosition =
let rec loop ch pos =
if Char.IsWhiteSpace(ch) then
if
Char.IsWhiteSpace(ch)
// edge case - end of file
|| pos = sourceText.Length - 1
then
pos
else
loop sourceText[pos + 1] (pos + 1)
Expand All @@ -54,8 +58,7 @@ type internal UseMutationWhenValueIsMutableCodeFixProvider [<ImportingConstructo
match lexerSymbolOpt with
| None -> return ValueNone
| Some lexerSymbol ->
let fcsTextLineNumber, textLine =
MutableCodeFixHelper.getLineNumberAndText sourceText adjustedPosition
let! fcsTextLineNumber, textLine = context.GetLineNumberAndText adjustedPosition

let! _, checkFileResults =
document.GetFSharpParseAndCheckResultsAsync(nameof UseMutationWhenValueIsMutableCodeFixProvider)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

module FSharp.Editor.Tests.CodeFixes.FixIndexerAccessLegacyTests

open Microsoft.VisualStudio.FSharp.Editor
open Xunit

open CodeFixTestFramework

let private codeFix = LegacyFixAddDotToIndexerAccessCodeFixProvider()

[<Fact>]
let ``Fixes FS3217`` () =
let code =
"""
let list = [ 1; 2; 3 ]
let first = list[2]
"""

let expected =
Some
{
Message = "Add . for indexer access."
FixedCode =
"""
let list = [ 1; 2; 3 ]
let first = list.[2]
"""
}

let actual = codeFix |> tryFix code (WithOption "--langversion:5")

Assert.Equal(expected, actual)
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ let f x =

Assert.Equal(expected, actual)

[<Fact>]
let ``Doesn't fix unrelated FS0020`` () =
let code =
"""
[<Theory>]
[<InlineData """
let square x = x * x
square 32
"""

""">]
[<InlineData """
let band = {| Name = "R.E.M." |}
band""">]
let ``Doesn't fix unrelated FS0020`` code =
let expected = None

let actual = codeFix |> tryFix code Auto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="CodeFixes\AddNewKeywordToDisposableConstructorInvocationTests.fs" />
<Compile Include="CodeFixes\RemoveUnusedBindingTests.fs" />
<Compile Include="CodeFixes\RemoveSuperfluousCaptureForUnionCaseWithNoDataTests.fs" />
<Compile Include="CodeFixes\FixIndexerAccessLegacyTests.fs" />
<Compile Include="CodeFixes\FixIndexerAccessTests.fs" />
<Compile Include="CodeFixes\DiscardUnusedValueTests.fs" />
<Compile Include="CodeFixes\PrefixUnusedValueTests.fs" />
Expand Down