Debian Codegraph @

framework/tests/whole-program-inference/non-annotated/ExpectedErrors.java

raw
1 import java.lang.reflect.Field;
2 import org.checkerframework.framework.qual.IgnoreInWholeProgramInference;
3 import testlib.wholeprograminference.qual.*;
4
5 /**
6 * This file contains expected errors that should exist even after the jaif type inference occurs.
7 */
8 public class ExpectedErrors {
9
10 // Case where the declared type is a supertype of the refined type.
11 private @Top int privateDeclaredField;
12 public @Top int publicDeclaredField;
13
14 // The type of both privateDeclaredField and publicDeclaredField are
15 // not refined to @WholeProgramInferenceBottom.
16 void assignFieldsToBottom() {
17 privateDeclaredField = getSibling1();
18 publicDeclaredField = getSibling1();
19 }
20
21 void testFields() {
22 // :: error: (argument.type.incompatible)
23 expectsSibling1(privateDeclaredField);
24 // :: error: (argument.type.incompatible)
25 expectsSibling1(publicDeclaredField);
26 }
27
28 // Case where the declared type is a subtype of the refined type.
29 private @WholeProgramInferenceBottom int privateDeclaredField2;
30 public @WholeProgramInferenceBottom int publicDeclaredField2;
31
32 // The refinement cannot happen and an assignemnt type incompatible error occurs.
33 void assignFieldsToTop() {
34 // :: error: (assignment.type.incompatible)
35 privateDeclaredField2 = getTop();
36 // :: error: (assignment.type.incompatible)
37 publicDeclaredField2 = getTop();
38 }
39
40 // No errors should be issued below:
41 void assignFieldsToBot() {
42 privateDeclaredField2 = getBottom();
43 publicDeclaredField2 = getBottom();
44 }
45
46 // Testing that the types above were not widened.
47 void testFields2() {
48 expectsBottom(privateDeclaredField2);
49 expectsBottom(publicDeclaredField2);
50 }
51
52 // LUB TEST
53 // The default type for fields is @Top.
54 private static int lubPrivateField;
55 public static int lubPublicField;
56
57 void assignFieldsToSibling1() {
58 lubPrivateField = getSibling1();
59 lubPublicField = getSibling1();
60 }
61
62 static {
63 lubPrivateField = getSibling2();
64 lubPublicField = getSibling2();
65 }
66
67 void testLUBFields1() {
68 // :: error: (argument.type.incompatible)
69 expectsSibling1(lubPrivateField);
70 // :: error: (argument.type.incompatible)
71 expectsSibling1(lubPublicField);
72 }
73
74 void testLUBFields2() {
75 // :: error: (argument.type.incompatible)
76 expectsSibling2(lubPrivateField);
77 // :: error: (argument.type.incompatible)
78 expectsSibling2(lubPublicField);
79 }
80
81 private static boolean bool = false;
82
83 public static int lubTest() {
84 if (bool) {
85 return (@Sibling1 int) 0;
86 } else {
87 return (@Sibling2 int) 0;
88 }
89 }
90
91 public @Sibling1 int getSibling1Wrong() {
92 int x = lubTest();
93 // :: error: (return.type.incompatible)
94 return x;
95 }
96
97 public @Sibling2 int getSibling2Wrong() {
98 int x = lubTest();
99 // :: error: (return.type.incompatible)
100 return x;
101 }
102
103 void expectsSibling1(@Sibling1 int t) {}
104
105 void expectsSibling2(@Sibling2 int t) {}
106
107 void expectsBottom(@WholeProgramInferenceBottom int t) {}
108
109 void expectsBottom(@WholeProgramInferenceBottom String t) {}
110
111 void expectsTop(@Top int t) {}
112
113 void expectsParent(@Parent int t) {}
114
115 static @Sibling1 int getSibling1() {
116 return 0;
117 }
118
119 static @Sibling2 int getSibling2() {
120 return 0;
121 }
122
123 @WholeProgramInferenceBottom int getBottom() {
124 return 0;
125 }
126
127 @Top int getTop() {
128 return 0;
129 }
130
131 // Method Field.setBoolean != ExpectedErrors.setBoolean.
132 // No refinement should happen.
133 void test(Field f) throws Exception {
134 f.setBoolean(null, false);
135 }
136
137 void setBoolean(Object o, boolean b) {
138 // :: error: (assignment.type.incompatible)
139 @WholeProgramInferenceBottom Object bot = o;
140 }
141
142 public class SuppressWarningsTest {
143 // Tests that whole-program inference in a @SuppressWarnings block is ignored.
144 private int i;
145 private int i2;
146
147 @SuppressWarnings("")
148 public void suppressWarningsTest() {
149 i = (@Sibling1 int) 0;
150 i2 = getSibling1();
151 }
152
153 public void suppressWarningsTest2() {
154 SuppressWarningsInner.i = (@Sibling1 int) 0;
155 SuppressWarningsInner.i2 = getSibling1();
156 }
157
158 public void suppressWarningsValidation() {
159 // :: error: (argument.type.incompatible)
160 expectsSibling1(i);
161 // :: error: (argument.type.incompatible)
162 expectsSibling1(i2);
163 // :: error: (argument.type.incompatible)
164 expectsSibling1(SuppressWarningsInner.i);
165 // :: error: (argument.type.incompatible)
166 expectsSibling1(SuppressWarningsInner.i2);
167 // :: error: (argument.type.incompatible)
168 expectsSibling1(suppressWarningsMethodReturn());
169
170 suppressWarningsMethodParams(getSibling1());
171 }
172
173 @SuppressWarnings("")
174 public int suppressWarningsMethodReturn() {
175 return getSibling1();
176 }
177
178 // It is problematic to automatically test whole-program inference for method
179 // params when suppressing warnings.
180 // Since we must use @SuppressWarnings() for the method,
181 // we won't be able to catch any error inside the method body.
182 // Verified manually that in the "annotated" folder param's type wasn't
183 // updated.
184 @SuppressWarnings("")
185 public void suppressWarningsMethodParams(int param) {}
186 }
187
188 @SuppressWarnings("")
189 static class SuppressWarningsInner {
190 public static int i;
191 public static int i2;
192 }
193
194 class NullTest {
195 // The default type for fields is @DefaultType.
196 private String privateField;
197 public String publicField;
198
199 // The types of both fields are not refined to @WholeProgramInferenceBottom,
200 // as whole-program inference never performs refinement in the presence
201 // of the null literal.
202 void assignFieldsToBottom() {
203 privateField = null;
204 publicField = null;
205 }
206
207 // Testing the refinement above.
208 void testFields() {
209 // :: error: (argument.type.incompatible)
210 expectsBottom(privateField);
211 // :: error: (argument.type.incompatible)
212 expectsBottom(publicField);
213 }
214 }
215
216 class IgnoreMetaAnnotationTest2 {
217 @ToIgnore int field;
218 @IgnoreInWholeProgramInference int field2;
219
220 void foo() {
221 field = getSibling1();
222 field2 = getSibling1();
223 }
224
225 void test() {
226 // :: error: (argument.type.incompatible)
227 expectsSibling1(field);
228 // :: error: (argument.type.incompatible)
229 expectsSibling1(field2);
230 }
231 }
232 }