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

Infer type of var variable from right-hand-side of assignment #4593

Closed
xenoterracide opened this issue Apr 28, 2021 · 4 comments
Closed

Infer type of var variable from right-hand-side of assignment #4593

xenoterracide opened this issue Apr 28, 2021 · 4 comments
Assignees

Comments

@xenoterracide
Copy link

xenoterracide commented Apr 28, 2021

  Map<String, @Nullable Object> getContext( Map<String, String> context ) {
    var map = new HashMap<String, @Nullable Object>();
    map.putAll( context );
    map.putAll( mapper.convertValue( cliConfiguration, Map.class ) );
    map.put( "configDir", configDir.getParent() );
    return Collections.unmodifiableMap( map );
  }

fails to compile with 2 errors

home/xeno/IdeaProjects/brix/config-loader/spi/src/main/java/com/xenoterracide/brix/configloader/spi/ConfigValueProcessor.java:75: error: [assignment.type.incompatible] incompatible types in assignment.
    var map = new HashMap<String, @Nullable Object>();
              ^
  found   : @Initialized @NonNull HashMap<@Initialized @NonNull String, @Initialized @Nullable Object>
  required: @UnknownInitialization @Nullable HashMap<@Initialized @NonNull String, @Initialized @NonNull Object>
/home/xeno/IdeaProjects/brix/config-loader/spi/src/main/java/com/xenoterracide/brix/configloader/spi/ConfigValueProcessor.java:78: error: [argument.type.incompatible] incompatible argument for parameter value of put.
    map.put( "configDir", configDir.getParent() );
                                             ^
  found   : @Initialized @Nullable Path
  required: @Initialized @NonNull Object

but this code has no errors

  Map<String, @Nullable Object> getContext( Map<String, String> context ) {
    Map<String, @Nullable Object> map = new HashMap<>();
    map.putAll( context );
    map.putAll( mapper.convertValue( cliConfiguration, Map.class ) );
    map.put( "configDir", configDir.getParent() );
    return Collections.unmodifiableMap( map );
  }

the only difference is that I'm not using var (I believe var uses left hand side inference as opposed to this being right hand side, but I may have them inverted)

related to #4587 (comment) but that's a request for better documentation, this is for an actual compiler improvement.

mernst added a commit that referenced this issue May 4, 2021
@mernst
Copy link
Member

mernst commented May 4, 2021

I added a simplified test case as Issue4593.java.

@mernst mernst changed the title left hand side inference issue Infer type of var variable from right-hand-side of assignment May 4, 2021
wmdietl pushed a commit to eisop/checker-framework that referenced this issue Jul 22, 2021
@spacether
Copy link

The var feature JEP286 was released in Java 10, which was released in 2018.
So this feature has been around for 6 years.
I am still seeing this issue in the checker framework v3.34.0
Any chance that fixing this can be prioritized? Thank you for all of your hard work!

@msridhar
Copy link
Contributor

msridhar commented Jan 5, 2024

@spacether I believe this was fixed by #5770, which was included in v3.35.0. There have been further fixes since then, so I suggest you give things a try with the latest Checker Framework release.

I'm going to go ahead and close this issue (sorry we missed closing it before), but if you notice a problem with var support please file a new issue.

@msridhar msridhar closed this as completed Jan 5, 2024
@spacether
Copy link

Upgrading fixed it, thanks!

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

5 participants