8016702: use of ternary operator in lambda expression gives incorrect results

Fri, 05 Jul 2013 11:04:22 +0100

author
mcimadamore
date
Fri, 05 Jul 2013 11:04:22 +0100
changeset 1890
bfbedbfc522a
parent 1889
b0386f0dc28e
child 1891
42b3c5e92461

8016702: use of ternary operator in lambda expression gives incorrect results
Summary: Constant types erroneously creep in during inference
Reviewed-by: jjg, vromero

src/share/classes/com/sun/tools/javac/comp/Attr.java file | annotate | diff | comparison | revisions
test/tools/javac/conditional/T8016702.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Jul 05 11:03:04 2013 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Jul 05 11:04:22 2013 +0100
     1.3 @@ -2392,7 +2392,7 @@
     1.4  
     1.5              ResultInfo bodyResultInfo = lambdaType.getReturnType() == Type.recoveryType ?
     1.6                  recoveryInfo :
     1.7 -                new ResultInfo(VAL, lambdaType.getReturnType(), funcContext);
     1.8 +                new LambdaResultInfo(lambdaType.getReturnType(), funcContext);
     1.9              localEnv.info.returnResult = bodyResultInfo;
    1.10  
    1.11              Log.DeferredDiagnosticHandler lambdaDeferredHandler = new Log.DeferredDiagnosticHandler(log);
    1.12 @@ -2584,6 +2584,28 @@
    1.13              }
    1.14          }
    1.15  
    1.16 +        class LambdaResultInfo extends ResultInfo {
    1.17 +
    1.18 +            LambdaResultInfo(Type pt, CheckContext checkContext) {
    1.19 +                super(VAL, pt, checkContext);
    1.20 +            }
    1.21 +
    1.22 +            @Override
    1.23 +            protected Type check(DiagnosticPosition pos, Type found) {
    1.24 +                return super.check(pos, found.baseType());
    1.25 +            }
    1.26 +
    1.27 +            @Override
    1.28 +            protected ResultInfo dup(CheckContext newContext) {
    1.29 +                return new LambdaResultInfo(pt, newContext);
    1.30 +            }
    1.31 +
    1.32 +            @Override
    1.33 +            protected ResultInfo dup(Type newPt) {
    1.34 +                return new LambdaResultInfo(newPt, checkContext);
    1.35 +            }
    1.36 +        }
    1.37 +
    1.38          /**
    1.39          * Lambda compatibility. Check that given return types, thrown types, parameter types
    1.40          * are compatible with the expected functional interface descriptor. This means that:
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/conditional/T8016702.java	Fri Jul 05 11:04:22 2013 +0100
     2.3 @@ -0,0 +1,66 @@
     2.4 +/*
     2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 8016702
    2.30 + * @summary use of ternary operator in lambda expression gives incorrect results
    2.31 + */
    2.32 +import java.util.Arrays;
    2.33 +import java.util.List;
    2.34 +
    2.35 +public class T8016702 {
    2.36 +
    2.37 +    static int assertionCount;
    2.38 +
    2.39 +    static void assertTrue(boolean b, String msg) {
    2.40 +        assertionCount++;
    2.41 +        if (!b) {
    2.42 +            throw new AssertionError(msg);
    2.43 +        }
    2.44 +    }
    2.45 +
    2.46 +    interface IntFunction<Y> {
    2.47 +        Y m(int x);
    2.48 +    }
    2.49 +
    2.50 +    void test(List<Integer> li) {
    2.51 +        map(i -> (i % 2 == 0) ? "" : "i="+i, li);
    2.52 +    }
    2.53 +
    2.54 +
    2.55 +    @SuppressWarnings("unchecked")
    2.56 +    <R> void map(IntFunction<R> mapper, List<Integer> li) {
    2.57 +        for (int i : li) {
    2.58 +            String res = (String)mapper.m(i);
    2.59 +            assertTrue((i % 2 == 0) ? res.isEmpty() : res.contains("" + i),
    2.60 +                    "i = " + i + " res = " + res);
    2.61 +        }
    2.62 +    }
    2.63 +
    2.64 +    public static void main(String[] args) {
    2.65 +        T8016702 tester = new T8016702();
    2.66 +        tester.test(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
    2.67 +        assertTrue(assertionCount == 10, "wrong assertion count: " + assertionCount);
    2.68 +    }
    2.69 +}

mercurial