eclipse-jdt / eclipse-jdt/eclipse.jdt.core

fatalOptionalError causes SuppressWarnings on statements to be ignored

Open
#1,282 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

*This corresponds to the [Bug 516289](https://bugs.eclipse.org/bugs/show_bug.cgi?id=516289) on the old Bugzilla, which still occurs on newer versions*

> It seems that the `org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled` causes the compiler to ignore statement scope `@SuppressWarnings` annotations.

This still happens today, tested on the following versions:

* ecj-4.28.jar: https://download.eclipse.org/eclipse/downloads/drops4/R-4.28-202306050440/
* ecj-4.29M1.jar: https://download.eclipse.org/eclipse/downloads/drops4/S-4.29M1-202307051800/
* ecj-I20230810-1800.jar: https://download.eclipse.org/eclipse/downloads/drops4/I20230810-1800/

`bad.prefs`:
```dosini
org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
org.eclipse.jdt.core.compiler.compliance=20
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled
org.eclipse.jdt.core.compiler.problem.rawTypeReference=error
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=enabled
org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
org.eclipse.jdt.core.compiler.source=20
```

`Test.java`:
```java
public class Test {
public static void main(String[] args) {
@SuppressWarnings("rawtypes")
Class enumClass = null;
System.out.println(enumClass);
}
}
```

```
$ java -jar ecj-4.28.jar -properties bad.prefs Test.java && java Test
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Enum is a raw type. References to generic type Enum should be parameterized

at Test.main(Test.java:4)
```

## Expected Behavior

The program compiles without warnings or errors and runs to completion while printing `null`.

## Workarounds

* This does not happen if the annotation appears on the method instead of the statement, so moving such annotation up to the method scope would work.
* Disabling `fatalOptionalError`

## Additional Context

```
$ java -version
openjdk version "20.0.2" 2023-07-18
OpenJDK Runtime Environment (build 20.0.2+9-Ubuntu-0ubuntu123.04)
OpenJDK 64-Bit Server VM (build 20.0.2+9-Ubuntu-0ubuntu123.04, mixed mode, sharing)
```

I have been playing around and digging into the code and I don't see what `fatalOptionalError` is actually supposed to be doing. From my understanding, problems set to "error" in the properties file are treated as optional errors (and in addition as fatal if `fatalOptionalError` is set) (`CompilerOptions#getSeverity`).

Excerpt from the documentation of `fatalOptionalError` in `JavaCore.java`:

> When disabled, optional errors are only considered as warnings, still carrying an error indication to make them more severe. Note that by default, optional errors are not fatal. Non-optional errors are always fatal.

I understand this as meaning that `fatalOptionalError=disabled` (the default) causes the compilation to succeed even if these optional errors occur and to print a non-fatal error.
In practice, I have not found situation where this happens, if such an error occurs, the compilation fails as well (take for instance the code above without the `SuppressWarnings` annotation).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.