Skip to content

Commit 554bf2e

Browse files
committed
FCS: allow getting captured types
1 parent 93c2231 commit 554bf2e

File tree

7 files changed

+67
-0
lines changed

7 files changed

+67
-0
lines changed

src/Compiler/Checking/Expressions/CheckExpressions.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6198,6 +6198,7 @@ and TcExprTuple (cenv: cenv) overallTy env tpenv (isExplicitStruct, args, m) =
61986198
let tupInfo, argTys = UnifyTupleTypeAndInferCharacteristics env.eContextInfo cenv env.DisplayEnv m overallTy isExplicitStruct args
61996199
let argsR, tpenv = TcExprsNoFlexes cenv env m tpenv argTys args
62006200
let expr = mkAnyTupled g m tupInfo argsR argTys
6201+
CallExprHasTypeSink cenv.tcSink (m, env.NameEnv, mkAnyTupledTy g tupInfo argTys, env.eAccessRights)
62016202
expr, tpenv
62026203
)
62036204

@@ -10652,6 +10653,8 @@ and TcLinearExprs bodyChecker cenv env overallTy tpenv isCompExpr synExpr cont =
1065210653
if not isRecovery && Option.isNone synElseExprOpt then
1065310654
UnifyTypes cenv env m g.unit_ty overallTy.Commit
1065410655

10656+
CallExprHasTypeSink cenv.tcSink (m, env.NameEnv, overallTy.Commit, env.eAccessRights)
10657+
1065510658
TcExprThatCanBeCtorBody cenv overallTy env tpenv synThenExpr
1065610659

1065710660
match synElseExprOpt with

src/Compiler/Service/FSharpCheckerResults.fs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,16 @@ type internal TypeCheckInfo
20402040
member scope.IsRelativeNameResolvableFromSymbol(cursorPos: pos, plid: string list, symbol: FSharpSymbol) : bool =
20412041
scope.IsRelativeNameResolvable(cursorPos, plid, symbol.Item)
20422042

2043+
member scope.TryGetCapturedType(range) =
2044+
sResolutions.CapturedExpressionTypings
2045+
|> Seq.tryFindBack (fun (_, _, _, m) -> equals m range)
2046+
|> Option.map (fun (ty, _, _, _) -> FSharpType(cenv, ty))
2047+
2048+
member scope.TryGetCapturedDisplayContext(range) =
2049+
sResolutions.CapturedExpressionTypings
2050+
|> Seq.tryFindBack (fun (_, _, _, m) -> equals m range)
2051+
|> Option.map (fun (_, q, _, _) -> FSharpDisplayContext(fun _ -> q.DisplayEnv))
2052+
20432053
/// Get the auto-complete items at a location
20442054
member _.GetDeclarations
20452055
(parseResultsOpt, line, lineStr, partialName, completionContextAtPos, getAllEntities, genBodyForOverriddenMeth)
@@ -3480,6 +3490,16 @@ type FSharpCheckFileResults
34803490

34813491
member _.DependencyFiles = dependencyFiles
34823492

3493+
member _.TryGetCapturedType(range: range) =
3494+
match details with
3495+
| None -> None
3496+
| Some(scope, _) -> scope.TryGetCapturedType(range)
3497+
3498+
member _.TryGetCapturedDisplayContext(range: range) =
3499+
match details with
3500+
| None -> None
3501+
| Some(scope, _) -> scope.TryGetCapturedDisplayContext(range)
3502+
34833503
member _.GetAllUsesOfAllSymbolsInFile(?cancellationToken: CancellationToken) =
34843504
match details with
34853505
| None -> Seq.empty

src/Compiler/Service/FSharpCheckerResults.fsi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ type public FSharpCheckFileResults =
269269
/// in the documentation for compiler service.
270270
member DependencyFiles: string[]
271271

272+
member TryGetCapturedType: range -> FSharpType option
273+
member TryGetCapturedDisplayContext: range -> FSharpDisplayContext option
274+
272275
/// <summary>Get the items for a declaration list</summary>
273276
///
274277
/// <param name="parsedFileResults">
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module FSharp.Compiler.Service.Tests.CapturedTypes
2+
3+
open FSharp.Compiler.Symbols
4+
open FSharp.Test.Assert
5+
open Xunit
6+
7+
let private displayContext =
8+
FSharpDisplayContext.Empty.WithShortTypeNames(true)
9+
10+
let tryGetCapturedType markedSource =
11+
let context, checkResults = Checker.getCheckedResolveContext markedSource
12+
checkResults.TryGetCapturedType context.SelectedRange.Value
13+
14+
let assertCapturedType expectedTypeString markedSource =
15+
let capturedType = tryGetCapturedType markedSource
16+
capturedType.Value.Format displayContext |> shouldEqual expectedTypeString
17+
18+
[<Fact>]
19+
let ``Expr - If 01`` () =
20+
assertCapturedType "int * int" "{selstart}if true then 1, 2 else 1, true{selend}"
21+
22+
[<Fact>]
23+
let ``Expr - Literal 01`` () =
24+
assertCapturedType "int" "{selstart}1{selend}"
25+
26+
[<Fact>]
27+
let ``Expr - Literal 02`` () =
28+
assertCapturedType "string" "{selstart}\"\"{selend}"
29+
30+
[<Fact>]
31+
let ``Expr - Tuple 01`` () =
32+
assertCapturedType "int * int" "{selstart}1, 2{selend}"
33+
34+
[<Fact>]
35+
let ``Expr - Tuple 02`` () =
36+
assertCapturedType "int * int" "if true then {selstart}1, 2{selend} else 1, true"

tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.debug.bsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,9 +2081,11 @@ FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: FSharp.Compiler.Text.Range[
20812081
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.CodeAnalysis.FSharpSymbolUse] GetSymbolUsesAtLocation(Int32, Int32, System.String, Microsoft.FSharp.Collections.FSharpList`1[System.String])
20822082
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Collections.FSharpList`1[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.CodeAnalysis.FSharpSymbolUse]] GetDeclarationListSymbols(Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.CodeAnalysis.FSharpParseFileResults], Int32, System.String, FSharp.Compiler.EditorServices.PartialLongName, Microsoft.FSharp.Core.FSharpOption`1[Microsoft.FSharp.Core.FSharpFunc`2[Microsoft.FSharp.Core.Unit,Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.EditorServices.AssemblySymbol]]], Microsoft.FSharp.Core.FSharpOption`1[System.Boolean])
20832083
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.CodeAnalysis.FSharpSymbolUse] GetSymbolUseAtLocation(Int32, Int32, System.String, Microsoft.FSharp.Collections.FSharpList`1[System.String])
2084+
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpDisplayContext] GetCapturedDisplayContext(FSharp.Compiler.Text.Range)
20842085
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpDisplayContext] GetDisplayContextForPos(FSharp.Compiler.Text.Position)
20852086
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpImplementationFileContents] ImplementationFile
20862087
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpImplementationFileContents] get_ImplementationFile()
2088+
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpType] GetCapturedType(FSharp.Compiler.Text.Range)
20872089
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText] GenerateSignature(Microsoft.FSharp.Core.FSharpOption`1[System.Int32])
20882090
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.CodeAnalysis.FSharpSymbolUse]] GetMethodsAsSymbols(Int32, Int32, System.String, Microsoft.FSharp.Collections.FSharpList`1[System.String])
20892091
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[System.String] GetF1Keyword(Int32, Int32, System.String, Microsoft.FSharp.Collections.FSharpList`1[System.String])

tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.release.bsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,9 +2081,11 @@ FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: FSharp.Compiler.Text.Range[
20812081
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.CodeAnalysis.FSharpSymbolUse] GetSymbolUsesAtLocation(Int32, Int32, System.String, Microsoft.FSharp.Collections.FSharpList`1[System.String])
20822082
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Collections.FSharpList`1[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.CodeAnalysis.FSharpSymbolUse]] GetDeclarationListSymbols(Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.CodeAnalysis.FSharpParseFileResults], Int32, System.String, FSharp.Compiler.EditorServices.PartialLongName, Microsoft.FSharp.Core.FSharpOption`1[Microsoft.FSharp.Core.FSharpFunc`2[Microsoft.FSharp.Core.Unit,Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.EditorServices.AssemblySymbol]]], Microsoft.FSharp.Core.FSharpOption`1[System.Boolean])
20832083
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.CodeAnalysis.FSharpSymbolUse] GetSymbolUseAtLocation(Int32, Int32, System.String, Microsoft.FSharp.Collections.FSharpList`1[System.String])
2084+
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpDisplayContext] GetCapturedDisplayContext(FSharp.Compiler.Text.Range)
20842085
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpDisplayContext] GetDisplayContextForPos(FSharp.Compiler.Text.Position)
20852086
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpImplementationFileContents] ImplementationFile
20862087
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpImplementationFileContents] get_ImplementationFile()
2088+
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpType] GetCapturedType(FSharp.Compiler.Text.Range)
20872089
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Text.ISourceText] GenerateSignature(Microsoft.FSharp.Core.FSharpOption`1[System.Int32])
20882090
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[Microsoft.FSharp.Collections.FSharpList`1[FSharp.Compiler.CodeAnalysis.FSharpSymbolUse]] GetMethodsAsSymbols(Int32, Int32, System.String, Microsoft.FSharp.Collections.FSharpList`1[System.String])
20892091
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[System.String] GetF1Keyword(Int32, Int32, System.String, Microsoft.FSharp.Collections.FSharpList`1[System.String])

tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<Compile Include="SourceTextTests.fs" />
7777
<Compile Include="SynExprTests.fs" />
7878
<Compile Include="SynPatTests.fs" />
79+
<Compile Include="CapturedTypes.fs" />
7980
<Compile Include="WarnScopeTests.fs" />
8081
</ItemGroup>
8182

0 commit comments

Comments
 (0)