1 /* 2 * Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 7062745 8006694 8129962 27 * @summary Regression: difference in overload resolution when two methods 28 * are maximally specific 29 * temporarily workaround combo tests are causing time out in several platforms 30 * @library /tools/javac/lib 31 * @modules jdk.compiler/com.sun.tools.javac.api 32 * jdk.compiler/com.sun.tools.javac.file 33 * jdk.compiler/com.sun.tools.javac.util 34 * @build combo.ComboTestHelper 35 * @run main GenericOverrideTest 36 */ 37 38 import java.io.IOException; 39 40 import combo.ComboInstance; 41 import combo.ComboParameter; 42 import combo.ComboTask.Result; 43 import combo.ComboTestHelper; 44 45 public class GenericOverrideTest extends ComboInstance<GenericOverrideTest> { 46 47 enum SourceLevel { 48 SOURCE_DEFAULT(); 49 50 String[] opts; 51 52 SourceLevel(String... opts) { 53 this.opts = opts; 54 } 55 } 56 57 enum SignatureKind implements ComboParameter { 58 NON_GENERIC(""), 59 GENERIC("<X>"); 60 61 String paramStr; 62 63 SignatureKind(String paramStr) { 64 this.paramStr = paramStr; 65 } 66 67 @Override 68 public String expand(String optParameter) { 69 return paramStr; 70 } 71 } 72 73 enum ReturnTypeKind implements ComboParameter { 74 LIST("List"), 75 ARRAYLIST("ArrayList"); 76 77 String retStr; 78 79 ReturnTypeKind(String retStr) { 80 this.retStr = retStr; 81 } 82 83 boolean moreSpecificThan(ReturnTypeKind that) { 84 switch (this) { 85 case LIST: 86 return that == this; 87 case ARRAYLIST: 88 return that == LIST || that == ARRAYLIST; 89 default: throw new AssertionError("Unexpected ret kind: " + this); 90 } 91 } 92 93 @Override 94 public String expand(String optParameter) { 95 return retStr; 96 } 97 } 98 99 enum TypeArgumentKind implements ComboParameter { 100 NONE(""), 101 UNBOUND("<?>"), 102 INTEGER("<Number>"), 103 NUMBER("<Integer>"), 104 TYPEVAR("<X>"); 105 106 String typeargStr; 107 108 TypeArgumentKind(String typeargStr) { 109 this.typeargStr = typeargStr; 110 } 111 112 boolean compatibleWith(SignatureKind sig) { 113 switch (this) { 114 case TYPEVAR: return sig != SignatureKind.NON_GENERIC; 115 default: return true; 116 } 117 } 118 119 boolean moreSpecificThan(TypeArgumentKind that) { 120 switch (this) { 121 case NONE: 122 return that == this; 123 case UNBOUND: 124 return that == this || that == NONE; 125 case INTEGER: 126 case NUMBER: 127 case TYPEVAR: 128 return that == this || that == NONE || that == UNBOUND; 129 default: throw new AssertionError("Unexpected typearg kind: " + this); 130 } 131 } 132 133 boolean assignableTo(TypeArgumentKind that, SignatureKind sig, SourceLevel level) { 134 switch (this) { 135 case NONE: 136 //this case needs to workaround to javac's impl of 15.12.2.8 being too strict 137 //ideally should be just 'return true' (see 7067746/8015505) 138 return level == SourceLevel.SOURCE_DEFAULT || 139 sig == SignatureKind.NON_GENERIC || that == NONE; 140 case UNBOUND: 141 return that == this || that == NONE; 142 case INTEGER: 143 case NUMBER: 144 return that == this || that == NONE || that == UNBOUND; 145 case TYPEVAR: 146 return true; 147 default: throw new AssertionError("Unexpected typearg kind: " + this); 148 } 149 } 150 151 @Override 152 public String expand(String optParameter) { 153 return typeargStr; 154 } 155 } 156 157 public static void main(String... args) throws Exception { 158 new ComboTestHelper<GenericOverrideTest>() 159 .withFilter(GenericOverrideTest::argMismatchFilter) 160 .withDimension("SOURCE", (x, level) -> x.level = level, SourceLevel.values()) 161 .withArrayDimension("SIG", (x, sig, idx) -> x.sigs[idx] = sig, 2, SignatureKind.values()) 162 .withArrayDimension("TARG", (x, targ, idx) -> x.targs[idx] = targ, 3, TypeArgumentKind.values()) 163 .withArrayDimension("RET", (x, ret, idx) -> x.rets[idx] = ret, 3, ReturnTypeKind.values()) 164 .run(GenericOverrideTest::new); 165 } 166 167 SignatureKind[] sigs = new SignatureKind[2]; 168 ReturnTypeKind[] rets = new ReturnTypeKind[3]; 169 TypeArgumentKind[] targs = new TypeArgumentKind[3]; 170 SourceLevel level; 171 172 boolean argMismatchFilter() { 173 return targs[0].compatibleWith(sigs[0]) && 174 targs[1].compatibleWith(sigs[1]) && 175 targs[2].compatibleWith(SignatureKind.NON_GENERIC); 176 } 177 178 String template = "import java.util.*;\n" + 179 "interface A { #{SIG[0]} #{RET[0]}#{TARG[0]} m(); }\n" + 180 "interface B { #{SIG[1]} #{RET[1]}#{TARG[1]} m(); }\n" + 181 "interface AB extends A, B {}\n" + 182 "class Test {\n" + 183 " void test(AB ab) { #{RET[2]}#{TARG[2]} n = ab.m(); }\n" + 184 "}"; 185 186 @Override 187 public void doWork() throws IOException { 188 newCompilationTask() 189 .withOption("-XDuseUnsharedTable") //this test relies on predictable name indexes! 190 .withOptions(level.opts) 191 .withSourceFromTemplate(template) 192 .analyze(this::check); 193 } 194 195 void check(Result<?> res) { 196 boolean errorExpected = false; 197 boolean loose = false; 198 int mostSpecific = 0; 199 200 //first check that either |R1| <: |R2| or |R2| <: |R1| 201 if (rets[0] != rets[1]) { 202 if (!rets[0].moreSpecificThan(rets[1]) && 203 !rets[1].moreSpecificThan(rets[0])) { 204 errorExpected = true; 205 } else { 206 mostSpecific = rets[0].moreSpecificThan(rets[1]) ? 1 : 2; 207 } 208 } else if (sigs[0] != sigs[1]) { 209 mostSpecific = sigs[0] == SignatureKind.GENERIC ? 2 : 1; 210 loose = true; 211 } 212 213 //check that either TA1 <= TA2 or TA2 <= TA1 (unless most specific return found above is raw) 214 if (!errorExpected) { 215 if (targs[0] != targs[1]) { 216 boolean ta1ms = targs[0].moreSpecificThan(targs[1]); 217 boolean ta2ms = targs[1].moreSpecificThan(targs[0]); 218 if (!ta1ms && !ta2ms) { 219 errorExpected = true; 220 } else if (mostSpecific != 0) { 221 errorExpected = !loose && targs[mostSpecific - 1] != TypeArgumentKind.NONE && 222 (mostSpecific == 1 ? !ta1ms : !ta2ms); 223 } else { 224 mostSpecific = ta1ms ? 1 : 2; 225 } 226 } 227 } 228 229 if (mostSpecific == 0) { 230 //when no signature is better than the other, an arbitrary choice 231 //must be made - javac always picks the second signature 232 mostSpecific = 2; 233 } 234 235 if (!errorExpected) { 236 ReturnTypeKind msrt = mostSpecific == 1 ? rets[0] : rets[1]; 237 TypeArgumentKind msta = mostSpecific == 1 ? targs[0] : targs[1]; 238 SignatureKind mssig = mostSpecific == 1 ? sigs[0] : sigs[1]; 239 240 //check that most specific is subsignature 241 errorExpected = sigs[0] != sigs[1] && 242 mssig == SignatureKind.GENERIC; 243 244 //finally, check that most specific return type is compatible with expected type 245 if (!msrt.moreSpecificThan(rets[2]) || 246 !msta.assignableTo(targs[2], mssig, level)) { 247 errorExpected = true; 248 } 249 } 250 251 if (errorExpected != res.hasErrors()) { 252 fail("invalid diagnostics for source:\n" + 253 res.compilationInfo() + 254 "\nFound error: " + res.hasErrors() + 255 "\nExpected error: " + errorExpected); 256 } 257 } 258 }