1

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.

6
  • 2
    you mean IntelliJ's compiler hasn't solved the halting problem? I'm shocked! ; ) Commented Mar 18, 2012 at 4:28
  • 1
    Fortunately, I don't need the halting problem solved, I just need IntelliJ to treat this was a warning, not an error. Commented Mar 18, 2012 at 5:19
  • @TonyEnnis: Re the bounty, what you're asking is impossible. I've updated my answer with an explanation. Commented Mar 22, 2012 at 1:06
  • I got what I wanted. Can't award the bounty for 2 more hours. Commented Mar 22, 2012 at 2:25
  • This is just a snippet. Down below, there's a 'goto' to 1001. Commented Mar 22, 2012 at 2:35

1 Answer 1

1
+50

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, i.e. 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) { ... }
        ...
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Rut roh. Are you saying that the Double TMP_1 in the 1000 case is out of scope with respect to the usage in 1001?
It is not actually out of scope (which is shared throughout the body of the switch).
The code I posted above is a snippet; both labels are used. I can't delete the 1001. What you see there is a for-loop implementation. Somewhere a little later, 'I' will be be incremented and control passed back to 1001. I am simulating BASIC's goto statement with a switch statement... I'd like this error to be a warning...
@TonyEnnis: Wait a second. How are you passing control back to 1001? Java doesn't support goto at all, even to switch labels.
muahahha. while(exitVar) { switch(goto) { ... } }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.