禁用intellij编译器错误 [英] disable an intellij compiler error

查看:107
本文介绍了禁用intellij编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到可能未初始化变量TMP_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) {

错误报告在第14行。在我的程序中,如果没有在案例1000中执行代码块,则无法访问案例1001.显然,Intellij无法解决这个问题。如何禁用此错误?我更愿意使用空指针异常来进行更改。

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.

源代码是由我编写的编译器生成的(源语言是一个古老的BASIC。)重新定位第11行的任务将非常困难。

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.

编辑 - 请参阅下面的Mechanical蜗牛的解释。这根本不是编译器错误;这是一个简单的程序错误。问题是我模拟BASIC的GOTO语句的方式要求我保留switch语句。当我这样做时,tmp变量超出范围。

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.

最终编辑 - 我更改了代码生成器以完全删除TMP变量。

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();
          }

以前for循环中的算术是使用tmp变量之前设置的2026年标签。现在因为没有,所以没有问题。

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.

推荐答案

Java编译器不够聪明,无法证明变量在执行初始化变量的代码之后,您将永远不会 1001 。请记住,Java变量声明是完全静态的;按照设计,Java只允许您的变量以有意义的方式使用,即在使用前进行初始化。并且证明这种情况发生,对于一般代码,相当于解决暂停问题。 (对于编译器知道的所有内容,表达式 I.compareTo(TMP_1)> 0 可能是无意义的,因为它引用了一个不存在的变量。(更确切地说,变量在开关语句的主体范围内声明,但如果跳到标签<$ c $,初始化它的代码将无法执行c>案例1001:。))

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:.))

您不能将此错误转变为警告;这是静态语言的缺点之一。特别是 Java语言规范,第16章要求:

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:


对于本地变量x的每次访问,x必须在访问之前明确赋值,或者发生编译时错误。

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

并且在访问之前变量不是明确赋值(在规范中定义)。 IntelliJ使用Java编译器(通常是javac)编译代码。因为你要做的事情需要是标准的错误,你想要的是不可能的(没有编辑编译器,然后它就不再是Java了。)

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).

相反,只需在周围范围内声明变量,并将其初始化为虚拟值。例如:

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) { ... }
        ...
    }
}

这篇关于禁用intellij编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆