Skip to content

Commit 56468c4

Browse files
author
Vicente Romero
committed
8322810: Lambda expression types can't be classes
Reviewed-by: mcimadamore
1 parent 60ba81d commit 56468c4

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3343,6 +3343,10 @@ TargetInfo getTargetInfo(JCPolyExpression that, ResultInfo resultInfo, List<Type
33433343
// do nothing
33443344
}
33453345
}
3346+
if (bound.tsym != syms.objectType.tsym && (!bound.isInterface() || (bound.tsym.flags() & ANNOTATION) != 0)) {
3347+
// bound must be j.l.Object or an interface, but not an annotation
3348+
reportIntersectionError(that, "not.an.intf.component", bound);
3349+
}
33463350
bound = types.removeWildcards(bound);
33473351
components.add(bound);
33483352
}
@@ -3366,6 +3370,11 @@ TargetInfo getTargetInfo(JCPolyExpression that, ResultInfo resultInfo, List<Type
33663370
return new TargetInfo(currentTarget, lambdaType);
33673371
}
33683372

3373+
private void reportIntersectionError(DiagnosticPosition pos, String key, Object... args) {
3374+
resultInfo.checkContext.report(pos,
3375+
diags.fragment(Fragments.BadIntersectionTargetForFunctionalExpr(diags.fragment(key, args))));
3376+
}
3377+
33693378
void preFlow(JCLambda tree) {
33703379
attrRecover.doRecovery();
33713380
new PostAttrAnalyzer() {

src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ compiler.misc.bad.intersection.target.for.functional.expr=\
337337

338338
# 0: symbol or type
339339
compiler.misc.not.an.intf.component=\
340-
component type {0} is not an interface
340+
component type {0} is not an interface or java.lang.Object
341341

342342
# 0: kind name, 1: message segment
343343
compiler.err.invalid.mref=\

test/langtools/tools/javac/diags/examples.not-yet.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ compiler.note.multiple.elements # needs user code
145145
compiler.err.preview.feature.disabled.classfile # preview feature support: needs compilation against classfile
146146
compiler.warn.preview.feature.use.classfile # preview feature support: needs compilation against classfile
147147
compiler.note.preview.plural.additional # preview feature support: diag test causes intermittent failures (see JDK-8201498)
148-
compiler.misc.bad.intersection.target.for.functional.expr # currently not generated, should be removed?
149-
compiler.misc.not.an.intf.component
150148
compiler.warn.declared.using.preview # after making sealed classes a final feature there is no other
151149
# preview feature but we should keep this key for future use just
152150
# in case

test/langtools/tools/javac/diags/examples/NotAnInterfaceComponent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,9 +21,9 @@
2121
* questions.
2222
*/
2323

24-
// key: compiler.misc.not.a.functional.intf.1
24+
// key: compiler.misc.bad.intersection.target.for.functional.expr
25+
// key: compiler.misc.not.an.intf.component
2526
// key: compiler.err.prob.found.req
26-
// key: compiler.misc.incompatible.abstracts
2727

2828
class NotAnInterfaceComponent {
2929
Object o = (String & Runnable) ()-> { };
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @test /nodynamiccopyright/
3+
* @bug 8322810
4+
* @summary Lambda expressions can implement classes
5+
* @compile/fail/ref=ClassInIntersectionTypeTest.out -XDrawDiagnostics ClassInIntersectionTypeTest.java
6+
*/
7+
8+
import java.io.Serializable;
9+
import java.lang.annotation.Annotation;
10+
11+
public class ClassInIntersectionTypeTest {
12+
// test 1
13+
void m1() {
14+
ClassInIntersectionTypeTest r1 = (ClassInIntersectionTypeTest & Runnable) () -> System.out.println("Hello, World!");
15+
ClassInIntersectionTypeTest r2 = (ClassInIntersectionTypeTest & Runnable) ClassInIntersectionTypeTest::run1;
16+
}
17+
18+
static void run1() {}
19+
20+
// test 2
21+
static void foo() {
22+
run2(() -> System.out.println("Hello, World!"));
23+
run2(ClassInIntersectionTypeTest::run1);
24+
}
25+
26+
static <T extends ClassInIntersectionTypeTest & Runnable> void run2(T t) {
27+
t.run();
28+
}
29+
30+
static Class<? extends Annotation> myAnnoType() { return null; }
31+
@interface Anno {}
32+
@interface Anno2 {}
33+
34+
Anno anno1 = (Anno & Serializable) ()-> null; // annotations not allowed
35+
Anno anno2 = (Serializable & Anno & Anno2) ()-> null; // annotations not allowed
36+
Anno anno3 = (Anno & Serializable) ClassInIntersectionTypeTest::myAnnoType; // annotations not allowed
37+
Anno anno4 = (Serializable & Anno2 & Anno) ClassInIntersectionTypeTest::myAnnoType; // annotations not allowed
38+
39+
static void bar() {
40+
annotationType(() -> null);
41+
annotationType(ClassInIntersectionTypeTest::myAnnoType);
42+
}
43+
44+
static <T extends Anno & Serializable> void annotationType(T t) {
45+
t.annotationType();
46+
}
47+
48+
Anno anno5 = ()-> null; // annotations are not functional interfaces
49+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ClassInIntersectionTypeTest.java:14:83: compiler.err.prob.found.req: (compiler.misc.bad.intersection.target.for.functional.expr: (compiler.misc.not.an.intf.component: ClassInIntersectionTypeTest))
2+
ClassInIntersectionTypeTest.java:15:83: compiler.err.prob.found.req: (compiler.misc.bad.intersection.target.for.functional.expr: (compiler.misc.not.an.intf.component: ClassInIntersectionTypeTest))
3+
ClassInIntersectionTypeTest.java:22:13: compiler.err.prob.found.req: (compiler.misc.infer.no.conforming.assignment.exists: T, (compiler.misc.bad.intersection.target.for.functional.expr: (compiler.misc.not.an.intf.component: ClassInIntersectionTypeTest)))
4+
ClassInIntersectionTypeTest.java:23:13: compiler.err.prob.found.req: (compiler.misc.infer.no.conforming.assignment.exists: T, (compiler.misc.bad.intersection.target.for.functional.expr: (compiler.misc.not.an.intf.component: ClassInIntersectionTypeTest)))
5+
ClassInIntersectionTypeTest.java:34:40: compiler.err.prob.found.req: (compiler.misc.bad.intersection.target.for.functional.expr: (compiler.misc.not.an.intf.component: ClassInIntersectionTypeTest.Anno))
6+
ClassInIntersectionTypeTest.java:35:48: compiler.err.prob.found.req: (compiler.misc.bad.intersection.target.for.functional.expr: (compiler.misc.not.an.intf.component: ClassInIntersectionTypeTest.Anno))
7+
ClassInIntersectionTypeTest.java:36:40: compiler.err.prob.found.req: (compiler.misc.bad.intersection.target.for.functional.expr: (compiler.misc.not.an.intf.component: ClassInIntersectionTypeTest.Anno))
8+
ClassInIntersectionTypeTest.java:37:48: compiler.err.prob.found.req: (compiler.misc.bad.intersection.target.for.functional.expr: (compiler.misc.not.an.intf.component: ClassInIntersectionTypeTest.Anno2))
9+
ClassInIntersectionTypeTest.java:40:23: compiler.err.prob.found.req: (compiler.misc.infer.no.conforming.assignment.exists: T, (compiler.misc.bad.intersection.target.for.functional.expr: (compiler.misc.not.an.intf.component: ClassInIntersectionTypeTest.Anno)))
10+
ClassInIntersectionTypeTest.java:41:23: compiler.err.prob.found.req: (compiler.misc.infer.no.conforming.assignment.exists: T, (compiler.misc.bad.intersection.target.for.functional.expr: (compiler.misc.not.an.intf.component: ClassInIntersectionTypeTest.Anno)))
11+
ClassInIntersectionTypeTest.java:48:18: compiler.err.prob.found.req: (compiler.misc.not.a.functional.intf: ClassInIntersectionTypeTest.Anno)
12+
11 errors

0 commit comments

Comments
 (0)