From 4ea2a11d10485d7f68579816a57854221ce0043a Mon Sep 17 00:00:00 2001 From: Suzanne Millstein Date: Thu, 3 Oct 2024 09:37:11 -0700 Subject: [PATCH 1/4] copy annotations for anonymous. --- .../checkerframework/framework/type/AnnotatedTypeFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java b/framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java index d0b39de5baf..e631649d6d7 100644 --- a/framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java +++ b/framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java @@ -2826,7 +2826,7 @@ protected ParameterizedExecutableType constructorFromUse( p.addAll(1, superCon.getParameterTypes()); con.setParameterTypes(p); } - con.getReturnType().replaceAnnotations(superCon.getReturnType().getPrimaryAnnotations()); + con.getReturnType().replaceAnnotations(type.getPrimaryAnnotations()); } else { con = AnnotatedTypes.asMemberOf(types, this, type, ctor, con); } From b58824e407ab8db821a72690f3526470982beb4e Mon Sep 17 00:00:00 2001 From: Suzanne Millstein Date: Thu, 3 Oct 2024 09:45:45 -0700 Subject: [PATCH 2/4] lub. --- .../tests/mustcall/FunctionalInterfaces.java | 52 +++++++++++++++++++ .../framework/type/AnnotatedTypeFactory.java | 8 ++- 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 checker/tests/mustcall/FunctionalInterfaces.java diff --git a/checker/tests/mustcall/FunctionalInterfaces.java b/checker/tests/mustcall/FunctionalInterfaces.java new file mode 100644 index 00000000000..d60efbbec07 --- /dev/null +++ b/checker/tests/mustcall/FunctionalInterfaces.java @@ -0,0 +1,52 @@ +// Test that the correct type is assigned to instantiations of functional +// interfaces. +// https://github.com/typetools/checker-framework/issues/6823 + +import java.io.Closeable; +import org.checkerframework.checker.mustcall.qual.*; + +public abstract class FunctionalInterfaces { + + @FunctionalInterface + public interface Actor extends Closeable { + void act(); + + @Override + default void close() {} + } + + public static class ActorImpl implements Actor { + @Override + public void act() {} + } + + public abstract void run(@MustCall({}) Actor a); + + public static void method() {} + + public void normalConstruction() { + + // :: error: (assignment) + @MustCall({}) Actor a = new ActorImpl(); + } + + public void inlineClass() { + + class ActorImplInline implements Actor { + @Override + public void act() {} + } + + // :: error: (assignment) + @MustCall({}) Actor a = new ActorImplInline(); + } + + public void anonymousClass() { + + // :: error: (assignment) + @MustCall({}) Actor a = + new Actor() { + public void act() {} + }; + } +} diff --git a/framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java b/framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java index e631649d6d7..f61a85ddd26 100644 --- a/framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java +++ b/framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java @@ -2826,7 +2826,13 @@ protected ParameterizedExecutableType constructorFromUse( p.addAll(1, superCon.getParameterTypes()); con.setParameterTypes(p); } - con.getReturnType().replaceAnnotations(type.getPrimaryAnnotations()); + Set lub = + qualHierarchy.leastUpperBoundsShallow( + type.getPrimaryAnnotations(), + type.getUnderlyingType(), + superCon.getReturnType().getPrimaryAnnotations(), + superCon.getReturnType().getUnderlyingType()); + con.getReturnType().replaceAnnotations(lub); } else { con = AnnotatedTypes.asMemberOf(types, this, type, ctor, con); } From 33fda208696337bd2f0d8d00202b0393e33a2f8d Mon Sep 17 00:00:00 2001 From: Suzanne Millstein Date: Fri, 4 Oct 2024 09:32:07 -0700 Subject: [PATCH 3/4] Move expected error to correct line. --- checker/tests/mustcall/FunctionalInterfaces.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checker/tests/mustcall/FunctionalInterfaces.java b/checker/tests/mustcall/FunctionalInterfaces.java index d60efbbec07..b556623ffa5 100644 --- a/checker/tests/mustcall/FunctionalInterfaces.java +++ b/checker/tests/mustcall/FunctionalInterfaces.java @@ -43,8 +43,8 @@ public void act() {} public void anonymousClass() { - // :: error: (assignment) @MustCall({}) Actor a = + // :: error: (assignment) new Actor() { public void act() {} }; From 95240ef5c449387f4cd772013d67e045107b34b4 Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Fri, 4 Oct 2024 12:13:31 -0700 Subject: [PATCH 4/4] Adjust expected diagnostic --- framework/tests/h1h2checker/AnonymousClasses.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/tests/h1h2checker/AnonymousClasses.java b/framework/tests/h1h2checker/AnonymousClasses.java index 3caa087de6a..b373de987cb 100644 --- a/framework/tests/h1h2checker/AnonymousClasses.java +++ b/framework/tests/h1h2checker/AnonymousClasses.java @@ -4,7 +4,7 @@ public class AnonymousClasses { private <@H1S1 T extends @H1S1 Comparator> void testGenericAnonymous() { - // :: error: (type.argument) :: error: (constructor.invocation) + // :: error: (type.argument) :: warning: (cast.unsafe.constructor.invocation) new @H1S1 Gen() {}; // :: error: (type.argument) :: warning: (cast.unsafe.constructor.invocation) new @H1S1 GenInter() {};