author | jjg |
Thu, 10 Jun 2010 16:08:01 -0700 | |
changeset 5847 | 1908176fd6e3 |
parent 5846 | 6df0e6bcb388 |
child 6154 | c1b0c6641b92 |
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 |
||
28 |
import com.sun.tools.javac.util.*; |
|
29 |
import com.sun.tools.javac.util.List; |
|
30 |
import com.sun.tools.javac.code.*; |
|
31 |
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
|
32 |
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
|
33 |
import com.sun.tools.javac.code.Symbol.*; |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
34 |
import com.sun.tools.javac.util.JCDiagnostic; |
10 | 35 |
|
36 |
import static com.sun.tools.javac.code.TypeTags.*; |
|
37 |
||
38 |
/** Helper class for type parameter inference, used by the attribution phase. |
|
39 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
40 |
* <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
|
41 |
* If you write code that depends on this, you do so at your own risk. |
10 | 42 |
* This code and its internal interfaces are subject to change or |
43 |
* deletion without notice.</b> |
|
44 |
*/ |
|
45 |
public class Infer { |
|
46 |
protected static final Context.Key<Infer> inferKey = |
|
47 |
new Context.Key<Infer>(); |
|
48 |
||
49 |
/** A value for prototypes that admit any type, including polymorphic ones. */ |
|
50 |
public static final Type anyPoly = new Type(NONE, null); |
|
51 |
||
52 |
Symtab syms; |
|
53 |
Types types; |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
54 |
Check chk; |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
55 |
Resolve rs; |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
56 |
JCDiagnostic.Factory diags; |
10 | 57 |
|
58 |
public static Infer instance(Context context) { |
|
59 |
Infer instance = context.get(inferKey); |
|
60 |
if (instance == null) |
|
61 |
instance = new Infer(context); |
|
62 |
return instance; |
|
63 |
} |
|
64 |
||
65 |
protected Infer(Context context) { |
|
66 |
context.put(inferKey, this); |
|
67 |
syms = Symtab.instance(context); |
|
68 |
types = Types.instance(context); |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
69 |
rs = Resolve.instance(context); |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
70 |
chk = Check.instance(context); |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
71 |
diags = JCDiagnostic.Factory.instance(context); |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
72 |
ambiguousNoInstanceException = |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
73 |
new NoInstanceException(true, diags); |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
74 |
unambiguousNoInstanceException = |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
75 |
new NoInstanceException(false, diags); |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
76 |
invalidInstanceException = |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
77 |
new InvalidInstanceException(diags); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
78 |
|
10 | 79 |
} |
80 |
||
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
81 |
public static class InferenceException extends RuntimeException { |
10 | 82 |
private static final long serialVersionUID = 0; |
83 |
||
84 |
JCDiagnostic diagnostic; |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
85 |
JCDiagnostic.Factory diags; |
10 | 86 |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
87 |
InferenceException(JCDiagnostic.Factory diags) { |
10 | 88 |
this.diagnostic = null; |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
89 |
this.diags = diags; |
10 | 90 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
91 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
92 |
InferenceException setMessage(String key, Object... args) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
93 |
this.diagnostic = diags.fragment(key, args); |
10 | 94 |
return this; |
95 |
} |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
96 |
|
10 | 97 |
public JCDiagnostic getDiagnostic() { |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
98 |
return diagnostic; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
99 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
100 |
} |
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 NoInstanceException extends InferenceException { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
103 |
private static final long serialVersionUID = 1; |
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 |
boolean isAmbiguous; // exist several incomparable best instances? |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
106 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
107 |
NoInstanceException(boolean isAmbiguous, JCDiagnostic.Factory diags) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
108 |
super(diags); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
109 |
this.isAmbiguous = isAmbiguous; |
10 | 110 |
} |
111 |
} |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
112 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
113 |
public static class InvalidInstanceException extends InferenceException { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
114 |
private static final long serialVersionUID = 2; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
115 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
116 |
InvalidInstanceException(JCDiagnostic.Factory diags) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
117 |
super(diags); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
118 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
119 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
120 |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
121 |
private final NoInstanceException ambiguousNoInstanceException; |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
122 |
private final NoInstanceException unambiguousNoInstanceException; |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
123 |
private final InvalidInstanceException invalidInstanceException; |
10 | 124 |
|
125 |
/*************************************************************************** |
|
126 |
* Auxiliary type values and classes |
|
127 |
***************************************************************************/ |
|
128 |
||
129 |
/** A mapping that turns type variables into undetermined type variables. |
|
130 |
*/ |
|
131 |
Mapping fromTypeVarFun = new Mapping("fromTypeVarFun") { |
|
132 |
public Type apply(Type t) { |
|
133 |
if (t.tag == TYPEVAR) return new UndetVar(t); |
|
134 |
else return t.map(this); |
|
135 |
} |
|
136 |
}; |
|
137 |
||
138 |
/** A mapping that returns its type argument with every UndetVar replaced |
|
139 |
* by its `inst' field. Throws a NoInstanceException |
|
140 |
* if this not possible because an `inst' field is null. |
|
141 |
*/ |
|
142 |
Mapping getInstFun = new Mapping("getInstFun") { |
|
143 |
public Type apply(Type t) { |
|
144 |
switch (t.tag) { |
|
145 |
case UNKNOWN: |
|
146 |
throw ambiguousNoInstanceException |
|
147 |
.setMessage("undetermined.type"); |
|
148 |
case UNDETVAR: |
|
149 |
UndetVar that = (UndetVar) t; |
|
150 |
if (that.inst == null) |
|
151 |
throw ambiguousNoInstanceException |
|
152 |
.setMessage("type.variable.has.undetermined.type", |
|
153 |
that.qtype); |
|
154 |
return apply(that.inst); |
|
155 |
default: |
|
156 |
return t.map(this); |
|
157 |
} |
|
158 |
} |
|
159 |
}; |
|
160 |
||
161 |
/*************************************************************************** |
|
162 |
* Mini/Maximization of UndetVars |
|
163 |
***************************************************************************/ |
|
164 |
||
165 |
/** Instantiate undetermined type variable to its minimal upper bound. |
|
166 |
* Throw a NoInstanceException if this not possible. |
|
167 |
*/ |
|
168 |
void maximizeInst(UndetVar that, Warner warn) throws NoInstanceException { |
|
169 |
if (that.inst == null) { |
|
170 |
if (that.hibounds.isEmpty()) |
|
171 |
that.inst = syms.objectType; |
|
172 |
else if (that.hibounds.tail.isEmpty()) |
|
173 |
that.inst = that.hibounds.head; |
|
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
174 |
else |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
175 |
that.inst = types.glb(that.hibounds); |
10 | 176 |
} |
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
177 |
if (that.inst == null || |
3139
01d76d311f8e
6835428: regression: return-type inference rejects valid code
mcimadamore
parents:
2212
diff
changeset
|
178 |
that.inst.isErroneous()) |
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
179 |
throw ambiguousNoInstanceException |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
180 |
.setMessage("no.unique.maximal.instance.exists", |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
181 |
that.qtype, that.hibounds); |
10 | 182 |
} |
183 |
//where |
|
184 |
private boolean isSubClass(Type t, final List<Type> ts) { |
|
185 |
t = t.baseType(); |
|
186 |
if (t.tag == TYPEVAR) { |
|
187 |
List<Type> bounds = types.getBounds((TypeVar)t); |
|
188 |
for (Type s : ts) { |
|
189 |
if (!types.isSameType(t, s.baseType())) { |
|
190 |
for (Type bound : bounds) { |
|
191 |
if (!isSubClass(bound, List.of(s.baseType()))) |
|
192 |
return false; |
|
193 |
} |
|
194 |
} |
|
195 |
} |
|
196 |
} else { |
|
197 |
for (Type s : ts) { |
|
198 |
if (!t.tsym.isSubClass(s.baseType().tsym, types)) |
|
199 |
return false; |
|
200 |
} |
|
201 |
} |
|
202 |
return true; |
|
203 |
} |
|
204 |
||
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
|
205 |
/** Instantiate undetermined type variable to the lub of all its lower bounds. |
10 | 206 |
* Throw a NoInstanceException if this not possible. |
207 |
*/ |
|
208 |
void minimizeInst(UndetVar that, Warner warn) throws NoInstanceException { |
|
209 |
if (that.inst == null) { |
|
210 |
if (that.lobounds.isEmpty()) |
|
211 |
that.inst = syms.botType; |
|
212 |
else if (that.lobounds.tail.isEmpty()) |
|
162
6620f2a8e265
6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents:
10
diff
changeset
|
213 |
that.inst = that.lobounds.head.isPrimitive() ? syms.errType : that.lobounds.head; |
10 | 214 |
else { |
215 |
that.inst = types.lub(that.lobounds); |
|
162
6620f2a8e265
6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents:
10
diff
changeset
|
216 |
} |
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
|
217 |
if (that.inst == null || that.inst.tag == ERROR) |
10 | 218 |
throw ambiguousNoInstanceException |
219 |
.setMessage("no.unique.minimal.instance.exists", |
|
220 |
that.qtype, that.lobounds); |
|
221 |
// VGJ: sort of inlined maximizeInst() below. Adding |
|
222 |
// bounds can cause lobounds that are above hibounds. |
|
223 |
if (that.hibounds.isEmpty()) |
|
224 |
return; |
|
225 |
Type hb = null; |
|
226 |
if (that.hibounds.tail.isEmpty()) |
|
227 |
hb = that.hibounds.head; |
|
228 |
else for (List<Type> bs = that.hibounds; |
|
229 |
bs.nonEmpty() && hb == null; |
|
230 |
bs = bs.tail) { |
|
231 |
if (isSubClass(bs.head, that.hibounds)) |
|
232 |
hb = types.fromUnknownFun.apply(bs.head); |
|
233 |
} |
|
234 |
if (hb == null || |
|
235 |
!types.isSubtypeUnchecked(hb, that.hibounds, warn) || |
|
236 |
!types.isSubtypeUnchecked(that.inst, hb, warn)) |
|
237 |
throw ambiguousNoInstanceException; |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
/*************************************************************************** |
|
242 |
* Exported Methods |
|
243 |
***************************************************************************/ |
|
244 |
||
245 |
/** Try to instantiate expression type `that' to given type `to'. |
|
246 |
* If a maximal instantiation exists which makes this type |
|
247 |
* a subtype of type `to', return the instantiated type. |
|
248 |
* If no instantiation exists, or if several incomparable |
|
249 |
* best instantiations exist throw a NoInstanceException. |
|
250 |
*/ |
|
251 |
public Type instantiateExpr(ForAll that, |
|
252 |
Type to, |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
253 |
Warner warn) throws InferenceException { |
10 | 254 |
List<Type> undetvars = Type.map(that.tvars, fromTypeVarFun); |
255 |
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
|
256 |
UndetVar uv = (UndetVar) l.head; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
257 |
TypeVar tv = (TypeVar)uv.qtype; |
10 | 258 |
ListBuffer<Type> hibounds = new ListBuffer<Type>(); |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
259 |
for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS).prependList(types.getBounds(tv))) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
260 |
if (!t.containsSome(that.tvars) && t.tag != BOT) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
261 |
hibounds.append(t); |
10 | 262 |
} |
263 |
} |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
264 |
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
|
265 |
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
|
266 |
uv.inst = inst.head; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
267 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
268 |
uv.hibounds = hibounds.toList(); |
10 | 269 |
} |
270 |
Type qtype1 = types.subst(that.qtype, that.tvars, undetvars); |
|
271 |
if (!types.isSubtype(qtype1, to)) { |
|
272 |
throw unambiguousNoInstanceException |
|
273 |
.setMessage("no.conforming.instance.exists", |
|
274 |
that.tvars, that.qtype, to); |
|
275 |
} |
|
276 |
for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) |
|
277 |
maximizeInst((UndetVar) l.head, warn); |
|
278 |
// System.out.println(" = " + qtype1.map(getInstFun));//DEBUG |
|
279 |
||
280 |
// check bounds |
|
281 |
List<Type> targs = Type.map(undetvars, getInstFun); |
|
282 |
targs = types.subst(targs, that.tvars, targs); |
|
283 |
checkWithinBounds(that.tvars, targs, warn); |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
284 |
return chk.checkType(warn.pos(), that.inst(targs, types), to); |
10 | 285 |
} |
286 |
||
287 |
/** Instantiate method type `mt' by finding instantiations of |
|
288 |
* `tvars' so that method can be applied to `argtypes'. |
|
289 |
*/ |
|
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
290 |
public Type instantiateMethod(final Env<AttrContext> env, |
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
291 |
List<Type> tvars, |
10 | 292 |
MethodType mt, |
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5520
diff
changeset
|
293 |
final Symbol msym, |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
294 |
final List<Type> argtypes, |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
295 |
final boolean allowBoxing, |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
296 |
final boolean useVarargs, |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
297 |
final Warner warn) throws InferenceException { |
10 | 298 |
//-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG |
299 |
List<Type> undetvars = Type.map(tvars, fromTypeVarFun); |
|
300 |
List<Type> formals = mt.argtypes; |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
301 |
//need to capture exactly once - otherwise subsequent |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
302 |
//applicability checks might fail |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
303 |
final List<Type> capturedArgs = types.capture(argtypes); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
304 |
List<Type> actuals = capturedArgs; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
305 |
List<Type> actualsNoCapture = argtypes; |
10 | 306 |
// instantiate all polymorphic argument types and |
307 |
// set up lower bounds constraints for undetvars |
|
308 |
Type varargsFormal = useVarargs ? formals.last() : null; |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
309 |
while (actuals.nonEmpty() && formals.head != varargsFormal) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
310 |
Type formal = formals.head; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
311 |
Type actual = actuals.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
312 |
Type actualNoCapture = actualsNoCapture.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
313 |
if (actual.tag == FORALL) |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
314 |
actual = instantiateArg((ForAll)actual, formal, tvars, warn); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
315 |
Type undetFormal = types.subst(formal, tvars, undetvars); |
10 | 316 |
boolean works = allowBoxing |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
317 |
? types.isConvertible(actual, undetFormal, warn) |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
318 |
: types.isSubtypeUnchecked(actual, undetFormal, warn); |
10 | 319 |
if (!works) { |
320 |
throw unambiguousNoInstanceException |
|
321 |
.setMessage("no.conforming.assignment.exists", |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
322 |
tvars, actualNoCapture, formal); |
10 | 323 |
} |
324 |
formals = formals.tail; |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
325 |
actuals = actuals.tail; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
326 |
actualsNoCapture = actualsNoCapture.tail; |
10 | 327 |
} |
328 |
if (formals.head != varargsFormal || // not enough args |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
329 |
!useVarargs && actuals.nonEmpty()) { // too many args |
10 | 330 |
// argument lists differ in length |
331 |
throw unambiguousNoInstanceException |
|
332 |
.setMessage("arg.length.mismatch"); |
|
333 |
} |
|
334 |
||
335 |
// for varargs arguments as well |
|
336 |
if (useVarargs) { |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
337 |
Type elemType = types.elemtype(varargsFormal); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
338 |
Type elemUndet = types.subst(elemType, tvars, undetvars); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
339 |
while (actuals.nonEmpty()) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
340 |
Type actual = actuals.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
341 |
Type actualNoCapture = actualsNoCapture.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
342 |
if (actual.tag == FORALL) |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
343 |
actual = instantiateArg((ForAll)actual, elemType, tvars, warn); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
344 |
boolean works = types.isConvertible(actual, elemUndet, warn); |
10 | 345 |
if (!works) { |
346 |
throw unambiguousNoInstanceException |
|
347 |
.setMessage("no.conforming.assignment.exists", |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
348 |
tvars, actualNoCapture, elemType); |
10 | 349 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
350 |
actuals = actuals.tail; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
351 |
actualsNoCapture = actualsNoCapture.tail; |
10 | 352 |
} |
353 |
} |
|
354 |
||
355 |
// minimize as yet undetermined type variables |
|
356 |
for (Type t : undetvars) |
|
357 |
minimizeInst((UndetVar) t, warn); |
|
358 |
||
359 |
/** Type variables instantiated to bottom */ |
|
360 |
ListBuffer<Type> restvars = new ListBuffer<Type>(); |
|
361 |
||
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
362 |
/** Undet vars instantiated to bottom */ |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
363 |
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
|
364 |
|
10 | 365 |
/** Instantiated types or TypeVars if under-constrained */ |
366 |
ListBuffer<Type> insttypes = new ListBuffer<Type>(); |
|
367 |
||
368 |
/** Instantiated types or UndetVars if under-constrained */ |
|
369 |
ListBuffer<Type> undettypes = new ListBuffer<Type>(); |
|
370 |
||
371 |
for (Type t : undetvars) { |
|
372 |
UndetVar uv = (UndetVar)t; |
|
373 |
if (uv.inst.tag == BOT) { |
|
374 |
restvars.append(uv.qtype); |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
375 |
restundet.append(uv); |
10 | 376 |
insttypes.append(uv.qtype); |
377 |
undettypes.append(uv); |
|
378 |
uv.inst = null; |
|
379 |
} else { |
|
380 |
insttypes.append(uv.inst); |
|
381 |
undettypes.append(uv.inst); |
|
382 |
} |
|
383 |
} |
|
384 |
checkWithinBounds(tvars, undettypes.toList(), warn); |
|
385 |
||
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
386 |
mt = (MethodType)types.subst(mt, tvars, insttypes.toList()); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
387 |
|
10 | 388 |
if (!restvars.isEmpty()) { |
389 |
// if there are uninstantiated variables, |
|
390 |
// quantify result type with them |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
391 |
final List<Type> inferredTypes = insttypes.toList(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
392 |
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
|
393 |
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
|
394 |
mt2.restype = new ForAll(restvars.toList(), mt.restype) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
395 |
@Override |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
396 |
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
|
397 |
for (Type t : restundet.toList()) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
398 |
UndetVar uv = (UndetVar)t; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
399 |
if (uv.qtype == tv) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
400 |
switch (ck) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
401 |
case EXTENDS: return uv.hibounds; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
402 |
case SUPER: return uv.lobounds; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
403 |
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
|
404 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
405 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
406 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
407 |
return List.nil(); |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
408 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
409 |
|
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
410 |
@Override |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
411 |
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
|
412 |
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
|
413 |
if (!rs.argumentsAcceptable(capturedArgs, formals, |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
414 |
allowBoxing, useVarargs, warn)) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
415 |
// inferred method is not applicable |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
416 |
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
|
417 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
418 |
// 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
|
419 |
checkWithinBounds(all_tvars, |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
420 |
types.subst(inferredTypes, tvars, inferred), warn); |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
421 |
if (useVarargs) { |
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5520
diff
changeset
|
422 |
chk.checkVararg(env.tree.pos(), formals, msym, env); |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
423 |
} |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
424 |
return super.inst(inferred, types); |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
425 |
}}; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
426 |
return mt2; |
10 | 427 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
428 |
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
|
429 |
// inferred method is not applicable |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
430 |
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
|
431 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
432 |
else { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
433 |
// return instantiated version of method type |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
434 |
return mt; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
435 |
} |
10 | 436 |
} |
437 |
//where |
|
438 |
||
439 |
/** Try to instantiate argument type `that' to given type `to'. |
|
440 |
* If this fails, try to insantiate `that' to `to' where |
|
441 |
* every occurrence of a type variable in `tvars' is replaced |
|
442 |
* by an unknown type. |
|
443 |
*/ |
|
444 |
private Type instantiateArg(ForAll that, |
|
445 |
Type to, |
|
446 |
List<Type> tvars, |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
447 |
Warner warn) throws InferenceException { |
10 | 448 |
List<Type> targs; |
449 |
try { |
|
450 |
return instantiateExpr(that, to, warn); |
|
451 |
} catch (NoInstanceException ex) { |
|
452 |
Type to1 = to; |
|
453 |
for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) |
|
454 |
to1 = types.subst(to1, List.of(l.head), List.of(syms.unknownType)); |
|
455 |
return instantiateExpr(that, to1, warn); |
|
456 |
} |
|
457 |
} |
|
458 |
||
459 |
/** check that type parameters are within their bounds. |
|
460 |
*/ |
|
461 |
private void checkWithinBounds(List<Type> tvars, |
|
462 |
List<Type> arguments, |
|
463 |
Warner warn) |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
464 |
throws InvalidInstanceException { |
10 | 465 |
for (List<Type> tvs = tvars, args = arguments; |
466 |
tvs.nonEmpty(); |
|
467 |
tvs = tvs.tail, args = args.tail) { |
|
468 |
if (args.head instanceof UndetVar) continue; |
|
469 |
List<Type> bounds = types.subst(types.getBounds((TypeVar)tvs.head), tvars, arguments); |
|
470 |
if (!types.isSubtypeUnchecked(args.head, bounds, warn)) |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
471 |
throw invalidInstanceException |
10 | 472 |
.setMessage("inferred.do.not.conform.to.bounds", |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
473 |
args.head, bounds); |
10 | 474 |
} |
475 |
} |
|
476 |
} |