forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstructorDataFlow.cs
128 lines (104 loc) · 4.93 KB
/
ConstructorDataFlow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Helpers;
using Mono.Linker.Tests.Cases.Expectations.Metadata;
namespace Mono.Linker.Tests.Cases.DataFlow
{
[SkipKeptItemsValidation]
[ExpectedNoWarnings]
public class ConstructorDataFlow
{
public static void Main ()
{
DataFlowInConstructor.Test ();
DataFlowInStaticConstructor.Test ();
}
class DataFlowInConstructor
{
[ExpectedWarning ("IL2072", nameof (GetUnknown), nameof (RequireAll))]
public DataFlowInConstructor ()
{
RequireAll (GetUnknown ());
}
[ExpectedWarning ("IL2072", nameof (GetUnknown), nameof (RequireAll), CompilerGeneratedCode = true)]
int field = RequireAll (GetUnknown ());
[ExpectedWarning ("IL2072", nameof (GetUnknown), nameof (RequireAll), CompilerGeneratedCode = true)]
int Property { get; } = RequireAll (GetUnknown ());
[ExpectedWarning ("IL2074", nameof (GetUnknown), nameof (annotatedField), CompilerGeneratedCode = true)]
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)]
Type annotatedField = GetUnknown ();
[ExpectedWarning ("IL2074", nameof (GetUnknown), nameof (AnnotatedProperty), CompilerGeneratedCode = true,
ProducedBy = Tool.Trimmer | Tool.NativeAot)] // https://github.com/dotnet/runtime/issues/93277
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)]
Type AnnotatedProperty { get; } = GetUnknown ();
[ExpectedWarning ("IL2074", nameof (GetUnknown), nameof (AnnotatedPropertyWithSetter), CompilerGeneratedCode = true,
ProducedBy = Tool.Trimmer | Tool.NativeAot)] // https://github.com/dotnet/runtime/issues/93277
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)]
Type AnnotatedPropertyWithSetter { get; set; } = GetUnknown ();
// The analyzer dataflow visitor asserts that we only see a return value
// inside of an IMethodSymbol. This testcase checks that we don't hit asserts
// in case the return statement is in a lambda owned by a field.
// When the lambda is analyzed, the OwningSymbol is still an IMethodSymbol
// (the symbol representing the lambda, not the field).
int fieldWithReturnStatementInInitializer = Execute(
[ExpectedWarning ("IL2072", nameof (GetUnknown), nameof (RequireAll))]
() => {
return RequireAll (GetUnknown ());
});
// When analyzer visits the lambda, its containing symbol is the compiler-generated
// backing field of the property, not the property itself.
Func<int> PropertyWithReturnStatementInInitializer { get; } =
[ExpectedWarning ("IL2072", nameof (GetUnknown), nameof (RequireAll))]
() => {
return RequireAll (GetUnknown ());
};
// For property accessors, the containing symbol is the accessor method.
Func<int> PropertyWithReturnStatementInGetter =>
[ExpectedWarning ("IL2072", nameof (GetUnknown), nameof (RequireAll))]
() => {
return RequireAll (GetUnknown ());
};
static int Execute(Func<int> f) => f();
int fieldWithThrowStatementInInitializer = string.Empty.Length == 0 ? throw new Exception() : 0;
int PropertyWithThrowStatementInInitializer { get; } = string.Empty.Length == 0 ? throw new Exception() : 0;
[ExpectedWarning ("IL2067", nameof (TryGetUnknown), nameof (RequireAll), CompilerGeneratedCode = true,
ProducedBy = Tool.Trimmer | Tool.NativeAot)] // https://github.com/dotnet/linker/issues/2158
int fieldWithLocalReferenceInInitializer = TryGetUnknown (out var type) ? RequireAll (type) : 0;
[ExpectedWarning ("IL2067", nameof (TryGetUnknown), nameof (RequireAll), CompilerGeneratedCode = true,
ProducedBy = Tool.Trimmer | Tool.NativeAot)] // https://github.com/dotnet/linker/issues/2158
int PropertyWithLocalReferenceInInitializer { get; } = TryGetUnknown (out var type) ? RequireAll (type) : 0;
public static void Test ()
{
var instance = new DataFlowInConstructor ();
var _ = instance.PropertyWithReturnStatementInGetter;
}
}
class DataFlowInStaticConstructor
{
[ExpectedWarning ("IL2072", nameof (GetUnknown), nameof (RequireAll))]
static DataFlowInStaticConstructor ()
{
RequireAll (GetUnknown ());
}
[ExpectedWarning ("IL2072", nameof (GetUnknown), nameof (RequireAll), CompilerGeneratedCode = true)]
static int field = RequireAll (GetUnknown ());
[ExpectedWarning ("IL2072", nameof (GetUnknown), nameof (RequireAll), CompilerGeneratedCode = true)]
static int Property { get; } = RequireAll (GetUnknown ());
public static void Test ()
{
}
}
static Type GetUnknown () => null;
static bool TryGetUnknown (out Type type)
{
type = null;
return true;
}
static int RequireAll ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type) => 0;
}
}