checker-framework icon indicating copy to clipboard operation
checker-framework copied to clipboard

Type argument inference failure with throws

Open smillst opened this issue 1 year ago • 4 comments

          FWIW @smillst I have encountered a similar error that I can file a new bug if you like and or maybe there is a way to annotate the "sneak throw" technique.
     /**
     * An error friendly {@link Function} for converting properties.
     *
     * @param <T> input type.
     * @param <R> output type.
     * @param <E> error type.
     */
    public interface PropertyFunction<T extends @Nullable Object, R extends @Nullable Object, E extends Exception>
            extends Function<T, R> {

        @Override
        default R apply(T t) {
            try {
                return _apply(t);
            }
            catch (Exception e) {
                // the error happens below here.
                sneakyThrow(e);
                throw new RuntimeException(e);
            }
        }

        /**
         * Apply that throws error.
         * @param t input
         * @return output
         * @throws E if an error happened in function.
         */
        public R _apply(T t) throws E;

        @SuppressWarnings("unchecked")
        private static <E extends Throwable> void sneakyThrow(final Throwable x) throws E {
            throw (E) x;
        }

    }
[type.arguments.not.inferred] Could not infer type arguments for PropertyFunction.sneakyThrow
  unsatisfiable constraint: @UnknownInitialization @Nullable RuntimeException</*Type args not initialized*/> <: @Initialized @NonNull Throwable

I'm not really sure how to properly annotate or ~~suppress the warning~~ (edit I had the suppress in the wrong place so that is why I could not suppress).

EDIT when I suppress the warning I get:

error: StructuralEqualityComparer: unexpected combination:  type: [DECLARED class org.checkerframework.framework.type.AnnotatedTypeMirror$AnnotatedDeclaredType] Object  supertype: [TYPEVAR class org.checkerframework.framework.type.AnnotatedTypeMirror$AnnotatedTypeVariable] R extends Object

Originally posted by @agentgt in https://github.com/typetools/checker-framework/issues/6629#issuecomment-2160984356

smillst avatar Jun 20 '24 17:06 smillst

@agentgt The type.arguments.not.inferred error is a bug. The error: StructuralEqualityComparer: unexpected combination:.. is also a bug, but I can't reproduce it.

smillst avatar Jun 20 '24 17:06 smillst

@smillst happy to try to setup a small reproducible project if that helps?

agentgt avatar Jun 20 '24 18:06 agentgt

@smillst happy to try to setup a small reproducible project if that helps?

For the error: StructuralEqualityComparer: unexpected combination:.. error? That would be helpful. If you do please submit it in a new issue. Thanks!

smillst avatar Jun 21 '24 15:06 smillst

@smillst Here is the reproducible project:

https://github.com/agentgt/checker-issues

It seems there is an issue with things enclosed in a sealed interface (even if they are not part of the hierarchy) where I have to add the witness so that checker can infer.

When I add the witness the issue goes away but not in my other project:

https://github.com/jstachio/rainbowgum/blob/main/core/src/main/java/io/jstach/rainbowgum/LogProperty.java

I will try to edit the code to see if I can get it to do the StructuralEqualityComparer error.

agentgt avatar Aug 06 '24 20:08 agentgt