File: Issue1096.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 (63 lines) | stat: -rw-r--r-- 1,747 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
// Test case for Issue 1096:
// https://github.com/typetools/checker-framework/issues/1027

import org.checkerframework.checker.initialization.qual.UnknownInitialization;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.checker.nullness.qual.RequiresNonNull;

class PreCond {
    Object f;
    @Nullable Object g;

    PreCond() {
        f = new Object();
        early();
        g = new Object();
        doNullable();
    }

    void earlyBad(@UnknownInitialization PreCond this) {
        // :: error: (dereference.of.nullable)
        f.toString();
    }

    @RequiresNonNull("this.f")
    void early(@UnknownInitialization PreCond this) {
        f.toString();
    }

    @RequiresNonNull("this.g")
    void doNullable(@UnknownInitialization PreCond this) {
        g.toString();
    }

    void foo(@UnknownInitialization PreCond this) {
        // Receiver is not fully initialized, so raise error
        // :: error: (contracts.precondition.not.satisfied)
        early();
    }

    void bar() {
        // Receiver is initialized, so non-null field f is definitely non-null
        early();
        // Nullable fields stay nullable
        // :: error: (contracts.precondition.not.satisfied)
        doNullable();
    }
}

class User {
    void foo(@UnknownInitialization PreCond pc) {
        // Receiver is not fully initialized, so raise error
        // :: error: (contracts.precondition.not.satisfied)
        pc.early();
    }

    void bar(PreCond pc) {
        // Receiver is initialized, so non-null field f is definitely non-null
        pc.early();
        // Nullable fields stay nullable
        // :: error: (contracts.precondition.not.satisfied)
        pc.doNullable();
    }
}