Skip to content

Commit

Permalink
Report auto property redux (#16116)
Browse files Browse the repository at this point in the history
* Report property in PostInferenceChecks.

* Move property item creation back to CheckDeclarations.

* Use StringComparison.Ordinal

* Update unit test according to new world.

* Update tests

* Update SyntaxTree tests

* Update SyntaxTree tests

* Update unit tests

* Return single property tooltip.

* Maybe fix fsharpqa tests

* Maybe fix fsharpqa tests

* Update tests

* Update test

* Add tests for properties during critical errors.

* Add test for property symbol on interface.

* Revert PostInferenceChecks.fs

* Clean up

---------

Co-authored-by: Petr <[email protected]>
  • Loading branch information
nojaf and psfinaki authored Nov 21, 2023
1 parent d41a0d7 commit 041420c
Show file tree
Hide file tree
Showing 274 changed files with 732 additions and 788 deletions.
61 changes: 20 additions & 41 deletions src/Compiler/Checking/CheckDeclarations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -997,25 +997,30 @@ module MutRecBindingChecking =
for b1, b2 in List.pairwise defnAs do
match b1, b2 with
| TyconBindingPhase2A.Phase2AMember {
SyntacticBinding = NormalizedBinding(valSynData = SynValData(transformedFromProperty = Some getPropertyIdent; memberFlags = Some mf))
SyntacticBinding = NormalizedBinding(pat = SynPat.Named(ident = SynIdent(ident = getIdent)); valSynData = SynValData(memberFlags = Some mf))
RecBindingInfo = RecursiveBindingInfo(vspec = vGet)
},
TyconBindingPhase2A.Phase2AMember {
SyntacticBinding = NormalizedBinding(valSynData = SynValData(transformedFromProperty = Some setPropertyIdent))
SyntacticBinding = NormalizedBinding(pat = SynPat.Named(ident = SynIdent(ident = setIdent)))
RecBindingInfo = RecursiveBindingInfo(vspec = vSet)
} when Range.equals getPropertyIdent.idRange setPropertyIdent.idRange ->
} when Range.equals getIdent.idRange setIdent.idRange ->
match vGet.ApparentEnclosingEntity with
| ParentNone -> ()
| Parent parentRef ->
let apparentEnclosingType = generalizedTyconRef g parentRef
let vGet, vSet = if mf.MemberKind = SynMemberKind.PropertyGet then vGet, vSet else vSet, vGet
let item =
Item.Property(
getPropertyIdent.idText,
[ PropInfo.FSProp(g, apparentEnclosingType, Some (mkLocalValRef vGet), Some (mkLocalValRef vSet)) ],
Some getPropertyIdent.idRange
)
CallNameResolutionSink cenv.tcSink (getPropertyIdent.idRange, envForTycon.NameEnv, item, emptyTyparInst, ItemOccurence.Binding, envForTycon.eAccessRights)
let apparentEnclosingType = generalizedTyconRef g parentRef
let vGet, vSet = if mf.MemberKind = SynMemberKind.PropertyGet then vGet, vSet else vSet, vGet
let propertyName =
if vGet.Id.idText.StartsWith("get_", StringComparison.Ordinal) then
vGet.Id.idText.Replace("get_", "")
else
vGet.Id.idText
let item =
Item.Property(
propertyName,
[ PropInfo.FSProp(g, apparentEnclosingType, Some (mkLocalValRef vGet), Some (mkLocalValRef vSet)) ],
Some getIdent.idRange
)
CallNameResolutionSink cenv.tcSink (getIdent.idRange, envForTycon.NameEnv, item, emptyTyparInst, ItemOccurence.Binding, envForTycon.eAccessRights)
| _ -> ()

// If no constructor call, insert Phase2AIncrClassCtorJustAfterSuperInit at start
Expand Down Expand Up @@ -4263,6 +4268,8 @@ module TcDeclarations =
// Only the keep the non-field-targeted attributes
let attribs = attribs |> List.filter (fun a -> match a.Target with Some t when t.idText = "field" -> false | _ -> true)
let fldId = ident (CompilerGeneratedName id.idText, mMemberPortion)
let headPatIds = if isStatic then [id] else [ident ("__", mMemberPortion);id]
let headPat = SynPat.LongIdent (SynLongIdent(headPatIds, [], List.replicate headPatIds.Length None), None, Some noInferredTypars, SynArgPats.Pats [], None, mMemberPortion)
let memberFlags = { memberFlags with GetterOrSetterIsCompilerGenerated = true }
let memberFlagsForSet = { memberFlagsForSet with GetterOrSetterIsCompilerGenerated = true }

Expand All @@ -4279,20 +4286,7 @@ module TcDeclarations =
let rhsExpr = SynExpr.Ident fldId
let retInfo = match tyOpt with None -> None | Some ty -> Some (None, SynReturnInfo((ty, SynInfo.unnamedRetVal), ty.Range))
let attribs = mkAttributeList attribs mMemberPortion
let headPatIds =
let id =
match mGetSetOpt with
| Some (GetSetKeywords.GetSet(get = mGet)) -> Ident(id.idText, mGet)
| _ -> id
if isStatic then [id] else [ident ("__", mMemberPortion);id]
let headPat = SynPat.LongIdent (SynLongIdent(headPatIds, [], List.replicate headPatIds.Length None), None, Some noInferredTypars, SynArgPats.Pats [], None, mMemberPortion)
let binding = mkSynBinding (xmlDoc, headPat) (access, false, false, mMemberPortion, DebugPointAtBinding.NoneAtInvisible, retInfo, rhsExpr, rhsExpr.Range, [], attribs, Some memberFlags, SynBindingTrivia.Zero)
let binding =
match mGetSetOpt with
| Some (GetSetKeywords.GetSet _) ->
// Only add the additional meta data to the SynBinding (SynValData) is both get and set are present.
updatePropertyIdentInSynBinding id binding
| _ -> binding
SynMemberDefn.Member (binding, mMemberPortion)
yield getter
| _ -> ()
Expand All @@ -4301,25 +4295,10 @@ module TcDeclarations =
| SynMemberKind.PropertySet
| SynMemberKind.PropertyGetSet ->
let setter =
let vId =
match mGetSetOpt with
| Some (GetSetKeywords.GetSet(set = mSet)) -> ident("v", mSet)
| _ -> ident("v", mMemberPortion)
let headPatIds =
let id =
match mGetSetOpt with
| Some (GetSetKeywords.GetSet(set = mSet)) -> Ident(id.idText, mSet)
| _ -> id
if isStatic then [id] else [ident ("__", mMemberPortion);id]
let vId = ident("v", mMemberPortion)
let headPat = SynPat.LongIdent (SynLongIdent(headPatIds, [], List.replicate headPatIds.Length None), None, Some noInferredTypars, SynArgPats.Pats [mkSynPatVar None vId], None, mMemberPortion)
let rhsExpr = mkSynAssign (SynExpr.Ident fldId) (SynExpr.Ident vId)
let binding = mkSynBinding (xmlDoc, headPat) (access, false, false, mMemberPortion, DebugPointAtBinding.NoneAtInvisible, None, rhsExpr, rhsExpr.Range, [], [], Some memberFlagsForSet, SynBindingTrivia.Zero)
let binding =
match mGetSetOpt with
| Some (GetSetKeywords.GetSet _) ->
// Only add the additional meta data to the SynBinding (SynValData) is both get and set are present.
updatePropertyIdentInSynBinding id binding
| _ -> binding
SynMemberDefn.Member (binding, mMemberPortion)
yield setter
| _ -> ()]
Expand Down
27 changes: 12 additions & 15 deletions src/Compiler/Checking/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2354,7 +2354,7 @@ module BindingNormalization =
NormalizedBindingPat(SynPat.InstanceMember(thisId, memberId, toolId, vis, m), PushMultiplePatternsToRhs cenv true args rhsExpr, valSynData, typars)

let private NormalizeStaticMemberBinding (cenv: cenv) (memberFlags: SynMemberFlags) valSynData id vis typars args m rhsExpr =
let (SynValData(valInfo = valSynInfo; thisIdOpt = thisIdOpt; transformedFromProperty = tp)) = valSynData
let (SynValData(valInfo = valSynInfo; thisIdOpt = thisIdOpt)) = valSynData
if memberFlags.IsInstance then
// instance method without adhoc "this" argument
error(Error(FSComp.SR.tcInstanceMemberRequiresTarget(), m))
Expand All @@ -2368,15 +2368,15 @@ module BindingNormalization =
// static property: these transformed into methods taking one "unit" argument
| [], SynMemberKind.Member ->
let memberFlags = {memberFlags with MemberKind = SynMemberKind.PropertyGet}
let valSynData = SynValData(Some memberFlags, valSynInfo, thisIdOpt, tp)
let valSynData = SynValData(Some memberFlags, valSynInfo, thisIdOpt)
NormalizedBindingPat(mkSynPatVar vis id,
PushOnePatternToRhs cenv true (SynPat.Const(SynConst.Unit, m)) rhsExpr,
valSynData,
typars)
| _ -> MakeNormalizedStaticOrValBinding cenv ValOrMemberBinding id vis typars args rhsExpr valSynData

let private NormalizeInstanceMemberBinding (cenv: cenv) (memberFlags: SynMemberFlags) valSynData thisId memberId (toolId: Ident option) vis typars args m rhsExpr =
let (SynValData(_, valSynInfo, thisIdOpt, tap)) = valSynData
let (SynValData(_, valSynInfo, thisIdOpt)) = valSynData

if not memberFlags.IsInstance then
// static method with adhoc "this" argument
Expand All @@ -2400,7 +2400,7 @@ module BindingNormalization =
(SynPat.InstanceMember(thisId, memberId, toolId, vis, m),
PushOnePatternToRhs cenv true (SynPat.Const(SynConst.Unit, m)) rhsExpr,
// Update the member info to record that this is a SynMemberKind.PropertyGet
SynValData(Some memberFlags, valSynInfo, thisIdOpt, tap),
SynValData(Some memberFlags, valSynInfo, thisIdOpt),
typars)

| _ ->
Expand Down Expand Up @@ -2511,10 +2511,10 @@ module EventDeclarationNormalization =
| _ -> error(BadEventTransformation m)

let private ConvertSynData m valSynData =
let (SynValData(memberFlagsOpt, valSynInfo, thisIdOpt, tap)) = valSynData
let (SynValData(memberFlagsOpt, valSynInfo, thisIdOpt)) = valSynData
let memberFlagsOpt = ConvertMemberFlagsOpt m memberFlagsOpt
let valSynInfo = ConvertSynInfo m valSynInfo
SynValData(memberFlagsOpt, valSynInfo, thisIdOpt, tap)
SynValData(memberFlagsOpt, valSynInfo, thisIdOpt)

let rec private RenameBindingPattern f declPattern =
match declPattern with
Expand Down Expand Up @@ -10557,8 +10557,8 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt
match rotRetSynAttrs with
| [] -> valSynData
| {Range=mHead} :: _ ->
let (SynValData(valMf, SynValInfo(args, SynArgInfo(attrs, opt, retId)), valId, tap)) = valSynData
SynValData(valMf, SynValInfo(args, SynArgInfo({Attributes=rotRetSynAttrs; Range=mHead} :: attrs, opt, retId)), valId, tap)
let (SynValData(valMf, SynValInfo(args, SynArgInfo(attrs, opt, retId)), valId)) = valSynData
SynValData(valMf, SynValInfo(args, SynArgInfo({Attributes=rotRetSynAttrs; Range=mHead} :: attrs, opt, retId)), valId)
retAttribs, valAttribs, valSynData

let isVolatile = HasFSharpAttribute g g.attrib_VolatileFieldAttribute valAttribs
Expand Down Expand Up @@ -11551,7 +11551,6 @@ and AnalyzeRecursiveInstanceMemberDecl
thisId,
memberId: Ident,
toolId: Ident option,
isTransformedProperty: bool,
bindingAttribs,
vis2,
tcrefContainerInfo,
Expand Down Expand Up @@ -11610,12 +11609,11 @@ and AnalyzeRecursiveInstanceMemberDecl
| None -> None

let memberInfo = MakeMemberDataAndMangledNameForMemberVal(g, tcref, isExtrinsic, bindingAttribs, optInferredImplSlotTys, memberFlags, valSynInfo, memberId, false)
// This line factored in the 'get' or 'set' as the identifier for a property declaration using "with get () = ... and set v = ..."
// We used to factored in the 'get' or 'set' as the identifier for a property declaration using "with get () = ... and set v = ..."
// It has been removed from FSharp.Compiler.Service because we want the property name to be the location of
// the definition of these symbols.
//
// See https://github.com/fsharp/FSharp.Compiler.Service/issues/79.
let memberId = match toolId with Some tid when isTransformedProperty -> ident(memberId.idText, tid.idRange) | _ -> memberId

envinner, tpenv, memberId, toolId, Some memberInfo, vis, vis2, None, enclosingDeclaredTypars, baseValOpt, explicitTyparInfo, bindingRhs, declaredTypars
| _ ->
Expand All @@ -11630,7 +11628,6 @@ and AnalyzeRecursiveDecl
declaredTypars,
thisIdOpt,
valSynInfo,
isTransformedProperty,
explicitTyparInfo,
newslotsOK,
overridesOK,
Expand Down Expand Up @@ -11675,7 +11672,7 @@ and AnalyzeRecursiveDecl
AnalyzeRecursiveInstanceMemberDecl
(cenv, envinner, tpenv, declKind,
synTyparDecls, valSynInfo, explicitTyparInfo, newslotsOK,
overridesOK, vis1, thisId, memberId, toolId, isTransformedProperty,
overridesOK, vis1, thisId, memberId, toolId,
bindingAttribs, vis2, tcrefContainerInfo,
memberFlagsOpt, ty, bindingRhs, mBinding)

Expand Down Expand Up @@ -11705,7 +11702,7 @@ and AnalyzeAndMakeAndPublishRecursiveValue
// Pull apart the inputs
let (NormalizedBinding(vis1, bindingKind, isInline, isMutable, bindingSynAttribs, bindingXmlDoc, synTyparDecls, valSynData, declPattern, bindingRhs, mBinding, debugPoint)) = binding
let (NormalizedBindingRhs(_, _, bindingExpr)) = bindingRhs
let (SynValData(memberFlagsOpt, valSynInfo, thisIdOpt, transformedFromProperty)) = valSynData
let (SynValData(memberFlagsOpt, valSynInfo, thisIdOpt)) = valSynData
let (ContainerInfo(altActualParent, tcrefContainerInfo)) = containerInfo

let attrTgt = declKind.AllowedAttribTargets memberFlagsOpt
Expand All @@ -11729,7 +11726,7 @@ and AnalyzeAndMakeAndPublishRecursiveValue
let envinner, tpenv, bindingId, toolIdOpt, memberInfoOpt, vis, vis2, safeThisValOpt, enclosingDeclaredTypars, baseValOpt, explicitTyparInfo, bindingRhs, declaredTypars =

AnalyzeRecursiveDecl (cenv, envinner, tpenv, declKind, synTyparDecls, declaredTypars, thisIdOpt, valSynInfo,
transformedFromProperty.IsSome, explicitTyparInfo,
explicitTyparInfo,
newslotsOK, overridesOK, vis1, declPattern, bindingAttribs, tcrefContainerInfo,
memberFlagsOpt, ty, bindingRhs, mBinding)

Expand Down
31 changes: 25 additions & 6 deletions src/Compiler/Service/FSharpCheckerResults.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1968,12 +1968,31 @@ type internal TypeCheckInfo
match declItemsOpt with
| None -> emptyToolTip
| Some (items, denv, _, m) ->
ToolTipText(
items
|> List.map (fun x ->
let symbol = Some(FSharpSymbol.Create(cenv, x.Item))
FormatStructuredDescriptionOfItem false infoReader tcAccessRights m denv x.ItemWithInst symbol width)
))
match items with
| [ { Kind = CompletionItemKind.Property } as prop
{ Kind = CompletionItemKind.Field }
{ Kind = CompletionItemKind.Field } ] ->
let symbol = FSharpSymbol.Create(cenv, prop.Item)

let tt =
FormatStructuredDescriptionOfItem
false
infoReader
tcAccessRights
m
denv
prop.ItemWithInst
(Some symbol)
width

ToolTipText([ tt ])
| _ ->
ToolTipText(
items
|> List.map (fun x ->
let symbol = Some(FSharpSymbol.Create(cenv, x.Item))
FormatStructuredDescriptionOfItem false infoReader tcAccessRights m denv x.ItemWithInst symbol width)
))
(fun err ->
Trace.TraceInformation(sprintf "FCS: recovering from error in GetStructuredToolTipText: '%s'" err)
ToolTipText [ ToolTipElement.CompositionError err ])
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/SyntaxTree/ParseHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ let mkSynMemberDefnGetSet
// should be unreachable, cover just in case
raiseParseErrorAt mWholeBindLhs (FSComp.SR.parsInvalidProperty ())

let valSynData = SynValData(Some(memFlags), valSynInfo, None, None)
let valSynData = SynValData(Some(memFlags), valSynInfo, None)

// Fold together the information from the first lambda pattern and the get/set binding
// This uses the 'this' variable from the first and the patterns for the get/set binding,
Expand Down
6 changes: 1 addition & 5 deletions src/Compiler/SyntaxTree/SyntaxTree.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,11 +1046,7 @@ type SynAttributes = SynAttributeList list

[<NoEquality; NoComparison>]
type SynValData =
| SynValData of
memberFlags: SynMemberFlags option *
valInfo: SynValInfo *
thisIdOpt: Ident option *
transformedFromProperty: Ident option
| SynValData of memberFlags: SynMemberFlags option * valInfo: SynValInfo * thisIdOpt: Ident option

member x.SynValInfo = (let (SynValData (valInfo = synValInfo)) = x in synValInfo)

Expand Down
8 changes: 1 addition & 7 deletions src/Compiler/SyntaxTree/SyntaxTree.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -1195,13 +1195,7 @@ type SynAttributes = SynAttributeList list
/// Represents extra information about the declaration of a value
[<NoEquality; NoComparison>]
type SynValData =
| SynValData of
memberFlags: SynMemberFlags option *
valInfo: SynValInfo *
thisIdOpt: Ident option *
/// Is only populated during type-checking when an property has both a getter and setter.
/// It is used to track the fact that the getter and setter are part of the same property when they are desugared.
transformedFromProperty: Ident option
| SynValData of memberFlags: SynMemberFlags option * valInfo: SynValInfo * thisIdOpt: Ident option

member SynValInfo: SynValInfo

Expand Down
Loading

0 comments on commit 041420c

Please sign in to comment.