Skip to content

Better support for Map.getOrDefault #723

Closed

Description

@XN137

by inferring nullability from the 2nd parameter

example test:

makeTestHelperWithArgs(
        Arrays.asList(
            "-d",
            temporaryFolder.getRoot().getAbsolutePath(),
            "-XepOpt:NullAway:AnnotatedPackages=com.uber",
            "-XepOpt:NullAway:AcknowledgeRestrictiveAnnotations=true"))
        .addSourceLines(
            "Test.java",
            "package com.uber;",
            "import java.util.Map;",
            "import com.google.common.collect.ImmutableMap;",
            "import org.jspecify.annotations.Nullable;",
            "class Test {",
            "   void testGetOrDefaultMap(Map<String, String> m, String string, @Nullable String nullableString) {",
            "     m.getOrDefault(\"key\", \"value\").toString();",
            "     m.getOrDefault(\"key\", string).toString();",
            "     m.getOrDefault(\"key\", null).toString();",
            "     m.getOrDefault(\"key\", nullableString).toString();",
            "   }",
            "",
            "   void testGetOrDefaultImmutableMap(ImmutableMap<String, String> im, String string, @Nullable String nullableString) {",
            "     // BUG: Diagnostic contains:",
            "     im.getOrDefault(\"key\", \"value\").toString();",
            "     // BUG: Diagnostic contains:",
            "     im.getOrDefault(\"key\", string).toString();",
            "     // BUG: Diagnostic contains:",
            "     im.getOrDefault(\"key\", null).toString();",
            "     // BUG: Diagnostic contains:",
            "     im.getOrDefault(\"key\", nullableString).toString();",
            "   }",
            "}")
        .doTest();

Map.getOrDefault seems to always get treated as returning non-null type
ImmutableMap.getOrDefault is treated as always returning a nullable type (due to its annotations and AcknowledgeRestrictiveAnnotations)

imo both should be considered non-null in the first 2 cases and nullable in the last 2 cases, WDYT?

hit this while trying to improve NullAway code in https://github.com/uber/NullAway/actions/runs/4016591896/jobs/6899826587 😅

Activity

  1. msridhar commented on Jan 27, 2023

    @msridhar
    Contributor

    I think we want to add Map.getOrDefault to this Map (with parameter index 1):

    https://github.com/uber/NullAway/blob/0a78a82df792014cb10320c4de0253f4e1ec7606/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java#L613

    Not 100% sure, but I think that modeling would override the annotations on ImmutableMap.getOrDefault. Does that sound right @lazaroclapp?

  2. lazaroclapp commented on Jan 27, 2023

    @lazaroclapp
    Contributor

    I think we want to add Map.getOrDefault to this Map (with parameter index 1):

    https://github.com/uber/NullAway/blob/0a78a82df792014cb10320c4de0253f4e1ec7606/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java#L613

    Not 100% sure, but I think that modeling would override the annotations on ImmutableMap.getOrDefault. Does that sound right @lazaroclapp?

    Worth testing, specially for the ImmutableMap case, but the idea makes sense to me and we would expect library models to override third party annotations (Not sure if anything gets weird with inheritance. Worst case we could add explicit models for both, but I am hoping a model for Map is enough).

  3. msridhar commented on Jan 27, 2023

    @msridhar
    Contributor

    I tried in #724 and it works for Map but not ImmutableMap. Need to understand better whether an inherited library model should win over given annotations with restrictive annotations enabled 😅

  4. XN137 commented on Jan 27, 2023

    @XN137
    ContributorAuthor

    https://github.com/google/guava/blob/76cd7fa14a567bc74e4a98052de55f4486a8c933/guava/src/com/google/common/collect/ImmutableMap.java#L975

    comments in the code discuss these annotations, if i understand this part correctly:

         * 1. We'll want to use something like @PolyNull once we can make that work for the various
         * platforms we target.
    

    then maybe it suggests that tools are allowed to be smarter than the current annotations.
    so i guess it should be implemented in a way which overrides the annotations for ImmutableMap.getOrDefault explicitly, even when AcknowledgeRestrictiveAnnotations is set.
    Also since we're trying to follow the contract of the base interface.

  5. msridhar commented on Jan 30, 2023

    @msridhar
    Contributor

    In #724 I added an explicit model for ImmutableMap.get() and it seemed to work. I think I'm ok with that approach for now. We can move discussion over there.

  6. added a commit that references this issue on Feb 1, 2023
    8e948db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions