简体   繁体   中英

disable an intellij compiler error

I'm getting a "Variable TMP_1 might not have been initialized" error. Here's the snippet:

10  case 1000:
11       Double TMP_1 = len(T$);
12       I = 1d;
13  case 1001:
14       if (I.compareTo(TMP_1) > 0) {

The error is being reported on line 14. In my program it isn't possible to get to case 1001 without executing the code block at case 1000. Apparently Intellij can't figure that out. How can I disable this error? I'd rather take my changes with a null pointer exception.

The source code was generated by a compiler I wrote (the source language is an ancient BASIC.) Relocating the assignment on line 11 would be very difficult.

EDIT - See Mechanical snail's explanation below. This isn't a compiler bug at all; this is a simple program bug. The issue is that the way I have simulated BASIC's GOTO statement requires that I leave the switch statement. And when I do the tmp variable goes out of scope.

Final edit - I changed the code generator to remove the TMP variables entirely.

case 2026:
          V = (asc(V$)) - (asc(" "));
          dataCursor.restore();
          for (J = 1d; J <= ((V * 8d) * 10d); J++) {
               X = dataCursor.read();
          }

Previously the arithmetic in the for loop was being done using tmp variables set before the 2026 label. Now because there aren't any, there's no problem.

The Java compiler isn't smart enough to prove that the variable you're switching on will never be 1001 until after the code that initializes the variable is executed. Remember that Java variable declarations are completely static; by design, Java only allows your variable to be used in ways that make sense, ie are initialized before use. And proving that this happens, for general code, is equivalent to solving the halting problem. (For all that the compiler knows, the expression I.compareTo(TMP_1) > 0 could be nonsense, since it refers to a nonexistent variable. (More precisely, the variable is declared in the scope of the switch statement's body, but the code that initializes it would not execute if you skip to the label case 1001: .))

You aren't permitted to turn this error into a warning; that's one of the drawbacks of a static language. In particular, the Java Language Specification, chapter 16 requires:

For every access of a local variable [...] x, x must be definitely assigned before the access, or a compile-time error occurs.

and the variable is not "definitely assigned" (as defined in the spec) before access. IntelliJ compiles your code using a Java compiler (usually javac). Since what you're trying to do is required to be an error by the standard, what you want is impossible (short of editing the compiler, and then it wouldn't be Java anymore).

Workaround

Instead, simply declare your variable in the surrounding scope, and initialize it to a dummy value. For example:

Double TMP_1 = null;
while(exitVar) {
    switch(lblToGoTo) {
        ...
        case 1000:
            TMP_1 = len(T$);
            I = 1d;
        case 1001:
            if (I.compareTo(TMP_1) > 0) { ... }
        ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM