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

Type refinement not respected in enhanced for loop #4004

Closed
mernst opened this issue Dec 13, 2020 · 2 comments
Closed

Type refinement not respected in enhanced for loop #4004

mernst opened this issue Dec 13, 2020 · 2 comments

Comments

@mernst
Copy link
Member

mernst commented Dec 13, 2020

import org.checkerframework.checker.nullness.qual.Nullable;

public class ForLoopTest {

    public final Class<?> @Nullable [] types = null;

    public boolean isAssignableFrom(Class<?> argType) {
        if (types == null) {
            return true;
        }

        // OK.
        types.toString();

        // Not OK!
        for (Class<? extends Object> c : types) {
            // body is irrelevant
        }
        return false;
    }
}

The output is:

ForLoopTest.java:13: error: [enhancedfor.type.incompatible] incompatible types in enhanced for loop.
        for (Class<? extends Object> c : types) {
                                         ^
  found   : @Initialized @NonNull Class<? extends @Initialized @Nullable Object>
  required: @UnknownInitialization @Nullable Class<? extends @Initialized @NonNull Object>
1 error
@wmdietl
Copy link
Member

wmdietl commented Dec 13, 2020

The problem is the mismatch in the type argument, not the refined type of types.
types uses the unbounded Class<?>, whereas c uses the constrained Class<? extends Object>.
The error goes away when you change c to Class<?> or change types to Class<? extends Object>.

@mernst
Copy link
Member Author

mernst commented Dec 14, 2020

You are right. Thanks for catching my goof.
(This is more motivation for #3623 which proposes to simplify error messages or highlight the relevant parts.)

@mernst mernst closed this as completed Dec 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants