From ed5736b66c677c37b8c53feeb8148d6f22dfb431 Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Tue, 6 Aug 2024 12:52:21 +0200 Subject: [PATCH] Symbols: add Mfv.ApparentEnclosingType --- .../Service/ServiceInterfaceStubGenerator.fs | 6 +++- src/Compiler/Symbols/Symbols.fs | 32 ++++++++++++++----- src/Compiler/Symbols/Symbols.fsi | 5 ++- tests/FSharp.Compiler.Service.Tests/Common.fs | 5 +++ .../ProjectAnalysisTests.fs | 8 ++--- 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/Compiler/Service/ServiceInterfaceStubGenerator.fs b/src/Compiler/Service/ServiceInterfaceStubGenerator.fs index e02df4c7da71..301d0eb01dbb 100644 --- a/src/Compiler/Service/ServiceInterfaceStubGenerator.fs +++ b/src/Compiler/Service/ServiceInterfaceStubGenerator.fs @@ -351,7 +351,11 @@ module InterfaceStubGenerator = // Ordinary instance members | _, true, _, name -> name + parArgs // Ordinary functions or values - | false, _, _, name when not (v.ApparentEnclosingEntity.HasAttribute()) -> + | false, _, _, name when + v.ApparentEnclosingEntity + |> Option.map _.HasAttribute() + |> Option.defaultValue false + |> not -> name + " " + parArgs // Ordinary static members or things (?) that require fully qualified access | _, _, _, name -> name + parArgs diff --git a/src/Compiler/Symbols/Symbols.fs b/src/Compiler/Symbols/Symbols.fs index 33965a93f2e8..c33278d05475 100644 --- a/src/Compiler/Symbols/Symbols.fs +++ b/src/Compiler/Symbols/Symbols.fs @@ -1713,20 +1713,36 @@ type FSharpMemberOrFunctionOrValue(cenv, d:FSharpMemberOrValData, item) = | ParentNone -> None | Parent p -> FSharpEntity(cenv, p) |> Some - member _.ApparentEnclosingEntity: FSharpEntity = + member _.ApparentEnclosingEntity: FSharpEntity option = let createEntity (ttype: TType) = - let tcref, tyargs = destAppTy cenv.g ttype - FSharpEntity(cenv, tcref, tyargs) + match tryAppTy cenv.g ttype with + | ValueSome(tcref, tyargs) -> Some(FSharpEntity(cenv, tcref, tyargs)) + | _ -> None checkIsResolved() - match d with + + match d with | E e -> createEntity e.ApparentEnclosingType | P p -> createEntity p.ApparentEnclosingType | M m | C m -> createEntity m.ApparentEnclosingType - | V v -> - match v.ApparentEnclosingEntity with - | ParentNone -> invalidOp "the value or member doesn't have a logical parent" - | Parent p -> FSharpEntity(cenv, p) + | V v -> + + match v.ApparentEnclosingEntity with + | ParentNone -> invalidOp "the value or member doesn't have a logical parent" + | Parent p -> Some(FSharpEntity(cenv, p)) + + member _.ApparentEnclosingType: FSharpType = + checkIsResolved() + + match d with + | E e -> FSharpType(cenv, e.ApparentEnclosingType) + | P p -> FSharpType(cenv, p.ApparentEnclosingType) + | M m | C m -> FSharpType(cenv, m.ApparentEnclosingType) + | V v -> + + match v.ApparentEnclosingEntity with + | ParentNone -> invalidOp "the value or member doesn't have a logical parent" + | Parent p -> FSharpType(cenv, generalizedTyconRef cenv.g p) member _.GenericParameters = checkIsResolved() diff --git a/src/Compiler/Symbols/Symbols.fsi b/src/Compiler/Symbols/Symbols.fsi index a61432accf19..ea0fb798acff 100644 --- a/src/Compiler/Symbols/Symbols.fsi +++ b/src/Compiler/Symbols/Symbols.fsi @@ -780,7 +780,10 @@ type FSharpMemberOrFunctionOrValue = member DeclaringEntity: FSharpEntity option /// Get the logical enclosing entity, which for an extension member is type being extended - member ApparentEnclosingEntity: FSharpEntity + member ApparentEnclosingEntity: FSharpEntity option + + /// Get the logical enclosing type, which for an extension member is type being extended + member ApparentEnclosingType: FSharpType /// Get the declaration location of the member, function or value member DeclarationLocation: range diff --git a/tests/FSharp.Compiler.Service.Tests/Common.fs b/tests/FSharp.Compiler.Service.Tests/Common.fs index 68e5c68598ad..6ec431bdff71 100644 --- a/tests/FSharp.Compiler.Service.Tests/Common.fs +++ b/tests/FSharp.Compiler.Service.Tests/Common.fs @@ -409,6 +409,11 @@ let getSymbolFullName (symbol: FSharpSymbol) = | :? FSharpField as field -> Some field.FullName | _ -> None +let tryGetEntityFullName (entity: FSharpEntity option) = + entity + |> Option.map _.FullName + |> Option.defaultValue "" + let assertContainsSymbolWithName name source = getSymbols source |> Seq.choose getSymbolName diff --git a/tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs b/tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs index f520a06bb2e3..cc48449611ca 100644 --- a/tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs +++ b/tests/FSharp.Compiler.Service.Tests/ProjectAnalysisTests.fs @@ -3297,9 +3297,9 @@ let ``Test Project23 property`` () = extensionProps |> Array.collect (fun f -> [| if f.HasGetterMethod then - yield (f.DeclaringEntity.Value.FullName, f.ApparentEnclosingEntity.FullName, f.GetterMethod.CompiledName, f.GetterMethod.DeclaringEntity.Value.FullName, attribsOfSymbol f) + yield (f.DeclaringEntity.Value.FullName, tryGetEntityFullName f.ApparentEnclosingEntity, f.GetterMethod.CompiledName, f.GetterMethod.DeclaringEntity.Value.FullName, attribsOfSymbol f) if f.HasSetterMethod then - yield (f.DeclaringEntity.Value.FullName, f.ApparentEnclosingEntity.FullName, f.SetterMethod.CompiledName, f.SetterMethod.DeclaringEntity.Value.FullName, attribsOfSymbol f) + yield (f.DeclaringEntity.Value.FullName, tryGetEntityFullName f.ApparentEnclosingEntity, f.SetterMethod.CompiledName, f.SetterMethod.DeclaringEntity.Value.FullName, attribsOfSymbol f) |]) |> Array.toList @@ -3342,9 +3342,9 @@ let ``Test Project23 extension properties' getters/setters should refer to the c match x.Symbol with | :? FSharpMemberOrFunctionOrValue as f -> if f.HasGetterMethod then - yield (f.DeclaringEntity.Value.FullName, f.GetterMethod.DeclaringEntity.Value.FullName, f.ApparentEnclosingEntity.FullName, f.GetterMethod.ApparentEnclosingEntity.FullName, attribsOfSymbol f) + yield (f.DeclaringEntity.Value.FullName, f.GetterMethod.DeclaringEntity.Value.FullName, tryGetEntityFullName f.ApparentEnclosingEntity, tryGetEntityFullName f.GetterMethod.ApparentEnclosingEntity, attribsOfSymbol f) if f.HasSetterMethod then - yield (f.DeclaringEntity.Value.FullName, f.SetterMethod.DeclaringEntity.Value.FullName, f.ApparentEnclosingEntity.FullName, f.SetterMethod.ApparentEnclosingEntity.FullName, attribsOfSymbol f) + yield (f.DeclaringEntity.Value.FullName, f.SetterMethod.DeclaringEntity.Value.FullName, tryGetEntityFullName f.ApparentEnclosingEntity, tryGetEntityFullName f.SetterMethod.ApparentEnclosingEntity, attribsOfSymbol f) | _ -> () |]) |> Array.toList