File: ExpectedErrors.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,104 kB
  • sloc: java: 145,916; xml: 839; sh: 518; makefile: 404; perl: 26
file content (232 lines) | stat: -rw-r--r-- 7,006 bytes parent folder | download | duplicates (3)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import java.lang.reflect.Field;
import org.checkerframework.framework.qual.IgnoreInWholeProgramInference;
import testlib.wholeprograminference.qual.*;

/**
 * This file contains expected errors that should exist even after the jaif type inference occurs.
 */
public class ExpectedErrors {

    // Case where the declared type is a supertype of the refined type.
    private @Top int privateDeclaredField;
    public @Top int publicDeclaredField;

    // The type of both privateDeclaredField and publicDeclaredField are
    // not refined to @WholeProgramInferenceBottom.
    void assignFieldsToBottom() {
        privateDeclaredField = getSibling1();
        publicDeclaredField = getSibling1();
    }

    void testFields() {
        // :: error: (argument.type.incompatible)
        expectsSibling1(privateDeclaredField);
        // :: error: (argument.type.incompatible)
        expectsSibling1(publicDeclaredField);
    }

    // Case where the declared type is a subtype of the refined type.
    private @WholeProgramInferenceBottom int privateDeclaredField2;
    public @WholeProgramInferenceBottom int publicDeclaredField2;

    // The refinement cannot happen and an assignemnt type incompatible error occurs.
    void assignFieldsToTop() {
        // :: error: (assignment.type.incompatible)
        privateDeclaredField2 = getTop();
        // :: error: (assignment.type.incompatible)
        publicDeclaredField2 = getTop();
    }

    // No errors should be issued below:
    void assignFieldsToBot() {
        privateDeclaredField2 = getBottom();
        publicDeclaredField2 = getBottom();
    }

    // Testing that the types above were not widened.
    void testFields2() {
        expectsBottom(privateDeclaredField2);
        expectsBottom(publicDeclaredField2);
    }

    // LUB TEST
    // The default type for fields is @Top.
    private static int lubPrivateField;
    public static int lubPublicField;

    void assignFieldsToSibling1() {
        lubPrivateField = getSibling1();
        lubPublicField = getSibling1();
    }

    static {
        lubPrivateField = getSibling2();
        lubPublicField = getSibling2();
    }

    void testLUBFields1() {
        // :: error: (argument.type.incompatible)
        expectsSibling1(lubPrivateField);
        // :: error: (argument.type.incompatible)
        expectsSibling1(lubPublicField);
    }

    void testLUBFields2() {
        // :: error: (argument.type.incompatible)
        expectsSibling2(lubPrivateField);
        // :: error: (argument.type.incompatible)
        expectsSibling2(lubPublicField);
    }

    private static boolean bool = false;

    public static int lubTest() {
        if (bool) {
            return (@Sibling1 int) 0;
        } else {
            return (@Sibling2 int) 0;
        }
    }

    public @Sibling1 int getSibling1Wrong() {
        int x = lubTest();
        // :: error: (return.type.incompatible)
        return x;
    }

    public @Sibling2 int getSibling2Wrong() {
        int x = lubTest();
        // :: error: (return.type.incompatible)
        return x;
    }

    void expectsSibling1(@Sibling1 int t) {}

    void expectsSibling2(@Sibling2 int t) {}

    void expectsBottom(@WholeProgramInferenceBottom int t) {}

    void expectsBottom(@WholeProgramInferenceBottom String t) {}

    void expectsTop(@Top int t) {}

    void expectsParent(@Parent int t) {}

    static @Sibling1 int getSibling1() {
        return 0;
    }

    static @Sibling2 int getSibling2() {
        return 0;
    }

    @WholeProgramInferenceBottom int getBottom() {
        return 0;
    }

    @Top int getTop() {
        return 0;
    }

    // Method Field.setBoolean != ExpectedErrors.setBoolean.
    // No refinement should happen.
    void test(Field f) throws Exception {
        f.setBoolean(null, false);
    }

    void setBoolean(Object o, boolean b) {
        // :: error: (assignment.type.incompatible)
        @WholeProgramInferenceBottom Object bot = o;
    }

    public class SuppressWarningsTest {
        // Tests that whole-program inference in a @SuppressWarnings block is ignored.
        private int i;
        private int i2;

        @SuppressWarnings("")
        public void suppressWarningsTest() {
            i = (@Sibling1 int) 0;
            i2 = getSibling1();
        }

        public void suppressWarningsTest2() {
            SuppressWarningsInner.i = (@Sibling1 int) 0;
            SuppressWarningsInner.i2 = getSibling1();
        }

        public void suppressWarningsValidation() {
            // :: error: (argument.type.incompatible)
            expectsSibling1(i);
            // :: error: (argument.type.incompatible)
            expectsSibling1(i2);
            // :: error: (argument.type.incompatible)
            expectsSibling1(SuppressWarningsInner.i);
            // :: error: (argument.type.incompatible)
            expectsSibling1(SuppressWarningsInner.i2);
            // :: error: (argument.type.incompatible)
            expectsSibling1(suppressWarningsMethodReturn());

            suppressWarningsMethodParams(getSibling1());
        }

        @SuppressWarnings("")
        public int suppressWarningsMethodReturn() {
            return getSibling1();
        }

        // It is problematic to automatically test whole-program inference for method
        // params when suppressing warnings.
        // Since we must use @SuppressWarnings() for the method,
        // we won't be able to catch any error inside the method body.
        // Verified manually that in the "annotated" folder param's type wasn't
        // updated.
        @SuppressWarnings("")
        public void suppressWarningsMethodParams(int param) {}
    }

    @SuppressWarnings("")
    static class SuppressWarningsInner {
        public static int i;
        public static int i2;
    }

    class NullTest {
        // The default type for fields is @DefaultType.
        private String privateField;
        public String publicField;

        // The types of both fields are not refined to @WholeProgramInferenceBottom,
        // as whole-program inference never performs refinement in the presence
        // of the null literal.
        void assignFieldsToBottom() {
            privateField = null;
            publicField = null;
        }

        // Testing the refinement above.
        void testFields() {
            // :: error: (argument.type.incompatible)
            expectsBottom(privateField);
            // :: error: (argument.type.incompatible)
            expectsBottom(publicField);
        }
    }

    class IgnoreMetaAnnotationTest2 {
        @ToIgnore int field;
        @IgnoreInWholeProgramInference int field2;

        void foo() {
            field = getSibling1();
            field2 = getSibling1();
        }

        void test() {
            // :: error: (argument.type.incompatible)
            expectsSibling1(field);
            // :: error: (argument.type.incompatible)
            expectsSibling1(field2);
        }
    }
}