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