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