From 1c1e7b3fbf304c2f9da0cbd14413c35466c972ef Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Sat, 1 Oct 2022 12:31:53 +0200 Subject: [PATCH 1/3] Check for ILEventDef custom attributes and make sure in this case the Obsolete attribute is raised --- src/Compiler/Checking/AttributeChecking.fs | 3 +++ src/Compiler/Checking/AttributeChecking.fsi | 2 ++ src/Compiler/Checking/CheckExpressions.fs | 9 ++++++++- .../Language/ObsoleteAttributeCheckingTests.fs | 2 ++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Compiler/Checking/AttributeChecking.fs b/src/Compiler/Checking/AttributeChecking.fs index 82f504865ab..de1aadfc803 100644 --- a/src/Compiler/Checking/AttributeChecking.fs +++ b/src/Compiler/Checking/AttributeChecking.fs @@ -412,6 +412,9 @@ let CheckEntityAttributes g (tcref: TyconRef) m = CheckILAttributes g (isByrefLikeTyconRef g m tcref) tcref.ILTyconRawMetadata.CustomAttrs m else CheckFSharpAttributes g tcref.Attribs m + +let CheckILEventAttributes g (tcref: TyconRef) cattrs m = + CheckILAttributes g (isByrefLikeTyconRef g m tcref) cattrs m /// Check the attributes associated with a method, returning warnings and errors as data. let CheckMethInfoAttributes g m tyargsOpt (minfo: MethInfo) = diff --git a/src/Compiler/Checking/AttributeChecking.fsi b/src/Compiler/Checking/AttributeChecking.fsi index 3236d3bfdbe..cfeaacda269 100644 --- a/src/Compiler/Checking/AttributeChecking.fsi +++ b/src/Compiler/Checking/AttributeChecking.fsi @@ -101,3 +101,5 @@ val IsSecurityAttribute: val IsSecurityCriticalAttribute: g: TcGlobals -> Attrib -> bool val IsAssemblyVersionAttribute: g: TcGlobals -> Attrib -> bool + +val CheckILEventAttributes: g: TcGlobals -> tcref: TyconRef -> cattrs: ILAttributes -> m: range -> OperationResult diff --git a/src/Compiler/Checking/CheckExpressions.fs b/src/Compiler/Checking/CheckExpressions.fs index 1b65421daba..4724ea727d0 100644 --- a/src/Compiler/Checking/CheckExpressions.fs +++ b/src/Compiler/Checking/CheckExpressions.fs @@ -9095,7 +9095,14 @@ and TcEventItemThen (cenv: cenv) overallTy env tpenv mItem mExprAndItem objDetai let (SigOfFunctionForDelegate(delInvokeMeth, delArgTys, _, _)) = GetSigOfFunctionForDelegate cenv.infoReader delTy mItem ad let objArgs = Option.toList (Option.map fst objDetails) MethInfoChecks g cenv.amap true None objArgs env.eAccessRights mItem delInvokeMeth - + + let ilEventDefCustomAttrs = + match einfo with + | ILEvent(ILEventInfo(_, ilEventDef))-> ilEventDef.CustomAttrs + | _ -> ILAttributes.Empty + + CheckILEventAttributes g einfo.DeclaringTyconRef ilEventDefCustomAttrs mItem |> CommitOperationResult + // This checks for and drops the 'object' sender let argsTy = ArgsTypeOfEventInfo cenv.infoReader mItem ad einfo if not (slotSigHasVoidReturnTy (delInvokeMeth.GetSlotSig(cenv.amap, mItem))) then errorR (nonStandardEventError einfo.EventName mItem) diff --git a/tests/FSharp.Compiler.ComponentTests/Language/ObsoleteAttributeCheckingTests.fs b/tests/FSharp.Compiler.ComponentTests/Language/ObsoleteAttributeCheckingTests.fs index 29ea071835f..1c3708ae7b9 100644 --- a/tests/FSharp.Compiler.ComponentTests/Language/ObsoleteAttributeCheckingTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/Language/ObsoleteAttributeCheckingTests.fs @@ -917,6 +917,7 @@ Class.ObsoleteEvent |> ignore (Warning 44, Line 3, Col 1, Line 3, Col 20, "This construct is deprecated. Field is obsolete"); (Warning 44, Line 4, Col 1, Line 4, Col 21, "This construct is deprecated. Method is obsolete"); (Warning 44, Line 5, Col 1, Line 5, Col 23, "This construct is deprecated. Property is obsolete") + (Warning 44, Line 6, Col 1, Line 6, Col 20, "This construct is deprecated. Event is obsolete") ] [] @@ -955,4 +956,5 @@ Class.ObsoleteEvent |> ignore (Error 101, Line 3, Col 1, Line 3, Col 20, "This construct is deprecated. Field is obsolete"); (Error 101, Line 4, Col 1, Line 4, Col 21, "This construct is deprecated. Method is obsolete"); (Error 101, Line 5, Col 1, Line 5, Col 23, "This construct is deprecated. Property is obsolete") + (Error 101, Line 6, Col 1, Line 6, Col 20, "This construct is deprecated. Event is obsolete") ] From 5fc222b332d4579ee1148aa68f07c8e48b94d233 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Sat, 1 Oct 2022 13:06:46 +0200 Subject: [PATCH 2/3] Fix formatting --- src/Compiler/Checking/AttributeChecking.fsi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compiler/Checking/AttributeChecking.fsi b/src/Compiler/Checking/AttributeChecking.fsi index cfeaacda269..622864eff4e 100644 --- a/src/Compiler/Checking/AttributeChecking.fsi +++ b/src/Compiler/Checking/AttributeChecking.fsi @@ -102,4 +102,4 @@ val IsSecurityCriticalAttribute: g: TcGlobals -> Attrib -> bool val IsAssemblyVersionAttribute: g: TcGlobals -> Attrib -> bool -val CheckILEventAttributes: g: TcGlobals -> tcref: TyconRef -> cattrs: ILAttributes -> m: range -> OperationResult +val CheckILEventAttributes: g: TcGlobals -> tcref: TyconRef -> cattrs: ILAttributes -> m: range -> OperationResult From 5dbc34e90522ea3c227fff4cd2c3d93534a90c7e Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Tue, 4 Oct 2022 18:59:12 +0200 Subject: [PATCH 3/3] Fix PR comments --- src/Compiler/Checking/CheckExpressions.fs | 7 +------ src/Compiler/Checking/infos.fs | 6 ++++++ src/Compiler/Checking/infos.fsi | 3 +++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Compiler/Checking/CheckExpressions.fs b/src/Compiler/Checking/CheckExpressions.fs index 4724ea727d0..bd675aba272 100644 --- a/src/Compiler/Checking/CheckExpressions.fs +++ b/src/Compiler/Checking/CheckExpressions.fs @@ -9095,13 +9095,8 @@ and TcEventItemThen (cenv: cenv) overallTy env tpenv mItem mExprAndItem objDetai let (SigOfFunctionForDelegate(delInvokeMeth, delArgTys, _, _)) = GetSigOfFunctionForDelegate cenv.infoReader delTy mItem ad let objArgs = Option.toList (Option.map fst objDetails) MethInfoChecks g cenv.amap true None objArgs env.eAccessRights mItem delInvokeMeth - - let ilEventDefCustomAttrs = - match einfo with - | ILEvent(ILEventInfo(_, ilEventDef))-> ilEventDef.CustomAttrs - | _ -> ILAttributes.Empty - CheckILEventAttributes g einfo.DeclaringTyconRef ilEventDefCustomAttrs mItem |> CommitOperationResult + CheckILEventAttributes g einfo.DeclaringTyconRef (einfo.GetCustomAttrs()) mItem |> CommitOperationResult // This checks for and drops the 'object' sender let argsTy = ArgsTypeOfEventInfo cenv.infoReader mItem ad einfo diff --git a/src/Compiler/Checking/infos.fs b/src/Compiler/Checking/infos.fs index 1dc35e9ab09..68e844fa919 100644 --- a/src/Compiler/Checking/infos.fs +++ b/src/Compiler/Checking/infos.fs @@ -2275,6 +2275,12 @@ type EventInfo = | ProvidedEvent (_, ei, _) -> ProvidedEventInfo.TaintedGetHashCode ei #endif override x.ToString() = "event " + x.EventName + + /// Get custom attributes for events (only applicable for IL events) + member x.GetCustomAttrs() = + match x with + | ILEvent(ILEventInfo(_, ilEventDef))-> ilEventDef.CustomAttrs + | _ -> ILAttributes.Empty //------------------------------------------------------------------------- // Helpers associated with getting and comparing method signatures diff --git a/src/Compiler/Checking/infos.fsi b/src/Compiler/Checking/infos.fsi index 63a24eb6502..550c7860b34 100644 --- a/src/Compiler/Checking/infos.fsi +++ b/src/Compiler/Checking/infos.fsi @@ -1009,6 +1009,9 @@ type EventInfo = /// Get the delegate type associated with the event. member GetDelegateType: amap: ImportMap * m: range -> TType + /// Get custom attributes for events (only applicable for IL events) + member GetCustomAttrs: unit -> ILAttributes + /// An exception type used to raise an error using the old error system. /// /// Error text: "A definition to be compiled as a .NET event does not have the expected form. Only property members can be compiled as .NET events."