lovubuntu / checker-framework

Automatically exported from code.google.com/p/checker-framework
0 stars 0 forks source link

@KeyFor unnecessarily required? / incompatible types in enhanced for loop #311

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Test.java:

    import java.util.Map;
    class Test {
        void test(Map<String, String> map) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
            }
        }
    }

java -jar checkers.jar -processor checkers.nullness.NullnessChecker Test.java

Results in:

    Test.java:4: error: [enhancedfor.type.incompatible] incompatible types in enhanced for loop.
            for (Map.Entry<String, String> entry : map.entrySet()) {
                                                               ^
      found   : Entry<@KeyFor("map") String, String>
      required: Entry<String, String>
    1 error

Taking a hint from #173, this strangely(?) works:

Test2.java:

    import java.util.Map;
    class Test2 {
        void test(Map<String, String> map) {
            for (Map.Entry<? extends String, ? extends String> entry : map.entrySet()) {
            }
        }
    }

Or maybe I'm missing something.  Thanks.

Original issue reported on code.google.com by trask.st...@gmail.com on 31 Mar 2014 at 5:56

GoogleCodeExporter commented 9 years ago
I get this error message in 1.7.5, I didn't get it previously in 1.7.2.

Original comment by trask.st...@gmail.com on 31 Mar 2014 at 6:28

GoogleCodeExporter commented 9 years ago
I'm not sure if this is related, but I also ran into this:

    Test.java:7: error: [type.argument.type.incompatible] incompatible types in type argument.
                for (Map.Entry<? extends String, /*@Nullable*/Object> entry : map.entrySet()) {
                                                 ^
      found   : @Initialized @Nullable Object
      required: @Initialized @NonNull Object
    1 error

Using Test.java:

    import java.util.Map;
    import java.util.HashMap;
    import checkers.nullness.quals.Nullable;
    class Test {
        void test() {
            Map<String, /*@Nullable*/Object> map = new HashMap<>();
            for (Map.Entry<? extends String, /*@Nullable*/Object> entry : map.entrySet()) {
            }
        }
    }

Changing "/*@Nullable*/Object" to "? extends /*@Nullable*/Object" makes the 
error go away.  I didn't get this in 1.7.2 either.

Original comment by trask.st...@gmail.com on 31 Mar 2014 at 6:56

GoogleCodeExporter commented 9 years ago

Original comment by michael.ernst@gmail.com on 15 Apr 2014 at 6:46

GoogleCodeExporter commented 9 years ago
Hi Trask,

sorry, I can't reproduce any of these issues.
I've added a test case:

checker-framework/checker/tests/nullness/generics/MapLoop.java

which I'll push later today, containing these examples.

Please re-open the issue if I'm missing something.

cu, WMD.

Original comment by wdi...@gmail.com on 10 May 2014 at 8:33

GoogleCodeExporter commented 9 years ago
I upgraded to latest and I'm not seeing this anymore. Thanks.

Original comment by trask.st...@gmail.com on 28 May 2014 at 5:21