Skip to content

Commit d779982

Browse files
committed
Fixup #1: perf, code quality, and test improvements
- PERF: Replace docHasInheritDoc with tryGetInheritDocXmlText to avoid double GetXmlText() allocation (GetXmlText joins lines each call) - CODE-QUALITY: Remove dead code in getImplicitTargetCrefForMember (memberName match always returned Some, making None branch unreachable) - TEST-CODE-QUALITY: Fix misleading test name (remove 'should emit warning' claim that was not verified) - TEST-COVERAGE: Strengthen inheritdoc preserves surrounding doc elements test with assertions for all three doc parts and no-inheritdoc check
1 parent 804ce28 commit d779982

2 files changed

Lines changed: 45 additions & 42 deletions

File tree

src/Compiler/Symbols/Symbols.fs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,19 @@ module Impl =
270270
| Some xmlDoc when not xmlDoc.IsEmpty -> Some(xmlDoc.GetXmlText())
271271
| _ -> None)
272272

273-
/// Quick check if XML doc text contains an <inheritdoc> element
274-
let docHasInheritDoc (doc: XmlDoc) =
275-
not doc.IsEmpty && doc.GetXmlText().IndexOf("<inheritdoc") >= 0
273+
/// Returns the XML text if it contains an <inheritdoc> element, or None.
274+
/// Avoids a second GetXmlText() allocation by returning the text for reuse.
275+
let tryGetInheritDocXmlText (doc: XmlDoc) =
276+
if doc.IsEmpty then None
277+
else
278+
let xmlText = doc.GetXmlText()
279+
if xmlText.IndexOf("<inheritdoc") >= 0 then Some xmlText else None
276280

277281
/// Creates an FSharpXmlDoc with <inheritdoc> elements expanded
278282
let makeExpandedXmlDoc (cenv: SymbolEnv) (implicitTargetCrefOpt: string option) (doc: XmlDoc) =
279-
if doc.IsEmpty then
280-
FSharpXmlDoc.FromXmlText doc
281-
else
282-
let resolveCref = buildCrefResolver cenv
283-
let expandedDoc = expandInheritDoc resolveCref implicitTargetCrefOpt doc.Range Set.empty doc
284-
FSharpXmlDoc.FromXmlText expandedDoc
283+
let resolveCref = buildCrefResolver cenv
284+
let expandedDoc = expandInheritDoc resolveCref implicitTargetCrefOpt doc.Range Set.empty doc
285+
FSharpXmlDoc.FromXmlText expandedDoc
285286

286287
let makeElaboratedXmlDoc (doc: XmlDoc) =
287288
makeReadOnlyCollection (doc.GetElaboratedXmlLines())
@@ -325,37 +326,34 @@ module Impl =
325326
// For overrides of base class abstract members, slot sigs may be empty.
326327
// Fall back to finding the base type and building a method cref from it.
327328
try
328-
let memberName =
329+
let name =
329330
match d with
330-
| V v -> Some v.DisplayName
331-
| M m | C m -> Some m.DisplayName
332-
| P p -> Some p.PropertyName
333-
| E e -> Some e.EventName
331+
| V v -> v.DisplayName
332+
| M m | C m -> m.DisplayName
333+
| P p -> p.PropertyName
334+
| E e -> e.EventName
334335

335-
match memberName with
336+
let declaringTyOpt =
337+
match d with
338+
| V v ->
339+
match v.TryDeclaringEntity with
340+
| Parent entityRef -> Some(generalizedTyconRef cenv.g entityRef)
341+
| ParentNone -> None
342+
| M m | C m -> Some m.ApparentEnclosingType
343+
| P p -> Some p.ApparentEnclosingType
344+
| E e -> Some e.ApparentEnclosingType
345+
346+
match declaringTyOpt with
347+
| Some declaringTy ->
348+
match GetSuperTypeOfType cenv.g cenv.amap range0 declaringTy with
349+
| Some baseTy when not (isObjTyAnyNullness cenv.g baseTy) ->
350+
match tryTcrefOfAppTy cenv.g baseTy with
351+
| ValueSome baseTcref ->
352+
let baseName = baseTcref.CompiledRepresentationForNamedType.FullName
353+
Some ("M:" + baseName + "." + name)
354+
| ValueNone -> None
355+
| _ -> None
336356
| None -> None
337-
| Some name ->
338-
let declaringTyOpt =
339-
match d with
340-
| V v ->
341-
match v.TryDeclaringEntity with
342-
| Parent entityRef -> Some(generalizedTyconRef cenv.g entityRef)
343-
| ParentNone -> None
344-
| M m | C m -> Some m.ApparentEnclosingType
345-
| P p -> Some p.ApparentEnclosingType
346-
| E e -> Some e.ApparentEnclosingType
347-
348-
match declaringTyOpt with
349-
| Some declaringTy ->
350-
match GetSuperTypeOfType cenv.g cenv.amap range0 declaringTy with
351-
| Some baseTy when not (isObjTyAnyNullness cenv.g baseTy) ->
352-
match tryTcrefOfAppTy cenv.g baseTy with
353-
| ValueSome baseTcref ->
354-
let baseName = baseTcref.CompiledRepresentationForNamedType.FullName
355-
Some ("M:" + baseName + "." + name)
356-
| ValueNone -> None
357-
| _ -> None
358-
| None -> None
359357
with _ -> None
360358

361359
let rescopeEntity optViewedCcu (entity: Entity) =
@@ -977,8 +975,9 @@ type FSharpEntity(cenv: SymbolEnv, entity: EntityRef, tyargs: TType list) =
977975
member _.XmlDoc =
978976
if isUnresolved() then XmlDoc.Empty |> makeXmlDoc else
979977
let doc = entity.XmlDoc
980-
if not (docHasInheritDoc doc) then makeXmlDoc doc
981-
else
978+
match tryGetInheritDocXmlText doc with
979+
| None -> makeXmlDoc doc
980+
| Some _ ->
982981
let implicitTarget = getImplicitTargetCrefForEntity cenv entity
983982
doc |> makeExpandedXmlDoc cenv implicitTarget
984983

@@ -2386,8 +2385,9 @@ type FSharpMemberOrFunctionOrValue(cenv, d:FSharpMemberOrValData, item) =
23862385
| P p -> p.XmlDoc
23872386
| M m | C m -> m.XmlDoc
23882387
| V v -> v.XmlDoc
2389-
if not (docHasInheritDoc doc) then makeXmlDoc doc
2390-
else
2388+
match tryGetInheritDocXmlText doc with
2389+
| None -> makeXmlDoc doc
2390+
| Some _ ->
23912391
// Only compute implicit target and build resolver when doc contains <inheritdoc>
23922392
let slotSigs =
23932393
match d with

tests/FSharp.Compiler.Service.Tests/XmlDocTests.fs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2117,7 +2117,7 @@ type DerivedType() = class end
21172117
Assert.DoesNotContain("inheritdoc", xmlText)
21182118

21192119
[<Fact>]
2120-
let ``unresolvable cref should not crash and should emit warning``() =
2120+
let ``unresolvable cref should not crash``() =
21212121
let code = """
21222122
module Test
21232123
@@ -2146,7 +2146,10 @@ type BaseType() = class end
21462146
type DerivedType() = class end
21472147
"""
21482148
let xmlText = getEntityXmlText code "DerivedType"
2149+
Assert.Contains("My own summary", xmlText)
21492150
Assert.Contains("My own remarks", xmlText)
2151+
Assert.Contains("Base summary", xmlText)
2152+
Assert.DoesNotContain("inheritdoc", xmlText)
21502153

21512154
[<Fact>]
21522155
let ``Discriminated Union - triple slash after case definition should warn``(): unit =

0 commit comments

Comments
 (0)