UnitsChecker: fix incompatible types in assignment

5 views
Skip to first unread message

Jonne Zutt

unread,
Aug 26, 2022, 10:14:30 AM8/26/22
to Checker Framework discussion
Hi all,

I'm trying out the nice checker-framework on my project, thanks for all the work!

It seems to work fine, but I am running into one problem where I'm wondering how I should use this. I have tried to provide a simple example below that illustrates what I'm running into.

The compiler gives me the following:

OptionalTest.java:24: error: [assignment] incompatible types in assignment.
        Optional<@m Integer> myOptionalMeters = resolveInt().map(this::calculate);
                                                                ^
  found   : @UnknownUnits Optional<@UnknownUnits Integer>
  required: @UnknownUnits Optional<@m Integer>

Do you have any advice on how to go about this? Is this a case where I should be suppressing warnings? Or is there a better way to deal with this?

It feels like annotations get lost in the generics used in the map() function. Is there anyone here that can shed more light on that?

Thanks a lot!


import static org.assertj.core.api.Assertions.*;
import java.util.*;
import org.checkerframework.checker.units.qual.*;
import org.junit.Test;

public class OptionalTest
{
    @SuppressWarnings("units")
    public static final @m int m = 1;

    Optional<Integer> resolveInt()
    {
        return Optional.of(5);
    }

    @m int calculate(int aValue)
    {
        return aValue * m;
    }

    @Test
    public void reproduce()
    {
        Optional<@m Integer> myOptionalMeters = resolveInt().map(this::calculate);
        assertThat(myOptionalMeters).isPresent();
    }
}

Michael Ernst

unread,
Aug 26, 2022, 10:40:14 AM8/26/22
to Jonne Zutt, Checker Framework discussion
Thanks for your message, and I'm sorry you are having trouble.

For now, please suppress the warning.

This is most likely due to https://github.com/typetools/checker-framework/issues/979.  We are actively working on fixing it.  Unfortunately, the person responsible has been on medical leave much of the past year.  We plan to pick it up again sometime in September.

-Mike

--

---
You received this message because you are subscribed to the Google Groups "Checker Framework discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to checker-framework-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/checker-framework-discuss/d9f7dfa9-0ec8-4fd2-9144-99a4c07f076fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages