TypeEqualityInInferenceTest.java revision 3504:30bfbfa94fad
1/* 2 * Copyright (c) 2016, 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 8159970 27 * @summary javac, JLS8 18.2.4 is not completely implemented by the compiler 28 * @library /tools/lib/types 29 * @modules jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * jdk.compiler/com.sun.tools.javac.code 32 * jdk.compiler/com.sun.tools.javac.comp 33 * jdk.compiler/com.sun.tools.javac.tree 34 * jdk.compiler/com.sun.tools.javac.util 35 * jdk.compiler/com.sun.tools.javac.file 36 * @build TypeHarness 37 * @run main TypeEqualityInInferenceTest 38 */ 39 40import java.util.ArrayList; 41import java.util.List; 42 43import com.sun.tools.javac.code.Type; 44import com.sun.tools.javac.code.Type.UndetVar; 45import com.sun.tools.javac.code.Type.UndetVar.InferenceBound; 46import com.sun.tools.javac.util.Assert; 47 48public class TypeEqualityInInferenceTest extends TypeHarness { 49 StrToTypeFactory strToTypeFactory; 50 51 public static void main(String... args) throws Exception { 52 new TypeEqualityInInferenceTest().runAll(); 53 } 54 55 void runAll() { 56 List<String> imports = new ArrayList<>(); 57 imports.add("java.util.*"); 58 List<String> typeVars = new ArrayList<>(); 59 typeVars.add("T"); 60 strToTypeFactory = new StrToTypeFactory(null, imports, typeVars); 61 62 runTest("List<? extends T>", "List<? extends String>", predef.stringType); 63 runTest("List<? extends T>", "List<?>", predef.objectType); 64 runTest("List<? super T>", "List<? super String>", predef.stringType); 65 } 66 67 void runTest(String freeTypeStr, String typeStr, Type equalityBoundType) { 68 Type freeType = strToTypeFactory.getType(freeTypeStr); 69 Type aType = strToTypeFactory.getType(typeStr); 70 71 withInferenceContext(strToTypeFactory.getTypeVars(), inferenceContext -> { 72 assertSameType(inferenceContext.asUndetVar(freeType), aType); 73 UndetVar undetVarForT = (UndetVar)inferenceContext.undetVars().head; 74 checkEqualityBound(undetVarForT, equalityBoundType); 75 }); 76 77 withInferenceContext(strToTypeFactory.getTypeVars(), inferenceContext -> { 78 assertSameType(aType, inferenceContext.asUndetVar(freeType)); 79 UndetVar undetVarForT = (UndetVar)inferenceContext.undetVars().head; 80 checkEqualityBound(undetVarForT, equalityBoundType); 81 }); 82 } 83 84 void checkEqualityBound(UndetVar uv, Type boundType) { 85 com.sun.tools.javac.util.List<Type> equalBounds = uv.getBounds(InferenceBound.EQ); 86 Assert.check(!equalBounds.isEmpty() && equalBounds.length() == 1, 87 "undetVar must have only one equality bound"); 88 Type bound = equalBounds.head; 89 Assert.check(bound == boundType, "equal bound must be of type " + boundType); 90 } 91} 92