Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We need a way to suppress all initialization errors #1590

Open
tmtron opened this issue Oct 16, 2017 · 9 comments
Open

We need a way to suppress all initialization errors #1590

tmtron opened this issue Oct 16, 2017 · 9 comments
Milestone

Comments

@tmtron
Copy link

tmtron commented Oct 16, 2017

We need a way to suppress all initialization errors (but not the nullness checks).

Example:

    public final class InitTest {
    	
    	private String field;
    	
    	public InitTest() {
    		init();
    	}
    
    	private void init() {
    		field = "";
    	}	
    }

The initialization checker of the checker-framework correctly reports the issues:

    InitTest.java:7: error: [initialization.fields.uninitialized] the constructor does not initialize fields: field
            public InitTest() {
                   ^
    InitTest.java:8: error: [method.invocation.invalid] call to init() not allowed on the given receiver.
                    init();
                        ^
      found   : @UnderInitialization(java.lang.Object.class) @NonNull InitTest
      required: @Initialized @NonNull InitTest
    2 errors

According to the docs of the Initialization Checker, it should be able to disable the initialization checker via a command line argument:

To disable initialization checking, supply the command-line argument
-AsuppressWarnings=uninitialized

When we use this argument (in checker-framework 2.2.1) we still get an initialization error:

    InitTest.java:8: error: [method.invocation.invalid] call to init() not allowed on the given receiver.
                    init();
                        ^
      found   : @UnderInitialization(java.lang.Object.class) @NonNull InitTest
      required: @Initialized @NonNull InitTest
    1 error

Quote from mernst in this Stackoverflow discussion "How to suppress all initialization errors" :

-AsuppressWarnings=uninitialized suppresses all warnings related to initialization itself. It lets you write a constructor that does not initialize all its fields, for example.
The -AsuppressWarnings=uninitialized affects messages that are printed as initialization errors but not errors that are printed as other types.

@flix-
Copy link

flix- commented Dec 22, 2020

Latest docs propose to use -AsuppressWarnings=fbc (https://checkerframework.org/manual/#initialization-checking-suppressing-warnings). Initialization warnings are gone however null checks also doesn't seem to be reported anymore:

public class Main {

  public static void main(String[] args) {
    foo(null);
  }

  public static void foo(String a) {}
}

Without suppressing any warnings I'm getting the following error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project parent: Compilation failure
[ERROR] Main.java:[4,8] error: [argument.type.incompatible] incompatible argument for parameter a of foo.
[ERROR]   found   : null

With -AsuppressWarnings=fbc compilation just runs fine without any warnings. The other mentioned option -AsuppressWarnings=initialization. doesn't have any impact. I was using version 3.8.0.

@mernst
Copy link
Member

mernst commented Dec 22, 2020

The manual's mention of the "fbc" SuppressWarnings string is wrong. I'm sorry about that. Thank you for pointing it out; we appreciate it.

There are two relevant SuppressWarnings strings:

  • "initialization" suppresses all nullness and initialization warnings. ("fbc" is exactly the same, for backward compatibility, but the manual should not mention it at all. Don't mention "fbc" SuppressWarnings string in manual #4060 fixes this.)
  • "initialization." suppresses only initialization warnings.

You did two experiments.

  • When you used "fbc", that suppressed all nullness and initialization warnings.
  • When you used "initialization.", that suppressed initialization warnings. Since the only error in the code you showed is a nullness warning, that warning remains.

The behavior you are seeing seems right to me, but let us know if there is another problem.

@flix-
Copy link

flix- commented Dec 22, 2020

Thank you for the clarification. However, the problem still persists. "initialization." does not suppress initialization warnings:

public class Foo {

  private String a;

  public Foo() {
    init();
  }

  public void init() {
    a = "gude";
  }
}
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project parent: Compilation failure: Compilation failure:
[ERROR] Foo.java:[5,9] error: [initialization.fields.uninitialized] the constructor does not initialize fields: a
[ERROR] Foo.java:[6,8] error: [method.invocation.invalid] call to init() not allowed on the given receiver.
[ERROR]   found   : @UnderInitialization @NonNull Foo
[ERROR]
[ERROR]   required: @Initialized @NonNull Foo

I am using the following configuration for the maven plugin:

<profile>
      <id>checkerframework</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <executions>
              <execution>
                <id>default-compile</id>
                <phase>compile</phase>
                <goals>
                  <goal>compile</goal>
                </goals>
                <configuration>
                  <fork>true</fork>
                  <compilerArgs>
                    <arg>-proc:only</arg>
                    <arg>-Xmaxerrs</arg>
                    <arg>10000</arg>
                    <arg>-Xmaxwarns</arg>
                    <arg>10000</arg>
                    <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
                    <arg>-AsuppressWarnings=initialization.</arg>
                  </compilerArgs>
                  <annotationProcessorPaths>
                    <path>
                      <groupId>org.checkerframework</groupId>
                      <artifactId>checker</artifactId>
                      <version>${checkerframework.version}</version>
                    </path>
                  </annotationProcessorPaths>
                  <annotationProcessors>
                    <annotationProcessor>org.checkerframework.checker.nullness.NullnessChecker</annotationProcessor>
                  </annotationProcessors>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      <properties>
        <!-- Temporary until animal-sniffer-maven-plugin is released with https://github.com/mojohaus/animal-sniffer/pull/91.
             Version 1.19 is broken:  https://github.com/mojohaus/animal-sniffer/issues/76. -->
        <animal.sniffer.skip>true</animal.sniffer.skip>
      </properties>
    </profile>

@mernst
Copy link
Member

mernst commented Dec 22, 2020

You are right. The behavior changed in version 3.2.0 early this year, but the documentation was not updated correctly at that time. The wrong manual is confusing to both users and developers!

To revise my previous statement, there are two relevant SuppressWarnings strings:

  • "nullness" suppresses all nullness and initialization warnings. ("fbc" is exactly the same, for backward compatibility only.)
  • "initialization" suppresses some initialization warnings -- those issued just by the Initialization Checker, but not those (like method.invocation.invalid) that are issued by the framework itself. This is unfortunate, but is difficult to fix, and is what this issue is about.

Thanks again for reporting this and bearing with us while we fix this documentation.

@flix-
Copy link

flix- commented Dec 22, 2020

I am still puzzled. I would like to only have null checking and no initialization warnings/errors. I tried "fbc", "initialization", and "initialization." options. None of them yield the desired behavior. Am I correctly assuming that currently there is no way to suppress initialization errors but have full null validation support?

@flix-
Copy link

flix- commented Feb 25, 2021

@mernst Any update on this?

@mernst
Copy link
Member

mernst commented Feb 25, 2021

@flix- Can you please take a look at the "Suppressing Warnings" section in the Initialization Checker manual and let me know if it is not clear? I think it directly answers your question, but if it is unclear or there is a bug, let us know and we will fix it. Thanks.

@starkos
Copy link

starkos commented Nov 23, 2022

Encountering this issue here. Adding Checker to a large existing project which does a lot of explicit dependency injection, passing class references around in constructors to be stored for later use. So we're seeing lots of errors like:

  found   : @UnderInitialization(com.its.idmp.aws.AwsInstallStep.class) @NonNull AwsInstallStep
  required: @Initialized @NonNull AwsInstallStep

In this case we know the code is fine. We want to turn off all of the @UnderInitialization warnings, and only those warnings. I've tried the examples mentioned above and none of them do it. Is there something in the manual I'm missing?

@mernst
Copy link
Member

mernst commented Nov 23, 2022

I'm sorry you are having trouble. Could you please provide a concrete example of code and exactly what you did that didn't work? Otherwise it is hard to make a suggestion.

Did you try the suggestion in the "Suppressing Warnings" section of the "Initialization Checker" section of the manual? It says:

You can write @SuppressWarnings("initialization") on a field, constructor, or class, or pass the command-line argument -AsuppressWarnings=initialization when running the Nullness Checker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants