author | mcimadamore |
Tue, 15 Sep 2015 13:43:44 +0100 | |
changeset 32709 | 55d136799f79 |
parent 31937 | ad43a6639c4a |
child 32915 | eb290482d42f |
permissions | -rw-r--r-- |
10 | 1 |
/* |
30014
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
2 |
* Copyright (c) 1999, 2015, 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.*; |
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
32 |
import com.sun.tools.javac.util.GraphUtils.DottableNode; |
15717 | 33 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
10 | 34 |
import com.sun.tools.javac.util.List; |
15717 | 35 |
import com.sun.tools.javac.code.*; |
36 |
import com.sun.tools.javac.code.Type.*; |
|
37 |
import com.sun.tools.javac.code.Type.UndetVar.InferenceBound; |
|
38 |
import com.sun.tools.javac.code.Symbol.*; |
|
39 |
import com.sun.tools.javac.comp.DeferredAttr.AttrMode; |
|
32709
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
40 |
import com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext; |
15717 | 41 |
import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph; |
42 |
import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph.Node; |
|
43 |
import com.sun.tools.javac.comp.Resolve.InapplicableMethodException; |
|
44 |
import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode; |
|
45 |
||
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
46 |
import java.io.File; |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
47 |
import java.io.FileWriter; |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
48 |
import java.io.IOException; |
15717 | 49 |
import java.util.ArrayList; |
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
50 |
import java.util.Collection; |
15717 | 51 |
import java.util.Collections; |
19914 | 52 |
import java.util.EnumMap; |
15717 | 53 |
import java.util.EnumSet; |
19914 | 54 |
import java.util.HashMap; |
15717 | 55 |
import java.util.HashSet; |
19914 | 56 |
import java.util.LinkedHashSet; |
57 |
import java.util.Map; |
|
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
58 |
import java.util.Properties; |
19914 | 59 |
import java.util.Set; |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
60 |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
61 |
import static com.sun.tools.javac.code.TypeTag.*; |
10 | 62 |
|
63 |
/** Helper class for type parameter inference, used by the attribution phase. |
|
64 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
65 |
* <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
|
66 |
* If you write code that depends on this, you do so at your own risk. |
10 | 67 |
* This code and its internal interfaces are subject to change or |
68 |
* deletion without notice.</b> |
|
69 |
*/ |
|
70 |
public class Infer { |
|
22163 | 71 |
protected static final Context.Key<Infer> inferKey = new Context.Key<>(); |
10 | 72 |
|
15717 | 73 |
Resolve rs; |
74 |
Check chk; |
|
10 | 75 |
Symtab syms; |
76 |
Types types; |
|
15717 | 77 |
JCDiagnostic.Factory diags; |
10816
ce8a7e9d8882
7098660: Write better overload resolution/inference tests
mcimadamore
parents:
10628
diff
changeset
|
78 |
Log log; |
10 | 79 |
|
15717 | 80 |
/** should the graph solver be used? */ |
81 |
boolean allowGraphInference; |
|
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
82 |
|
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
83 |
/** |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
84 |
* folder in which the inference dependency graphs should be written. |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
85 |
*/ |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
86 |
final private String dependenciesFolder; |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
87 |
|
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
88 |
/** |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
89 |
* List of graphs awaiting to be dumped to a file. |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
90 |
*/ |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
91 |
private List<String> pendingGraphs; |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
92 |
|
10 | 93 |
public static Infer instance(Context context) { |
94 |
Infer instance = context.get(inferKey); |
|
95 |
if (instance == null) |
|
96 |
instance = new Infer(context); |
|
97 |
return instance; |
|
98 |
} |
|
99 |
||
100 |
protected Infer(Context context) { |
|
101 |
context.put(inferKey, this); |
|
15717 | 102 |
|
103 |
rs = Resolve.instance(context); |
|
104 |
chk = Check.instance(context); |
|
10 | 105 |
syms = Symtab.instance(context); |
106 |
types = Types.instance(context); |
|
15717 | 107 |
diags = JCDiagnostic.Factory.instance(context); |
10816
ce8a7e9d8882
7098660: Write better overload resolution/inference tests
mcimadamore
parents:
10628
diff
changeset
|
108 |
log = Log.instance(context); |
13440
1ea85052f98d
7187104: Inference cleanup: remove redundant exception classes in Infer.java
mcimadamore
parents:
13438
diff
changeset
|
109 |
inferenceException = new InferenceException(diags); |
15717 | 110 |
Options options = Options.instance(context); |
111 |
allowGraphInference = Source.instance(context).allowGraphInference() |
|
112 |
&& options.isUnset("useLegacyInference"); |
|
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
113 |
dependenciesFolder = options.get("dumpInferenceGraphsTo"); |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
114 |
pendingGraphs = List.nil(); |
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
115 |
|
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
116 |
emptyContext = new InferenceContext(this, List.<Type>nil()); |
10 | 117 |
} |
118 |
||
15717 | 119 |
/** A value for prototypes that admit any type, including polymorphic ones. */ |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18395
diff
changeset
|
120 |
public static final Type anyPoly = new JCNoType(); |
15717 | 121 |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
122 |
/** |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
123 |
* This exception class is design to store a list of diagnostics corresponding |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
124 |
* to inference errors that can arise during a method applicability check. |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
125 |
*/ |
11707
532f41763bc9
7129801: Merge the two method applicability routines
mcimadamore
parents:
11550
diff
changeset
|
126 |
public static class InferenceException extends InapplicableMethodException { |
10 | 127 |
private static final long serialVersionUID = 0; |
128 |
||
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
129 |
List<JCDiagnostic> messages = List.nil(); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
130 |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
131 |
InferenceException(JCDiagnostic.Factory diags) { |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
132 |
super(diags); |
10 | 133 |
} |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
134 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
135 |
@Override |
19914 | 136 |
InapplicableMethodException setMessage() { |
137 |
//no message to set |
|
138 |
return this; |
|
139 |
} |
|
140 |
||
141 |
@Override |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
142 |
InapplicableMethodException setMessage(JCDiagnostic diag) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
143 |
messages = messages.append(diag); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
144 |
return this; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
145 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
146 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
147 |
@Override |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
148 |
public JCDiagnostic getDiagnostic() { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
149 |
return messages.head; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
150 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
151 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
152 |
void clear() { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
153 |
messages = List.nil(); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
154 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
155 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
156 |
|
15717 | 157 |
protected final InferenceException inferenceException; |
10 | 158 |
|
15717 | 159 |
// <editor-fold defaultstate="collapsed" desc="Inference routines"> |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
160 |
/** |
15717 | 161 |
* Main inference entry point - instantiate a generic method type |
162 |
* using given argument types and (possibly) an expected target-type. |
|
10 | 163 |
*/ |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
164 |
Type instantiateMethod( Env<AttrContext> env, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
165 |
List<Type> tvars, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
166 |
MethodType mt, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
167 |
Attr.ResultInfo resultInfo, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
168 |
MethodSymbol msym, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
169 |
List<Type> argtypes, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
170 |
boolean allowBoxing, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
171 |
boolean useVarargs, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
172 |
Resolve.MethodResolutionContext resolveContext, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
173 |
Warner warn) throws InferenceException { |
10 | 174 |
//-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG |
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
175 |
final InferenceContext inferenceContext = new InferenceContext(this, tvars); //B0 |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
176 |
inferenceException.clear(); |
15717 | 177 |
try { |
178 |
DeferredAttr.DeferredAttrContext deferredAttrContext = |
|
18910
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
179 |
resolveContext.deferredAttrContext(msym, inferenceContext, resultInfo, warn); |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
180 |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
181 |
resolveContext.methodCheck.argumentsAcceptable(env, deferredAttrContext, //B2 |
15717 | 182 |
argtypes, mt.getParameterTypes(), warn); |
32709
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
183 |
|
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
184 |
if (allowGraphInference && resultInfo != null && resultInfo.pt == anyPoly) { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
185 |
//we are inside method attribution - just return a partially inferred type |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
186 |
return new PartiallyInferredMethodType(mt, inferenceContext, env, warn); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
187 |
} else if (allowGraphInference && |
15717 | 188 |
resultInfo != null && |
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
189 |
!warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) { |
15717 | 190 |
//inject return constraints earlier |
191 |
checkWithinBounds(inferenceContext, warn); //propagation |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
192 |
Type newRestype = generateReturnConstraints(env.tree, resultInfo, //B3 |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
193 |
mt, inferenceContext); |
18911 | 194 |
mt = (MethodType)types.createMethodTypeWithReturn(mt, newRestype); |
15717 | 195 |
//propagate outwards if needed |
196 |
if (resultInfo.checkContext.inferenceContext().free(resultInfo.pt)) { |
|
197 |
//propagate inference context outwards and exit |
|
198 |
inferenceContext.dupTo(resultInfo.checkContext.inferenceContext()); |
|
199 |
deferredAttrContext.complete(); |
|
200 |
return mt; |
|
201 |
} |
|
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
202 |
} |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
203 |
|
15037
8ba16ac61b1a
8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents:
14547
diff
changeset
|
204 |
deferredAttrContext.complete(); |
10 | 205 |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
206 |
// minimize as yet undetermined type variables |
15717 | 207 |
if (allowGraphInference) { |
208 |
inferenceContext.solve(warn); |
|
209 |
} else { |
|
210 |
inferenceContext.solveLegacy(true, warn, LegacyInferenceSteps.EQ_LOWER.steps); //minimizeInst |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
211 |
} |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
212 |
|
15705 | 213 |
mt = (MethodType)inferenceContext.asInstType(mt); |
10 | 214 |
|
15717 | 215 |
if (!allowGraphInference && |
216 |
inferenceContext.restvars().nonEmpty() && |
|
217 |
resultInfo != null && |
|
218 |
!warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) { |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
219 |
generateReturnConstraints(env.tree, resultInfo, mt, inferenceContext); |
15717 | 220 |
inferenceContext.solveLegacy(false, warn, LegacyInferenceSteps.EQ_UPPER.steps); //maximizeInst |
221 |
mt = (MethodType)inferenceContext.asInstType(mt); |
|
222 |
} |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
223 |
|
15717 | 224 |
if (resultInfo != null && rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) { |
225 |
log.note(env.tree.pos, "deferred.method.inst", msym, mt, resultInfo.pt); |
|
12915
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
226 |
} |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
227 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
228 |
// return instantiated version of method type |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
229 |
return mt; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
230 |
} finally { |
15717 | 231 |
if (resultInfo != null || !allowGraphInference) { |
232 |
inferenceContext.notifyChange(); |
|
233 |
} else { |
|
234 |
inferenceContext.notifyChange(inferenceContext.boundedVars()); |
|
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
14047
diff
changeset
|
235 |
} |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
236 |
if (resultInfo == null) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
237 |
/* if the is no result info then we can clear the capture types |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
238 |
* cache without affecting any result info check |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
239 |
*/ |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
240 |
inferenceContext.captureTypeCache.clear(); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
241 |
} |
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
242 |
dumpGraphsIfNeeded(env.tree, msym, resolveContext); |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
243 |
} |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
244 |
} |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
245 |
|
32709
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
246 |
/** |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
247 |
* A partially infered method/constructor type; such a type can be checked multiple times |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
248 |
* against different targets. |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
249 |
*/ |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
250 |
public class PartiallyInferredMethodType extends MethodType { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
251 |
public PartiallyInferredMethodType(MethodType mtype, InferenceContext inferenceContext, Env<AttrContext> env, Warner warn) { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
252 |
super(mtype.getParameterTypes(), mtype.getReturnType(), mtype.getThrownTypes(), mtype.tsym); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
253 |
this.inferenceContext = inferenceContext; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
254 |
this.env = env; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
255 |
this.warn = warn; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
256 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
257 |
|
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
258 |
/** The inference context. */ |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
259 |
final InferenceContext inferenceContext; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
260 |
|
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
261 |
/** The attribution environment. */ |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
262 |
Env<AttrContext> env; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
263 |
|
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
264 |
/** The warner. */ |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
265 |
final Warner warn; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
266 |
|
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
267 |
@Override |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
268 |
public boolean isPartial() { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
269 |
return true; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
270 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
271 |
|
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
272 |
/** |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
273 |
* Checks this type against a target; this means generating return type constraints, solve |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
274 |
* and then roll back the results (to avoid poolluting the context). |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
275 |
*/ |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
276 |
Type check(Attr.ResultInfo resultInfo) { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
277 |
Warner noWarnings = new Warner(null); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
278 |
inferenceException.clear(); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
279 |
List<Type> saved_undet = null; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
280 |
try { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
281 |
/** we need to save the inference context before generating target type constraints. |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
282 |
* This constraints may pollute the inference context and make it useless in case we |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
283 |
* need to use it several times: with several targets. |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
284 |
*/ |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
285 |
saved_undet = inferenceContext.save(); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
286 |
if (allowGraphInference && !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
287 |
//inject return constraints earlier |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
288 |
checkWithinBounds(inferenceContext, noWarnings); //propagation |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
289 |
Type res = generateReturnConstraints(env.tree, resultInfo, //B3 |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
290 |
this, inferenceContext); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
291 |
|
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
292 |
if (resultInfo.checkContext.inferenceContext().free(resultInfo.pt)) { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
293 |
//propagate inference context outwards and exit |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
294 |
inferenceContext.dupTo(resultInfo.checkContext.inferenceContext(), |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
295 |
resultInfo.checkContext.deferredAttrContext().insideOverloadPhase()); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
296 |
return res; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
297 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
298 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
299 |
inferenceContext.solve(noWarnings); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
300 |
return inferenceContext.asInstType(this).getReturnType(); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
301 |
} catch (InferenceException ex) { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
302 |
resultInfo.checkContext.report(null, ex.getDiagnostic()); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
303 |
Assert.error(); //cannot get here (the above should throw) |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
304 |
return null; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
305 |
} finally { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
306 |
if (saved_undet != null) { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
307 |
inferenceContext.rollback(saved_undet); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
308 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
309 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
310 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
311 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
312 |
|
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
313 |
private void dumpGraphsIfNeeded(DiagnosticPosition pos, Symbol msym, Resolve.MethodResolutionContext rsContext) { |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
314 |
int round = 0; |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
315 |
try { |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
316 |
for (String graph : pendingGraphs.reverse()) { |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
317 |
Assert.checkNonNull(dependenciesFolder); |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
318 |
Name name = msym.name == msym.name.table.names.init ? |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
319 |
msym.owner.name : msym.name; |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
320 |
String filename = String.format("%s@%s[mode=%s,step=%s]_%d.dot", |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
321 |
name, |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
322 |
pos.getStartPosition(), |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
323 |
rsContext.attrMode(), |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
324 |
rsContext.step, |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
325 |
round); |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
326 |
File dotFile = new File(dependenciesFolder, filename); |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
327 |
try (FileWriter fw = new FileWriter(dotFile)) { |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
328 |
fw.append(graph); |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
329 |
} |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
330 |
round++; |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
331 |
} |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
332 |
} catch (IOException ex) { |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
333 |
Assert.error("Error occurred when dumping inference graph: " + ex.getMessage()); |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
334 |
} finally { |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
335 |
pendingGraphs = List.nil(); |
10 | 336 |
} |
8616
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
337 |
} |
10 | 338 |
|
15717 | 339 |
/** |
340 |
* Generate constraints from the generic method's return type. If the method |
|
341 |
* call occurs in a context where a type T is expected, use the expected |
|
342 |
* type to derive more constraints on the generic method inference variables. |
|
343 |
*/ |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
344 |
Type generateReturnConstraints(JCTree tree, Attr.ResultInfo resultInfo, |
15717 | 345 |
MethodType mt, InferenceContext inferenceContext) { |
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
346 |
InferenceContext rsInfoInfContext = resultInfo.checkContext.inferenceContext(); |
18911 | 347 |
Type from = mt.getReturnType(); |
348 |
if (mt.getReturnType().containsAny(inferenceContext.inferencevars) && |
|
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
349 |
rsInfoInfContext != emptyContext) { |
18911 | 350 |
from = types.capture(from); |
351 |
//add synthetic captured ivars |
|
352 |
for (Type t : from.getTypeArguments()) { |
|
353 |
if (t.hasTag(TYPEVAR) && ((TypeVar)t).isCaptured()) { |
|
354 |
inferenceContext.addVar((TypeVar)t); |
|
355 |
} |
|
356 |
} |
|
357 |
} |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
358 |
Type qtype = inferenceContext.asUndetVar(from); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
359 |
Type to = resultInfo.pt; |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
360 |
|
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
361 |
if (qtype.hasTag(VOID)) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
362 |
to = syms.voidType; |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
363 |
} else if (to.hasTag(NONE)) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
364 |
to = from.isPrimitive() ? from : syms.objectType; |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
365 |
} else if (qtype.hasTag(UNDETVAR)) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
366 |
if (resultInfo.pt.isReference()) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
367 |
to = generateReturnConstraintsUndetVarToReference( |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
368 |
tree, (UndetVar)qtype, to, resultInfo, inferenceContext); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
369 |
} else { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
370 |
if (to.isPrimitive()) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
371 |
to = generateReturnConstraintsPrimitive(tree, (UndetVar)qtype, to, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
372 |
resultInfo, inferenceContext); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
373 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
374 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
375 |
} |
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
376 |
Assert.check(allowGraphInference || !rsInfoInfContext.free(to), |
15717 | 377 |
"legacy inference engine cannot handle constraints on both sides of a subtyping assertion"); |
378 |
//we need to skip capture? |
|
379 |
Warner retWarn = new Warner(); |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
380 |
if (!resultInfo.checkContext.compatible(qtype, rsInfoInfContext.asUndetVar(to), retWarn) || |
18008
6d75e3886bac
8015505: Spurious inference error when return type of generic method requires unchecked conversion to target
mcimadamore
parents:
16967
diff
changeset
|
381 |
//unchecked conversion is not allowed in source 7 mode |
6d75e3886bac
8015505: Spurious inference error when return type of generic method requires unchecked conversion to target
mcimadamore
parents:
16967
diff
changeset
|
382 |
(!allowGraphInference && retWarn.hasLint(Lint.LintCategory.UNCHECKED))) { |
15717 | 383 |
throw inferenceException |
384 |
.setMessage("infer.no.conforming.instance.exists", |
|
385 |
inferenceContext.restvars(), mt.getReturnType(), to); |
|
386 |
} |
|
18911 | 387 |
return from; |
15717 | 388 |
} |
18910
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
389 |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
390 |
private Type generateReturnConstraintsPrimitive(JCTree tree, UndetVar from, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
391 |
Type to, Attr.ResultInfo resultInfo, InferenceContext inferenceContext) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
392 |
if (!allowGraphInference) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
393 |
//if legacy, just return boxed type |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
394 |
return types.boxedClass(to).type; |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
395 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
396 |
//if graph inference we need to skip conflicting boxed bounds... |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
397 |
for (Type t : from.getBounds(InferenceBound.EQ, InferenceBound.UPPER, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
398 |
InferenceBound.LOWER)) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
399 |
Type boundAsPrimitive = types.unboxedType(t); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
400 |
if (boundAsPrimitive == null || boundAsPrimitive.hasTag(NONE)) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
401 |
continue; |
18910
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
402 |
} |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
403 |
return generateReferenceToTargetConstraint(tree, from, to, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
404 |
resultInfo, inferenceContext); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
405 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
406 |
return types.boxedClass(to).type; |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
407 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
408 |
|
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
409 |
private Type generateReturnConstraintsUndetVarToReference(JCTree tree, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
410 |
UndetVar from, Type to, Attr.ResultInfo resultInfo, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
411 |
InferenceContext inferenceContext) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
412 |
Type captureOfTo = types.capture(to); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
413 |
/* T is a reference type, but is not a wildcard-parameterized type, and either |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
414 |
*/ |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
415 |
if (captureOfTo == to) { //not a wildcard parameterized type |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
416 |
/* i) B2 contains a bound of one of the forms alpha = S or S <: alpha, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
417 |
* where S is a wildcard-parameterized type, or |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
418 |
*/ |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
419 |
for (Type t : from.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
420 |
Type captureOfBound = types.capture(t); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
421 |
if (captureOfBound != t) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
422 |
return generateReferenceToTargetConstraint(tree, from, to, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
423 |
resultInfo, inferenceContext); |
15717 | 424 |
} |
12468
1100643c0209
7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents:
12335
diff
changeset
|
425 |
} |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
426 |
|
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
427 |
/* ii) B2 contains two bounds of the forms S1 <: alpha and S2 <: alpha, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
428 |
* where S1 and S2 have supertypes that are two different |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
429 |
* parameterizations of the same generic class or interface. |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
430 |
*/ |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
431 |
for (Type aLowerBound : from.getBounds(InferenceBound.LOWER)) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
432 |
for (Type anotherLowerBound : from.getBounds(InferenceBound.LOWER)) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
433 |
if (aLowerBound != anotherLowerBound && |
26663
f2d8845a9bb2
8055963: Inference failure with nested invocation
mcimadamore
parents:
26267
diff
changeset
|
434 |
!inferenceContext.free(aLowerBound) && |
f2d8845a9bb2
8055963: Inference failure with nested invocation
mcimadamore
parents:
26267
diff
changeset
|
435 |
!inferenceContext.free(anotherLowerBound) && |
f2d8845a9bb2
8055963: Inference failure with nested invocation
mcimadamore
parents:
26267
diff
changeset
|
436 |
commonSuperWithDiffParameterization(aLowerBound, anotherLowerBound)) { |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
437 |
return generateReferenceToTargetConstraint(tree, from, to, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
438 |
resultInfo, inferenceContext); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
439 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
440 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
441 |
} |
10 | 442 |
} |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
443 |
|
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
444 |
/* T is a parameterization of a generic class or interface, G, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
445 |
* and B2 contains a bound of one of the forms alpha = S or S <: alpha, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
446 |
* where there exists no type of the form G<...> that is a |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
447 |
* supertype of S, but the raw type G is a supertype of S |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
448 |
*/ |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
449 |
if (to.isParameterized()) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
450 |
for (Type t : from.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
451 |
Type sup = types.asSuper(t, to.tsym); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
452 |
if (sup != null && sup.isRaw()) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
453 |
return generateReferenceToTargetConstraint(tree, from, to, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
454 |
resultInfo, inferenceContext); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
455 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
456 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
457 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
458 |
return to; |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
459 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
460 |
|
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
461 |
private boolean commonSuperWithDiffParameterization(Type t, Type s) { |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
462 |
for (Pair<Type, Type> supers : getParameterizedSupers(t, s)) { |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
463 |
if (!types.isSameType(supers.fst, supers.snd)) return true; |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
464 |
} |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
465 |
return false; |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
466 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
467 |
|
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
468 |
private Type generateReferenceToTargetConstraint(JCTree tree, UndetVar from, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
469 |
Type to, Attr.ResultInfo resultInfo, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
470 |
InferenceContext inferenceContext) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
471 |
inferenceContext.solve(List.of(from.qtype), new Warner()); |
25279 | 472 |
inferenceContext.notifyChange(); |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
473 |
Type capturedType = resultInfo.checkContext.inferenceContext() |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
474 |
.cachedCapture(tree, from.inst, false); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
475 |
if (types.isConvertible(capturedType, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
476 |
resultInfo.checkContext.inferenceContext().asUndetVar(to))) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
477 |
//effectively skip additional return-type constraint generation (compatibility) |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
478 |
return syms.objectType; |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
479 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
480 |
return to; |
18910
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
481 |
} |
12468
1100643c0209
7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents:
12335
diff
changeset
|
482 |
|
15717 | 483 |
/** |
484 |
* Infer cyclic inference variables as described in 15.12.2.8. |
|
485 |
*/ |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
486 |
void instantiateAsUninferredVars(List<Type> vars, InferenceContext inferenceContext) { |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
487 |
ListBuffer<Type> todo = new ListBuffer<>(); |
15717 | 488 |
//step 1 - create fresh tvars |
489 |
for (Type t : vars) { |
|
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
490 |
UndetVar uv = (UndetVar)inferenceContext.asUndetVar(t); |
15717 | 491 |
List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER); |
492 |
if (Type.containsAny(upperBounds, vars)) { |
|
16967
79d444669f3f
7015104: use new subtype of TypeSymbol for type parameters
jfranck
parents:
16805
diff
changeset
|
493 |
TypeSymbol fresh_tvar = new TypeVariableSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner); |
29147
4cba0458106b
8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents:
27391
diff
changeset
|
494 |
fresh_tvar.type = new TypeVar(fresh_tvar, types.makeIntersectionType(uv.getBounds(InferenceBound.UPPER)), null); |
15717 | 495 |
todo.append(uv); |
496 |
uv.inst = fresh_tvar.type; |
|
497 |
} else if (upperBounds.nonEmpty()) { |
|
498 |
uv.inst = types.glb(upperBounds); |
|
499 |
} else { |
|
500 |
uv.inst = syms.objectType; |
|
12468
1100643c0209
7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents:
12335
diff
changeset
|
501 |
} |
15717 | 502 |
} |
503 |
//step 2 - replace fresh tvars in their bounds |
|
504 |
List<Type> formals = vars; |
|
505 |
for (Type t : todo) { |
|
506 |
UndetVar uv = (UndetVar)t; |
|
507 |
TypeVar ct = (TypeVar)uv.inst; |
|
508 |
ct.bound = types.glb(inferenceContext.asInstTypes(types.getBounds(ct))); |
|
509 |
if (ct.bound.isErroneous()) { |
|
510 |
//report inference error if glb fails |
|
511 |
reportBoundError(uv, BoundErrorKind.BAD_UPPER); |
|
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
512 |
} |
15717 | 513 |
formals = formals.tail; |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
514 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
515 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
516 |
|
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
517 |
/** |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
518 |
* 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
|
519 |
* 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
|
520 |
* 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
|
521 |
*/ |
12335
4725d88691dd
7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents:
12334
diff
changeset
|
522 |
Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env, |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
523 |
MethodSymbol spMethod, // sig. poly. method or null if none |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
524 |
Resolve.MethodResolutionContext resolveContext, |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
525 |
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
|
526 |
final Type restype; |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
527 |
|
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
528 |
//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
|
529 |
//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
|
530 |
//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
|
531 |
//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
|
532 |
//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
|
533 |
//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
|
534 |
//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
|
535 |
|
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
536 |
switch (env.next.tree.getTag()) { |
10950 | 537 |
case TYPECAST: |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
538 |
JCTypeCast castTree = (JCTypeCast)env.next.tree; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
539 |
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
|
540 |
castTree.clazz.type : |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
541 |
syms.objectType; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
542 |
break; |
10950 | 543 |
case EXEC: |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
544 |
JCTree.JCExpressionStatement execTree = |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
545 |
(JCTree.JCExpressionStatement)env.next.tree; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
546 |
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
|
547 |
syms.voidType : |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
548 |
syms.objectType; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
549 |
break; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
550 |
default: |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
551 |
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
|
552 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
553 |
|
29554
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
554 |
List<Type> paramtypes = argtypes.map(new ImplicitArgType(spMethod, resolveContext.step)); |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
555 |
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
|
556 |
spMethod.getThrownTypes() : |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
557 |
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
|
558 |
|
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
559 |
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
|
560 |
restype, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
561 |
exType, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
562 |
syms.methodClass); |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
563 |
return mtype; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
564 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
565 |
//where |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
566 |
class ImplicitArgType extends DeferredAttr.DeferredTypeMap { |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
567 |
|
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
568 |
public ImplicitArgType(Symbol msym, Resolve.MethodResolutionPhase phase) { |
25279 | 569 |
(rs.deferredAttr).super(AttrMode.SPECULATIVE, msym, phase); |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
570 |
} |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
571 |
|
29554
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
572 |
@Override |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
573 |
public Type visitClassType(ClassType t, Void aVoid) { |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
574 |
return types.erasure(t); |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
575 |
} |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
576 |
|
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
577 |
@Override |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
578 |
public Type visitType(Type t, Void _unused) { |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
579 |
if (t.hasTag(DEFERRED)) { |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
580 |
return visit(super.visitType(t, null)); |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
581 |
} else if (t.hasTag(BOT)) |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
582 |
// nulls type as the marker type Null (which has no instances) |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
583 |
// infer as java.lang.Void for now |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
584 |
t = types.boxedClass(syms.voidType).type; |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
585 |
return t; |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
586 |
} |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
587 |
} |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
588 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
589 |
/** |
15717 | 590 |
* This method is used to infer a suitable target SAM in case the original |
591 |
* SAM type contains one or more wildcards. An inference process is applied |
|
592 |
* so that wildcard bounds, as well as explicit lambda/method ref parameters |
|
593 |
* (where applicable) are used to constraint the solution. |
|
594 |
*/ |
|
595 |
public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface, |
|
596 |
List<Type> paramTypes, Check.CheckContext checkContext) { |
|
597 |
if (types.capture(funcInterface) == funcInterface) { |
|
598 |
//if capture doesn't change the type then return the target unchanged |
|
599 |
//(this means the target contains no wildcards!) |
|
600 |
return funcInterface; |
|
601 |
} else { |
|
602 |
Type formalInterface = funcInterface.tsym.type; |
|
603 |
InferenceContext funcInterfaceContext = |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
604 |
new InferenceContext(this, funcInterface.tsym.type.getTypeArguments()); |
15717 | 605 |
|
606 |
Assert.check(paramTypes != null); |
|
607 |
//get constraints from explicit params (this is done by |
|
608 |
//checking that explicit param types are equal to the ones |
|
609 |
//in the functional interface descriptors) |
|
610 |
List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes(); |
|
611 |
if (descParameterTypes.size() != paramTypes.size()) { |
|
612 |
checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda")); |
|
613 |
return types.createErrorType(funcInterface); |
|
614 |
} |
|
615 |
for (Type p : descParameterTypes) { |
|
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
616 |
if (!types.isSameType(funcInterfaceContext.asUndetVar(p), paramTypes.head)) { |
15717 | 617 |
checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface)); |
618 |
return types.createErrorType(funcInterface); |
|
619 |
} |
|
620 |
paramTypes = paramTypes.tail; |
|
621 |
} |
|
622 |
||
623 |
try { |
|
624 |
funcInterfaceContext.solve(funcInterfaceContext.boundedVars(), types.noWarnings); |
|
625 |
} catch (InferenceException ex) { |
|
626 |
checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface)); |
|
627 |
} |
|
628 |
||
629 |
List<Type> actualTypeargs = funcInterface.getTypeArguments(); |
|
630 |
for (Type t : funcInterfaceContext.undetvars) { |
|
631 |
UndetVar uv = (UndetVar)t; |
|
632 |
if (uv.inst == null) { |
|
633 |
uv.inst = actualTypeargs.head; |
|
634 |
} |
|
635 |
actualTypeargs = actualTypeargs.tail; |
|
636 |
} |
|
637 |
||
638 |
Type owntype = funcInterfaceContext.asInstType(formalInterface); |
|
639 |
if (!chk.checkValidGenericType(owntype)) { |
|
640 |
//if the inferred functional interface type is not well-formed, |
|
641 |
//or if it's not a subtype of the original target, issue an error |
|
642 |
checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface)); |
|
643 |
} |
|
25279 | 644 |
//propagate constraints as per JLS 18.2.1 |
645 |
checkContext.compatible(owntype, funcInterface, types.noWarnings); |
|
15717 | 646 |
return owntype; |
647 |
} |
|
648 |
} |
|
649 |
// </editor-fold> |
|
650 |
||
651 |
// <editor-fold defaultstate="collapsed" desc="Bound checking"> |
|
652 |
/** |
|
653 |
* Check bounds and perform incorporation |
|
654 |
*/ |
|
655 |
void checkWithinBounds(InferenceContext inferenceContext, |
|
656 |
Warner warn) throws InferenceException { |
|
657 |
MultiUndetVarListener mlistener = new MultiUndetVarListener(inferenceContext.undetvars); |
|
18904
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
658 |
List<Type> saved_undet = inferenceContext.save(); |
15717 | 659 |
try { |
660 |
while (true) { |
|
661 |
mlistener.reset(); |
|
662 |
if (!allowGraphInference) { |
|
663 |
//in legacy mode we lack of transitivity, so bound check |
|
664 |
//cannot be run in parallel with other incoprporation rounds |
|
665 |
for (Type t : inferenceContext.undetvars) { |
|
666 |
UndetVar uv = (UndetVar)t; |
|
667 |
IncorporationStep.CHECK_BOUNDS.apply(uv, inferenceContext, warn); |
|
668 |
} |
|
669 |
} |
|
670 |
for (Type t : inferenceContext.undetvars) { |
|
671 |
UndetVar uv = (UndetVar)t; |
|
672 |
//bound incorporation |
|
673 |
EnumSet<IncorporationStep> incorporationSteps = allowGraphInference ? |
|
674 |
incorporationStepsGraph : incorporationStepsLegacy; |
|
675 |
for (IncorporationStep is : incorporationSteps) { |
|
18911 | 676 |
if (is.accepts(uv, inferenceContext)) { |
677 |
is.apply(uv, inferenceContext, warn); |
|
678 |
} |
|
15717 | 679 |
} |
680 |
} |
|
681 |
if (!mlistener.changed || !allowGraphInference) break; |
|
682 |
} |
|
683 |
} |
|
684 |
finally { |
|
685 |
mlistener.detach(); |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
686 |
if (incorporationCache.size() == MAX_INCORPORATION_STEPS) { |
18904
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
687 |
inferenceContext.rollback(saved_undet); |
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
688 |
} |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
689 |
incorporationCache.clear(); |
15717 | 690 |
} |
691 |
} |
|
692 |
//where |
|
693 |
/** |
|
694 |
* This listener keeps track of changes on a group of inference variable |
|
695 |
* bounds. Note: the listener must be detached (calling corresponding |
|
696 |
* method) to make sure that the underlying inference variable is |
|
697 |
* left in a clean state. |
|
698 |
*/ |
|
699 |
class MultiUndetVarListener implements UndetVar.UndetVarListener { |
|
700 |
||
701 |
boolean changed; |
|
702 |
List<Type> undetvars; |
|
703 |
||
704 |
public MultiUndetVarListener(List<Type> undetvars) { |
|
705 |
this.undetvars = undetvars; |
|
706 |
for (Type t : undetvars) { |
|
707 |
UndetVar uv = (UndetVar)t; |
|
708 |
uv.listener = this; |
|
709 |
} |
|
710 |
} |
|
711 |
||
712 |
public void varChanged(UndetVar uv, Set<InferenceBound> ibs) { |
|
713 |
//avoid non-termination |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
714 |
if (incorporationCache.size() < MAX_INCORPORATION_STEPS) { |
15717 | 715 |
changed = true; |
716 |
} |
|
717 |
} |
|
718 |
||
719 |
void reset() { |
|
720 |
changed = false; |
|
721 |
} |
|
722 |
||
723 |
void detach() { |
|
724 |
for (Type t : undetvars) { |
|
725 |
UndetVar uv = (UndetVar)t; |
|
726 |
uv.listener = null; |
|
727 |
} |
|
728 |
} |
|
22163 | 729 |
} |
15717 | 730 |
|
22163 | 731 |
/** max number of incorporation rounds */ |
15717 | 732 |
static final int MAX_INCORPORATION_STEPS = 100; |
733 |
||
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
734 |
/* If for two types t and s there is a least upper bound that contains |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
735 |
* parameterized types G1, G2 ... Gn, then there exists supertypes of 't' of the form |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
736 |
* G1<T1, ..., Tn>, G2<T1, ..., Tn>, ... Gn<T1, ..., Tn> and supertypes of 's' of the form |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
737 |
* G1<S1, ..., Sn>, G2<S1, ..., Sn>, ... Gn<S1, ..., Sn> which will be returned by this method. |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
738 |
* If no such common supertypes exists then an empty list is returned. |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
739 |
* |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
740 |
* As an example for the following input: |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
741 |
* |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
742 |
* t = java.util.ArrayList<java.lang.String> |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
743 |
* s = java.util.List<T> |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
744 |
* |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
745 |
* we get this ouput (singleton list): |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
746 |
* |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
747 |
* [Pair[java.util.List<java.lang.String>,java.util.List<T>]] |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
748 |
*/ |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
749 |
private List<Pair<Type, Type>> getParameterizedSupers(Type t, Type s) { |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
750 |
Type lubResult = types.lub(t, s); |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
751 |
if (lubResult == syms.errType || lubResult == syms.botType) { |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
752 |
return List.nil(); |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
753 |
} |
29147
4cba0458106b
8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents:
27391
diff
changeset
|
754 |
List<Type> supertypesToCheck = lubResult.isIntersection() ? |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
755 |
((IntersectionClassType)lubResult).getComponents() : |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
756 |
List.of(lubResult); |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
757 |
ListBuffer<Pair<Type, Type>> commonSupertypes = new ListBuffer<>(); |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
758 |
for (Type sup : supertypesToCheck) { |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
759 |
if (sup.isParameterized()) { |
27391
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
760 |
Type asSuperOfT = asSuper(t, sup); |
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
761 |
Type asSuperOfS = asSuper(s, sup); |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
762 |
commonSupertypes.add(new Pair<>(asSuperOfT, asSuperOfS)); |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
763 |
} |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
764 |
} |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
765 |
return commonSupertypes.toList(); |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
766 |
} |
27391
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
767 |
//where |
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
768 |
private Type asSuper(Type t, Type sup) { |
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
769 |
return (sup.hasTag(ARRAY)) ? |
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
770 |
new ArrayType(asSuper(types.elemtype(t), types.elemtype(sup)), syms.arrayClass) : |
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
771 |
types.asSuper(t, sup.tsym); |
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
772 |
} |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
773 |
|
15717 | 774 |
/** |
775 |
* This enumeration defines an entry point for doing inference variable |
|
776 |
* bound incorporation - it can be used to inject custom incorporation |
|
777 |
* logic into the basic bound checking routine |
|
778 |
*/ |
|
779 |
enum IncorporationStep { |
|
780 |
/** |
|
781 |
* Performs basic bound checking - i.e. is the instantiated type for a given |
|
782 |
* inference variable compatible with its bounds? |
|
783 |
*/ |
|
784 |
CHECK_BOUNDS() { |
|
785 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
786 |
Infer infer = inferenceContext.infer; |
15717 | 787 |
uv.substBounds(inferenceContext.inferenceVars(), inferenceContext.instTypes(), infer.types); |
788 |
infer.checkCompatibleUpperBounds(uv, inferenceContext); |
|
789 |
if (uv.inst != null) { |
|
790 |
Type inst = uv.inst; |
|
791 |
for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
|
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
792 |
if (!isSubtype(inst, inferenceContext.asUndetVar(u), warn, infer)) { |
15717 | 793 |
infer.reportBoundError(uv, BoundErrorKind.UPPER); |
794 |
} |
|
795 |
} |
|
796 |
for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
797 |
if (!isSubtype(inferenceContext.asUndetVar(l), inst, warn, infer)) { |
15717 | 798 |
infer.reportBoundError(uv, BoundErrorKind.LOWER); |
799 |
} |
|
800 |
} |
|
801 |
for (Type e : uv.getBounds(InferenceBound.EQ)) { |
|
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
802 |
if (!isSameType(inst, inferenceContext.asUndetVar(e), infer)) { |
15717 | 803 |
infer.reportBoundError(uv, BoundErrorKind.EQ); |
804 |
} |
|
805 |
} |
|
806 |
} |
|
807 |
} |
|
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
808 |
|
18911 | 809 |
@Override |
810 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
|
811 |
//applies to all undetvars |
|
812 |
return true; |
|
813 |
} |
|
15717 | 814 |
}, |
815 |
/** |
|
816 |
* Check consistency of equality constraints. This is a slightly more aggressive |
|
817 |
* inference routine that is designed as to maximize compatibility with JDK 7. |
|
818 |
* Note: this is not used in graph mode. |
|
819 |
*/ |
|
820 |
EQ_CHECK_LEGACY() { |
|
821 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
822 |
Infer infer = inferenceContext.infer; |
15717 | 823 |
Type eq = null; |
824 |
for (Type e : uv.getBounds(InferenceBound.EQ)) { |
|
825 |
Assert.check(!inferenceContext.free(e)); |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
826 |
if (eq != null && !isSameType(e, eq, infer)) { |
15717 | 827 |
infer.reportBoundError(uv, BoundErrorKind.EQ); |
828 |
} |
|
829 |
eq = e; |
|
830 |
for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
831 |
Assert.check(!inferenceContext.free(l)); |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
832 |
if (!isSubtype(l, e, warn, infer)) { |
15717 | 833 |
infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER); |
834 |
} |
|
835 |
} |
|
836 |
for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
|
837 |
if (inferenceContext.free(u)) continue; |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
838 |
if (!isSubtype(e, u, warn, infer)) { |
15717 | 839 |
infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER); |
840 |
} |
|
841 |
} |
|
842 |
} |
|
843 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
844 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
845 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
846 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
847 |
return !uv.isCaptured() && uv.getBounds(InferenceBound.EQ).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
848 |
} |
15717 | 849 |
}, |
850 |
/** |
|
851 |
* Check consistency of equality constraints. |
|
852 |
*/ |
|
853 |
EQ_CHECK() { |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
854 |
@Override |
15717 | 855 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
856 |
Infer infer = inferenceContext.infer; |
15717 | 857 |
for (Type e : uv.getBounds(InferenceBound.EQ)) { |
858 |
if (e.containsAny(inferenceContext.inferenceVars())) continue; |
|
859 |
for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
|
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
860 |
if (!isSubtype(e, inferenceContext.asUndetVar(u), warn, infer)) { |
15717 | 861 |
infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER); |
862 |
} |
|
863 |
} |
|
864 |
for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
865 |
if (!isSubtype(inferenceContext.asUndetVar(l), e, warn, infer)) { |
15717 | 866 |
infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER); |
867 |
} |
|
868 |
} |
|
869 |
} |
|
870 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
871 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
872 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
873 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
874 |
return !uv.isCaptured() && uv.getBounds(InferenceBound.EQ).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
875 |
} |
15717 | 876 |
}, |
877 |
/** |
|
878 |
* Given a bound set containing {@code alpha <: T} and {@code alpha :> S} |
|
879 |
* perform {@code S <: T} (which could lead to new bounds). |
|
880 |
*/ |
|
881 |
CROSS_UPPER_LOWER() { |
|
882 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
883 |
Infer infer = inferenceContext.infer; |
15717 | 884 |
for (Type b1 : uv.getBounds(InferenceBound.UPPER)) { |
885 |
for (Type b2 : uv.getBounds(InferenceBound.LOWER)) { |
|
30014
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
886 |
if (!isSubtype(inferenceContext.asUndetVar(b2), inferenceContext.asUndetVar(b1), warn , infer)) { |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
887 |
infer.reportBoundError(uv, BoundErrorKind.BAD_UPPER_LOWER); |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
888 |
} |
15717 | 889 |
} |
890 |
} |
|
891 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
892 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
893 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
894 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
895 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
896 |
uv.getBounds(InferenceBound.UPPER).nonEmpty() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
897 |
uv.getBounds(InferenceBound.LOWER).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
898 |
} |
15717 | 899 |
}, |
900 |
/** |
|
901 |
* Given a bound set containing {@code alpha <: T} and {@code alpha == S} |
|
902 |
* perform {@code S <: T} (which could lead to new bounds). |
|
903 |
*/ |
|
904 |
CROSS_UPPER_EQ() { |
|
905 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
906 |
Infer infer = inferenceContext.infer; |
15717 | 907 |
for (Type b1 : uv.getBounds(InferenceBound.UPPER)) { |
908 |
for (Type b2 : uv.getBounds(InferenceBound.EQ)) { |
|
30014
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
909 |
if (!isSubtype(inferenceContext.asUndetVar(b2), inferenceContext.asUndetVar(b1), warn, infer)) { |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
910 |
infer.reportBoundError(uv, BoundErrorKind.BAD_UPPER_EQUAL); |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
911 |
} |
15717 | 912 |
} |
913 |
} |
|
914 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
915 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
916 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
917 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
918 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
919 |
uv.getBounds(InferenceBound.EQ).nonEmpty() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
920 |
uv.getBounds(InferenceBound.UPPER).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
921 |
} |
15717 | 922 |
}, |
923 |
/** |
|
924 |
* Given a bound set containing {@code alpha :> S} and {@code alpha == T} |
|
925 |
* perform {@code S <: T} (which could lead to new bounds). |
|
926 |
*/ |
|
927 |
CROSS_EQ_LOWER() { |
|
928 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
929 |
Infer infer = inferenceContext.infer; |
15717 | 930 |
for (Type b1 : uv.getBounds(InferenceBound.EQ)) { |
931 |
for (Type b2 : uv.getBounds(InferenceBound.LOWER)) { |
|
30014
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
932 |
if (!isSubtype(inferenceContext.asUndetVar(b2), inferenceContext.asUndetVar(b1), warn, infer)) { |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
933 |
infer.reportBoundError(uv, BoundErrorKind.BAD_EQUAL_LOWER); |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
934 |
} |
16567
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
935 |
} |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
936 |
} |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
937 |
} |
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
938 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
939 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
940 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
941 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
942 |
uv.getBounds(InferenceBound.EQ).nonEmpty() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
943 |
uv.getBounds(InferenceBound.LOWER).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
944 |
} |
16567
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
945 |
}, |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
946 |
/** |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
947 |
* Given a bound set containing {@code alpha <: P<T>} and |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
948 |
* {@code alpha <: P<S>} where P is a parameterized type, |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
949 |
* perform {@code T = S} (which could lead to new bounds). |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
950 |
*/ |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
951 |
CROSS_UPPER_UPPER() { |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
952 |
@Override |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
953 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
954 |
Infer infer = inferenceContext.infer; |
29555
71f15ff4b409
8048838: type inference performance regression
mcimadamore
parents:
29554
diff
changeset
|
955 |
List<Type> boundList = uv.getBounds(InferenceBound.UPPER).stream() |
71f15ff4b409
8048838: type inference performance regression
mcimadamore
parents:
29554
diff
changeset
|
956 |
.collect(infer.types.closureCollector(true, infer.types::isSameType)); |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
957 |
List<Type> boundListTail = boundList.tail; |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
958 |
while (boundList.nonEmpty()) { |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
959 |
List<Type> tmpTail = boundListTail; |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
960 |
while (tmpTail.nonEmpty()) { |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
961 |
Type b1 = boundList.head; |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
962 |
Type b2 = tmpTail.head; |
27228
da96e0643b5c
8057800: Method reference with generic type creates NPE when compiling
vromero
parents:
27224
diff
changeset
|
963 |
/* This wildcard check is temporary workaround. This code may need to be |
da96e0643b5c
8057800: Method reference with generic type creates NPE when compiling
vromero
parents:
27224
diff
changeset
|
964 |
* revisited once spec bug JDK-7034922 is fixed. |
da96e0643b5c
8057800: Method reference with generic type creates NPE when compiling
vromero
parents:
27224
diff
changeset
|
965 |
*/ |
da96e0643b5c
8057800: Method reference with generic type creates NPE when compiling
vromero
parents:
27224
diff
changeset
|
966 |
if (b1 != b2 && !b1.hasTag(WILDCARD) && !b2.hasTag(WILDCARD)) { |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
967 |
for (Pair<Type, Type> commonSupers : infer.getParameterizedSupers(b1, b2)) { |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
968 |
List<Type> allParamsSuperBound1 = commonSupers.fst.allparams(); |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
969 |
List<Type> allParamsSuperBound2 = commonSupers.snd.allparams(); |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
970 |
while (allParamsSuperBound1.nonEmpty() && allParamsSuperBound2.nonEmpty()) { |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
971 |
//traverse the list of all params comparing them |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
972 |
if (!allParamsSuperBound1.head.hasTag(WILDCARD) && |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
973 |
!allParamsSuperBound2.head.hasTag(WILDCARD)) { |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
974 |
if (!isSameType(inferenceContext.asUndetVar(allParamsSuperBound1.head), |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
975 |
inferenceContext.asUndetVar(allParamsSuperBound2.head), infer)) { |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
976 |
infer.reportBoundError(uv, BoundErrorKind.BAD_UPPER); |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
977 |
} |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
978 |
} |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
979 |
allParamsSuperBound1 = allParamsSuperBound1.tail; |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
980 |
allParamsSuperBound2 = allParamsSuperBound2.tail; |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
981 |
} |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
982 |
Assert.check(allParamsSuperBound1.isEmpty() && allParamsSuperBound2.isEmpty()); |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
983 |
} |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
984 |
} |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
985 |
tmpTail = tmpTail.tail; |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
986 |
} |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
987 |
boundList = boundList.tail; |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
988 |
boundListTail = boundList.tail; |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
989 |
} |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
990 |
} |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
991 |
|
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
992 |
@Override |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
993 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
994 |
return !uv.isCaptured() && |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
995 |
uv.getBounds(InferenceBound.UPPER).nonEmpty(); |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
996 |
} |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
997 |
}, |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
998 |
/** |
16567
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
999 |
* Given a bound set containing {@code alpha == S} and {@code alpha == T} |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
1000 |
* perform {@code S == T} (which could lead to new bounds). |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
1001 |
*/ |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
1002 |
CROSS_EQ_EQ() { |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
1003 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1004 |
Infer infer = inferenceContext.infer; |
16567
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
1005 |
for (Type b1 : uv.getBounds(InferenceBound.EQ)) { |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
1006 |
for (Type b2 : uv.getBounds(InferenceBound.EQ)) { |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
1007 |
if (b1 != b2) { |
30014
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1008 |
if (!isSameType(inferenceContext.asUndetVar(b2), inferenceContext.asUndetVar(b1), infer)) { |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1009 |
infer.reportBoundError(uv, BoundErrorKind.BAD_EQ); |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1010 |
} |
15717 | 1011 |
} |
1012 |
} |
|
1013 |
} |
|
1014 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1015 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1016 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1017 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1018 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1019 |
uv.getBounds(InferenceBound.EQ).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1020 |
} |
15717 | 1021 |
}, |
1022 |
/** |
|
1023 |
* Given a bound set containing {@code alpha <: beta} propagate lower bounds |
|
1024 |
* from alpha to beta; also propagate upper bounds from beta to alpha. |
|
1025 |
*/ |
|
1026 |
PROP_UPPER() { |
|
1027 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1028 |
Infer infer = inferenceContext.infer; |
15717 | 1029 |
for (Type b : uv.getBounds(InferenceBound.UPPER)) { |
1030 |
if (inferenceContext.inferenceVars().contains(b)) { |
|
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
1031 |
UndetVar uv2 = (UndetVar)inferenceContext.asUndetVar(b); |
18911 | 1032 |
if (uv2.isCaptured()) continue; |
15717 | 1033 |
//alpha <: beta |
16341
cc4b4e3c0f12
8009545: Graph inference: dependencies between inference variables should be set during incorporation
mcimadamore
parents:
16321
diff
changeset
|
1034 |
//0. set beta :> alpha |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1035 |
addBound(InferenceBound.LOWER, uv2, inferenceContext.asInstType(uv.qtype), infer); |
15717 | 1036 |
//1. copy alpha's lower to beta's |
1037 |
for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1038 |
addBound(InferenceBound.LOWER, uv2, inferenceContext.asInstType(l), infer); |
15717 | 1039 |
} |
1040 |
//2. copy beta's upper to alpha's |
|
1041 |
for (Type u : uv2.getBounds(InferenceBound.UPPER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1042 |
addBound(InferenceBound.UPPER, uv, inferenceContext.asInstType(u), infer); |
15717 | 1043 |
} |
1044 |
} |
|
1045 |
} |
|
1046 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1047 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1048 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1049 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1050 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1051 |
uv.getBounds(InferenceBound.UPPER).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1052 |
} |
15717 | 1053 |
}, |
1054 |
/** |
|
1055 |
* Given a bound set containing {@code alpha :> beta} propagate lower bounds |
|
1056 |
* from beta to alpha; also propagate upper bounds from alpha to beta. |
|
1057 |
*/ |
|
1058 |
PROP_LOWER() { |
|
1059 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1060 |
Infer infer = inferenceContext.infer; |
15717 | 1061 |
for (Type b : uv.getBounds(InferenceBound.LOWER)) { |
1062 |
if (inferenceContext.inferenceVars().contains(b)) { |
|
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
1063 |
UndetVar uv2 = (UndetVar)inferenceContext.asUndetVar(b); |
18911 | 1064 |
if (uv2.isCaptured()) continue; |
15717 | 1065 |
//alpha :> beta |
16341
cc4b4e3c0f12
8009545: Graph inference: dependencies between inference variables should be set during incorporation
mcimadamore
parents:
16321
diff
changeset
|
1066 |
//0. set beta <: alpha |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1067 |
addBound(InferenceBound.UPPER, uv2, inferenceContext.asInstType(uv.qtype), infer); |
15717 | 1068 |
//1. copy alpha's upper to beta's |
1069 |
for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1070 |
addBound(InferenceBound.UPPER, uv2, inferenceContext.asInstType(u), infer); |
15717 | 1071 |
} |
1072 |
//2. copy beta's lower to alpha's |
|
1073 |
for (Type l : uv2.getBounds(InferenceBound.LOWER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1074 |
addBound(InferenceBound.LOWER, uv, inferenceContext.asInstType(l), infer); |
15717 | 1075 |
} |
1076 |
} |
|
1077 |
} |
|
1078 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1079 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1080 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1081 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1082 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1083 |
uv.getBounds(InferenceBound.LOWER).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1084 |
} |
15717 | 1085 |
}, |
1086 |
/** |
|
1087 |
* Given a bound set containing {@code alpha == beta} propagate lower/upper |
|
1088 |
* bounds from alpha to beta and back. |
|
1089 |
*/ |
|
1090 |
PROP_EQ() { |
|
1091 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1092 |
Infer infer = inferenceContext.infer; |
15717 | 1093 |
for (Type b : uv.getBounds(InferenceBound.EQ)) { |
1094 |
if (inferenceContext.inferenceVars().contains(b)) { |
|
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
1095 |
UndetVar uv2 = (UndetVar)inferenceContext.asUndetVar(b); |
18911 | 1096 |
if (uv2.isCaptured()) continue; |
15717 | 1097 |
//alpha == beta |
16341
cc4b4e3c0f12
8009545: Graph inference: dependencies between inference variables should be set during incorporation
mcimadamore
parents:
16321
diff
changeset
|
1098 |
//0. set beta == alpha |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1099 |
addBound(InferenceBound.EQ, uv2, inferenceContext.asInstType(uv.qtype), infer); |
15717 | 1100 |
//1. copy all alpha's bounds to beta's |
1101 |
for (InferenceBound ib : InferenceBound.values()) { |
|
1102 |
for (Type b2 : uv.getBounds(ib)) { |
|
1103 |
if (b2 != uv2) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1104 |
addBound(ib, uv2, inferenceContext.asInstType(b2), infer); |
15717 | 1105 |
} |
1106 |
} |
|
1107 |
} |
|
1108 |
//2. copy all beta's bounds to alpha's |
|
1109 |
for (InferenceBound ib : InferenceBound.values()) { |
|
1110 |
for (Type b2 : uv2.getBounds(ib)) { |
|
1111 |
if (b2 != uv) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1112 |
addBound(ib, uv, inferenceContext.asInstType(b2), infer); |
15717 | 1113 |
} |
1114 |
} |
|
1115 |
} |
|
1116 |
} |
|
1117 |
} |
|
1118 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1119 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1120 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1121 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1122 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1123 |
uv.getBounds(InferenceBound.EQ).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
1124 |
} |
15717 | 1125 |
}; |
1126 |
||
1127 |
abstract void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn); |
|
18911 | 1128 |
|
1129 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
|
1130 |
return !uv.isCaptured(); |
|
1131 |
} |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1132 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1133 |
boolean isSubtype(Type s, Type t, Warner warn, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1134 |
return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn, infer); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1135 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1136 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1137 |
boolean isSameType(Type s, Type t, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1138 |
return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null, infer); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1139 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1140 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1141 |
void addBound(InferenceBound ib, UndetVar uv, Type b, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1142 |
doIncorporationOp(opFor(ib), uv, b, null, infer); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1143 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1144 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1145 |
IncorporationBinaryOpKind opFor(InferenceBound boundKind) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1146 |
switch (boundKind) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1147 |
case EQ: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1148 |
return IncorporationBinaryOpKind.ADD_EQ_BOUND; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1149 |
case LOWER: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1150 |
return IncorporationBinaryOpKind.ADD_LOWER_BOUND; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1151 |
case UPPER: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1152 |
return IncorporationBinaryOpKind.ADD_UPPER_BOUND; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1153 |
default: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1154 |
Assert.error("Can't get here!"); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1155 |
return null; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1156 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1157 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1158 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1159 |
boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1160 |
IncorporationBinaryOp newOp = infer.new IncorporationBinaryOp(opKind, op1, op2); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1161 |
Boolean res = infer.incorporationCache.get(newOp); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1162 |
if (res == null) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1163 |
infer.incorporationCache.put(newOp, res = newOp.apply(warn)); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1164 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1165 |
return res; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1166 |
} |
15717 | 1167 |
} |
1168 |
||
1169 |
/** incorporation steps to be executed when running in legacy mode */ |
|
1170 |
EnumSet<IncorporationStep> incorporationStepsLegacy = EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY); |
|
1171 |
||
1172 |
/** incorporation steps to be executed when running in graph mode */ |
|
1173 |
EnumSet<IncorporationStep> incorporationStepsGraph = |
|
1174 |
EnumSet.complementOf(EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY)); |
|
1175 |
||
1176 |
/** |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1177 |
* Three kinds of basic operation are supported as part of an incorporation step: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1178 |
* (i) subtype check, (ii) same type check and (iii) bound addition (either |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1179 |
* upper/lower/eq bound). |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1180 |
*/ |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1181 |
enum IncorporationBinaryOpKind { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1182 |
IS_SUBTYPE() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1183 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1184 |
boolean apply(Type op1, Type op2, Warner warn, Types types) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1185 |
return types.isSubtypeUnchecked(op1, op2, warn); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1186 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1187 |
}, |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1188 |
IS_SAME_TYPE() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1189 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1190 |
boolean apply(Type op1, Type op2, Warner warn, Types types) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1191 |
return types.isSameType(op1, op2); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1192 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1193 |
}, |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1194 |
ADD_UPPER_BOUND() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1195 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1196 |
boolean apply(Type op1, Type op2, Warner warn, Types types) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1197 |
UndetVar uv = (UndetVar)op1; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1198 |
uv.addBound(InferenceBound.UPPER, op2, types); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1199 |
return true; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1200 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1201 |
}, |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1202 |
ADD_LOWER_BOUND() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1203 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1204 |
boolean apply(Type op1, Type op2, Warner warn, Types types) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1205 |
UndetVar uv = (UndetVar)op1; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1206 |
uv.addBound(InferenceBound.LOWER, op2, types); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1207 |
return true; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1208 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1209 |
}, |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1210 |
ADD_EQ_BOUND() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1211 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1212 |
boolean apply(Type op1, Type op2, Warner warn, Types types) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1213 |
UndetVar uv = (UndetVar)op1; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1214 |
uv.addBound(InferenceBound.EQ, op2, types); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1215 |
return true; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1216 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1217 |
}; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1218 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1219 |
abstract boolean apply(Type op1, Type op2, Warner warn, Types types); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1220 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1221 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1222 |
/** |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1223 |
* This class encapsulates a basic incorporation operation; incorporation |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1224 |
* operations takes two type operands and a kind. Each operation performed |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1225 |
* during an incorporation round is stored in a cache, so that operations |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1226 |
* are not executed unnecessarily (which would potentially lead to adding |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1227 |
* same bounds over and over). |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1228 |
*/ |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1229 |
class IncorporationBinaryOp { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1230 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1231 |
IncorporationBinaryOpKind opKind; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1232 |
Type op1; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1233 |
Type op2; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1234 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1235 |
IncorporationBinaryOp(IncorporationBinaryOpKind opKind, Type op1, Type op2) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1236 |
this.opKind = opKind; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1237 |
this.op1 = op1; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1238 |
this.op2 = op2; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1239 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1240 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1241 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1242 |
public boolean equals(Object o) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1243 |
if (!(o instanceof IncorporationBinaryOp)) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1244 |
return false; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1245 |
} else { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1246 |
IncorporationBinaryOp that = (IncorporationBinaryOp)o; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1247 |
return opKind == that.opKind && |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1248 |
types.isSameType(op1, that.op1, true) && |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1249 |
types.isSameType(op2, that.op2, true); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1250 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1251 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1252 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1253 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1254 |
public int hashCode() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1255 |
int result = opKind.hashCode(); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1256 |
result *= 127; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1257 |
result += types.hashCode(op1); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1258 |
result *= 127; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1259 |
result += types.hashCode(op2); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1260 |
return result; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1261 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1262 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1263 |
boolean apply(Warner warn) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1264 |
return opKind.apply(op1, op2, warn, types); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1265 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1266 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1267 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1268 |
/** an incorporation cache keeps track of all executed incorporation-related operations */ |
22163 | 1269 |
Map<IncorporationBinaryOp, Boolean> incorporationCache = new HashMap<>(); |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1270 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1271 |
/** |
15717 | 1272 |
* Make sure that the upper bounds we got so far lead to a solvable inference |
1273 |
* variable by making sure that a glb exists. |
|
1274 |
*/ |
|
1275 |
void checkCompatibleUpperBounds(UndetVar uv, InferenceContext inferenceContext) { |
|
1276 |
List<Type> hibounds = |
|
1277 |
Type.filter(uv.getBounds(InferenceBound.UPPER), new BoundFilter(inferenceContext)); |
|
1278 |
Type hb = null; |
|
1279 |
if (hibounds.isEmpty()) |
|
1280 |
hb = syms.objectType; |
|
1281 |
else if (hibounds.tail.isEmpty()) |
|
1282 |
hb = hibounds.head; |
|
1283 |
else |
|
1284 |
hb = types.glb(hibounds); |
|
1285 |
if (hb == null || hb.isErroneous()) |
|
1286 |
reportBoundError(uv, BoundErrorKind.BAD_UPPER); |
|
1287 |
} |
|
1288 |
//where |
|
1289 |
protected static class BoundFilter implements Filter<Type> { |
|
1290 |
||
1291 |
InferenceContext inferenceContext; |
|
1292 |
||
1293 |
public BoundFilter(InferenceContext inferenceContext) { |
|
1294 |
this.inferenceContext = inferenceContext; |
|
1295 |
} |
|
1296 |
||
1297 |
@Override |
|
1298 |
public boolean accepts(Type t) { |
|
1299 |
return !t.isErroneous() && !inferenceContext.free(t) && |
|
1300 |
!t.hasTag(BOT); |
|
1301 |
} |
|
22163 | 1302 |
} |
15717 | 1303 |
|
1304 |
/** |
|
1305 |
* This enumeration defines all possible bound-checking related errors. |
|
1306 |
*/ |
|
1307 |
enum BoundErrorKind { |
|
1308 |
/** |
|
1309 |
* The (uninstantiated) inference variable has incompatible upper bounds. |
|
1310 |
*/ |
|
1311 |
BAD_UPPER() { |
|
1312 |
@Override |
|
1313 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
1314 |
return ex.setMessage("incompatible.upper.bounds", uv.qtype, |
|
1315 |
uv.getBounds(InferenceBound.UPPER)); |
|
1316 |
} |
|
1317 |
}, |
|
1318 |
/** |
|
30014
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1319 |
* The (uninstantiated) inference variable has incompatible equality constraints. |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1320 |
*/ |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1321 |
BAD_EQ() { |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1322 |
@Override |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1323 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1324 |
return ex.setMessage("incompatible.eq.bounds", uv.qtype, |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1325 |
uv.getBounds(InferenceBound.EQ)); |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1326 |
} |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1327 |
}, |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1328 |
/** |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1329 |
* The (uninstantiated) inference variable has incompatible upper lower bounds. |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1330 |
*/ |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1331 |
BAD_UPPER_LOWER() { |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1332 |
@Override |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1333 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1334 |
return ex.setMessage("incompatible.upper.lower.bounds", uv.qtype, |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1335 |
uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.LOWER)); |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1336 |
} |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1337 |
}, |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1338 |
/** |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1339 |
* The (uninstantiated) inference variable has incompatible upper equal bounds. |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1340 |
*/ |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1341 |
BAD_UPPER_EQUAL() { |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1342 |
@Override |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1343 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1344 |
return ex.setMessage("incompatible.upper.eq.bounds", uv.qtype, |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1345 |
uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.EQ)); |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1346 |
} |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1347 |
}, |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1348 |
/** |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1349 |
* The (uninstantiated) inference variable has incompatible upper equal bounds. |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1350 |
*/ |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1351 |
BAD_EQUAL_LOWER() { |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1352 |
@Override |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1353 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1354 |
return ex.setMessage("incompatible.eq.lower.bounds", uv.qtype, |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1355 |
uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.LOWER)); |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1356 |
} |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1357 |
}, |
fc1f2b200c1f
8078024: javac, several incorporation steps are silently failing when an error should be reported
vromero
parents:
29555
diff
changeset
|
1358 |
/** |
15717 | 1359 |
* An equality constraint is not compatible with an upper bound. |
1360 |
*/ |
|
1361 |
BAD_EQ_UPPER() { |
|
1362 |
@Override |
|
1363 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
1364 |
return ex.setMessage("incompatible.eq.upper.bounds", uv.qtype, |
|
1365 |
uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.UPPER)); |
|
1366 |
} |
|
1367 |
}, |
|
1368 |
/** |
|
1369 |
* An equality constraint is not compatible with a lower bound. |
|
1370 |
*/ |
|
1371 |
BAD_EQ_LOWER() { |
|
1372 |
@Override |
|
1373 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
1374 |
return ex.setMessage("incompatible.eq.lower.bounds", uv.qtype, |
|
1375 |
uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.LOWER)); |
|
1376 |
} |
|
1377 |
}, |
|
1378 |
/** |
|
1379 |
* Instantiated inference variable is not compatible with an upper bound. |
|
1380 |
*/ |
|
1381 |
UPPER() { |
|
1382 |
@Override |
|
1383 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
1384 |
return ex.setMessage("inferred.do.not.conform.to.upper.bounds", uv.inst, |
|
1385 |
uv.getBounds(InferenceBound.UPPER)); |
|
1386 |
} |
|
1387 |
}, |
|
1388 |
/** |
|
1389 |
* Instantiated inference variable is not compatible with a lower bound. |
|
1390 |
*/ |
|
1391 |
LOWER() { |
|
1392 |
@Override |
|
1393 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
1394 |
return ex.setMessage("inferred.do.not.conform.to.lower.bounds", uv.inst, |
|
1395 |
uv.getBounds(InferenceBound.LOWER)); |
|
1396 |
} |
|
1397 |
}, |
|
1398 |
/** |
|
1399 |
* Instantiated inference variable is not compatible with an equality constraint. |
|
1400 |
*/ |
|
1401 |
EQ() { |
|
1402 |
@Override |
|
1403 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
1404 |
return ex.setMessage("inferred.do.not.conform.to.eq.bounds", uv.inst, |
|
1405 |
uv.getBounds(InferenceBound.EQ)); |
|
1406 |
} |
|
1407 |
}; |
|
1408 |
||
1409 |
abstract InapplicableMethodException setMessage(InferenceException ex, UndetVar uv); |
|
1410 |
} |
|
1411 |
||
1412 |
/** |
|
1413 |
* Report a bound-checking error of given kind |
|
1414 |
*/ |
|
1415 |
void reportBoundError(UndetVar uv, BoundErrorKind bk) { |
|
1416 |
throw bk.setMessage(inferenceException, uv); |
|
1417 |
} |
|
1418 |
// </editor-fold> |
|
1419 |
||
1420 |
// <editor-fold defaultstate="collapsed" desc="Inference engine"> |
|
1421 |
/** |
|
1422 |
* Graph inference strategy - act as an input to the inference solver; a strategy is |
|
1423 |
* composed of two ingredients: (i) find a node to solve in the inference graph, |
|
1424 |
* and (ii) tell th engine when we are done fixing inference variables |
|
1425 |
*/ |
|
1426 |
interface GraphStrategy { |
|
19914 | 1427 |
|
1428 |
/** |
|
1429 |
* A NodeNotFoundException is thrown whenever an inference strategy fails |
|
1430 |
* to pick the next node to solve in the inference graph. |
|
1431 |
*/ |
|
1432 |
public static class NodeNotFoundException extends RuntimeException { |
|
1433 |
private static final long serialVersionUID = 0; |
|
1434 |
||
1435 |
InferenceGraph graph; |
|
1436 |
||
1437 |
public NodeNotFoundException(InferenceGraph graph) { |
|
1438 |
this.graph = graph; |
|
1439 |
} |
|
1440 |
} |
|
15717 | 1441 |
/** |
1442 |
* Pick the next node (leaf) to solve in the graph |
|
1443 |
*/ |
|
19914 | 1444 |
Node pickNode(InferenceGraph g) throws NodeNotFoundException; |
15717 | 1445 |
/** |
1446 |
* Is this the last step? |
|
1447 |
*/ |
|
1448 |
boolean done(); |
|
1449 |
} |
|
1450 |
||
1451 |
/** |
|
1452 |
* Simple solver strategy class that locates all leaves inside a graph |
|
1453 |
* and picks the first leaf as the next node to solve |
|
1454 |
*/ |
|
1455 |
abstract class LeafSolver implements GraphStrategy { |
|
1456 |
public Node pickNode(InferenceGraph g) { |
|
19914 | 1457 |
if (g.nodes.isEmpty()) { |
1458 |
//should not happen |
|
1459 |
throw new NodeNotFoundException(g); |
|
22163 | 1460 |
} |
15717 | 1461 |
return g.nodes.get(0); |
1462 |
} |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1463 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1464 |
boolean isSubtype(Type s, Type t, Warner warn, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1465 |
return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn, infer); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1466 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1467 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1468 |
boolean isSameType(Type s, Type t, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1469 |
return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null, infer); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1470 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1471 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1472 |
void addBound(InferenceBound ib, UndetVar uv, Type b, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1473 |
doIncorporationOp(opFor(ib), uv, b, null, infer); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1474 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1475 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1476 |
IncorporationBinaryOpKind opFor(InferenceBound boundKind) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1477 |
switch (boundKind) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1478 |
case EQ: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1479 |
return IncorporationBinaryOpKind.ADD_EQ_BOUND; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1480 |
case LOWER: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1481 |
return IncorporationBinaryOpKind.ADD_LOWER_BOUND; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1482 |
case UPPER: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1483 |
return IncorporationBinaryOpKind.ADD_UPPER_BOUND; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1484 |
default: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1485 |
Assert.error("Can't get here!"); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1486 |
return null; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1487 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1488 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1489 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1490 |
boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1491 |
IncorporationBinaryOp newOp = infer.new IncorporationBinaryOp(opKind, op1, op2); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1492 |
Boolean res = infer.incorporationCache.get(newOp); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1493 |
if (res == null) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1494 |
infer.incorporationCache.put(newOp, res = newOp.apply(warn)); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1495 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1496 |
return res; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1497 |
} |
15717 | 1498 |
} |
1499 |
||
1500 |
/** |
|
1501 |
* This solver uses an heuristic to pick the best leaf - the heuristic |
|
1502 |
* tries to select the node that has maximal probability to contain one |
|
1503 |
* or more inference variables in a given list |
|
1504 |
*/ |
|
1505 |
abstract class BestLeafSolver extends LeafSolver { |
|
1506 |
||
19914 | 1507 |
/** list of ivars of which at least one must be solved */ |
15717 | 1508 |
List<Type> varsToSolve; |
1509 |
||
1510 |
BestLeafSolver(List<Type> varsToSolve) { |
|
1511 |
this.varsToSolve = varsToSolve; |
|
1512 |
} |
|
1513 |
||
1514 |
/** |
|
19914 | 1515 |
* Computes a path that goes from a given node to the leafs in the graph. |
1516 |
* Typically this will start from a node containing a variable in |
|
1517 |
* {@code varsToSolve}. For any given path, the cost is computed as the total |
|
1518 |
* number of type-variables that should be eagerly instantiated across that path. |
|
15717 | 1519 |
*/ |
19914 | 1520 |
Pair<List<Node>, Integer> computeTreeToLeafs(Node n) { |
1521 |
Pair<List<Node>, Integer> cachedPath = treeCache.get(n); |
|
1522 |
if (cachedPath == null) { |
|
1523 |
//cache miss |
|
1524 |
if (n.isLeaf()) { |
|
1525 |
//if leaf, stop |
|
22163 | 1526 |
cachedPath = new Pair<>(List.of(n), n.data.length()); |
19914 | 1527 |
} else { |
1528 |
//if non-leaf, proceed recursively |
|
22163 | 1529 |
Pair<List<Node>, Integer> path = new Pair<>(List.of(n), n.data.length()); |
19914 | 1530 |
for (Node n2 : n.getAllDependencies()) { |
1531 |
if (n2 == n) continue; |
|
1532 |
Pair<List<Node>, Integer> subpath = computeTreeToLeafs(n2); |
|
22163 | 1533 |
path = new Pair<>(path.fst.prependList(subpath.fst), |
1534 |
path.snd + subpath.snd); |
|
19914 | 1535 |
} |
1536 |
cachedPath = path; |
|
1537 |
} |
|
1538 |
//save results in cache |
|
1539 |
treeCache.put(n, cachedPath); |
|
1540 |
} |
|
1541 |
return cachedPath; |
|
18916
d93bea397df9
8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents:
18911
diff
changeset
|
1542 |
} |
d93bea397df9
8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents:
18911
diff
changeset
|
1543 |
|
19914 | 1544 |
/** cache used to avoid redundant computation of tree costs */ |
22163 | 1545 |
final Map<Node, Pair<List<Node>, Integer>> treeCache = new HashMap<>(); |
19914 | 1546 |
|
1547 |
/** constant value used to mark non-existent paths */ |
|
22163 | 1548 |
final Pair<List<Node>, Integer> noPath = new Pair<>(null, Integer.MAX_VALUE); |
15717 | 1549 |
|
1550 |
/** |
|
1551 |
* Pick the leaf that minimize cost |
|
1552 |
*/ |
|
1553 |
@Override |
|
1554 |
public Node pickNode(final InferenceGraph g) { |
|
19914 | 1555 |
treeCache.clear(); //graph changes at every step - cache must be cleared |
1556 |
Pair<List<Node>, Integer> bestPath = noPath; |
|
15717 | 1557 |
for (Node n : g.nodes) { |
19914 | 1558 |
if (!Collections.disjoint(n.data, varsToSolve)) { |
1559 |
Pair<List<Node>, Integer> path = computeTreeToLeafs(n); |
|
1560 |
//discard all paths containing at least a node in the |
|
1561 |
//closure computed above |
|
1562 |
if (path.snd < bestPath.snd) { |
|
1563 |
bestPath = path; |
|
1564 |
} |
|
15717 | 1565 |
} |
1566 |
} |
|
19914 | 1567 |
if (bestPath == noPath) { |
1568 |
//no path leads there |
|
1569 |
throw new NodeNotFoundException(g); |
|
1570 |
} |
|
1571 |
return bestPath.fst.head; |
|
15717 | 1572 |
} |
1573 |
} |
|
1574 |
||
1575 |
/** |
|
1576 |
* The inference process can be thought of as a sequence of steps. Each step |
|
1577 |
* instantiates an inference variable using a subset of the inference variable |
|
1578 |
* bounds, if certain condition are met. Decisions such as the sequence in which |
|
1579 |
* steps are applied, or which steps are to be applied are left to the inference engine. |
|
1580 |
*/ |
|
1581 |
enum InferenceStep { |
|
1582 |
||
1583 |
/** |
|
1584 |
* Instantiate an inference variables using one of its (ground) equality |
|
1585 |
* constraints |
|
1586 |
*/ |
|
1587 |
EQ(InferenceBound.EQ) { |
|
1588 |
@Override |
|
1589 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
1590 |
return filterBounds(uv, inferenceContext).head; |
|
1591 |
} |
|
1592 |
}, |
|
1593 |
/** |
|
1594 |
* Instantiate an inference variables using its (ground) lower bounds. Such |
|
1595 |
* bounds are merged together using lub(). |
|
1596 |
*/ |
|
1597 |
LOWER(InferenceBound.LOWER) { |
|
1598 |
@Override |
|
1599 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1600 |
Infer infer = inferenceContext.infer; |
15717 | 1601 |
List<Type> lobounds = filterBounds(uv, inferenceContext); |
18395
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1602 |
//note: lobounds should have at least one element |
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1603 |
Type owntype = lobounds.tail.tail == null ? lobounds.head : infer.types.lub(lobounds); |
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1604 |
if (owntype.isPrimitive() || owntype.hasTag(ERROR)) { |
15717 | 1605 |
throw infer.inferenceException |
1606 |
.setMessage("no.unique.minimal.instance.exists", |
|
1607 |
uv.qtype, lobounds); |
|
1608 |
} else { |
|
1609 |
return owntype; |
|
1610 |
} |
|
1611 |
} |
|
1612 |
}, |
|
1613 |
/** |
|
18909
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1614 |
* Infer uninstantiated/unbound inference variables occurring in 'throws' |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1615 |
* clause as RuntimeException |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1616 |
*/ |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1617 |
THROWS(InferenceBound.UPPER) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1618 |
@Override |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1619 |
public boolean accepts(UndetVar t, InferenceContext inferenceContext) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1620 |
if ((t.qtype.tsym.flags() & Flags.THROWS) == 0) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1621 |
//not a throws undet var |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1622 |
return false; |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1623 |
} |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1624 |
if (t.getBounds(InferenceBound.EQ, InferenceBound.LOWER, InferenceBound.UPPER) |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1625 |
.diff(t.getDeclaredBounds()).nonEmpty()) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1626 |
//not an unbounded undet var |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1627 |
return false; |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1628 |
} |
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1629 |
Infer infer = inferenceContext.infer; |
18909
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1630 |
for (Type db : t.getDeclaredBounds()) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1631 |
if (t.isInterface()) continue; |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1632 |
if (infer.types.asSuper(infer.syms.runtimeExceptionType, db.tsym) != null) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1633 |
//declared bound is a supertype of RuntimeException |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1634 |
return true; |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1635 |
} |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1636 |
} |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1637 |
//declared bound is more specific then RuntimeException - give up |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1638 |
return false; |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1639 |
} |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1640 |
|
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1641 |
@Override |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1642 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1643 |
return inferenceContext.infer.syms.runtimeExceptionType; |
18909
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1644 |
} |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1645 |
}, |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1646 |
/** |
15717 | 1647 |
* Instantiate an inference variables using its (ground) upper bounds. Such |
1648 |
* bounds are merged together using glb(). |
|
1649 |
*/ |
|
1650 |
UPPER(InferenceBound.UPPER) { |
|
1651 |
@Override |
|
1652 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1653 |
Infer infer = inferenceContext.infer; |
15717 | 1654 |
List<Type> hibounds = filterBounds(uv, inferenceContext); |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24226
diff
changeset
|
1655 |
//note: hibounds should have at least one element |
18395
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1656 |
Type owntype = hibounds.tail.tail == null ? hibounds.head : infer.types.glb(hibounds); |
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1657 |
if (owntype.isPrimitive() || owntype.hasTag(ERROR)) { |
15717 | 1658 |
throw infer.inferenceException |
1659 |
.setMessage("no.unique.maximal.instance.exists", |
|
1660 |
uv.qtype, hibounds); |
|
1661 |
} else { |
|
1662 |
return owntype; |
|
1663 |
} |
|
1664 |
} |
|
1665 |
}, |
|
1666 |
/** |
|
1667 |
* Like the former; the only difference is that this step can only be applied |
|
1668 |
* if all upper bounds are ground. |
|
1669 |
*/ |
|
1670 |
UPPER_LEGACY(InferenceBound.UPPER) { |
|
1671 |
@Override |
|
1672 |
public boolean accepts(UndetVar t, InferenceContext inferenceContext) { |
|
18911 | 1673 |
return !inferenceContext.free(t.getBounds(ib)) && !t.isCaptured(); |
15717 | 1674 |
} |
1675 |
||
1676 |
@Override |
|
1677 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
1678 |
return UPPER.solve(uv, inferenceContext); |
|
1679 |
} |
|
18911 | 1680 |
}, |
1681 |
/** |
|
1682 |
* Like the former; the only difference is that this step can only be applied |
|
1683 |
* if all upper/lower bounds are ground. |
|
1684 |
*/ |
|
1685 |
CAPTURED(InferenceBound.UPPER) { |
|
1686 |
@Override |
|
1687 |
public boolean accepts(UndetVar t, InferenceContext inferenceContext) { |
|
19127
8a0cbd5cb055
8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents:
18918
diff
changeset
|
1688 |
return t.isCaptured() && |
8a0cbd5cb055
8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents:
18918
diff
changeset
|
1689 |
!inferenceContext.free(t.getBounds(InferenceBound.UPPER, InferenceBound.LOWER)); |
18911 | 1690 |
} |
1691 |
||
1692 |
@Override |
|
1693 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1694 |
Infer infer = inferenceContext.infer; |
18911 | 1695 |
Type upper = UPPER.filterBounds(uv, inferenceContext).nonEmpty() ? |
1696 |
UPPER.solve(uv, inferenceContext) : |
|
1697 |
infer.syms.objectType; |
|
1698 |
Type lower = LOWER.filterBounds(uv, inferenceContext).nonEmpty() ? |
|
1699 |
LOWER.solve(uv, inferenceContext) : |
|
1700 |
infer.syms.botType; |
|
1701 |
CapturedType prevCaptured = (CapturedType)uv.qtype; |
|
24396 | 1702 |
return new CapturedType(prevCaptured.tsym.name, prevCaptured.tsym.owner, |
26781
a786b07c7b91
8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents:
26663
diff
changeset
|
1703 |
upper, lower, prevCaptured.wildcard); |
18911 | 1704 |
} |
15717 | 1705 |
}; |
1706 |
||
1707 |
final InferenceBound ib; |
|
1708 |
||
1709 |
InferenceStep(InferenceBound ib) { |
|
1710 |
this.ib = ib; |
|
1711 |
} |
|
1712 |
||
1713 |
/** |
|
1714 |
* Find an instantiated type for a given inference variable within |
|
1715 |
* a given inference context |
|
1716 |
*/ |
|
1717 |
abstract Type solve(UndetVar uv, InferenceContext inferenceContext); |
|
1718 |
||
1719 |
/** |
|
1720 |
* Can the inference variable be instantiated using this step? |
|
1721 |
*/ |
|
1722 |
public boolean accepts(UndetVar t, InferenceContext inferenceContext) { |
|
18911 | 1723 |
return filterBounds(t, inferenceContext).nonEmpty() && !t.isCaptured(); |
15717 | 1724 |
} |
1725 |
||
1726 |
/** |
|
1727 |
* Return the subset of ground bounds in a given bound set (i.e. eq/lower/upper) |
|
1728 |
*/ |
|
1729 |
List<Type> filterBounds(UndetVar uv, InferenceContext inferenceContext) { |
|
1730 |
return Type.filter(uv.getBounds(ib), new BoundFilter(inferenceContext)); |
|
1731 |
} |
|
1732 |
} |
|
1733 |
||
1734 |
/** |
|
1735 |
* This enumeration defines the sequence of steps to be applied when the |
|
1736 |
* solver works in legacy mode. The steps in this enumeration reflect |
|
1737 |
* the behavior of old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8). |
|
1738 |
*/ |
|
1739 |
enum LegacyInferenceSteps { |
|
1740 |
||
1741 |
EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)), |
|
1742 |
EQ_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.UPPER_LEGACY)); |
|
1743 |
||
1744 |
final EnumSet<InferenceStep> steps; |
|
1745 |
||
1746 |
LegacyInferenceSteps(EnumSet<InferenceStep> steps) { |
|
1747 |
this.steps = steps; |
|
1748 |
} |
|
1749 |
} |
|
1750 |
||
1751 |
/** |
|
1752 |
* This enumeration defines the sequence of steps to be applied when the |
|
1753 |
* graph solver is used. This order is defined so as to maximize compatibility |
|
1754 |
* w.r.t. old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8). |
|
1755 |
*/ |
|
1756 |
enum GraphInferenceSteps { |
|
1757 |
||
1758 |
EQ(EnumSet.of(InferenceStep.EQ)), |
|
1759 |
EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)), |
|
18911 | 1760 |
EQ_LOWER_THROWS_UPPER_CAPTURED(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER, InferenceStep.UPPER, InferenceStep.THROWS, InferenceStep.CAPTURED)); |
15717 | 1761 |
|
1762 |
final EnumSet<InferenceStep> steps; |
|
1763 |
||
1764 |
GraphInferenceSteps(EnumSet<InferenceStep> steps) { |
|
1765 |
this.steps = steps; |
|
1766 |
} |
|
1767 |
} |
|
1768 |
||
1769 |
/** |
|
19914 | 1770 |
* There are two kinds of dependencies between inference variables. The basic |
1771 |
* kind of dependency (or bound dependency) arises when a variable mention |
|
1772 |
* another variable in one of its bounds. There's also a more subtle kind |
|
1773 |
* of dependency that arises when a variable 'might' lead to better constraints |
|
1774 |
* on another variable (this is typically the case with variables holding up |
|
1775 |
* stuck expressions). |
|
1776 |
*/ |
|
1777 |
enum DependencyKind implements GraphUtils.DependencyKind { |
|
1778 |
||
1779 |
/** bound dependency */ |
|
1780 |
BOUND("dotted"), |
|
1781 |
/** stuck dependency */ |
|
1782 |
STUCK("dashed"); |
|
1783 |
||
1784 |
final String dotSyle; |
|
1785 |
||
1786 |
private DependencyKind(String dotSyle) { |
|
1787 |
this.dotSyle = dotSyle; |
|
1788 |
} |
|
1789 |
} |
|
1790 |
||
1791 |
/** |
|
15717 | 1792 |
* This is the graph inference solver - the solver organizes all inference variables in |
1793 |
* a given inference context by bound dependencies - in the general case, such dependencies |
|
1794 |
* would lead to a cyclic directed graph (hence the name); the dependency info is used to build |
|
1795 |
* an acyclic graph, where all cyclic variables are bundled together. An inference |
|
1796 |
* step corresponds to solving a node in the acyclic graph - this is done by |
|
1797 |
* relying on a given strategy (see GraphStrategy). |
|
1798 |
*/ |
|
1799 |
class GraphSolver { |
|
1800 |
||
1801 |
InferenceContext inferenceContext; |
|
19914 | 1802 |
Map<Type, Set<Type>> stuckDeps; |
15717 | 1803 |
Warner warn; |
1804 |
||
19914 | 1805 |
GraphSolver(InferenceContext inferenceContext, Map<Type, Set<Type>> stuckDeps, Warner warn) { |
15717 | 1806 |
this.inferenceContext = inferenceContext; |
19914 | 1807 |
this.stuckDeps = stuckDeps; |
15717 | 1808 |
this.warn = warn; |
1809 |
} |
|
1810 |
||
1811 |
/** |
|
1812 |
* Solve variables in a given inference context. The amount of variables |
|
1813 |
* to be solved, and the way in which the underlying acyclic graph is explored |
|
1814 |
* depends on the selected solver strategy. |
|
1815 |
*/ |
|
1816 |
void solve(GraphStrategy sstrategy) { |
|
1817 |
checkWithinBounds(inferenceContext, warn); //initial propagation of bounds |
|
19914 | 1818 |
InferenceGraph inferenceGraph = new InferenceGraph(stuckDeps); |
15717 | 1819 |
while (!sstrategy.done()) { |
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
1820 |
if (dependenciesFolder != null) { |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
1821 |
//add this graph to the pending queue |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
1822 |
pendingGraphs = pendingGraphs.prepend(inferenceGraph.toDot()); |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
1823 |
} |
15717 | 1824 |
InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph); |
1825 |
List<Type> varsToSolve = List.from(nodeToSolve.data); |
|
18904
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
1826 |
List<Type> saved_undet = inferenceContext.save(); |
15717 | 1827 |
try { |
1828 |
//repeat until all variables are solved |
|
1829 |
outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) { |
|
1830 |
//for each inference phase |
|
1831 |
for (GraphInferenceSteps step : GraphInferenceSteps.values()) { |
|
1832 |
if (inferenceContext.solveBasic(varsToSolve, step.steps)) { |
|
1833 |
checkWithinBounds(inferenceContext, warn); |
|
1834 |
continue outer; |
|
1835 |
} |
|
1836 |
} |
|
1837 |
//no progress |
|
18395
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1838 |
throw inferenceException.setMessage(); |
15717 | 1839 |
} |
1840 |
} |
|
1841 |
catch (InferenceException ex) { |
|
18395
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1842 |
//did we fail because of interdependent ivars? |
18904
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
1843 |
inferenceContext.rollback(saved_undet); |
15717 | 1844 |
instantiateAsUninferredVars(varsToSolve, inferenceContext); |
1845 |
checkWithinBounds(inferenceContext, warn); |
|
1846 |
} |
|
1847 |
inferenceGraph.deleteNode(nodeToSolve); |
|
1848 |
} |
|
1849 |
} |
|
1850 |
||
1851 |
/** |
|
1852 |
* The dependencies between the inference variables that need to be solved |
|
1853 |
* form a (possibly cyclic) graph. This class reduces the original dependency graph |
|
1854 |
* to an acyclic version, where cyclic nodes are folded into a single 'super node'. |
|
1855 |
*/ |
|
1856 |
class InferenceGraph { |
|
1857 |
||
1858 |
/** |
|
1859 |
* This class represents a node in the graph. Each node corresponds |
|
1860 |
* to an inference variable and has edges (dependencies) on other |
|
1861 |
* nodes. The node defines an entry point that can be used to receive |
|
1862 |
* updates on the structure of the graph this node belongs to (used to |
|
1863 |
* keep dependencies in sync). |
|
1864 |
*/ |
|
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1865 |
class Node extends GraphUtils.TarjanNode<ListBuffer<Type>, Node> implements DottableNode<ListBuffer<Type>, Node> { |
15717 | 1866 |
|
19914 | 1867 |
/** map listing all dependencies (grouped by kind) */ |
1868 |
EnumMap<DependencyKind, Set<Node>> deps; |
|
15717 | 1869 |
|
1870 |
Node(Type ivar) { |
|
1871 |
super(ListBuffer.of(ivar)); |
|
22163 | 1872 |
this.deps = new EnumMap<>(DependencyKind.class); |
19914 | 1873 |
} |
1874 |
||
1875 |
@Override |
|
1876 |
public GraphUtils.DependencyKind[] getSupportedDependencyKinds() { |
|
1877 |
return DependencyKind.values(); |
|
15717 | 1878 |
} |
1879 |
||
19914 | 1880 |
public Iterable<? extends Node> getAllDependencies() { |
1881 |
return getDependencies(DependencyKind.values()); |
|
15717 | 1882 |
} |
1883 |
||
1884 |
@Override |
|
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1885 |
public Collection<? extends Node> getDependenciesByKind(GraphUtils.DependencyKind dk) { |
19914 | 1886 |
return getDependencies((DependencyKind)dk); |
1887 |
} |
|
1888 |
||
1889 |
/** |
|
1890 |
* Retrieves all dependencies with given kind(s). |
|
1891 |
*/ |
|
1892 |
protected Set<Node> getDependencies(DependencyKind... depKinds) { |
|
22163 | 1893 |
Set<Node> buf = new LinkedHashSet<>(); |
19914 | 1894 |
for (DependencyKind dk : depKinds) { |
1895 |
Set<Node> depsByKind = deps.get(dk); |
|
1896 |
if (depsByKind != null) { |
|
1897 |
buf.addAll(depsByKind); |
|
15717 | 1898 |
} |
1899 |
} |
|
19914 | 1900 |
return buf; |
1901 |
} |
|
1902 |
||
1903 |
/** |
|
1904 |
* Adds dependency with given kind. |
|
1905 |
*/ |
|
1906 |
protected void addDependency(DependencyKind dk, Node depToAdd) { |
|
1907 |
Set<Node> depsByKind = deps.get(dk); |
|
1908 |
if (depsByKind == null) { |
|
22163 | 1909 |
depsByKind = new LinkedHashSet<>(); |
19914 | 1910 |
deps.put(dk, depsByKind); |
1911 |
} |
|
1912 |
depsByKind.add(depToAdd); |
|
1913 |
} |
|
1914 |
||
1915 |
/** |
|
1916 |
* Add multiple dependencies of same given kind. |
|
1917 |
*/ |
|
1918 |
protected void addDependencies(DependencyKind dk, Set<Node> depsToAdd) { |
|
1919 |
for (Node n : depsToAdd) { |
|
1920 |
addDependency(dk, n); |
|
1921 |
} |
|
1922 |
} |
|
1923 |
||
1924 |
/** |
|
1925 |
* Remove a dependency, regardless of its kind. |
|
1926 |
*/ |
|
1927 |
protected Set<DependencyKind> removeDependency(Node n) { |
|
1928 |
Set<DependencyKind> removedKinds = new HashSet<>(); |
|
1929 |
for (DependencyKind dk : DependencyKind.values()) { |
|
1930 |
Set<Node> depsByKind = deps.get(dk); |
|
1931 |
if (depsByKind == null) continue; |
|
1932 |
if (depsByKind.remove(n)) { |
|
1933 |
removedKinds.add(dk); |
|
1934 |
} |
|
1935 |
} |
|
1936 |
return removedKinds; |
|
15717 | 1937 |
} |
1938 |
||
19914 | 1939 |
/** |
1940 |
* Compute closure of a give node, by recursively walking |
|
1941 |
* through all its dependencies (of given kinds) |
|
1942 |
*/ |
|
1943 |
protected Set<Node> closure(DependencyKind... depKinds) { |
|
1944 |
boolean progress = true; |
|
22163 | 1945 |
Set<Node> closure = new HashSet<>(); |
19914 | 1946 |
closure.add(this); |
1947 |
while (progress) { |
|
1948 |
progress = false; |
|
22163 | 1949 |
for (Node n1 : new HashSet<>(closure)) { |
19914 | 1950 |
progress = closure.addAll(n1.getDependencies(depKinds)); |
1951 |
} |
|
1952 |
} |
|
1953 |
return closure; |
|
15717 | 1954 |
} |
1955 |
||
19914 | 1956 |
/** |
1957 |
* Is this node a leaf? This means either the node has no dependencies, |
|
1958 |
* or it just has self-dependencies. |
|
1959 |
*/ |
|
1960 |
protected boolean isLeaf() { |
|
1961 |
//no deps, or only one self dep |
|
1962 |
Set<Node> allDeps = getDependencies(DependencyKind.BOUND, DependencyKind.STUCK); |
|
1963 |
if (allDeps.isEmpty()) return true; |
|
1964 |
for (Node n : allDeps) { |
|
1965 |
if (n != this) { |
|
1966 |
return false; |
|
1967 |
} |
|
1968 |
} |
|
1969 |
return true; |
|
1970 |
} |
|
1971 |
||
1972 |
/** |
|
1973 |
* Merge this node with another node, acquiring its dependencies. |
|
1974 |
* This routine is used to merge all cyclic node together and |
|
1975 |
* form an acyclic graph. |
|
1976 |
*/ |
|
1977 |
protected void mergeWith(List<? extends Node> nodes) { |
|
15717 | 1978 |
for (Node n : nodes) { |
1979 |
Assert.check(n.data.length() == 1, "Attempt to merge a compound node!"); |
|
1980 |
data.appendList(n.data); |
|
19914 | 1981 |
for (DependencyKind dk : DependencyKind.values()) { |
1982 |
addDependencies(dk, n.getDependencies(dk)); |
|
1983 |
} |
|
15717 | 1984 |
} |
1985 |
//update deps |
|
22163 | 1986 |
EnumMap<DependencyKind, Set<Node>> deps2 = new EnumMap<>(DependencyKind.class); |
19914 | 1987 |
for (DependencyKind dk : DependencyKind.values()) { |
1988 |
for (Node d : getDependencies(dk)) { |
|
1989 |
Set<Node> depsByKind = deps2.get(dk); |
|
1990 |
if (depsByKind == null) { |
|
22163 | 1991 |
depsByKind = new LinkedHashSet<>(); |
19914 | 1992 |
deps2.put(dk, depsByKind); |
1993 |
} |
|
1994 |
if (data.contains(d.data.first())) { |
|
1995 |
depsByKind.add(this); |
|
1996 |
} else { |
|
1997 |
depsByKind.add(d); |
|
1998 |
} |
|
15717 | 1999 |
} |
2000 |
} |
|
2001 |
deps = deps2; |
|
2002 |
} |
|
2003 |
||
19914 | 2004 |
/** |
2005 |
* Notify all nodes that something has changed in the graph |
|
2006 |
* topology. |
|
2007 |
*/ |
|
2008 |
private void graphChanged(Node from, Node to) { |
|
2009 |
for (DependencyKind dk : removeDependency(from)) { |
|
15717 | 2010 |
if (to != null) { |
19914 | 2011 |
addDependency(dk, to); |
15717 | 2012 |
} |
2013 |
} |
|
2014 |
} |
|
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2015 |
|
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2016 |
@Override |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2017 |
public Properties nodeAttributes() { |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2018 |
Properties p = new Properties(); |
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
2019 |
p.put("label", "\"" + toString() + "\""); |
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2020 |
return p; |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2021 |
} |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2022 |
|
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2023 |
@Override |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2024 |
public Properties dependencyAttributes(Node sink, GraphUtils.DependencyKind dk) { |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2025 |
Properties p = new Properties(); |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2026 |
p.put("style", ((DependencyKind)dk).dotSyle); |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2027 |
if (dk == DependencyKind.STUCK) return p; |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2028 |
else { |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2029 |
StringBuilder buf = new StringBuilder(); |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2030 |
String sep = ""; |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2031 |
for (Type from : data) { |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2032 |
UndetVar uv = (UndetVar)inferenceContext.asUndetVar(from); |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2033 |
for (Type bound : uv.getBounds(InferenceBound.values())) { |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2034 |
if (bound.containsAny(List.from(sink.data))) { |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2035 |
buf.append(sep); |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2036 |
buf.append(bound); |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2037 |
sep = ","; |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2038 |
} |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2039 |
} |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2040 |
} |
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
2041 |
p.put("label", "\"" + buf.toString() + "\""); |
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2042 |
} |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2043 |
return p; |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
2044 |
} |
15717 | 2045 |
} |
2046 |
||
2047 |
/** the nodes in the inference graph */ |
|
2048 |
ArrayList<Node> nodes; |
|
2049 |
||
19914 | 2050 |
InferenceGraph(Map<Type, Set<Type>> optDeps) { |
2051 |
initNodes(optDeps); |
|
2052 |
} |
|
2053 |
||
2054 |
/** |
|
2055 |
* Basic lookup helper for retrieving a graph node given an inference |
|
2056 |
* variable type. |
|
2057 |
*/ |
|
2058 |
public Node findNode(Type t) { |
|
2059 |
for (Node n : nodes) { |
|
2060 |
if (n.data.contains(t)) { |
|
2061 |
return n; |
|
2062 |
} |
|
2063 |
} |
|
2064 |
return null; |
|
15717 | 2065 |
} |
2066 |
||
2067 |
/** |
|
2068 |
* Delete a node from the graph. This update the underlying structure |
|
2069 |
* of the graph (including dependencies) via listeners updates. |
|
2070 |
*/ |
|
2071 |
public void deleteNode(Node n) { |
|
2072 |
Assert.check(nodes.contains(n)); |
|
2073 |
nodes.remove(n); |
|
2074 |
notifyUpdate(n, null); |
|
2075 |
} |
|
2076 |
||
2077 |
/** |
|
2078 |
* Notify all nodes of a change in the graph. If the target node is |
|
2079 |
* {@code null} the source node is assumed to be removed. |
|
2080 |
*/ |
|
2081 |
void notifyUpdate(Node from, Node to) { |
|
2082 |
for (Node n : nodes) { |
|
2083 |
n.graphChanged(from, to); |
|
2084 |
} |
|
2085 |
} |
|
2086 |
||
2087 |
/** |
|
2088 |
* Create the graph nodes. First a simple node is created for every inference |
|
2089 |
* variables to be solved. Then Tarjan is used to found all connected components |
|
2090 |
* in the graph. For each component containing more than one node, a super node is |
|
19914 | 2091 |
* created, effectively replacing the original cyclic nodes. |
15717 | 2092 |
*/ |
19914 | 2093 |
void initNodes(Map<Type, Set<Type>> stuckDeps) { |
2094 |
//add nodes |
|
22163 | 2095 |
nodes = new ArrayList<>(); |
15717 | 2096 |
for (Type t : inferenceContext.restvars()) { |
2097 |
nodes.add(new Node(t)); |
|
2098 |
} |
|
19914 | 2099 |
//add dependencies |
15717 | 2100 |
for (Node n_i : nodes) { |
2101 |
Type i = n_i.data.first(); |
|
19914 | 2102 |
Set<Type> optDepsByNode = stuckDeps.get(i); |
15717 | 2103 |
for (Node n_j : nodes) { |
2104 |
Type j = n_j.data.first(); |
|
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
2105 |
UndetVar uv_i = (UndetVar)inferenceContext.asUndetVar(i); |
15717 | 2106 |
if (Type.containsAny(uv_i.getBounds(InferenceBound.values()), List.of(j))) { |
19914 | 2107 |
//update i's bound dependencies |
2108 |
n_i.addDependency(DependencyKind.BOUND, n_j); |
|
2109 |
} |
|
2110 |
if (optDepsByNode != null && optDepsByNode.contains(j)) { |
|
2111 |
//update i's stuck dependencies |
|
2112 |
n_i.addDependency(DependencyKind.STUCK, n_j); |
|
15717 | 2113 |
} |
2114 |
} |
|
2115 |
} |
|
19914 | 2116 |
//merge cyclic nodes |
22163 | 2117 |
ArrayList<Node> acyclicNodes = new ArrayList<>(); |
15717 | 2118 |
for (List<? extends Node> conSubGraph : GraphUtils.tarjan(nodes)) { |
2119 |
if (conSubGraph.length() > 1) { |
|
2120 |
Node root = conSubGraph.head; |
|
2121 |
root.mergeWith(conSubGraph.tail); |
|
2122 |
for (Node n : conSubGraph) { |
|
2123 |
notifyUpdate(n, root); |
|
2124 |
} |
|
2125 |
} |
|
16321
75e64bcd3ab0
8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents:
15717
diff
changeset
|
2126 |
acyclicNodes.add(conSubGraph.head); |
15717 | 2127 |
} |
16321
75e64bcd3ab0
8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents:
15717
diff
changeset
|
2128 |
nodes = acyclicNodes; |
15717 | 2129 |
} |
2130 |
||
2131 |
/** |
|
2132 |
* Debugging: dot representation of this graph |
|
2133 |
*/ |
|
2134 |
String toDot() { |
|
2135 |
StringBuilder buf = new StringBuilder(); |
|
2136 |
for (Type t : inferenceContext.undetvars) { |
|
2137 |
UndetVar uv = (UndetVar)t; |
|
2138 |
buf.append(String.format("var %s - upper bounds = %s, lower bounds = %s, eq bounds = %s\\n", |
|
2139 |
uv.qtype, uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.LOWER), |
|
2140 |
uv.getBounds(InferenceBound.EQ))); |
|
2141 |
} |
|
2142 |
return GraphUtils.toDot(nodes, "inferenceGraph" + hashCode(), buf.toString()); |
|
2143 |
} |
|
2144 |
} |
|
2145 |
} |
|
2146 |
// </editor-fold> |
|
2147 |
||
2148 |
// <editor-fold defaultstate="collapsed" desc="Inference context"> |
|
2149 |
/** |
|
15705 | 2150 |
* Functional interface for defining inference callbacks. Certain actions |
2151 |
* (i.e. subtyping checks) might need to be redone after all inference variables |
|
2152 |
* have been fixed. |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2153 |
*/ |
15705 | 2154 |
interface FreeTypeListener { |
2155 |
void typesInferred(InferenceContext inferenceContext); |
|
2156 |
} |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2157 |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
2158 |
final InferenceContext emptyContext; |
15705 | 2159 |
// </editor-fold> |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2160 |
} |