Skip to content

Commit f9aae25

Browse files
Copilotabonie
authored andcommitted
Fix completion inconsistently filtering obsolete fields and events
Add ILFieldInfoIsUnseen and EventInfoIsUnseen functions to filter obsolete IL fields and events from completion, matching existing behavior for methods and properties. Also update ItemIsUnseen to handle ILField and Event items. Fixes #13693 Agent-Logs-Url: https://github.com/dotnet/fsharp/sessions/1d21d452-3f55-4d56-898c-0b50980050b5 Co-authored-by: abonie <20281641+abonie@users.noreply.github.com>
1 parent d85a74c commit f9aae25

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Fix `YieldFromFinal`/`ReturnFromFinal` being incorrectly called in non-tail positions (`for`, `use`, `use!`, `try/with` handler). ([Issue #19402](https://github.com/dotnet/fsharp/issues/19402), [PR #19403](https://github.com/dotnet/fsharp/pull/19403))
1818
* Fixed how the source ranges of warn directives are reported (as trivia) in the parser output (by not reporting leading spaces). ([Issue #19405](https://github.com/dotnet/fsharp/issues/19405), [PR #19408]((https://github.com/dotnet/fsharp/pull/19408)))
1919
* Fix UoM value type `ToString()` returning garbage values when `--checknulls+` is enabled, caused by double address-taking in codegen. ([Issue #19435](https://github.com/dotnet/fsharp/issues/19435), [PR #19440](https://github.com/dotnet/fsharp/pull/19440))
20+
* Fix completion inconsistently showing some obsolete members (fields and events) while hiding others (methods and properties). All obsolete members are now consistently hidden by default. ([Issue #13693](https://github.com/dotnet/fsharp/issues/13693), [PR #19506](https://github.com/dotnet/fsharp/pull/19506))
2021

2122
### Added
2223

src/Compiler/Checking/AttributeChecking.fs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,27 @@ let PropInfoIsUnseen _m allowObsolete pinfo =
639639
CheckProvidedAttributesForUnseen (pi.PApply((fun st -> (st :> IProvidedCustomAttributeProvider)), m)) m
640640
#endif
641641

642+
/// Indicate if an ILFieldInfo has 'Obsolete' attribute.
643+
/// Used to suppress the item in intellisense.
644+
let ILFieldInfoIsUnseen (finfo: ILFieldInfo) =
645+
match finfo with
646+
| ILFieldInfo(_, fdef) -> CheckILAttributesForUnseen fdef.CustomAttrs
647+
#if !NO_TYPEPROVIDERS
648+
| ProvidedField(_amap, fi, m) ->
649+
CheckProvidedAttributesForUnseen (fi.PApply((fun st -> (st :> IProvidedCustomAttributeProvider)), m)) m
650+
#endif
651+
652+
/// Indicate if an EventInfo has 'Obsolete' or 'CompilerMessageAttribute'.
653+
/// Used to suppress the item in intellisense.
654+
let EventInfoIsUnseen allowObsolete (einfo: EventInfo) =
655+
match einfo with
656+
| ILEvent(ILEventInfo(_, ilEventDef)) -> CheckILAttributesForUnseen ilEventDef.CustomAttrs
657+
| FSEvent(g, _, addValRef, _) -> CheckFSharpAttributesForUnseen g addValRef.Attribs allowObsolete
658+
#if !NO_TYPEPROVIDERS
659+
| ProvidedEvent(_amap, ei, m) ->
660+
CheckProvidedAttributesForUnseen (ei.PApply((fun st -> (st :> IProvidedCustomAttributeProvider)), m)) m
661+
#endif
662+
642663
/// Check the attributes on a union case, returning errors and warnings as data.
643664
let CheckUnionCaseAttributes g (x:UnionCaseRef) m =
644665
trackErrors {

src/Compiler/Checking/AttributeChecking.fsi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ val MethInfoIsUnseen: g: TcGlobals -> m: range -> ty: TType -> minfo: MethInfo -
101101

102102
val PropInfoIsUnseen: _m: 'a -> allowObsolete: bool -> pinfo: PropInfo -> bool
103103

104+
val ILFieldInfoIsUnseen: finfo: ILFieldInfo -> bool
105+
106+
val EventInfoIsUnseen: allowObsolete: bool -> einfo: EventInfo -> bool
107+
104108
val CheckEntityAttributes: g: TcGlobals -> tcref: TyconRef -> m: range -> OperationResult<unit>
105109

106110
val CheckUnionCaseAttributes: g: TcGlobals -> x: UnionCaseRef -> m: range -> OperationResult<unit>

src/Compiler/Checking/NameResolution.fs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4393,6 +4393,8 @@ let ItemIsUnseen ad g amap m allowObsolete item =
43934393
isUnseenNameOfOperator || IsValUnseen ad g m allowObsolete x
43944394
| Item.UnionCase(x, _) -> IsUnionCaseUnseen ad g amap m allowObsolete x.UnionCaseRef
43954395
| Item.ExnCase x -> IsTyconUnseen ad g amap m allowObsolete x
4396+
| Item.ILField finfo -> not allowObsolete && ILFieldInfoIsUnseen finfo
4397+
| Item.Event einfo -> not allowObsolete && EventInfoIsUnseen allowObsolete einfo
43964398
| _ -> false
43974399

43984400
let ItemOfTyconRef ncenv m (x: TyconRef) =
@@ -4467,7 +4469,8 @@ let ResolveCompletionsInType (ncenv: NameResolver) nenv (completionTargets: Reso
44674469
ncenv.InfoReader.GetEventInfosOfType(None, ad, m, ty)
44684470
|> List.filter (fun x ->
44694471
IsStandardEventInfo ncenv.InfoReader m ad x &&
4470-
x.IsStatic = statics)
4472+
x.IsStatic = statics &&
4473+
(allowObsolete || not (EventInfoIsUnseen allowObsolete x)))
44714474
else []
44724475

44734476
let nestedTypes =
@@ -4482,7 +4485,8 @@ let ResolveCompletionsInType (ncenv: NameResolver) nenv (completionTargets: Reso
44824485
|> List.filter (fun x ->
44834486
not x.IsSpecialName &&
44844487
x.IsStatic = statics &&
4485-
IsILFieldInfoAccessible g amap m ad x)
4488+
IsILFieldInfoAccessible g amap m ad x &&
4489+
(allowObsolete || not (ILFieldInfoIsUnseen x)))
44864490

44874491
let qinfos =
44884492
ncenv.InfoReader.GetTraitInfosInType None ty

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,28 @@ exception E
691691
try () with E{caret}
692692
"""
693693

694+
// https://github.com/dotnet/fsharp/issues/13693
695+
[<Fact>]
696+
let ``Event - Instance 01`` () =
697+
assertItem "Ev" """
698+
type T() =
699+
[<System.Obsolete; CLIEvent>]
700+
member _.Ev = Event<System.EventHandler, _>().Publish
701+
702+
T().{caret}
703+
"""
704+
705+
// https://github.com/dotnet/fsharp/issues/13693
706+
[<Fact>]
707+
let ``Event - Static 01`` () =
708+
assertItem "Ev" """
709+
type T() =
710+
[<System.Obsolete; CLIEvent>]
711+
static member Ev = Event<System.EventHandler, _>().Publish
712+
713+
T.{caret}
714+
"""
715+
694716

695717
module PatternNameSuggestions =
696718
let private suggestPatternNames = { FSharpCodeCompletionOptions.Default with SuggestPatternNames = true }

0 commit comments

Comments
 (0)