author | mcimadamore |
Mon, 13 Dec 2010 15:11:00 -0800 | |
changeset 7643 | a067a0cda531 |
parent 7331 | 02ffc087c654 |
child 7681 | 1f0819a3341f |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.comp; |
|
27 |
||
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
28 |
import com.sun.tools.javac.tree.JCTree; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
29 |
import com.sun.tools.javac.tree.JCTree.JCTypeCast; |
10 | 30 |
import com.sun.tools.javac.util.*; |
31 |
import com.sun.tools.javac.util.List; |
|
32 |
import com.sun.tools.javac.code.*; |
|
33 |
import com.sun.tools.javac.code.Type.*; |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
34 |
import com.sun.tools.javac.code.Type.ForAll.ConstraintKind; |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
35 |
import com.sun.tools.javac.code.Symbol.*; |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
36 |
import com.sun.tools.javac.util.JCDiagnostic; |
10 | 37 |
|
38 |
import static com.sun.tools.javac.code.TypeTags.*; |
|
39 |
||
40 |
/** Helper class for type parameter inference, used by the attribution phase. |
|
41 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
42 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
43 |
* If you write code that depends on this, you do so at your own risk. |
10 | 44 |
* This code and its internal interfaces are subject to change or |
45 |
* deletion without notice.</b> |
|
46 |
*/ |
|
47 |
public class Infer { |
|
48 |
protected static final Context.Key<Infer> inferKey = |
|
49 |
new Context.Key<Infer>(); |
|
50 |
||
51 |
/** A value for prototypes that admit any type, including polymorphic ones. */ |
|
52 |
public static final Type anyPoly = new Type(NONE, null); |
|
53 |
||
54 |
Symtab syms; |
|
55 |
Types types; |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
56 |
Check chk; |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
57 |
Resolve rs; |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
58 |
JCDiagnostic.Factory diags; |
10 | 59 |
|
60 |
public static Infer instance(Context context) { |
|
61 |
Infer instance = context.get(inferKey); |
|
62 |
if (instance == null) |
|
63 |
instance = new Infer(context); |
|
64 |
return instance; |
|
65 |
} |
|
66 |
||
67 |
protected Infer(Context context) { |
|
68 |
context.put(inferKey, this); |
|
69 |
syms = Symtab.instance(context); |
|
70 |
types = Types.instance(context); |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
71 |
rs = Resolve.instance(context); |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
72 |
chk = Check.instance(context); |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
73 |
diags = JCDiagnostic.Factory.instance(context); |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
74 |
ambiguousNoInstanceException = |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
75 |
new NoInstanceException(true, diags); |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
76 |
unambiguousNoInstanceException = |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
77 |
new NoInstanceException(false, diags); |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
78 |
invalidInstanceException = |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
79 |
new InvalidInstanceException(diags); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
80 |
|
10 | 81 |
} |
82 |
||
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
83 |
public static class InferenceException extends Resolve.InapplicableMethodException { |
10 | 84 |
private static final long serialVersionUID = 0; |
85 |
||
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
86 |
InferenceException(JCDiagnostic.Factory diags) { |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
87 |
super(diags); |
10 | 88 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
89 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
90 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
91 |
public static class NoInstanceException extends InferenceException { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
92 |
private static final long serialVersionUID = 1; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
93 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
94 |
boolean isAmbiguous; // exist several incomparable best instances? |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
95 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
96 |
NoInstanceException(boolean isAmbiguous, JCDiagnostic.Factory diags) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
97 |
super(diags); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
98 |
this.isAmbiguous = isAmbiguous; |
10 | 99 |
} |
100 |
} |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
101 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
102 |
public static class InvalidInstanceException extends InferenceException { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
103 |
private static final long serialVersionUID = 2; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
104 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
105 |
InvalidInstanceException(JCDiagnostic.Factory diags) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
106 |
super(diags); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
107 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
108 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
109 |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
110 |
private final NoInstanceException ambiguousNoInstanceException; |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
111 |
private final NoInstanceException unambiguousNoInstanceException; |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
112 |
private final InvalidInstanceException invalidInstanceException; |
10 | 113 |
|
114 |
/*************************************************************************** |
|
115 |
* Auxiliary type values and classes |
|
116 |
***************************************************************************/ |
|
117 |
||
118 |
/** A mapping that turns type variables into undetermined type variables. |
|
119 |
*/ |
|
120 |
Mapping fromTypeVarFun = new Mapping("fromTypeVarFun") { |
|
121 |
public Type apply(Type t) { |
|
122 |
if (t.tag == TYPEVAR) return new UndetVar(t); |
|
123 |
else return t.map(this); |
|
124 |
} |
|
125 |
}; |
|
126 |
||
127 |
/** A mapping that returns its type argument with every UndetVar replaced |
|
128 |
* by its `inst' field. Throws a NoInstanceException |
|
129 |
* if this not possible because an `inst' field is null. |
|
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
130 |
* Note: mutually referring undertvars will be left uninstantiated |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
131 |
* (that is, they will be replaced by the underlying type-variable). |
10 | 132 |
*/ |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
133 |
|
10 | 134 |
Mapping getInstFun = new Mapping("getInstFun") { |
135 |
public Type apply(Type t) { |
|
136 |
switch (t.tag) { |
|
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
137 |
case UNKNOWN: |
10 | 138 |
throw ambiguousNoInstanceException |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
139 |
.setMessage("undetermined.type"); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
140 |
case UNDETVAR: |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
141 |
UndetVar that = (UndetVar) t; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
142 |
if (that.inst == null) |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
143 |
throw ambiguousNoInstanceException |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
144 |
.setMessage("type.variable.has.undetermined.type", |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
145 |
that.qtype); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
146 |
return isConstraintCyclic(that) ? |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
147 |
that.qtype : |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
148 |
apply(that.inst); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
149 |
default: |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
150 |
return t.map(this); |
10 | 151 |
} |
152 |
} |
|
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
153 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
154 |
private boolean isConstraintCyclic(UndetVar uv) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
155 |
Types.UnaryVisitor<Boolean> constraintScanner = |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
156 |
new Types.UnaryVisitor<Boolean>() { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
157 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
158 |
List<Type> seen = List.nil(); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
159 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
160 |
Boolean visit(List<Type> ts) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
161 |
for (Type t : ts) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
162 |
if (visit(t)) return true; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
163 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
164 |
return false; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
165 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
166 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
167 |
public Boolean visitType(Type t, Void ignored) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
168 |
return false; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
169 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
170 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
171 |
@Override |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
172 |
public Boolean visitClassType(ClassType t, Void ignored) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
173 |
if (t.isCompound()) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
174 |
return visit(types.supertype(t)) || |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
175 |
visit(types.interfaces(t)); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
176 |
} else { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
177 |
return visit(t.getTypeArguments()); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
178 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
179 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
180 |
@Override |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
181 |
public Boolean visitWildcardType(WildcardType t, Void ignored) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
182 |
return visit(t.type); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
183 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
184 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
185 |
@Override |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
186 |
public Boolean visitUndetVar(UndetVar t, Void ignored) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
187 |
if (seen.contains(t)) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
188 |
return true; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
189 |
} else { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
190 |
seen = seen.prepend(t); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
191 |
return visit(t.inst); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
192 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
193 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
194 |
}; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
195 |
return constraintScanner.visit(uv); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
196 |
} |
10 | 197 |
}; |
198 |
||
199 |
/*************************************************************************** |
|
200 |
* Mini/Maximization of UndetVars |
|
201 |
***************************************************************************/ |
|
202 |
||
203 |
/** Instantiate undetermined type variable to its minimal upper bound. |
|
204 |
* Throw a NoInstanceException if this not possible. |
|
205 |
*/ |
|
206 |
void maximizeInst(UndetVar that, Warner warn) throws NoInstanceException { |
|
207 |
if (that.inst == null) { |
|
208 |
if (that.hibounds.isEmpty()) |
|
209 |
that.inst = syms.objectType; |
|
210 |
else if (that.hibounds.tail.isEmpty()) |
|
211 |
that.inst = that.hibounds.head; |
|
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
212 |
else |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
213 |
that.inst = types.glb(that.hibounds); |
10 | 214 |
} |
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
215 |
if (that.inst == null || |
3139
01d76d311f8e
6835428: regression: return-type inference rejects valid code
mcimadamore
parents:
2212
diff
changeset
|
216 |
that.inst.isErroneous()) |
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
217 |
throw ambiguousNoInstanceException |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
218 |
.setMessage("no.unique.maximal.instance.exists", |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
219 |
that.qtype, that.hibounds); |
10 | 220 |
} |
221 |
//where |
|
222 |
private boolean isSubClass(Type t, final List<Type> ts) { |
|
223 |
t = t.baseType(); |
|
224 |
if (t.tag == TYPEVAR) { |
|
225 |
List<Type> bounds = types.getBounds((TypeVar)t); |
|
226 |
for (Type s : ts) { |
|
227 |
if (!types.isSameType(t, s.baseType())) { |
|
228 |
for (Type bound : bounds) { |
|
229 |
if (!isSubClass(bound, List.of(s.baseType()))) |
|
230 |
return false; |
|
231 |
} |
|
232 |
} |
|
233 |
} |
|
234 |
} else { |
|
235 |
for (Type s : ts) { |
|
236 |
if (!t.tsym.isSubClass(s.baseType().tsym, types)) |
|
237 |
return false; |
|
238 |
} |
|
239 |
} |
|
240 |
return true; |
|
241 |
} |
|
242 |
||
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
243 |
/** Instantiate undetermined type variable to the lub of all its lower bounds. |
10 | 244 |
* Throw a NoInstanceException if this not possible. |
245 |
*/ |
|
246 |
void minimizeInst(UndetVar that, Warner warn) throws NoInstanceException { |
|
247 |
if (that.inst == null) { |
|
248 |
if (that.lobounds.isEmpty()) |
|
249 |
that.inst = syms.botType; |
|
250 |
else if (that.lobounds.tail.isEmpty()) |
|
162
6620f2a8e265
6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents:
10
diff
changeset
|
251 |
that.inst = that.lobounds.head.isPrimitive() ? syms.errType : that.lobounds.head; |
10 | 252 |
else { |
253 |
that.inst = types.lub(that.lobounds); |
|
162
6620f2a8e265
6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents:
10
diff
changeset
|
254 |
} |
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
255 |
if (that.inst == null || that.inst.tag == ERROR) |
10 | 256 |
throw ambiguousNoInstanceException |
257 |
.setMessage("no.unique.minimal.instance.exists", |
|
258 |
that.qtype, that.lobounds); |
|
259 |
// VGJ: sort of inlined maximizeInst() below. Adding |
|
260 |
// bounds can cause lobounds that are above hibounds. |
|
261 |
if (that.hibounds.isEmpty()) |
|
262 |
return; |
|
263 |
Type hb = null; |
|
264 |
if (that.hibounds.tail.isEmpty()) |
|
265 |
hb = that.hibounds.head; |
|
266 |
else for (List<Type> bs = that.hibounds; |
|
267 |
bs.nonEmpty() && hb == null; |
|
268 |
bs = bs.tail) { |
|
269 |
if (isSubClass(bs.head, that.hibounds)) |
|
270 |
hb = types.fromUnknownFun.apply(bs.head); |
|
271 |
} |
|
272 |
if (hb == null || |
|
273 |
!types.isSubtypeUnchecked(hb, that.hibounds, warn) || |
|
274 |
!types.isSubtypeUnchecked(that.inst, hb, warn)) |
|
275 |
throw ambiguousNoInstanceException; |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
/*************************************************************************** |
|
280 |
* Exported Methods |
|
281 |
***************************************************************************/ |
|
282 |
||
283 |
/** Try to instantiate expression type `that' to given type `to'. |
|
284 |
* If a maximal instantiation exists which makes this type |
|
285 |
* a subtype of type `to', return the instantiated type. |
|
286 |
* If no instantiation exists, or if several incomparable |
|
287 |
* best instantiations exist throw a NoInstanceException. |
|
288 |
*/ |
|
289 |
public Type instantiateExpr(ForAll that, |
|
290 |
Type to, |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
291 |
Warner warn) throws InferenceException { |
10 | 292 |
List<Type> undetvars = Type.map(that.tvars, fromTypeVarFun); |
293 |
for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) { |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
294 |
UndetVar uv = (UndetVar) l.head; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
295 |
TypeVar tv = (TypeVar)uv.qtype; |
10 | 296 |
ListBuffer<Type> hibounds = new ListBuffer<Type>(); |
6154
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
5847
diff
changeset
|
297 |
for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS)) { |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
298 |
hibounds.append(types.subst(t, that.tvars, undetvars)); |
10 | 299 |
} |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
300 |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
301 |
List<Type> inst = that.getConstraints(tv, ConstraintKind.EQUAL); |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
302 |
if (inst.nonEmpty() && inst.head.tag != BOT) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
303 |
uv.inst = inst.head; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
304 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
305 |
uv.hibounds = hibounds.toList(); |
10 | 306 |
} |
307 |
Type qtype1 = types.subst(that.qtype, that.tvars, undetvars); |
|
7331
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
6934
diff
changeset
|
308 |
if (!types.isSubtype(qtype1, |
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
6934
diff
changeset
|
309 |
qtype1.tag == UNDETVAR ? types.boxedTypeOrType(to) : to)) { |
10 | 310 |
throw unambiguousNoInstanceException |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
311 |
.setMessage("infer.no.conforming.instance.exists", |
10 | 312 |
that.tvars, that.qtype, to); |
313 |
} |
|
314 |
for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) |
|
315 |
maximizeInst((UndetVar) l.head, warn); |
|
316 |
// System.out.println(" = " + qtype1.map(getInstFun));//DEBUG |
|
317 |
||
318 |
// check bounds |
|
319 |
List<Type> targs = Type.map(undetvars, getInstFun); |
|
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
320 |
if (Type.containsAny(targs, that.tvars)) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
321 |
//replace uninferred type-vars |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
322 |
targs = types.subst(targs, |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
323 |
that.tvars, |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
324 |
instaniateAsUninferredVars(undetvars, that.tvars)); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
325 |
} |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
326 |
return chk.checkType(warn.pos(), that.inst(targs, types), to); |
10 | 327 |
} |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
328 |
//where |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
329 |
private List<Type> instaniateAsUninferredVars(List<Type> undetvars, List<Type> tvars) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
330 |
ListBuffer<Type> new_targs = ListBuffer.lb(); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
331 |
//step 1 - create syntethic captured vars |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
332 |
for (Type t : undetvars) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
333 |
UndetVar uv = (UndetVar)t; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
334 |
Type newArg = new CapturedType(t.tsym.name, t.tsym, uv.inst, syms.botType, null); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
335 |
new_targs = new_targs.append(newArg); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
336 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
337 |
//step 2 - replace synthetic vars in their bounds |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
338 |
for (Type t : new_targs.toList()) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
339 |
CapturedType ct = (CapturedType)t; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
340 |
ct.bound = types.subst(ct.bound, tvars, new_targs.toList()); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
341 |
WildcardType wt = new WildcardType(ct.bound, BoundKind.EXTENDS, syms.boundClass); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
342 |
ct.wildcard = wt; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
343 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
344 |
return new_targs.toList(); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
345 |
} |
10 | 346 |
|
347 |
/** Instantiate method type `mt' by finding instantiations of |
|
348 |
* `tvars' so that method can be applied to `argtypes'. |
|
349 |
*/ |
|
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
350 |
public Type instantiateMethod(final Env<AttrContext> env, |
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
351 |
List<Type> tvars, |
10 | 352 |
MethodType mt, |
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5520
diff
changeset
|
353 |
final Symbol msym, |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
354 |
final List<Type> argtypes, |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
355 |
final boolean allowBoxing, |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
356 |
final boolean useVarargs, |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
357 |
final Warner warn) throws InferenceException { |
10 | 358 |
//-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG |
359 |
List<Type> undetvars = Type.map(tvars, fromTypeVarFun); |
|
360 |
List<Type> formals = mt.argtypes; |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
361 |
//need to capture exactly once - otherwise subsequent |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
362 |
//applicability checks might fail |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
363 |
final List<Type> capturedArgs = types.capture(argtypes); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
364 |
List<Type> actuals = capturedArgs; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
365 |
List<Type> actualsNoCapture = argtypes; |
10 | 366 |
// instantiate all polymorphic argument types and |
367 |
// set up lower bounds constraints for undetvars |
|
368 |
Type varargsFormal = useVarargs ? formals.last() : null; |
|
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
369 |
if (varargsFormal == null && |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
370 |
actuals.size() != formals.size()) { |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
371 |
throw unambiguousNoInstanceException |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
372 |
.setMessage("infer.arg.length.mismatch"); |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
373 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
374 |
while (actuals.nonEmpty() && formals.head != varargsFormal) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
375 |
Type formal = formals.head; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
376 |
Type actual = actuals.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
377 |
Type actualNoCapture = actualsNoCapture.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
378 |
if (actual.tag == FORALL) |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
379 |
actual = instantiateArg((ForAll)actual, formal, tvars, warn); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
380 |
Type undetFormal = types.subst(formal, tvars, undetvars); |
10 | 381 |
boolean works = allowBoxing |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
382 |
? types.isConvertible(actual, undetFormal, warn) |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
383 |
: types.isSubtypeUnchecked(actual, undetFormal, warn); |
10 | 384 |
if (!works) { |
385 |
throw unambiguousNoInstanceException |
|
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
386 |
.setMessage("infer.no.conforming.assignment.exists", |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
387 |
tvars, actualNoCapture, formal); |
10 | 388 |
} |
389 |
formals = formals.tail; |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
390 |
actuals = actuals.tail; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
391 |
actualsNoCapture = actualsNoCapture.tail; |
10 | 392 |
} |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
393 |
|
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
394 |
if (formals.head != varargsFormal) // not enough args |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
395 |
throw unambiguousNoInstanceException.setMessage("infer.arg.length.mismatch"); |
10 | 396 |
|
397 |
// for varargs arguments as well |
|
398 |
if (useVarargs) { |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
399 |
Type elemType = types.elemtype(varargsFormal); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
400 |
Type elemUndet = types.subst(elemType, tvars, undetvars); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
401 |
while (actuals.nonEmpty()) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
402 |
Type actual = actuals.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
403 |
Type actualNoCapture = actualsNoCapture.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
404 |
if (actual.tag == FORALL) |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
405 |
actual = instantiateArg((ForAll)actual, elemType, tvars, warn); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
406 |
boolean works = types.isConvertible(actual, elemUndet, warn); |
10 | 407 |
if (!works) { |
408 |
throw unambiguousNoInstanceException |
|
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
409 |
.setMessage("infer.no.conforming.assignment.exists", |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
410 |
tvars, actualNoCapture, elemType); |
10 | 411 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
412 |
actuals = actuals.tail; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
413 |
actualsNoCapture = actualsNoCapture.tail; |
10 | 414 |
} |
415 |
} |
|
416 |
||
417 |
// minimize as yet undetermined type variables |
|
418 |
for (Type t : undetvars) |
|
419 |
minimizeInst((UndetVar) t, warn); |
|
420 |
||
421 |
/** Type variables instantiated to bottom */ |
|
422 |
ListBuffer<Type> restvars = new ListBuffer<Type>(); |
|
423 |
||
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
424 |
/** Undet vars instantiated to bottom */ |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
425 |
final ListBuffer<Type> restundet = new ListBuffer<Type>(); |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
426 |
|
10 | 427 |
/** Instantiated types or TypeVars if under-constrained */ |
428 |
ListBuffer<Type> insttypes = new ListBuffer<Type>(); |
|
429 |
||
430 |
/** Instantiated types or UndetVars if under-constrained */ |
|
431 |
ListBuffer<Type> undettypes = new ListBuffer<Type>(); |
|
432 |
||
433 |
for (Type t : undetvars) { |
|
434 |
UndetVar uv = (UndetVar)t; |
|
435 |
if (uv.inst.tag == BOT) { |
|
436 |
restvars.append(uv.qtype); |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
437 |
restundet.append(uv); |
10 | 438 |
insttypes.append(uv.qtype); |
439 |
undettypes.append(uv); |
|
440 |
uv.inst = null; |
|
441 |
} else { |
|
442 |
insttypes.append(uv.inst); |
|
443 |
undettypes.append(uv.inst); |
|
444 |
} |
|
445 |
} |
|
446 |
checkWithinBounds(tvars, undettypes.toList(), warn); |
|
447 |
||
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
448 |
mt = (MethodType)types.subst(mt, tvars, insttypes.toList()); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
449 |
|
10 | 450 |
if (!restvars.isEmpty()) { |
451 |
// if there are uninstantiated variables, |
|
452 |
// quantify result type with them |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
453 |
final List<Type> inferredTypes = insttypes.toList(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
454 |
final List<Type> all_tvars = tvars; //this is the wrong tvars |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
455 |
final MethodType mt2 = new MethodType(mt.argtypes, null, mt.thrown, syms.methodClass); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
456 |
mt2.restype = new ForAll(restvars.toList(), mt.restype) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
457 |
@Override |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
458 |
public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
459 |
for (Type t : restundet.toList()) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
460 |
UndetVar uv = (UndetVar)t; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
461 |
if (uv.qtype == tv) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
462 |
switch (ck) { |
6154
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
5847
diff
changeset
|
463 |
case EXTENDS: return uv.hibounds.appendList(types.subst(types.getBounds(tv), all_tvars, inferredTypes)); |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
464 |
case SUPER: return uv.lobounds; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
465 |
case EQUAL: return uv.inst != null ? List.of(uv.inst) : List.<Type>nil(); |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
466 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
467 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
468 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
469 |
return List.nil(); |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
470 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
471 |
|
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
472 |
@Override |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
473 |
public Type inst(List<Type> inferred, Types types) throws NoInstanceException { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
474 |
List<Type> formals = types.subst(mt2.argtypes, tvars, inferred); |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
475 |
if (!rs.argumentsAcceptable(capturedArgs, formals, |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
476 |
allowBoxing, useVarargs, warn)) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
477 |
// inferred method is not applicable |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
478 |
throw invalidInstanceException.setMessage("inferred.do.not.conform.to.params", formals, argtypes); |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
479 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
480 |
// check that inferred bounds conform to their bounds |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
481 |
checkWithinBounds(all_tvars, |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
482 |
types.subst(inferredTypes, tvars, inferred), warn); |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
483 |
if (useVarargs) { |
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7331
diff
changeset
|
484 |
chk.checkVararg(env.tree.pos(), formals, msym); |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
485 |
} |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
486 |
return super.inst(inferred, types); |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
487 |
}}; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
488 |
return mt2; |
10 | 489 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
490 |
else if (!rs.argumentsAcceptable(capturedArgs, mt.getParameterTypes(), allowBoxing, useVarargs, warn)) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
491 |
// inferred method is not applicable |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
492 |
throw invalidInstanceException.setMessage("inferred.do.not.conform.to.params", mt.getParameterTypes(), argtypes); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
493 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
494 |
else { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
495 |
// return instantiated version of method type |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
496 |
return mt; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
497 |
} |
10 | 498 |
} |
499 |
//where |
|
500 |
||
501 |
/** Try to instantiate argument type `that' to given type `to'. |
|
502 |
* If this fails, try to insantiate `that' to `to' where |
|
503 |
* every occurrence of a type variable in `tvars' is replaced |
|
504 |
* by an unknown type. |
|
505 |
*/ |
|
506 |
private Type instantiateArg(ForAll that, |
|
507 |
Type to, |
|
508 |
List<Type> tvars, |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
509 |
Warner warn) throws InferenceException { |
10 | 510 |
List<Type> targs; |
511 |
try { |
|
512 |
return instantiateExpr(that, to, warn); |
|
513 |
} catch (NoInstanceException ex) { |
|
514 |
Type to1 = to; |
|
515 |
for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) |
|
516 |
to1 = types.subst(to1, List.of(l.head), List.of(syms.unknownType)); |
|
517 |
return instantiateExpr(that, to1, warn); |
|
518 |
} |
|
519 |
} |
|
520 |
||
521 |
/** check that type parameters are within their bounds. |
|
522 |
*/ |
|
6154
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
5847
diff
changeset
|
523 |
void checkWithinBounds(List<Type> tvars, |
10 | 524 |
List<Type> arguments, |
525 |
Warner warn) |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
526 |
throws InvalidInstanceException { |
10 | 527 |
for (List<Type> tvs = tvars, args = arguments; |
528 |
tvs.nonEmpty(); |
|
529 |
tvs = tvs.tail, args = args.tail) { |
|
530 |
if (args.head instanceof UndetVar) continue; |
|
531 |
List<Type> bounds = types.subst(types.getBounds((TypeVar)tvs.head), tvars, arguments); |
|
532 |
if (!types.isSubtypeUnchecked(args.head, bounds, warn)) |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
533 |
throw invalidInstanceException |
10 | 534 |
.setMessage("inferred.do.not.conform.to.bounds", |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
535 |
args.head, bounds); |
10 | 536 |
} |
537 |
} |
|
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
538 |
|
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
539 |
/** |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
540 |
* Compute a synthetic method type corresponding to the requested polymorphic |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
541 |
* method signature. If no explicit return type is supplied, a provisional |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
542 |
* return type is computed (just Object in case of non-transitional 292) |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
543 |
*/ |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
544 |
Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env, Type site, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
545 |
Name name, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
546 |
MethodSymbol spMethod, // sig. poly. method or null if none |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
547 |
List<Type> argtypes, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
548 |
List<Type> typeargtypes) { |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
549 |
final Type restype; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
550 |
if (rs.allowTransitionalJSR292 && typeargtypes.nonEmpty()) { |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
551 |
restype = typeargtypes.head; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
552 |
} else { |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
553 |
//The return type for a polymorphic signature call is computed from |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
554 |
//the enclosing tree E, as follows: if E is a cast, then use the |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
555 |
//target type of the cast expression as a return type; if E is an |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
556 |
//expression statement, the return type is 'void' - otherwise the |
6934
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
557 |
//return type is simply 'Object'. A correctness check ensures that |
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
558 |
//env.next refers to the lexically enclosing environment in which |
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
559 |
//the polymorphic signature call environment is nested. |
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
560 |
|
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
561 |
switch (env.next.tree.getTag()) { |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
562 |
case JCTree.TYPECAST: |
6934
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
563 |
JCTypeCast castTree = (JCTypeCast)env.next.tree; |
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
564 |
restype = (castTree.expr == env.tree) ? |
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
565 |
castTree.clazz.type : |
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
566 |
syms.objectType; |
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
567 |
break; |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
568 |
case JCTree.EXEC: |
6934
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
569 |
JCTree.JCExpressionStatement execTree = |
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
570 |
(JCTree.JCExpressionStatement)env.next.tree; |
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
571 |
restype = (execTree.expr == env.tree) ? |
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
572 |
syms.voidType : |
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
573 |
syms.objectType; |
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
574 |
break; |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
575 |
default: |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
576 |
restype = syms.objectType; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
577 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
578 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
579 |
|
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
580 |
List<Type> paramtypes = Type.map(argtypes, implicitArgType); |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
581 |
List<Type> exType = spMethod != null ? |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
582 |
spMethod.getThrownTypes() : |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
583 |
List.of(syms.throwableType); // make it throw all exceptions |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
584 |
|
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
585 |
MethodType mtype = new MethodType(paramtypes, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
586 |
restype, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
587 |
exType, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
588 |
syms.methodClass); |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
589 |
return mtype; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
590 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
591 |
//where |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
592 |
Mapping implicitArgType = new Mapping ("implicitArgType") { |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
593 |
public Type apply(Type t) { |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
594 |
t = types.erasure(t); |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
595 |
if (t.tag == BOT) |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
596 |
// nulls type as the marker type Null (which has no instances) |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
597 |
// infer as java.lang.Void for now |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
598 |
t = types.boxedClass(syms.voidType).type; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
599 |
return t; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
600 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
601 |
}; |
10 | 602 |
} |