Disable compiler error intellij

I get the error "Variable TMP_1 may not have been initialized." Here's a 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 reported on line 14. In my program, it is not possible to get case 1001 without executing the code in case 1000. Obviously Intellij cannot figure it out. How can I disable this error? I would rather accept my changes with a null pointer exception.

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

EDIT - see mechanical snail explanation below. This is not a compiler error at all; this is a simple program error. The problem is that the way I simulated the BASIC GOTO statement requires me to leave the switch statement. And when I do the tmp variable is out of scope.

Final edit - I modified the code generator to remove the TMP variables completely.

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 done using the tmp variables set before the 2026 label. Now, because there are none, no problem.

+1


source to share


1 answer


The Java compiler is not smart enough to prove that the variable you include will never be 1001

until the code that initializes the variable is executed. Remember that Java variable declarations are completely static; by design, Java only allows you to use your variable in a way that makes sense, i.e. initialized before use. And proving that this is happening, for general code, is equivalent to solving the halting problem. (For everything the compiler knows, the expression I.compareTo(TMP_1) > 0

might be meaningless because it refers to a non-existent variable. (More precisely, the variable is declared in body scope switch

, but the code that initializes it won't execute if you go to the label case 1001:

.))

You are not allowed to turn this error into a warning; which is one of the disadvantages of a static language. Specifically, the Java Language Specification, Chapter 16 requires:

For each access to a local variable [...] x, x must be definitely defined before access, or a compile-time error occurs.

and the variable is not "definitely assigned" (as defined in the specification) before being accessed. IntelliJ compiles your code with a Java compiler (usually javac). Since what you are trying to do must be a bug by the standard, what you want is not possible (without waiting for compiler editing and then no more Java).



Bypass

Instead, just declare a variable in the surrounding scope and initialize it with 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) { ... }
        ...
    }
}

      

+1


source







All Articles