Skip to content

Commit e0100ac

Browse files
authored
Symbols: add Mfv.ApparentEnclosingType (#17494)
1 parent 0827571 commit e0100ac

File tree

8 files changed

+54
-21
lines changed

8 files changed

+54
-21
lines changed

docs/release-notes/.FSharp.Compiler.Service/10.0.100.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
### Added
2+
* Symbols: add Mfv.ApparentEnclosingType ([PR #17494](https://github.com/dotnet/fsharp/pull/17494))
23
* Add opt-in warning attribute not valid for union case with fields [PR #18532](https://github.com/dotnet/fsharp/pull/18532))
34
* Add support for `when 'T : Enum` library-only static optimization constraint. ([PR #18546](https://github.com/dotnet/fsharp/pull/18546))
45
* Add support for tail calls in computation expressions ([PR #18804](https://github.com/dotnet/fsharp/pull/18804))

src/Compiler/Service/ServiceInterfaceStubGenerator.fs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,12 @@ module InterfaceStubGenerator =
351351
// Ordinary instance members
352352
| _, true, _, name -> name + parArgs
353353
// Ordinary functions or values
354-
| false, _, _, name when not (v.ApparentEnclosingEntity.HasAttribute<RequireQualifiedAccessAttribute>()) ->
354+
| false, _, _, name when
355+
v.ApparentEnclosingEntity
356+
|> Option.map _.HasAttribute<RequireQualifiedAccessAttribute>()
357+
|> Option.defaultValue false
358+
|> not
359+
->
355360
name + " " + parArgs
356361
// Ordinary static members or things (?) that require fully qualified access
357362
| _, _, _, name -> name + parArgs

src/Compiler/Symbols/Symbols.fs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,20 +1722,36 @@ type FSharpMemberOrFunctionOrValue(cenv, d:FSharpMemberOrValData, item) =
17221722
| ParentNone -> None
17231723
| Parent p -> FSharpEntity(cenv, p) |> Some
17241724

1725-
member _.ApparentEnclosingEntity: FSharpEntity =
1725+
member _.ApparentEnclosingEntity: FSharpEntity option =
17261726
let createEntity (ttype: TType) =
1727-
let tcref, tyargs = destAppTy cenv.g ttype
1728-
FSharpEntity(cenv, tcref, tyargs)
1727+
match tryAppTy cenv.g ttype with
1728+
| ValueSome(tcref, tyargs) -> Some(FSharpEntity(cenv, tcref, tyargs))
1729+
| _ -> None
17291730

17301731
checkIsResolved()
1731-
match d with
1732+
1733+
match d with
17321734
| E e -> createEntity e.ApparentEnclosingType
17331735
| P p -> createEntity p.ApparentEnclosingType
17341736
| M m | C m -> createEntity m.ApparentEnclosingType
1735-
| V v ->
1736-
match v.ApparentEnclosingEntity with
1737-
| ParentNone -> invalidOp "the value or member doesn't have a logical parent"
1738-
| Parent p -> FSharpEntity(cenv, p)
1737+
| V v ->
1738+
1739+
match v.ApparentEnclosingEntity with
1740+
| ParentNone -> invalidOp "the value or member doesn't have a logical parent"
1741+
| Parent p -> Some(FSharpEntity(cenv, p))
1742+
1743+
member _.ApparentEnclosingType: FSharpType =
1744+
checkIsResolved()
1745+
1746+
match d with
1747+
| E e -> FSharpType(cenv, e.ApparentEnclosingType)
1748+
| P p -> FSharpType(cenv, p.ApparentEnclosingType)
1749+
| M m | C m -> FSharpType(cenv, m.ApparentEnclosingType)
1750+
| V v ->
1751+
1752+
match v.ApparentEnclosingEntity with
1753+
| ParentNone -> invalidOp "the value or member doesn't have a logical parent"
1754+
| Parent p -> FSharpType(cenv, generalizedTyconRef cenv.g p)
17391755

17401756
member _.GenericParameters =
17411757
checkIsResolved()

src/Compiler/Symbols/Symbols.fsi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,10 @@ type FSharpMemberOrFunctionOrValue =
794794
member DeclaringEntity: FSharpEntity option
795795

796796
/// Get the logical enclosing entity, which for an extension member is type being extended
797-
member ApparentEnclosingEntity: FSharpEntity
797+
member ApparentEnclosingEntity: FSharpEntity option
798+
799+
/// Get the logical enclosing type, which for an extension member is type being extended
800+
member ApparentEnclosingType: FSharpType
798801

799802
/// Get the declaration location of the member, function or value
800803
member DeclarationLocation: range

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,11 @@ let getSymbolFullName (symbol: FSharpSymbol) =
421421
| :? FSharpField as field -> Some field.FullName
422422
| _ -> None
423423

424+
let tryGetEntityFullName (entity: FSharpEntity option) =
425+
entity
426+
|> Option.map _.FullName
427+
|> Option.defaultValue ""
428+
424429
let assertContainsSymbolWithName name source =
425430
getSymbols source
426431
|> Seq.choose getSymbolName

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,8 +2081,8 @@ 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] TryGetCapturedDisplayContext(FSharp.Compiler.Text.Range)
20852084
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpDisplayContext] GetDisplayContextForPos(FSharp.Compiler.Text.Position)
2085+
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpDisplayContext] TryGetCapturedDisplayContext(FSharp.Compiler.Text.Range)
20862086
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpImplementationFileContents] ImplementationFile
20872087
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpImplementationFileContents] get_ImplementationFile()
20882088
FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpType] TryGetCapturedType(FSharp.Compiler.Text.Range)
@@ -5630,8 +5630,6 @@ FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: Boolean get_IsValCompiled
56305630
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: Boolean get_IsValue()
56315631
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpAccessibility Accessibility
56325632
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpAccessibility get_Accessibility()
5633-
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpEntity ApparentEnclosingEntity
5634-
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpEntity get_ApparentEnclosingEntity()
56355633
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpInlineAnnotation InlineAnnotation
56365634
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpInlineAnnotation get_InlineAnnotation()
56375635
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue EventAddMethod
@@ -5644,8 +5642,10 @@ FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.F
56445642
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue get_SetterMethod()
56455643
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpParameter ReturnParameter
56465644
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpParameter get_ReturnParameter()
5645+
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpType ApparentEnclosingType
56475646
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpType EventDelegateType
56485647
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpType FullType
5648+
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpType get_ApparentEnclosingType()
56495649
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpType get_EventDelegateType()
56505650
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpType get_FullType()
56515651
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Symbols.FSharpXmlDoc XmlDoc
@@ -5654,7 +5654,9 @@ FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Text.Rang
56545654
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Text.Range get_DeclarationLocation()
56555655
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: FSharp.Compiler.Text.TaggedText[] FormatLayout(FSharp.Compiler.Symbols.FSharpDisplayContext)
56565656
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: Int32 GetHashCode()
5657+
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpEntity] ApparentEnclosingEntity
56575658
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpEntity] DeclaringEntity
5659+
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpEntity] get_ApparentEnclosingEntity()
56585660
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpEntity] get_DeclaringEntity()
56595661
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue] EventForFSharpProperty
56605662
FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue: Microsoft.FSharp.Core.FSharpOption`1[FSharp.Compiler.Symbols.FSharpMemberOrFunctionOrValue] get_EventForFSharpProperty()

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3317,9 +3317,9 @@ let ``Test Project23 property`` () =
33173317
extensionProps
33183318
|> Array.collect (fun f ->
33193319
[| if f.HasGetterMethod then
3320-
yield (f.DeclaringEntity.Value.FullName, f.ApparentEnclosingEntity.FullName, f.GetterMethod.CompiledName, f.GetterMethod.DeclaringEntity.Value.FullName, attribsOfSymbol f)
3320+
yield (f.DeclaringEntity.Value.FullName, tryGetEntityFullName f.ApparentEnclosingEntity, f.GetterMethod.CompiledName, f.GetterMethod.DeclaringEntity.Value.FullName, attribsOfSymbol f)
33213321
if f.HasSetterMethod then
3322-
yield (f.DeclaringEntity.Value.FullName, f.ApparentEnclosingEntity.FullName, f.SetterMethod.CompiledName, f.SetterMethod.DeclaringEntity.Value.FullName, attribsOfSymbol f)
3322+
yield (f.DeclaringEntity.Value.FullName, tryGetEntityFullName f.ApparentEnclosingEntity, f.SetterMethod.CompiledName, f.SetterMethod.DeclaringEntity.Value.FullName, attribsOfSymbol f)
33233323
|])
33243324
|> Array.toList
33253325

@@ -3362,9 +3362,9 @@ let ``Test Project23 extension properties' getters/setters should refer to the c
33623362
match x.Symbol with
33633363
| :? FSharpMemberOrFunctionOrValue as f ->
33643364
if f.HasGetterMethod then
3365-
yield (f.DeclaringEntity.Value.FullName, f.GetterMethod.DeclaringEntity.Value.FullName, f.ApparentEnclosingEntity.FullName, f.GetterMethod.ApparentEnclosingEntity.FullName, attribsOfSymbol f)
3365+
yield (f.DeclaringEntity.Value.FullName, f.GetterMethod.DeclaringEntity.Value.FullName, tryGetEntityFullName f.ApparentEnclosingEntity, tryGetEntityFullName f.GetterMethod.ApparentEnclosingEntity, attribsOfSymbol f)
33663366
if f.HasSetterMethod then
3367-
yield (f.DeclaringEntity.Value.FullName, f.SetterMethod.DeclaringEntity.Value.FullName, f.ApparentEnclosingEntity.FullName, f.SetterMethod.ApparentEnclosingEntity.FullName, attribsOfSymbol f)
3367+
yield (f.DeclaringEntity.Value.FullName, f.SetterMethod.DeclaringEntity.Value.FullName, tryGetEntityFullName f.ApparentEnclosingEntity, tryGetEntityFullName f.SetterMethod.ApparentEnclosingEntity, attribsOfSymbol f)
33683368
| _ -> ()
33693369
|])
33703370
|> Array.toList

vsintegration/src/FSharp.Editor/Navigation/GoToDefinition.fs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ type internal GoToDefinition(metadataAsSource: FSharpMetadataAsSourceService) =
204204
match targetSymbolUse.Symbol with
205205
| :? FSharpEntity as symbol -> symbol.TryGetMetadataText() |> Option.map (fun text -> text, symbol.DisplayName)
206206
| :? FSharpMemberOrFunctionOrValue as symbol ->
207-
symbol.ApparentEnclosingEntity.TryGetMetadataText()
208-
|> Option.map (fun text -> text, symbol.ApparentEnclosingEntity.DisplayName)
207+
symbol.ApparentEnclosingEntity
208+
|> Option.bind (fun entity -> entity.TryGetMetadataText() |> Option.map (fun text -> text, entity.DisplayName))
209+
209210
| :? FSharpField as symbol ->
210211
match symbol.DeclaringEntity with
211212
| Some entity ->
@@ -652,8 +653,8 @@ type internal GoToDefinition(metadataAsSource: FSharpMetadataAsSourceService) =
652653
match targetSymbolUse.Symbol with
653654
| :? FSharpEntity as symbol -> symbol.TryGetMetadataText() |> Option.map (fun text -> text, symbol.DisplayName)
654655
| :? FSharpMemberOrFunctionOrValue as symbol ->
655-
symbol.ApparentEnclosingEntity.TryGetMetadataText()
656-
|> Option.map (fun text -> text, symbol.ApparentEnclosingEntity.DisplayName)
656+
symbol.ApparentEnclosingEntity
657+
|> Option.bind (fun entity -> entity.TryGetMetadataText() |> Option.map (fun text -> text, entity.DisplayName))
657658
| :? FSharpField as symbol ->
658659
match symbol.DeclaringEntity with
659660
| Some entity ->

0 commit comments

Comments
 (0)