Skip to content

Latest commit

 

History

History
124 lines (97 loc) · 8.56 KB

LDM-2026-08-12.md

File metadata and controls

124 lines (97 loc) · 8.56 KB

C# Language Design Meeting for August 12th, 2026

Agenda

Quote of the Day

  • "TTL (time to lattice mentioned): ~12 minutes"

Discussion

Type parameter inference from constraints implementation review

Champion issue: #9453
Spec: https://github.com/dotnet/csharplang/blob/05eb4800fc3dc76259ccd49ac01f1eeb49222380/proposals/type-parameter-inference-from-constraints.md
Implementation: dotnet/roslyn#84655

We reviewed the implementation of type parameter inference from constraints. Today, method type inference obtains bounds from arguments, so it cannot infer a type parameter that appears only in another type parameter's constraints. For example, given M<TEnumerable, TElement>(TEnumerable value) where TEnumerable : IEnumerable<TElement>, a List<int> argument provides a bound for TEnumerable but no direct bound for TElement. The proposal allows the constraint relationship to supply the missing information.

The implementation extends the dependency relationship between type variables. In the example, TElement depends on TEnumerable because it occurs in a constraint on TEnumerable. Once TEnumerable has been fixed, the compiler can perform lower-bound inference from its fixed type to the constraint and obtain a bound for TElement.

The simplest implementation applied these new bounds as each type parameter was fixed, but that made inference depend on type parameter declaration order. A parameter considered later could see information unavailable to an equivalent parameter considered earlier. The implementation instead fixes parameters in batches: it computes every parameter in a batch from the same snapshot, commits the results together, propagates the resulting constraint bounds, and then begins the next batch. This preserves ordering independence while still allowing information to flow through multiple levels of constraints.

Constraint-derived bounds also need to be weaker than bounds obtained through existing argument and output inference. Without that distinction, a constraint involving variance could produce a less specific type and replace a type that ordinary inference already selected. The implementation therefore tracks constraint bounds separately and uses them only when a type parameter has no ordinary bounds. Calls for which inference already succeeded should consequently retain the same inferred types.

We considered whether the compiler should instead gather all upper and lower bounds and choose the strongest result. That could improve some existing contravariant inference cases, but it would change inferred types across a much broader set of programs. The implementation deliberately takes the narrower approach. It still has the intended compatibility impact that a generic overload which was previously inapplicable may become applicable, but it avoids changing successful inferences merely because constraints now participate.

We also looked at a motivating scenario from the ref struct closures for lambdas proposal. High-performance LINQ-like APIs can represent enumerators, operations, and predicates as precise generic struct types so that Native AOT and the JIT can inline through the entire operation. Without better inference, spelling the resulting nested generic types at call sites quickly becomes impractical. Constraint inference would allow an API to infer related element and operation types while preserving the concrete types needed for optimization. We did not make any decisions about ref struct closures or functional interfaces as part of this review; they were discussed only as a potential use case.

Conclusion

We were satisfied with the implementation approach. The proposal itself remains unchanged; the implementation will fix type parameters in batches and prefer ordinary inference bounds over constraint-derived bounds.

Union pattern matching targets

Champion issue: #9662
Spec: https://github.com/dotnet/csharplang/blob/05eb4800fc3dc76259ccd49ac01f1eeb49222380/proposals/csharp-15.0/unions.md#union-matching
Working group notes: https://github.com/dotnet/csharplang/blob/05eb4800fc3dc76259ccd49ac01f1eeb49222380/meetings/working-groups/discriminated-unions/union-patterns.md

We revisited how patterns choose between a union instance and its contained value. We previously discussed this question because always redirecting patterns to the contained value breaks existing code when an established union-like type opts into language support. In particular, an untyped property pattern that previously examined properties on the wrapper would suddenly be applied to its object-typed value instead.

The design subsequently moved to a "try both" rule for patterns containing a type test. Such a pattern first tests the union instance and, if that fails, tests the contained value. This preserves more existing type tests when a type opts into union behavior, but implementation experience exposed significant costs.

The first problem is predictability. A type pattern can match either object depending on the runtime types involved. For example, a union whose cases include an IEnumerable<T> might itself implement IEnumerable<T>. A pattern intended to select the enumerable case would then match the wrapper first. Code in the wrapper's own enumerator could consequently re-enter that enumerator instead of reaching the contained value, leading to recursion and a stack overflow. An explicit unwrap can fix the code, but the need for it is not apparent from the pattern, and a later cleanup can easily reintroduce the bug. As an example:

union OneOrMany(int, IEnumerable<int>) : IEnumerable<int>
{
    public IEnumerator<int> GetEnumerator() => this switch
    {
        int value => SingleEnumerator(value),
        IEnumerable<int> values => values.GetEnumerator() // Oops, accidental recursion
    };
}

The second problem is the complexity imposed on pattern analysis. With "try both", the compiler cannot always determine statically whether a successful pattern matched the wrapper or its value. That distinction affects the type and value of declared pattern variables and complicates decision-tree construction. The current implementation had to reject useful generic cases involving pattern variables as a stopgap. Completing the model would require substantial implementation work, with uncertain complexity and performance costs, merely to support behavior that remains difficult for readers to predict.

We therefore reconsidered the syntax-directed alternative. Under that model, non-null constant patterns and patterns with an explicit type test target the contained value. Untyped property and positional patterns, together with var and discard patterns, target the union instance. Null patterns retain their special behavior because both a reference-type union and its contained value can independently be null.

This rule continues to preserve the primary compatibility scenario. Existing untyped property patterns still apply to members on a wrapper after its type opts into union support. It does give up the ability to write a direct type test against the wrapper while the input is statically known to be a union. We considered that a less important scenario than reliably matching case types against the contained value, which is the main purpose of a union. Code that needs to inspect the wrapper can suppress union-aware matching by first converting the input to a non-union static type, and untyped property patterns can continue to inspect wrapper properties directly.

The syntax-directed rule also makes the target visible in source rather than deriving it from semantic or runtime facts. There remain superficially similar patterns with different targets, such as a typed property pattern and an untyped property pattern, but the distinction is stable and explainable. We preferred that limitation over a rule under which the same syntax can select different objects at runtime.

Conclusion

We will replace the "try both" behavior with the syntax-directed model. Patterns containing a type test and non-null constant patterns apply to the union's contained value. Untyped property and positional patterns, var patterns, and discard patterns apply to the union instance. Null patterns retain their existing special handling.