---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 15 Edition"
java_version: 15
chapter: "Chapter 18. Type Inference"
section: "18.2.1"
heading: "18.2.1. Expression Compatibility Constraints"
parent: "18.2. Reduction"
source: https://docs.oracle.com/javase/specs/jls/se15/html/jls-18.html#jls-18.2.1
id: "18.2.1"
url: https://jvm.obfuscated.software/jls/se15/18.2.1.html
raw: https://jvm.obfuscated.software/jls/se15/18.2.1.md
---

# 18.2.1. Expression Compatibility Constraints

A constraint formula of the form ‹*Expression* → T› is reduced as follows:

- If T is a proper type, the constraint reduces to *true* if the expression is compatible in a loose invocation context with T ([§5.3](5.3.md)), and *false* otherwise.

- Otherwise, if the expression is a standalone expression ([§15.2](15.2.md)) of type S, the constraint reduces to ‹S → T›.

- Otherwise, the expression is a poly expression ([§15.2](15.2.md)). The result depends on the form of the expression:

  - If the expression is a parenthesized expression of the form `(` *Expression*' `)`, the constraint reduces to ‹*Expression*' → T›.

  - If the expression is a class instance creation expression or a method invocation expression, the constraint reduces to the bound set B_3 which would be used to determine the expression's compatibility with target type T, as defined in [§18.5.2.1](18.5.2.1.md). (For a class instance creation expression, the corresponding "method" used for inference is defined in [§15.9.3](15.9.3.md).)

    This bound set may contain new inference variables, as well as dependencies between these new variables and the inference variables in T.

  - If the expression is a conditional expression of the form `e_1` `?` `e_2` `:` `e_3`, the constraint reduces to two constraint formulas, ‹`e_2` → T› and ‹`e_3` → T›.

  - If the expression is a lambda expression or a method reference expression, the result is specified below.

  - If the expression is a `switch` expression with result expressions `e_1`, ..., `e_n`, the constraint reduces to *n* constraint formulas, ‹`e_1` → T›, ..., ‹`e_n` → T›.

By treating nested generic method invocations as poly expressions, we improve the behavior of inference for nested invocations. For example, the following is illegal in Java SE 7 but legal in Java SE 8:

```
ProcessBuilder b = new ProcessBuilder(Collections.emptyList());
  // ProcessBuilder's constructor expects a List<String>
```

When *both* the outer and the nested invocation require inference, the problem is more difficult. For example:

```
List<String> ls = new ArrayList<>(Collections.emptyList());
```

Our approach is to "lift" the bounds inferred for the nested invocation (simply { α `<:` `Object` } in the case of `emptyList`) into the outer inference process (in this case, trying to infer β where the constructor is for type `ArrayList<`β`>`). We also infer dependencies between the nested inference variables and the outer inference variables (the constraint ‹`List<`α`>` → `Collection<`β`>`› would reduce to the dependency α = β). In this way, resolution of the inference variables in the nested invocation can wait until additional information can be inferred from the outer invocation (based on the assignment target, β = `String`).

A constraint formula of the form ‹*LambdaExpression* → T›, where T mentions at least one inference variable, is reduced as follows:

- If T is not a functional interface type ([§9.8](9.8.md)), the constraint reduces to *false*.

- Otherwise, let T' be the ground target type derived from T, as specified in [§15.27.3](15.27.3.md). If [§18.5.3](18.5.3.md) is used to derive a functional interface type which is parameterized, then the test that F`<`A'_1, ..., A'_m`>` is a subtype of F`<`A_1, ..., A_m`>` is not performed (instead, it is asserted with a constraint formula below). Let the target function type for the lambda expression be the function type of T'. Then:

  - If no valid function type can be found, the constraint reduces to *false*.

  - Otherwise, the congruence of *LambdaExpression* with the target function type is asserted as follows:

    - If the number of lambda parameters differs from the number of parameter types of the function type, the constraint reduces to *false*.

    - If the lambda expression is implicitly typed and one or more of the function type's parameter types is not a proper type, the constraint reduces to *false*.

      This condition never arises in practice, due to the handling of implicitly typed lambda expressions in [§18.5.1](18.5.1.md) and the substitution applied to the target type in [§18.5.2.2](18.5.2.2.md).

    - If the function type's result is `void` and the lambda body is neither a statement expression nor a void-compatible block, the constraint reduces to *false*.

    - If the function type's result is not `void` and the lambda body is a block that is not value-compatible, the constraint reduces to *false*.

    - Otherwise, the constraint reduces to all of the following constraint formulas:

      - If the lambda parameters have explicitly declared types F_1, ..., F_n and the function type has parameter types G_1, ..., G_n, then (i) for all *i* (1 ≤ *i* ≤ *n*), ‹F_i = G_i›, and (ii) ‹T' `<:` T›.

      - If the function type's return type is a (non-`void`) type R, assume the lambda's parameter types are the same as the function type's parameter types. Then:

        - If R is a proper type, and if the lambda body or some result expression in the lambda body is not compatible in an assignment context with R, then *false*.
        - Otherwise, if R is not a proper type, then where the lambda body has the form *Expression*, the constraint ‹*Expression* → R›; or where the lambda body is a block with result expressions `e_1`, ..., `e_m`, for all *i* (1 ≤ *i* ≤ *m*), ‹`e_i` → R›.

The key piece of information to derive from a compatibility constraint involving a lambda expression is the set of bounds on inference variables appearing in the target function type's return type. This is crucial, because functional interfaces are often generic, and many methods operating on these types are generic, too.

In the simplest case, a lambda expression may simply provide a lower bound for an inference variable:

```
<T> List<T> makeThree(Factory<T> factory) { ... }
String s = makeThree(() -> "abc").get(2);
```

In more complex cases, a result expression may be a poly expression - perhaps even another lambda expression - and so the inference variable might be passed through multiple constraint formulas with different target types before a bound is produced.

Most of the work described in this section precedes assertions about the result expressions; its purpose is to derive the lambda expression's function type, and to check for expressions that are clearly disqualified from compatibility.

We do *not* attempt to produce bounds on inference variables that appear in the target function type's `throws` clause. This is because exception containment is not part of compatibility ([§15.27.3](15.27.3.md)) - in particular, it must not influence method applicability ([§18.5.1](18.5.1.md)). However, we *do* get bounds on these variables later, because invocation type inference ([§18.5.2.2](18.5.2.2.md)) produces exception containment constraint formulas ([§18.2.5](18.2.5.md)).

Note that if the target type is an inference variable, or if the target type's parameter types contain inference variables, we produce *false*. During invocation type inference ([§18.5.2.2](18.5.2.2.md)), extra substitutions are performed in order to instantiate these inference variables, thus avoiding this scenario. (In other words, reduction will, in practice, never be "invoked" with a target type of one of these forms.)

Finally, note that the result expressions of a lambda expression are required by [§15.27.3](15.27.3.md) to be compatible in an assignment context with the target type's return type, R. If R is a proper type, such as `Byte` derived from `Function<α,Byte>`, then assignability is easy enough to test, and reduction does so above. If R is not a proper type, such as α derived from `Function<String,α>`, then we make the simplifying assumption above that loose invocation compatibility will be sufficient. The difference between assignment compatibility and loose invocation compatibility is that only assignment allows narrowing of constant expressions, such as `Byte b = 100;`. Consequently, our simplifying assumption is not completeness-preserving: given target return type α and an integer literal result expression `100`, it is conceivable that α could be instantiated to `Byte`, but reduction will not in fact produce such a bound.

A constraint formula of the form ‹*MethodReference* → T›, where T mentions at least one inference variable, is reduced as follows:

- If T is not a functional interface type, or if T is a functional interface type that does not have a function type ([§9.9](9.9.md)), the constraint reduces to *false*.

- Otherwise, if there does not exist a potentially applicable method for the method reference when targeting T, the constraint reduces to *false*.

- Otherwise, if the method reference is exact ([§15.13.1](15.13.1.md)), then let P_1, ..., P_n be the parameter types of the function type of T, and let F_1, ..., F_k be the parameter types of the potentially applicable method. The constraint reduces to a new set of constraints, as follows:

  - In the special case where *n* = *k*+1, the parameter of type P_1 is to act as the target reference of the invocation. The method reference expression necessarily has the form *ReferenceType `::` [TypeArguments] Identifier*. The constraint reduces to ‹P_1 `<:` *ReferenceType*› and, for all *i* (2 ≤ *i* ≤ *n*), ‹P_i → F_{i-1}›.

    In all other cases, *n* = *k*, and the constraint reduces to, for all *i* (1 ≤ *i* ≤ *n*), ‹P_i → F_i›.

  - If the function type's result is not `void`, let R be its return type. Then, if the result of the potentially applicable compile-time declaration is `void`, the constraint reduces to *false*. Otherwise, the constraint reduces to ‹R' → R›, where R' is the result of applying capture conversion ([§5.1.10](5.1.10.md)) to the return type of the potentially applicable compile-time declaration.

- Otherwise, the method reference is inexact, and:

  - If one or more of the function type's parameter types is not a proper type, the constraint reduces to *false*.

    This condition never arises in practice, due to the handling of inexact method references in [§18.5.1](18.5.1.md) and the substitution applied to the target type in [§18.5.2.2](18.5.2.2.md).

  - Otherwise, a search for a compile-time declaration is performed, as specified in [§15.13.1](15.13.1.md). If there is no compile-time declaration for the method reference, the constraint reduces to *false*. Otherwise, there is a compile-time declaration, and: (let R be the result of the function type)

    - If R is `void`, the constraint reduces to *true*.

    - Otherwise, if the method reference expression elides *TypeArguments*, and the compile-time declaration is a generic method, and the return type of the compile-time declaration mentions at least one of the method's type parameters, then:

      - If R mentions one of the type parameters of the function type, the constraint reduces to *false*.

        In this case, a constraint in terms of R might lead an inference variable to be bound by an out-of-scope type variable. Since instantiating an inference variable with an out-of-scope type variable is nonsensical, we prefer to avoid the situation by giving up immediately whenever the possibility arises. This simplification is not completeness-preserving.

      - If R does not mention one of the type parameters of the function type, then the constraint reduces to the bound set B_3 which would be used to determine the method reference's compatibility when targeting the return type of the function type, as defined in [§18.5.2.1](18.5.2.1.md). B_3 may contain new inference variables, as well as dependencies between these new variables and the inference variables in T.

        The strategy used to determine a return type for a generic referenced method follows the pattern used earlier in this section for generic method invocations. This may involve "lifting" bounds into the outer context and inferring dependencies between the two sets of inference variables.

    - Otherwise, let R' be the result of applying capture conversion ([§5.1.10](5.1.10.md)) to the return type of the invocation type ([§15.12.2.6](15.12.2.6.md)) of the compile-time declaration. If R' is `void`, the constraint reduces to *false*; otherwise, the constraint reduces to ‹R' → R›.
