src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
changeset 50181 f854b76b6a0c
parent 50086 66b0f0134aad
child 50200 9ce050c4711b
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Fri May 18 14:51:06 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Fri May 18 09:22:31 2018 -0700
@@ -2656,12 +2656,29 @@
                  * the target will be updated to SAM
                  */
                 currentTarget = targetChecker.visit(currentTarget, that);
-                if (explicitParamTypes != null) {
-                    currentTarget = infer.instantiateFunctionalInterface(that,
-                            currentTarget, explicitParamTypes, resultInfo.checkContext);
+                if (!currentTarget.isIntersection()) {
+                    if (explicitParamTypes != null) {
+                        currentTarget = infer.instantiateFunctionalInterface(that,
+                                currentTarget, explicitParamTypes, resultInfo.checkContext);
+                    }
+                    currentTarget = types.removeWildcards(currentTarget);
+                    lambdaType = types.findDescriptorType(currentTarget);
+                } else {
+                    IntersectionClassType ict = (IntersectionClassType)currentTarget;
+                    ListBuffer<Type> components = new ListBuffer<>();
+                    for (Type bound : ict.getExplicitComponents()) {
+                        if (explicitParamTypes != null) {
+                            bound = infer.instantiateFunctionalInterface(that,
+                                    bound, explicitParamTypes, resultInfo.checkContext);
+                        }
+                        bound = types.removeWildcards(bound);
+                        components.add(bound);
+                    }
+                    currentTarget = types.makeIntersectionType(components.toList());
+                    currentTarget.tsym.flags_field |= INTERFACE;
+                    lambdaType = types.findDescriptorType(currentTarget);
                 }
-                currentTarget = types.removeWildcards(currentTarget);
-                lambdaType = types.findDescriptorType(currentTarget);
+
             } else {
                 currentTarget = Type.recoveryType;
                 lambdaType = fallbackDescriptorType(that);
@@ -2701,27 +2718,11 @@
             }
 
             public Type visitIntersectionClassType(IntersectionClassType ict, DiagnosticPosition pos) {
-                Symbol desc = types.findDescriptorSymbol(makeNotionalInterface(ict));
-                Type target = null;
-                for (Type bound : ict.getExplicitComponents()) {
-                    TypeSymbol boundSym = bound.tsym;
-                    if (bound.tsym == syms.objectType.tsym) {
-                        continue;
-                    }
-                    if (types.isFunctionalInterface(boundSym) &&
-                            types.findDescriptorSymbol(boundSym) == desc) {
-                        target = bound;
-                    } else if (!boundSym.isInterface() || (boundSym.flags() & ANNOTATION) != 0) {
-                        //bound must be an interface
-                        reportIntersectionError(pos, "not.an.intf.component", boundSym);
-                    }
-                }
-                return target != null ?
-                        target :
-                        ict.getExplicitComponents().head; //error recovery
+                types.findDescriptorSymbol(makeNotionalInterface(ict, pos));
+                return ict;
             }
 
-            private TypeSymbol makeNotionalInterface(IntersectionClassType ict) {
+            private TypeSymbol makeNotionalInterface(IntersectionClassType ict, DiagnosticPosition pos) {
                 ListBuffer<Type> targs = new ListBuffer<>();
                 ListBuffer<Type> supertypes = new ListBuffer<>();
                 for (Type i : ict.interfaces_field) {
@@ -2735,11 +2736,6 @@
                 notionalIntf.tsym.flags_field |= INTERFACE;
                 return notionalIntf.tsym;
             }
-
-            private void reportIntersectionError(DiagnosticPosition pos, String key, Object... args) {
-                resultInfo.checkContext.report(pos,
-                                               diags.fragment(Fragments.BadIntersectionTargetForFunctionalExpr(diags.fragment(key, args))));
-            }
         };
 
         private Type fallbackDescriptorType(JCExpression tree) {
@@ -3283,20 +3279,9 @@
                     inferenceContext -> setFunctionalInfo(env, fExpr, pt, inferenceContext.asInstType(descriptorType),
                     inferenceContext.asInstType(primaryTarget), checkContext));
         } else {
-            ListBuffer<Type> targets = new ListBuffer<>();
             if (pt.hasTag(CLASS)) {
-                if (pt.isCompound()) {
-                    targets.append(types.removeWildcards(primaryTarget)); //this goes first
-                    for (Type t : ((IntersectionClassType)pt()).interfaces_field) {
-                        if (t != primaryTarget) {
-                            targets.append(types.removeWildcards(t));
-                        }
-                    }
-                } else {
-                    targets.append(types.removeWildcards(primaryTarget));
-                }
+                fExpr.target = primaryTarget;
             }
-            fExpr.targets = targets.toList();
             if (checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.CHECK &&
                     pt != Type.recoveryType) {
                 //check that functional interface class is well-formed
@@ -3306,7 +3291,7 @@
                      * above.
                      */
                     ClassSymbol csym = types.makeFunctionalInterfaceClass(env,
-                            names.empty, List.of(fExpr.targets.head), ABSTRACT);
+                            names.empty, fExpr.target, ABSTRACT);
                     if (csym != null) {
                         chk.checkImplementations(env.tree, csym, csym);
                         try {
@@ -3317,7 +3302,7 @@
                             types.findDescriptorType(csym.type);
                         } catch (FunctionDescriptorLookupError err) {
                             resultInfo.checkContext.report(fExpr,
-                                    diags.fragment(Fragments.NoSuitableFunctionalIntfInst(fExpr.targets.head)));
+                                    diags.fragment(Fragments.NoSuitableFunctionalIntfInst(fExpr.target)));
                         }
                     }
                 } catch (Types.FunctionDescriptorLookupError ex) {
@@ -5185,8 +5170,8 @@
         @Override
         public void visitLambda(JCLambda that) {
             super.visitLambda(that);
-            if (that.targets == null) {
-                that.targets = List.nil();
+            if (that.target == null) {
+                that.target = syms.unknownType;
             }
         }
 
@@ -5197,8 +5182,8 @@
                 that.sym = new MethodSymbol(0, names.empty, dummyMethodType(),
                         syms.noSymbol);
             }
-            if (that.targets == null) {
-                that.targets = List.nil();
+            if (that.target == null) {
+                that.target = syms.unknownType;
             }
         }
     }