author | jjg |
Wed, 21 Sep 2011 21:56:53 -0700 | |
changeset 10637 | 2ea5fbb913ac |
parent 10628 | dca7012223bc |
child 10816 | ce8a7e9d8882 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
8229
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
2 |
* Copyright (c) 1999, 2011, 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; |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
30 |
import com.sun.tools.javac.tree.TreeInfo; |
10 | 31 |
import com.sun.tools.javac.util.*; |
32 |
import com.sun.tools.javac.util.List; |
|
33 |
import com.sun.tools.javac.code.*; |
|
34 |
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
|
35 |
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
|
36 |
import com.sun.tools.javac.code.Symbol.*; |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
37 |
import com.sun.tools.javac.util.JCDiagnostic; |
10 | 38 |
|
39 |
import static com.sun.tools.javac.code.TypeTags.*; |
|
40 |
||
41 |
/** Helper class for type parameter inference, used by the attribution phase. |
|
42 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
43 |
* <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
|
44 |
* If you write code that depends on this, you do so at your own risk. |
10 | 45 |
* This code and its internal interfaces are subject to change or |
46 |
* deletion without notice.</b> |
|
47 |
*/ |
|
48 |
public class Infer { |
|
49 |
protected static final Context.Key<Infer> inferKey = |
|
50 |
new Context.Key<Infer>(); |
|
51 |
||
52 |
/** A value for prototypes that admit any type, including polymorphic ones. */ |
|
53 |
public static final Type anyPoly = new Type(NONE, null); |
|
54 |
||
55 |
Symtab syms; |
|
56 |
Types types; |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
57 |
Check chk; |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
58 |
Resolve rs; |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
59 |
JCDiagnostic.Factory diags; |
10 | 60 |
|
61 |
public static Infer instance(Context context) { |
|
62 |
Infer instance = context.get(inferKey); |
|
63 |
if (instance == null) |
|
64 |
instance = new Infer(context); |
|
65 |
return instance; |
|
66 |
} |
|
67 |
||
68 |
protected Infer(Context context) { |
|
69 |
context.put(inferKey, this); |
|
70 |
syms = Symtab.instance(context); |
|
71 |
types = Types.instance(context); |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
72 |
rs = Resolve.instance(context); |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
73 |
chk = Check.instance(context); |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
74 |
diags = JCDiagnostic.Factory.instance(context); |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
75 |
ambiguousNoInstanceException = |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
76 |
new NoInstanceException(true, diags); |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
77 |
unambiguousNoInstanceException = |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
78 |
new NoInstanceException(false, diags); |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
79 |
invalidInstanceException = |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
80 |
new InvalidInstanceException(diags); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
81 |
|
10 | 82 |
} |
83 |
||
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
84 |
public static class InferenceException extends Resolve.InapplicableMethodException { |
10 | 85 |
private static final long serialVersionUID = 0; |
86 |
||
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
87 |
InferenceException(JCDiagnostic.Factory diags) { |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
88 |
super(diags); |
10 | 89 |
} |
3140
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 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
92 |
public static class NoInstanceException extends InferenceException { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
93 |
private static final long serialVersionUID = 1; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
94 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
95 |
boolean isAmbiguous; // exist several incomparable best instances? |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
96 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
97 |
NoInstanceException(boolean isAmbiguous, JCDiagnostic.Factory diags) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
98 |
super(diags); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
99 |
this.isAmbiguous = isAmbiguous; |
10 | 100 |
} |
101 |
} |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
102 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
103 |
public static class InvalidInstanceException extends InferenceException { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
104 |
private static final long serialVersionUID = 2; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
105 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
106 |
InvalidInstanceException(JCDiagnostic.Factory diags) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
107 |
super(diags); |
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 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
110 |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
111 |
private final NoInstanceException ambiguousNoInstanceException; |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
112 |
private final NoInstanceException unambiguousNoInstanceException; |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
113 |
private final InvalidInstanceException invalidInstanceException; |
10 | 114 |
|
115 |
/*************************************************************************** |
|
116 |
* Auxiliary type values and classes |
|
117 |
***************************************************************************/ |
|
118 |
||
119 |
/** A mapping that turns type variables into undetermined type variables. |
|
120 |
*/ |
|
121 |
Mapping fromTypeVarFun = new Mapping("fromTypeVarFun") { |
|
122 |
public Type apply(Type t) { |
|
123 |
if (t.tag == TYPEVAR) return new UndetVar(t); |
|
124 |
else return t.map(this); |
|
125 |
} |
|
126 |
}; |
|
127 |
||
128 |
/** A mapping that returns its type argument with every UndetVar replaced |
|
129 |
* by its `inst' field. Throws a NoInstanceException |
|
130 |
* 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
|
131 |
* Note: mutually referring undertvars will be left uninstantiated |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
132 |
* (that is, they will be replaced by the underlying type-variable). |
10 | 133 |
*/ |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
134 |
|
10 | 135 |
Mapping getInstFun = new Mapping("getInstFun") { |
136 |
public Type apply(Type t) { |
|
137 |
switch (t.tag) { |
|
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
138 |
case UNKNOWN: |
10 | 139 |
throw ambiguousNoInstanceException |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
140 |
.setMessage("undetermined.type"); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
141 |
case UNDETVAR: |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
142 |
UndetVar that = (UndetVar) t; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
143 |
if (that.inst == null) |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
144 |
throw ambiguousNoInstanceException |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
145 |
.setMessage("type.variable.has.undetermined.type", |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
146 |
that.qtype); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
147 |
return isConstraintCyclic(that) ? |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
148 |
that.qtype : |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
149 |
apply(that.inst); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
150 |
default: |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
151 |
return t.map(this); |
10 | 152 |
} |
153 |
} |
|
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
154 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
155 |
private boolean isConstraintCyclic(UndetVar uv) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
156 |
Types.UnaryVisitor<Boolean> constraintScanner = |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
157 |
new Types.UnaryVisitor<Boolean>() { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
158 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
159 |
List<Type> seen = List.nil(); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
160 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
161 |
Boolean visit(List<Type> ts) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
162 |
for (Type t : ts) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
163 |
if (visit(t)) return true; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
164 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
165 |
return false; |
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 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
168 |
public Boolean visitType(Type t, Void ignored) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
169 |
return false; |
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 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
172 |
@Override |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
173 |
public Boolean visitClassType(ClassType t, Void ignored) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
174 |
if (t.isCompound()) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
175 |
return visit(types.supertype(t)) || |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
176 |
visit(types.interfaces(t)); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
177 |
} else { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
178 |
return visit(t.getTypeArguments()); |
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 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
181 |
@Override |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
182 |
public Boolean visitWildcardType(WildcardType t, Void ignored) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
183 |
return visit(t.type); |
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 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
186 |
@Override |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
187 |
public Boolean visitUndetVar(UndetVar t, Void ignored) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
188 |
if (seen.contains(t)) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
189 |
return true; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
190 |
} else { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
191 |
seen = seen.prepend(t); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
192 |
return visit(t.inst); |
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 |
}; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
196 |
return constraintScanner.visit(uv); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
197 |
} |
10 | 198 |
}; |
199 |
||
200 |
/*************************************************************************** |
|
201 |
* Mini/Maximization of UndetVars |
|
202 |
***************************************************************************/ |
|
203 |
||
204 |
/** Instantiate undetermined type variable to its minimal upper bound. |
|
205 |
* Throw a NoInstanceException if this not possible. |
|
206 |
*/ |
|
207 |
void maximizeInst(UndetVar that, Warner warn) throws NoInstanceException { |
|
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
208 |
List<Type> hibounds = Type.filter(that.hibounds, errorFilter); |
10 | 209 |
if (that.inst == null) { |
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
210 |
if (hibounds.isEmpty()) |
10 | 211 |
that.inst = syms.objectType; |
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
212 |
else if (hibounds.tail.isEmpty()) |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
213 |
that.inst = hibounds.head; |
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
214 |
else |
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
215 |
that.inst = types.glb(hibounds); |
10 | 216 |
} |
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
217 |
if (that.inst == null || |
3139
01d76d311f8e
6835428: regression: return-type inference rejects valid code
mcimadamore
parents:
2212
diff
changeset
|
218 |
that.inst.isErroneous()) |
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
219 |
throw ambiguousNoInstanceException |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
220 |
.setMessage("no.unique.maximal.instance.exists", |
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
221 |
that.qtype, hibounds); |
10 | 222 |
} |
223 |
//where |
|
224 |
private boolean isSubClass(Type t, final List<Type> ts) { |
|
225 |
t = t.baseType(); |
|
226 |
if (t.tag == TYPEVAR) { |
|
227 |
List<Type> bounds = types.getBounds((TypeVar)t); |
|
228 |
for (Type s : ts) { |
|
229 |
if (!types.isSameType(t, s.baseType())) { |
|
230 |
for (Type bound : bounds) { |
|
231 |
if (!isSubClass(bound, List.of(s.baseType()))) |
|
232 |
return false; |
|
233 |
} |
|
234 |
} |
|
235 |
} |
|
236 |
} else { |
|
237 |
for (Type s : ts) { |
|
238 |
if (!t.tsym.isSubClass(s.baseType().tsym, types)) |
|
239 |
return false; |
|
240 |
} |
|
241 |
} |
|
242 |
return true; |
|
243 |
} |
|
244 |
||
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
245 |
private Filter<Type> errorFilter = new Filter<Type>() { |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
246 |
@Override |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
247 |
public boolean accepts(Type t) { |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
248 |
return !t.isErroneous(); |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
249 |
} |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
250 |
}; |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
251 |
|
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
|
252 |
/** Instantiate undetermined type variable to the lub of all its lower bounds. |
10 | 253 |
* Throw a NoInstanceException if this not possible. |
254 |
*/ |
|
255 |
void minimizeInst(UndetVar that, Warner warn) throws NoInstanceException { |
|
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
256 |
List<Type> lobounds = Type.filter(that.lobounds, errorFilter); |
10 | 257 |
if (that.inst == null) { |
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
258 |
if (lobounds.isEmpty()) |
10 | 259 |
that.inst = syms.botType; |
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
260 |
else if (lobounds.tail.isEmpty()) |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
261 |
that.inst = lobounds.head.isPrimitive() ? syms.errType : lobounds.head; |
10 | 262 |
else { |
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
263 |
that.inst = types.lub(lobounds); |
162
6620f2a8e265
6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents:
10
diff
changeset
|
264 |
} |
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
|
265 |
if (that.inst == null || that.inst.tag == ERROR) |
10 | 266 |
throw ambiguousNoInstanceException |
267 |
.setMessage("no.unique.minimal.instance.exists", |
|
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
268 |
that.qtype, lobounds); |
10 | 269 |
// VGJ: sort of inlined maximizeInst() below. Adding |
270 |
// bounds can cause lobounds that are above hibounds. |
|
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
271 |
List<Type> hibounds = Type.filter(that.hibounds, errorFilter); |
10628
dca7012223bc
7086601: Error message bug: cause for method mismatch is 'null'
mcimadamore
parents:
9720
diff
changeset
|
272 |
Type hb = null; |
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
273 |
if (hibounds.isEmpty()) |
10628
dca7012223bc
7086601: Error message bug: cause for method mismatch is 'null'
mcimadamore
parents:
9720
diff
changeset
|
274 |
hb = syms.objectType; |
dca7012223bc
7086601: Error message bug: cause for method mismatch is 'null'
mcimadamore
parents:
9720
diff
changeset
|
275 |
else if (hibounds.tail.isEmpty()) |
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
276 |
hb = hibounds.head; |
10628
dca7012223bc
7086601: Error message bug: cause for method mismatch is 'null'
mcimadamore
parents:
9720
diff
changeset
|
277 |
else |
dca7012223bc
7086601: Error message bug: cause for method mismatch is 'null'
mcimadamore
parents:
9720
diff
changeset
|
278 |
hb = types.glb(hibounds); |
10 | 279 |
if (hb == null || |
10628
dca7012223bc
7086601: Error message bug: cause for method mismatch is 'null'
mcimadamore
parents:
9720
diff
changeset
|
280 |
hb.isErroneous()) |
dca7012223bc
7086601: Error message bug: cause for method mismatch is 'null'
mcimadamore
parents:
9720
diff
changeset
|
281 |
throw ambiguousNoInstanceException |
dca7012223bc
7086601: Error message bug: cause for method mismatch is 'null'
mcimadamore
parents:
9720
diff
changeset
|
282 |
.setMessage("incompatible.upper.bounds", |
dca7012223bc
7086601: Error message bug: cause for method mismatch is 'null'
mcimadamore
parents:
9720
diff
changeset
|
283 |
that.qtype, hibounds); |
10 | 284 |
} |
285 |
} |
|
286 |
||
287 |
/*************************************************************************** |
|
288 |
* Exported Methods |
|
289 |
***************************************************************************/ |
|
290 |
||
291 |
/** Try to instantiate expression type `that' to given type `to'. |
|
292 |
* If a maximal instantiation exists which makes this type |
|
293 |
* a subtype of type `to', return the instantiated type. |
|
294 |
* If no instantiation exists, or if several incomparable |
|
295 |
* best instantiations exist throw a NoInstanceException. |
|
296 |
*/ |
|
297 |
public Type instantiateExpr(ForAll that, |
|
298 |
Type to, |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
299 |
Warner warn) throws InferenceException { |
10 | 300 |
List<Type> undetvars = Type.map(that.tvars, fromTypeVarFun); |
301 |
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
|
302 |
UndetVar uv = (UndetVar) l.head; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
303 |
TypeVar tv = (TypeVar)uv.qtype; |
10 | 304 |
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
|
305 |
for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS)) { |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
306 |
hibounds.append(types.subst(t, that.tvars, undetvars)); |
10 | 307 |
} |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
308 |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
309 |
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
|
310 |
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
|
311 |
uv.inst = inst.head; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
312 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
313 |
uv.hibounds = hibounds.toList(); |
10 | 314 |
} |
315 |
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
|
316 |
if (!types.isSubtype(qtype1, |
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
6934
diff
changeset
|
317 |
qtype1.tag == UNDETVAR ? types.boxedTypeOrType(to) : to)) { |
10 | 318 |
throw unambiguousNoInstanceException |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
319 |
.setMessage("infer.no.conforming.instance.exists", |
10 | 320 |
that.tvars, that.qtype, to); |
321 |
} |
|
322 |
for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) |
|
323 |
maximizeInst((UndetVar) l.head, warn); |
|
324 |
// System.out.println(" = " + qtype1.map(getInstFun));//DEBUG |
|
325 |
||
326 |
// check bounds |
|
327 |
List<Type> targs = Type.map(undetvars, getInstFun); |
|
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
328 |
if (Type.containsAny(targs, that.tvars)) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
329 |
//replace uninferred type-vars |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
330 |
targs = types.subst(targs, |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
331 |
that.tvars, |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
332 |
instaniateAsUninferredVars(undetvars, that.tvars)); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
333 |
} |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
334 |
return chk.checkType(warn.pos(), that.inst(targs, types), to); |
10 | 335 |
} |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
336 |
//where |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
337 |
private List<Type> instaniateAsUninferredVars(List<Type> undetvars, List<Type> tvars) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
338 |
ListBuffer<Type> new_targs = ListBuffer.lb(); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
339 |
//step 1 - create syntethic captured vars |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
340 |
for (Type t : undetvars) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
341 |
UndetVar uv = (UndetVar)t; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
342 |
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
|
343 |
new_targs = new_targs.append(newArg); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
344 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
345 |
//step 2 - replace synthetic vars in their bounds |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
346 |
for (Type t : new_targs.toList()) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
347 |
CapturedType ct = (CapturedType)t; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
348 |
ct.bound = types.subst(ct.bound, tvars, new_targs.toList()); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
349 |
WildcardType wt = new WildcardType(ct.bound, BoundKind.EXTENDS, syms.boundClass); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
350 |
ct.wildcard = wt; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
351 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
352 |
return new_targs.toList(); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
353 |
} |
10 | 354 |
|
355 |
/** Instantiate method type `mt' by finding instantiations of |
|
356 |
* `tvars' so that method can be applied to `argtypes'. |
|
357 |
*/ |
|
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
358 |
public Type instantiateMethod(final Env<AttrContext> env, |
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
359 |
List<Type> tvars, |
10 | 360 |
MethodType mt, |
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5520
diff
changeset
|
361 |
final Symbol msym, |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
362 |
final List<Type> argtypes, |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
363 |
final boolean allowBoxing, |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
364 |
final boolean useVarargs, |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
365 |
final Warner warn) throws InferenceException { |
10 | 366 |
//-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG |
367 |
List<Type> undetvars = Type.map(tvars, fromTypeVarFun); |
|
368 |
List<Type> formals = mt.argtypes; |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
369 |
//need to capture exactly once - otherwise subsequent |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
370 |
//applicability checks might fail |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
371 |
final List<Type> capturedArgs = types.capture(argtypes); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
372 |
List<Type> actuals = capturedArgs; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
373 |
List<Type> actualsNoCapture = argtypes; |
10 | 374 |
// instantiate all polymorphic argument types and |
375 |
// set up lower bounds constraints for undetvars |
|
376 |
Type varargsFormal = useVarargs ? formals.last() : null; |
|
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
377 |
if (varargsFormal == null && |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
378 |
actuals.size() != formals.size()) { |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
379 |
throw unambiguousNoInstanceException |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
380 |
.setMessage("infer.arg.length.mismatch"); |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
381 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
382 |
while (actuals.nonEmpty() && formals.head != varargsFormal) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
383 |
Type formal = formals.head; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
384 |
Type actual = actuals.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
385 |
Type actualNoCapture = actualsNoCapture.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
386 |
if (actual.tag == FORALL) |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
387 |
actual = instantiateArg((ForAll)actual, formal, tvars, warn); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
388 |
Type undetFormal = types.subst(formal, tvars, undetvars); |
10 | 389 |
boolean works = allowBoxing |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
390 |
? types.isConvertible(actual, undetFormal, warn) |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
391 |
: types.isSubtypeUnchecked(actual, undetFormal, warn); |
10 | 392 |
if (!works) { |
393 |
throw unambiguousNoInstanceException |
|
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
394 |
.setMessage("infer.no.conforming.assignment.exists", |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
395 |
tvars, actualNoCapture, formal); |
10 | 396 |
} |
397 |
formals = formals.tail; |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
398 |
actuals = actuals.tail; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
399 |
actualsNoCapture = actualsNoCapture.tail; |
10 | 400 |
} |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
401 |
|
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
402 |
if (formals.head != varargsFormal) // not enough args |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
403 |
throw unambiguousNoInstanceException.setMessage("infer.arg.length.mismatch"); |
10 | 404 |
|
405 |
// for varargs arguments as well |
|
406 |
if (useVarargs) { |
|
9720
bc06a797f393
7042566: Regression: new ambiguity between varargs method
mcimadamore
parents:
8616
diff
changeset
|
407 |
Type elemType = types.elemtype(varargsFormal); |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
408 |
Type elemUndet = types.subst(elemType, tvars, undetvars); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
409 |
while (actuals.nonEmpty()) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
410 |
Type actual = actuals.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
411 |
Type actualNoCapture = actualsNoCapture.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
412 |
if (actual.tag == FORALL) |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
413 |
actual = instantiateArg((ForAll)actual, elemType, tvars, warn); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
414 |
boolean works = types.isConvertible(actual, elemUndet, warn); |
10 | 415 |
if (!works) { |
416 |
throw unambiguousNoInstanceException |
|
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
417 |
.setMessage("infer.no.conforming.assignment.exists", |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
418 |
tvars, actualNoCapture, elemType); |
10 | 419 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
420 |
actuals = actuals.tail; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
421 |
actualsNoCapture = actualsNoCapture.tail; |
10 | 422 |
} |
423 |
} |
|
424 |
||
425 |
// minimize as yet undetermined type variables |
|
426 |
for (Type t : undetvars) |
|
427 |
minimizeInst((UndetVar) t, warn); |
|
428 |
||
429 |
/** Type variables instantiated to bottom */ |
|
430 |
ListBuffer<Type> restvars = new ListBuffer<Type>(); |
|
431 |
||
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
432 |
/** Undet vars instantiated to bottom */ |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
433 |
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
|
434 |
|
10 | 435 |
/** Instantiated types or TypeVars if under-constrained */ |
436 |
ListBuffer<Type> insttypes = new ListBuffer<Type>(); |
|
437 |
||
438 |
/** Instantiated types or UndetVars if under-constrained */ |
|
439 |
ListBuffer<Type> undettypes = new ListBuffer<Type>(); |
|
440 |
||
441 |
for (Type t : undetvars) { |
|
442 |
UndetVar uv = (UndetVar)t; |
|
443 |
if (uv.inst.tag == BOT) { |
|
444 |
restvars.append(uv.qtype); |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
445 |
restundet.append(uv); |
10 | 446 |
insttypes.append(uv.qtype); |
447 |
undettypes.append(uv); |
|
448 |
uv.inst = null; |
|
449 |
} else { |
|
450 |
insttypes.append(uv.inst); |
|
451 |
undettypes.append(uv.inst); |
|
452 |
} |
|
453 |
} |
|
454 |
checkWithinBounds(tvars, undettypes.toList(), warn); |
|
455 |
||
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
456 |
mt = (MethodType)types.subst(mt, tvars, insttypes.toList()); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
457 |
|
10 | 458 |
if (!restvars.isEmpty()) { |
459 |
// if there are uninstantiated variables, |
|
460 |
// quantify result type with them |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
461 |
final List<Type> inferredTypes = insttypes.toList(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
462 |
final List<Type> all_tvars = tvars; //this is the wrong tvars |
8616
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
463 |
return new UninferredMethodType(mt, restvars.toList()) { |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
464 |
@Override |
8616
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
465 |
List<Type> getConstraints(TypeVar tv, ConstraintKind ck) { |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
466 |
for (Type t : restundet.toList()) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
467 |
UndetVar uv = (UndetVar)t; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
468 |
if (uv.qtype == tv) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
469 |
switch (ck) { |
6154
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
5847
diff
changeset
|
470 |
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
|
471 |
case SUPER: return uv.lobounds; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
472 |
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
|
473 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
474 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
475 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
476 |
return List.nil(); |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
477 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
478 |
@Override |
8616
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
479 |
void check(List<Type> inferred, Types types) throws NoInstanceException { |
8229
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
480 |
// check that actuals conform to inferred formals |
8616
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
481 |
checkArgumentsAcceptable(env, capturedArgs, getParameterTypes(), allowBoxing, useVarargs, warn); |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
482 |
// 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
|
483 |
checkWithinBounds(all_tvars, |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
484 |
types.subst(inferredTypes, tvars, inferred), warn); |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
485 |
if (useVarargs) { |
8616
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
486 |
chk.checkVararg(env.tree.pos(), getParameterTypes(), msym); |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
487 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
488 |
}}; |
10 | 489 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
490 |
else { |
8229
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
491 |
// check that actuals conform to inferred formals |
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
492 |
checkArgumentsAcceptable(env, capturedArgs, mt.getParameterTypes(), allowBoxing, useVarargs, warn); |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
493 |
// return instantiated version of method type |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
494 |
return mt; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
495 |
} |
10 | 496 |
} |
497 |
//where |
|
498 |
||
8616
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
499 |
/** |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
500 |
* A delegated type representing a partially uninferred method type. |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
501 |
* The return type of a partially uninferred method type is a ForAll |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
502 |
* type - when the return type is instantiated (see Infer.instantiateExpr) |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
503 |
* the underlying method type is also updated. |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
504 |
*/ |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
505 |
static abstract class UninferredMethodType extends DelegatedType { |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
506 |
|
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
507 |
final List<Type> tvars; |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
508 |
|
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
509 |
public UninferredMethodType(MethodType mtype, List<Type> tvars) { |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
510 |
super(METHOD, new MethodType(mtype.argtypes, null, mtype.thrown, mtype.tsym)); |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
511 |
this.tvars = tvars; |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
512 |
asMethodType().restype = new UninferredReturnType(tvars, mtype.restype); |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
513 |
} |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
514 |
|
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
515 |
@Override |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
516 |
public MethodType asMethodType() { |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
517 |
return qtype.asMethodType(); |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
518 |
} |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
519 |
|
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
520 |
@Override |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
521 |
public Type map(Mapping f) { |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
522 |
return qtype.map(f); |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
523 |
} |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
524 |
|
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
525 |
void instantiateReturnType(Type restype, List<Type> inferred, Types types) throws NoInstanceException { |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
526 |
//update method type with newly inferred type-arguments |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
527 |
qtype = new MethodType(types.subst(getParameterTypes(), tvars, inferred), |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
528 |
restype, |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
529 |
types.subst(UninferredMethodType.this.getThrownTypes(), tvars, inferred), |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
530 |
UninferredMethodType.this.qtype.tsym); |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
531 |
check(inferred, types); |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
532 |
} |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
533 |
|
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
534 |
abstract void check(List<Type> inferred, Types types) throws NoInstanceException; |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
535 |
|
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
536 |
abstract List<Type> getConstraints(TypeVar tv, ConstraintKind ck); |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
537 |
|
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
538 |
class UninferredReturnType extends ForAll { |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
539 |
public UninferredReturnType(List<Type> tvars, Type restype) { |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
540 |
super(tvars, restype); |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
541 |
} |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
542 |
@Override |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
543 |
public Type inst(List<Type> actuals, Types types) { |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
544 |
Type newRestype = super.inst(actuals, types); |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
545 |
instantiateReturnType(newRestype, actuals, types); |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
546 |
return newRestype; |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
547 |
} |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
548 |
@Override |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
549 |
public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) { |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
550 |
return UninferredMethodType.this.getConstraints(tv, ck); |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
551 |
} |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
552 |
} |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
553 |
} |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
554 |
|
8229
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
555 |
private void checkArgumentsAcceptable(Env<AttrContext> env, List<Type> actuals, List<Type> formals, |
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
556 |
boolean allowBoxing, boolean useVarargs, Warner warn) { |
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
557 |
try { |
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
558 |
rs.checkRawArgumentsAcceptable(env, actuals, formals, |
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
559 |
allowBoxing, useVarargs, warn); |
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
560 |
} |
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
561 |
catch (Resolve.InapplicableMethodException ex) { |
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
562 |
// inferred method is not applicable |
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
563 |
throw invalidInstanceException.setMessage(ex.getDiagnostic()); |
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
564 |
} |
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
565 |
} |
39266c1b1b0e
6313164: javac generates code that fails byte code verification for the varargs feature
mcimadamore
parents:
8048
diff
changeset
|
566 |
|
8616
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
567 |
/** Try to instantiate argument type `that' to given type `to'. |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
568 |
* If this fails, try to insantiate `that' to `to' where |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
569 |
* every occurrence of a type variable in `tvars' is replaced |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
570 |
* by an unknown type. |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
571 |
*/ |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
572 |
private Type instantiateArg(ForAll that, |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
573 |
Type to, |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
574 |
List<Type> tvars, |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
575 |
Warner warn) throws InferenceException { |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
576 |
List<Type> targs; |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
577 |
try { |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
578 |
return instantiateExpr(that, to, warn); |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
579 |
} catch (NoInstanceException ex) { |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
580 |
Type to1 = to; |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
581 |
for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
582 |
to1 = types.subst(to1, List.of(l.head), List.of(syms.unknownType)); |
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
583 |
return instantiateExpr(that, to1, warn); |
10 | 584 |
} |
8616
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
585 |
} |
10 | 586 |
|
587 |
/** check that type parameters are within their bounds. |
|
588 |
*/ |
|
6154
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
5847
diff
changeset
|
589 |
void checkWithinBounds(List<Type> tvars, |
10 | 590 |
List<Type> arguments, |
591 |
Warner warn) |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
592 |
throws InvalidInstanceException { |
10 | 593 |
for (List<Type> tvs = tvars, args = arguments; |
594 |
tvs.nonEmpty(); |
|
595 |
tvs = tvs.tail, args = args.tail) { |
|
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
596 |
if (args.head instanceof UndetVar || |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8036
diff
changeset
|
597 |
tvars.head.getUpperBound().isErroneous()) continue; |
10 | 598 |
List<Type> bounds = types.subst(types.getBounds((TypeVar)tvs.head), tvars, arguments); |
599 |
if (!types.isSubtypeUnchecked(args.head, bounds, warn)) |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
600 |
throw invalidInstanceException |
10 | 601 |
.setMessage("inferred.do.not.conform.to.bounds", |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
602 |
args.head, bounds); |
10 | 603 |
} |
604 |
} |
|
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
605 |
|
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
606 |
/** |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
607 |
* Compute a synthetic method type corresponding to the requested polymorphic |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
608 |
* method signature. The target return type is computed from the immediately |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
609 |
* enclosing scope surrounding the polymorphic-signature call. |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
610 |
*/ |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
611 |
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
|
612 |
Name name, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
613 |
MethodSymbol spMethod, // sig. poly. method or null if none |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
614 |
List<Type> argtypes) { |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
615 |
final Type restype; |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
616 |
|
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
617 |
//The return type for a polymorphic signature call is computed from |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
618 |
//the enclosing tree E, as follows: if E is a cast, then use the |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
619 |
//target type of the cast expression as a return type; if E is an |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
620 |
//expression statement, the return type is 'void' - otherwise the |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
621 |
//return type is simply 'Object'. A correctness check ensures that |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
622 |
//env.next refers to the lexically enclosing environment in which |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
623 |
//the polymorphic signature call environment is nested. |
6934
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
624 |
|
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
625 |
switch (env.next.tree.getTag()) { |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
626 |
case JCTree.TYPECAST: |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
627 |
JCTypeCast castTree = (JCTypeCast)env.next.tree; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
628 |
restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ? |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
629 |
castTree.clazz.type : |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
630 |
syms.objectType; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
631 |
break; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
632 |
case JCTree.EXEC: |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
633 |
JCTree.JCExpressionStatement execTree = |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
634 |
(JCTree.JCExpressionStatement)env.next.tree; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
635 |
restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ? |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
636 |
syms.voidType : |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
637 |
syms.objectType; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
638 |
break; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
639 |
default: |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
640 |
restype = syms.objectType; |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
641 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
642 |
|
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
643 |
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
|
644 |
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
|
645 |
spMethod.getThrownTypes() : |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
646 |
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
|
647 |
|
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
648 |
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
|
649 |
restype, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
650 |
exType, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
651 |
syms.methodClass); |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
652 |
return mtype; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
653 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
654 |
//where |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
655 |
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
|
656 |
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
|
657 |
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
|
658 |
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
|
659 |
// 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
|
660 |
// 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
|
661 |
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
|
662 |
return t; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
663 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
664 |
}; |
8616
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
665 |
} |