diff options
author | Sven Boemer <sbomer@gmail.com> | 2022-07-27 02:55:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-27 02:55:54 +0300 |
commit | 145f0d270f6350bea941fbd947802ad319180ed7 (patch) | |
tree | 2ca0cdfe2ee2f2f17d05ea020da3109a54e4cf82 | |
parent | 907020b288d2b5c0d37de7e136e662008deb8e57 (diff) |
Add more tests for parameter dataflow (#2918)
Add tests for some edge cases due to the analyzer not treating
captured parameters as hoisted variables like the linker does.
Currently the analyzer doesn't detect hoisted parameters, and it
will treat them like normal parameters - warning if a parameter
is assigned a value with an annotation that doesn't match the
parameter annotation. The linker will treat captured parameters
the same as other captured variables, tracking all values that
get assigned. So there is a subtle difference in the warning
behavior:
- Linker won't warn on assignment to annotated captured
parameter, but analyzer will.
- Linker will produce dataflow warnings for all values assigned
to a captured parameter, but analyzer will not.
-rw-r--r-- | test/Mono.Linker.Tests.Cases/DataFlow/CompilerGeneratedCodeDataflow.cs | 59 |
1 files changed, 51 insertions, 8 deletions
diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/CompilerGeneratedCodeDataflow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/CompilerGeneratedCodeDataflow.cs index 73d160de7..6129eaaeb 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/CompilerGeneratedCodeDataflow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/CompilerGeneratedCodeDataflow.cs @@ -311,7 +311,7 @@ namespace Mono.Linker.Tests.Cases.DataFlow void LocalFunction () => t.RequiresAll (); } - public static void ReadCapturedParameter (Type tParameter) + public static void ReadCapturedParameter (Type tParameter = null) { LocalFunction (); @@ -319,15 +319,35 @@ namespace Mono.Linker.Tests.Cases.DataFlow void LocalFunction () => tParameter.RequiresAll (); } - public static void ReadCapturedParameterAfterWrite (Type tParameter) + public static void ReadCapturedAnnotatedParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] Type tParameter = null) + { + LocalFunction (); + + [ExpectedWarning ("IL2067", nameof (ReadCapturedAnnotatedParameter), "tParameter", nameof (DataFlowTypeExtensions.RequiresAll))] + void LocalFunction () => tParameter.RequiresAll (); + } + + public static void ReadCapturedParameterAfterWrite (Type tParameter = null) { tParameter = GetWithPublicMethods (); LocalFunction (); + // We produce dataflow warnings for the unknown parameter even though it has been overwritten + // with a value that satisfies the requirement. [ExpectedWarning ("IL2067", "tParameter", nameof (DataFlowTypeExtensions.RequiresPublicMethods))] void LocalFunction () => tParameter.RequiresPublicMethods (); } + [ExpectedWarning ("IL2072", "tParameter", nameof (GetWithPublicFields), ProducedBy = ProducedBy.Analyzer)] + public static void ReadCapturedParameterAfterWriteMismatch ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] Type tParameter = null) + { + tParameter = GetWithPublicFields (); + LocalFunction (); + + [ExpectedWarning ("IL2067", "tParameter", nameof (DataFlowTypeExtensions.RequiresPublicFields))] + void LocalFunction () => tParameter.RequiresPublicFields (); + } + [ExpectedWarning ("IL2072", nameof (GetWithPublicFields), nameof (DataFlowTypeExtensions.RequiresAll))] static void ReadCapturedVariableWithUnhoistedLocals () { @@ -363,8 +383,10 @@ namespace Mono.Linker.Tests.Cases.DataFlow ReadCapturedVariableInMultipleFunctions (); ReadCapturedVariableInCallGraphCycle (); ReadCapturedVariableWithBackwardsBranch (); - ReadCapturedParameter (null); - ReadCapturedParameterAfterWrite (null); + ReadCapturedParameter (); + ReadCapturedAnnotatedParameter (); + ReadCapturedParameterAfterWrite (); + ReadCapturedParameterAfterWriteMismatch (); ReadCapturedVariableWithUnhoistedLocals (); WriteCapturedVariable (); } @@ -416,7 +438,7 @@ namespace Mono.Linker.Tests.Cases.DataFlow lambda (); } - public static void ReadCapturedParameter (Type tParameter) + public static void ReadCapturedParameter (Type tParameter = null) { var lambda = [ExpectedWarning ("IL2067", nameof (ReadCapturedParameter), "tParameter", nameof (DataFlowTypeExtensions.RequiresAll))] @@ -425,7 +447,16 @@ namespace Mono.Linker.Tests.Cases.DataFlow lambda (); } - public static void ReadCapturedParameterAfterWrite (Type tParameter) + public static void ReadCapturedAnnotatedParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] Type tParameter = null) + { + var lambda = + [ExpectedWarning ("IL2067", nameof (ReadCapturedAnnotatedParameter), "tParameter", nameof (DataFlowTypeExtensions.RequiresAll))] + () => tParameter.RequiresAll (); + + lambda (); + } + + public static void ReadCapturedParameterAfterWrite (Type tParameter = null) { tParameter = GetWithPublicMethods (); var lambda = @@ -436,6 +467,16 @@ namespace Mono.Linker.Tests.Cases.DataFlow lambda (); } + [ExpectedWarning ("IL2072", "tParameter", nameof (GetWithPublicFields), ProducedBy = ProducedBy.Analyzer)] + public static void ReadCapturedParameterAfterWriteMismatch ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] Type tParameter = null) + { + tParameter = GetWithPublicFields (); + var lambda = + [ExpectedWarning ("IL2067", "tParameter", nameof (DataFlowTypeExtensions.RequiresPublicFields))] + () => tParameter.RequiresPublicFields (); + lambda (); + } + [ExpectedWarning ("IL2072", nameof (GetWithPublicFields), nameof (DataFlowTypeExtensions.RequiresAll))] static void ReadCapturedVariableWithUnhoistedLocals () { @@ -465,8 +506,10 @@ namespace Mono.Linker.Tests.Cases.DataFlow WarningsInBodyUnused (); ReadCapturedVariable (); ReadCapturedVariableAfterWriteAfterDefinition (); - ReadCapturedParameter (null); - ReadCapturedParameterAfterWrite (null); + ReadCapturedParameter (); + ReadCapturedAnnotatedParameter (); + ReadCapturedParameterAfterWrite (); + ReadCapturedParameterAfterWriteMismatch (); ReadCapturedVariableWithUnhoistedLocals (); WriteCapturedVariable (); } |