1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+
4+ using System ;
5+ using System . Diagnostics . CodeAnalysis ;
6+ using Mono . Linker . Tests . Cases . Expectations . Assertions ;
7+ using Mono . Linker . Tests . Cases . Expectations . Helpers ;
8+ using Mono . Linker . Tests . Cases . Expectations . Metadata ;
9+
10+ namespace Mono . Linker . Tests . Cases . DataFlow
11+ {
12+ [ SkipKeptItemsValidation ]
13+ [ ExpectedNoWarnings ]
14+ class MethodByRefParameterDataFlow
15+ {
16+ public static void Main ( )
17+ {
18+ Type typeWithMethods = _fieldWithMethods ;
19+
20+ TestAssignStaticToAnnotatedRefParameter ( ref typeWithMethods ) ;
21+ TestAssignParameterToAnnotatedRefParameter ( ref typeWithMethods , typeof ( TestType ) ) ;
22+ }
23+
24+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods ) ]
25+ static Type _fieldWithMethods = null ;
26+
27+ [ ExpectedWarning ( "IL2026" , "Message for --TestType.Requires--" ) ]
28+
29+ // https://github.com/dotnet/linker/issues/2158
30+ // The type.GetMethods call generates a warning because we're not able to correctly track the value of the "this".
31+ // (there's a ldind.ref insruction here which we currently don't handle and the "this" becomes unknown)
32+ [ ExpectedWarning ( "IL2065" ) ]
33+ static void TestAssignStaticToAnnotatedRefParameter ( [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods ) ] ref Type type )
34+ {
35+ type = typeof ( TestTypeWithRequires ) ;
36+ type . GetMethods ( ) ; // Should not warn
37+ }
38+
39+ // The warning message is REALLY confusing (basically wrong) since it talks about "calling the method with wrong argument"
40+ // which is definitely not the case here.
41+ [ ExpectedWarning ( "IL2067" , "typeWithFields" ) ]
42+
43+ // https://github.com/dotnet/linker/issues/2158
44+ // The type.GetMethods call generates a warning because we're not able to correctly track the value of the "this".
45+ // (there's a ldind.ref insruction here which we currently don't handle and the "this" becomes unknown)
46+ [ ExpectedWarning ( "IL2065" ) ]
47+ static void TestAssignParameterToAnnotatedRefParameter (
48+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods ) ] ref Type type ,
49+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicFields ) ] Type typeWithFields )
50+ {
51+ type = typeWithFields ; // Should warn
52+ type . GetMethods ( ) ; // Should not warn
53+ }
54+
55+ class TestTypeWithRequires
56+ {
57+ [ RequiresUnreferencedCode ( "Message for --TestType.Requires--" ) ]
58+ public static void Requires ( ) { }
59+ }
60+
61+ class TestType
62+ {
63+ }
64+ }
65+ }
0 commit comments