typetools / typetools/checker-framework

Lock Checker incompatible with Stream API + method reference

Open
#5,012 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

When running a streaming operator with method references while the Lock Checker is enabled (javac -processor org.checkerframework.checker.lock.LockChecker Foo.java), a type error gets generated:

import java.util.stream.Stream;
import org.checkerframework.checker.lock.qual.GuardSatisfied;

public class Foo {
    public static @GuardSatisfied(1) Object bar( @GuardSatisfied(1) Object o ) {
        return o;
    }
    public static Stream<Object> baz( Stream<Object> a ) {
        return a.map( Foo::bar );
    }
}

This code gives the error:

error: [methodref.return] Incompatible return type
  found   : @GuardSatisfied(1) Object

  required: @GuardedBy Object

  Consequence: method in @GuardedBy IntegerParameter

    @GuardSatisfied(1) Object bar(@GuardSatisfied(1) Object p0)

  is not a valid method reference for method in @GuardedBy Function<@GuardedBy Object, @GuardedBy Object>

    @GuardedBy Object apply(@GuardedBy Function<@GuardedBy Object, @GuardedBy Object> this, @GuardedBy Object p0)
error: [methodref.param] Incompatible parameter type for o
  found   : @GuardSatisfied(1) Object

  required: @GuardedBy Object

  Consequence: method in @GuardedBy IntegerParameter

    @GuardSatisfied(1) Object bar(@GuardSatisfied(1) Object p0)

  is not a valid method reference for method in @GuardedBy Function<@GuardedBy Object, @GuardedBy Object>

    @GuardedBy Object apply(@GuardedBy Function<@GuardedBy Object, @GuardedBy Object> this, @GuardedBy Object p0)

Using a lambda with the same method does not have the error and compiles fine:

import java.util.stream.Stream;
import org.checkerframework.checker.lock.qual.GuardSatisfied;

public class Foo {
    public static @GuardSatisfied(1) Object bar( @GuardSatisfied(1) Object o ) {
        return o;
    }
    public static Stream<Object> baz( Stream<Object> a ) {
        return a.map( o -> Foo.bar( o ) );
    }
}

It seems that the type system does not recognize @GuardSatisfied as compatible with @GuardedBy, which makes methods annotated for use with the Lock Checker not usable by method reference. This was observed on the latest version (3.21.1).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the provided Foo.java reproducer with javac and the Lock Checker processor, then compare the method-reference and lambda cases. Trace the Lock Checker handling of method references and @GuardSatisfied versus @GuardedBy; done when the method-reference version compiles without the reported errors, with regression coverage for the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.