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

Try and find record type from LongIdent in Computation expr. #1

Open
wants to merge 2 commits into
base: net232-1
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion src/Compiler/Checking/CheckComputationExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2214,7 +2214,28 @@ let TcSequenceExpressionEntry (cenv: cenv) env (overallTy: OverallTy) tpenv (has
()

if not hasBuilder && not cenv.g.compilingFSharpCore then
CallExprHasTypeSink cenv.tcSink (m, env.NameEnv, overallTy.Commit, env.eAccessRights)
match comp with
| SynExpr.LongIdent(longDotId = SynLongIdent(id = lid)) when lid.Length > 1 ->
let potentialRecordTypeIdentifier = List.take (lid.Length - 1) lid
let result =
ResolveTypeLongIdent
cenv.tcSink
cenv.nameResolver
ItemOccurence.UseInType
OpenQualified
env.NameEnv
env.AccessRights
potentialRecordTypeIdentifier
TypeNameResolutionStaticArgsInfo.DefiniteEmpty
PermitDirectReferenceToGeneratedType.Yes

match result with
| Result (_, tyconRef) when tyconRef.IsRecordTycon ->
let ty = generalizedTyconRef cenv.g tyconRef
CallExprHasTypeSink cenv.tcSink (m, env.NameEnv, ty, env.eAccessRights)
| _ -> CallExprHasTypeSink cenv.tcSink (m, env.NameEnv, overallTy.Commit, env.eAccessRights)
| _ -> CallExprHasTypeSink cenv.tcSink (m, env.NameEnv, overallTy.Commit, env.eAccessRights)

error(Error(FSComp.SR.tcInvalidSequenceExpressionSyntaxForm(), m))

TcSequenceExpression cenv env tpenv comp overallTy m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
<Compile Include="FSharpChecker\CommonWorkflows.fs" />
<Compile Include="FSharpChecker\SymbolUse.fs" />
<Compile Include="FSharpChecker\FindReferences.fs" />
<Compile Include="FSharpChecker\GetTypeOfExpressionTests.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module GetTypeOfExpressionTests

open Xunit
open FSharp.Compiler.Text
open FSharp.Test.Compiler

[<Fact>]
let ``Record type in Computation expression`` () =
FSharp """
type Entry =
{
Idx: int
FileName: string
/// Own deps
DependencyCount: int
/// Being depended on
DependentCount: int
LineCount: int
}

let x =
{
Entry.ReSharperFSharpRulezzz
}
"""
|> typecheckResults
|> fun typeCheckResults ->
let anyRange = typeCheckResults.GetAllUsesOfAllSymbolsInFile() |> Seq.head |> fun symbolUse -> symbolUse.Range
let mExpr = Range.mkRange anyRange.FileName (Position.mkPos 14 4) (Position.mkPos 16 5)
let t = typeCheckResults.GetTypeOfExpression(mExpr).Value
Assert.True t.TypeDefinition.IsFSharpRecord

[<Fact>]
let ``Qualified RecordType name in computation expression`` () =
FSharp """
module Module1 =
type R1 =
{ Field1: int }

type R2 =
{ Field2: int }

module Module2 =
{ Module1.ReSharperFSharpRulezzz } // Both Field1 and Field2 should be available, we can't assume any record type and report it

{ R1.ReSharperFSharpRulezzz } // Field1 should be available, _maybe_ we can assume the record type for the completion fix, but it's a wrong hack, because `R1` is not an expression of `R1` type. Reporting it would enable things like `let` and `with` templates and features like Introduce Variable would also see it wrongly

{ Module1.R1.ReSharperFSharpRulezzz } // Field1 is actually available from FCS completion, maybe we could check why it works
"""
|> typecheckResults
|> fun typeCheckResults ->
let anyRange = typeCheckResults.GetAllUsesOfAllSymbolsInFile() |> Seq.head |> fun symbolUse -> symbolUse.Range
let m1 = Range.mkRange anyRange.FileName (Position.mkPos 10 4) (Position.mkPos 10 38)
let m2 = Range.mkRange anyRange.FileName (Position.mkPos 12 4) (Position.mkPos 12 33)
let m3 = Range.mkRange anyRange.FileName (Position.mkPos 14 4) (Position.mkPos 14 41)

let t1 = typeCheckResults.GetTypeOfExpression(m1).Value
Assert.False t1.TypeDefinition.IsFSharpRecord
let t2 = typeCheckResults.GetTypeOfExpression(m2).Value
Assert.False t2.TypeDefinition.IsFSharpRecord
let t3 = typeCheckResults.GetTypeOfExpression(m3).Value
Assert.True t3.TypeDefinition.IsFSharpRecord