8041663: Sensitive dependence on location of nested interface

Fri, 09 May 2014 12:55:58 +0200

author
jlahoda
date
Fri, 09 May 2014 12:55:58 +0200
changeset 2387
cf626fb754aa
parent 2386
f8e84de96252
child 2389
f1fbe29e36d1

8041663: Sensitive dependence on location of nested interface
Summary: Adding a method among ambiguous candidates only if it is more specific than some of the existing candidates.
Reviewed-by: dlsmith, vromero

src/share/classes/com/sun/tools/javac/comp/Resolve.java file | annotate | diff | comparison | revisions
test/tools/javac/resolve/AmbiguityErrorTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Fri May 09 09:36:35 2014 +0200
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Fri May 09 12:55:58 2014 +0200
     1.3 @@ -1524,14 +1524,22 @@
     1.4              if (m2SignatureMoreSpecific) return m2;
     1.5              return ambiguityError(m1, m2);
     1.6          case AMBIGUOUS:
     1.7 -            //check if m1 is more specific than all ambiguous methods in m2
     1.8 +            //compare m1 to ambiguous methods in m2
     1.9              AmbiguityError e = (AmbiguityError)m2.baseSymbol();
    1.10 +            boolean m1MoreSpecificThanAnyAmbiguous = true;
    1.11 +            boolean allAmbiguousMoreSpecificThanM1 = true;
    1.12              for (Symbol s : e.ambiguousSyms) {
    1.13 -                if (mostSpecific(argtypes, m1, s, env, site, allowBoxing, useVarargs) != m1) {
    1.14 -                    return e.addAmbiguousSymbol(m1);
    1.15 -                }
    1.16 +                Symbol moreSpecific = mostSpecific(argtypes, m1, s, env, site, allowBoxing, useVarargs);
    1.17 +                m1MoreSpecificThanAnyAmbiguous &= moreSpecific == m1;
    1.18 +                allAmbiguousMoreSpecificThanM1 &= moreSpecific == s;
    1.19              }
    1.20 -            return m1;
    1.21 +            if (m1MoreSpecificThanAnyAmbiguous)
    1.22 +                return m1;
    1.23 +            //if m1 is more specific than some ambiguous methods, but other ambiguous methods are
    1.24 +            //more specific than m1, add it as a new ambiguous method:
    1.25 +            if (!allAmbiguousMoreSpecificThanM1)
    1.26 +                e.addAmbiguousSymbol(m1);
    1.27 +            return e;
    1.28          default:
    1.29              throw new AssertionError();
    1.30          }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/resolve/AmbiguityErrorTest.java	Fri May 09 12:55:58 2014 +0200
     2.3 @@ -0,0 +1,75 @@
     2.4 +/*
     2.5 + * Copyright (c) 2014, 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 8041663
    2.30 + */
    2.31 +
    2.32 +public class AmbiguityErrorTest {
    2.33 +
    2.34 +    public interface A { }
    2.35 +
    2.36 +    public interface B extends A { }
    2.37 +
    2.38 +    public interface C {
    2.39 +        A m(B strategy);
    2.40 +    }
    2.41 +
    2.42 +    public interface D {
    2.43 +        A m(A strategy);
    2.44 +        A m(B strategy);
    2.45 +    }
    2.46 +
    2.47 +    public interface T1 extends C, D { }
    2.48 +    public interface T2 extends D, C { }
    2.49 +
    2.50 +    int count;
    2.51 +
    2.52 +    class T1Impl implements T1, T2 {
    2.53 +        public A m(B strategy) {
    2.54 +            count++;
    2.55 +            return null;
    2.56 +        }
    2.57 +        public A m(A strategy) {
    2.58 +            throw new AssertionError("Should not get here.");
    2.59 +        }
    2.60 +    }
    2.61 +
    2.62 +    public static void main(String... args) {
    2.63 +        new AmbiguityErrorTest().test();
    2.64 +    }
    2.65 +
    2.66 +    void test() {
    2.67 +        T1 t1 = new T1Impl();
    2.68 +        T2 t2 = new T1Impl();
    2.69 +        final B b = new B() { };
    2.70 +        t1.m(b);
    2.71 +        t2.m(b);
    2.72 +
    2.73 +        if (count != 2) {
    2.74 +            throw new IllegalStateException("Did not call the methods properly");
    2.75 +        }
    2.76 +    }
    2.77 +
    2.78 +}

mercurial