---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 8 Edition"
java_version: 8
chapter: "Chapter 18. Type Inference"
section: "18.5.2"
heading: "18.5.2. Invocation Type Inference"
parent: "18.5. Uses of Inference"
source: https://docs.oracle.com/javase/specs/jls/se8/html/jls-18.html#jls-18.5.2
id: "18.5.2"
url: https://jvm.obfuscated.software/jls/se8/18.5.2.html
raw: https://jvm.obfuscated.software/jls/se8/18.5.2.md
---

# 18.5.2. Invocation Type Inference

Given a method invocation that provides no explicit type arguments, and a corresponding most specific applicable generic method `m`, the process to infer the invocation type ([§15.12.2.6](15.12.2.6.md)) of the chosen method is as follows:

- Let θ be the substitution `[`P_1:=α_1, ..., P_p:=α_p`]` defined in [§18.5.1](18.5.1.md) to replace the type parameters of `m` with inference variables.

- Let B_2 be the bound set produced by reduction in order to demonstrate that `m` is applicable in [§18.5.1](18.5.1.md). (While it was necessary in [§18.5.1](18.5.1.md) to demonstrate that the inference variables in B_2 could be resolved, in order to establish applicability, the instantiations produced by this resolution step are *not* considered part of B_2.)

- If the invocation is not a poly expression, let the bound set B_3 be the same as B_2.

  If the invocation is a poly expression, let the bound set B_3 be derived from B_2 as follows. Let R be the return type of `m`, let T be the invocation's target type, and then:

  - If unchecked conversion was necessary for the method to be applicable during constraint set reduction in [§18.5.1](18.5.1.md), the constraint formula ‹|R| → T› is reduced and incorporated with B_2.

  - Otherwise, if R θ is a parameterized type, G`<`A_1, ..., A_n`>`, and one of A_1, ..., A_n is a wildcard, then, for fresh inference variables β_1, ..., β_n, the constraint formula ‹G`<`β_1, ..., β_n`>` → T› is reduced and incorporated, along with the bound G`<`β_1, ..., β_n`>` = capture(G`<`A_1, ..., A_n`>`), with B_2.

  - Otherwise, if R θ is an inference variable α, and one of the following is true:

    - T is a reference type, but is not a wildcard-parameterized type, and either i) B_2 contains a bound of one of the forms α = S or S `<:` α, where S is a wildcard-parameterized type, or ii) B_2 contains two bounds of the forms S_1 `<:` α and S_2 `<:` α, where S_1 and S_2 have supertypes that are two different parameterizations of the same generic class or interface.
    - T is a parameterization of a generic class or interface, G, and B_2 contains a bound of one of the forms α = S or S `<:` α, where there exists no type of the form G`<`...`>` that is a supertype of S, but the raw type |G`<`...`>`| is a supertype of S.
    - T is a primitive type, and one of the primitive wrapper classes mentioned in [§5.1.7](5.1.7.md) is an instantiation, upper bound, or lower bound for α in B_2.

    then α is resolved in B_2, and where the capture of the resulting instantiation of α is U, the constraint formula ‹U → T› is reduced and incorporated with B_2.

  - Otherwise, the constraint formula ‹R θ → T› is reduced and incorporated with B_2.

- A set of constraint formulas, C, is constructed as follows.

  Let `e_1`, ..., `e_k` be the actual argument expressions of the invocation. If `m` is applicable by strict or loose invocation, let F_1, ..., F_k be the formal parameter types of `m`; if `m` is applicable by variable arity invocation, let F_1, ..., F_k the first *k* variable arity parameter types of `m` ([§15.12.2.4](15.12.2.4.md)). Then:

  - For all *i* (1 ≤ *i* ≤ *k*), if `e_i` is not pertinent to applicability, C contains ‹`e_i` → F_i θ›.

  - For all *i* (1 ≤ *i* ≤ *k*), additional constraints may be included, depending on the form of `e_i`:

    - If `e_i` is a *LambdaExpression*, C contains ‹*LambdaExpression* →_{throws} F_i θ›.

      In addition, the lambda body is searched for additional constraints:

      - For a block lambda body, the search is applied recursively to each result expression.
      - For a poly class instance creation expression ([§15.9](15.9.md)) or a poly method invocation expression ([§15.12](15.12.md)), C contains all the constraint formulas that would appear in the set C generated by §18.5.2 when inferring the poly expression's invocation type.
      - For a parenthesized expression, the search is applied recursively to the contained expression.
      - For a conditional expression, the search is applied recursively to the second and third operands.
      - For a lambda expression, the search is applied recursively to the lambda body.

    - If `e_i` is a *MethodReference*, C contains ‹*MethodReference* →_{throws} F_i θ›.

    - If `e_i` is a poly class instance creation expression ([§15.9](15.9.md)) or a poly method invocation expression ([§15.12](15.12.md)), C contains all the constraint formulas that would appear in the set C generated by §18.5.2 when inferring the poly expression's invocation type.

    - If `e_i` is a parenthesized expression, these rules are applied recursively to the contained expression.

    - If `e_i` is a conditional expression, these rules are applied recursively to the second and third operands.

- While C is not empty, the following process is repeated, starting with the bound set B_3 and accumulating new bounds into a "current" bound set, ultimately producing a new bound set, B_4:

  1. A subset of constraints is selected in C, satisfying the property that, for each constraint, no input variable can influence an output variable of another constraint in C. The terms *input variable* and *output variable* are defined below. An inference variable α *can influence* an inference variable β if α depends on the resolution of β ([§18.4](18.4.md)), or vice versa; or if there exists a third inference variable γ such that α can influence γ and γ can influence β.

     If this subset is empty, then there is a cycle (or cycles) in the graph of dependencies between constraints. In this case, all constraints are considered that participate in a dependency cycle (or cycles) and do not depend on any constraints outside of the cycle (or cycles). A single constraint is selected from the considered constraints, as follows:

     - If any of the considered constraints have the form ‹*Expression* → T›, then the selected constraint is the considered constraint of this form that contains the expression to the left ([§3.5](3.5.md)) of the expression of every other considered constraint of this form.
     - If no considered constraint has the form ‹*Expression* → T›, then the selected constraint is the considered constraint that contains the expression to the left of the expression of every other considered constraint.

  2. The selected constraint(s) are removed from C.

  3. The input variables α_1, ..., α_m of all the selected constraint(s) are resolved.

  4. Where T_1, ..., T_m are the instantiations of α_1, ..., α_m, the substitution `[`α_1:=T_1, ..., α_m:=T_m`]` is applied to every constraint.

  5. The constraint(s) resulting from substitution are reduced and incorporated with the current bound set.

- Finally, if B_4 does not contain the bound *false*, the inference variables in B_4 are resolved.

  If resolution succeeds with instantiations T_1, ..., T_p for inference variables α_1, ..., α_p, let θ' be the substitution `[`P_1:=T_1, ..., P_p:=T_p`]`. Then:

  - If unchecked conversion was necessary for the method to be applicable during constraint set reduction in [§18.5.1](18.5.1.md), then the parameter types of the invocation type of `m` are obtained by applying θ' to the parameter types of `m`'s type, and the return type and thrown types of the invocation type of `m` are given by the erasure of the return type and thrown types of `m`'s type.
  - If unchecked conversion was not necessary for the method to be applicable, then the invocation type of `m` is obtained by applying θ' to the type of `m`.

  If B_4 contains the bound *false*, or if resolution fails, then a compile-time error occurs.

Invocation type inference may require carefully sequencing the reduction of constraint formulas of the forms ‹*Expression* → T›, ‹*LambdaExpression* →_{throws} T›, and ‹*MethodReference* →_{throws} T›. To facilitate this sequencing, the *input variables* of these constraints are defined as follows:

- For ‹*LambdaExpression* → T›:

  - If T is an inference variable, it is the (only) input variable.
  - If T is a functional interface type, and a function type can be derived from T ([§15.27.3](15.27.3.md)), then the input variables include i) if the lambda expression is implicitly typed, the inference variables mentioned by the function type's parameter types; and ii) if the function type's return type, R, is not `void`, then for each result expression `e` in the lambda body (or for the body itself if it is an expression), the input variables of ‹`e` → R›.
  - Otherwise, there are no input variables.

- For ‹*LambdaExpression* →_{throws} T›:

  - If T is an inference variable, it is the (only) input variable.
  - If T is a functional interface type, and a function type can be derived, as described in [§15.27.3](15.27.3.md), the input variables include i) if the lambda expression is implicitly typed, the inference variables mentioned by the function type's parameter types; and ii) the inference variables mentioned by the function type's return type.
  - Otherwise, there are no input variables.

- For ‹*MethodReference* → T›:

  - If T is an inference variable, it is the (only) input variable.
  - If T is a functional interface type with a function type, and if the method reference is inexact ([§15.13.1](15.13.1.md)), the input variables are the inference variables mentioned by the function type's parameter types.
  - Otherwise, there are no input variables.

- For ‹*MethodReference* →_{throws} T›:

  - If T is an inference variable, it is the (only) input variable.
  - If T is a functional interface type with a function type, and if the method reference is inexact ([§15.13.1](15.13.1.md)), the input variables are the inference variables mentioned by the function type's parameter types and the function type's return type.
  - Otherwise, there are no input variables.

- For ‹*Expression* → T›, if *Expression* is a parenthesized expression:

  Where the contained expression of *Expression* is *Expression*', the input variables are the input variables of ‹*Expression*' → T›.

- For ‹*ConditionalExpression* → T›:

  Where the conditional expression has the form `e_1` `?` `e_2` `:` `e_3`, the input variables are the input variables of ‹`e_2` → T› and ‹`e_3` → T›.

- For all other constraint formulas, there are no input variables.

The *output variables* of these constraints are all inference variables mentioned by the type on the right-hand side of the constraint, T, that are not input variables.

It is important to note that two "rounds" of inference are involved in finding the type of a method invocation. This is necessary to allow a target type to influence the type of the invocation without allowing it to influence the choice of an applicable method. The first round produces a bound set and tests that a resolution exists, but does not commit to that resolution. The second round reduces additional constraints and then performs a second resolution, this time "for real".

Consider the example from the previous section:

```
List<Number> ln = Arrays.asList(1, 2.0);
```

The most specific applicable method was identified as:

```
public static <T> List<T> asList(T... a)
```

In order to complete type-checking of the method invocation, we must determine whether it is compatible with its target type, `List<Number>`.

The bound set used to demonstrate applicability in the previous section, B_2, was:

{ α `<:` `Object`, `Integer` `<:` α, `Double` `<:` α }

The new constraint formula set is as follows:

{ ‹`List<α>` → `List<Number>`› }

This compatibility constraint produces an equality bound for α, which is included in the new bound set, B_3:

{ α `<:` `Object`, `Integer` `<:` α, `Double` `<:` α, α = `Number` }

These bounds are trivially resolved:

α = `Number`

Finally, we perform a substitution on the declared return type of `asList` to determine that the method invocation has type `List<Number>`; clearly, this is compatible with the target type.

This inference strategy is different than the Java SE 7 Edition of *The Java® Language Specification*, which would have instantiated α based on its lower bounds (before even considering the invocation's target type), as we did in the previous section. This would result in a type error, since the resulting type is not a subtype of `List<Number>`.

Under various special circumstances, based on the bounds appearing in B_2, we eagerly resolve an inference variable that appears as the return type of the invocation. This is to avoid unfortunate situations in which the usual constraint, ‹R θ → T›, is not completeness-preserving. It is, unfortunately, possible that by eagerly resolving the variable, we are unable to make use of bounds that would be inferred later. It is also possible that, in some cases, bounds that will later be inferred from the invocation arguments (such as implicitly typed lambda expressions) would have caused a different outcome if they had been present in B_2. Despite these limitations, the strategy allows for reasonable outcomes in typical use cases, and is backwards compatible with the algorithm in the Java SE 7 Edition of *The Java® Language Specification*.
