Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection literals: update type inference #7284

Merged
merged 9 commits into from
Jun 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions proposals/collection-literals.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,27 +300,20 @@ var a = AsArray([1, 2, 3]); // AsArray<int>(int[])
static T[] AsArray<T>(T[] arg) => arg;
```

The *natural element type* of a collection literal expression is the [*best common type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#116315-finding-the-best-common-type-of-a-set-of-expressions) of:
* For each *expression element* `Ei`, the expression `Ei`
* For each *spread element* `Si`, the *iteration type* of `Si`
The [*type inference*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#1163-type-inference) rules are **updated** to include inferences from collection literal expressions.

The existing [*type inference*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#1163-type-inference) rules are **updated** to include inferences from the *natural element type* of collection literal expressions.
Collection literal expressions only contribute to *lower bounds*, even when passed as arguments to `in` parameters so *lower-bound* are updated but *upper-bound* and *exact* inferences are not.

> 11.6.3.10 Lower-bound inferences
>
> A *lower-bound inference from* a type `U` *to* a type `V` is made as follows:
>
> - ...
> - **Otherwise, if `V` is a single-dimensional array type `V₁[]` or `V` is a constructible collection type `C<V₁>` with element type `V₁`, and `U` is a collection literal with element type `U₁`, then a *lower-bound inference* is made from `U₁` to `V₁`.**
> - Otherwise, no inferences are made.

> 11.6.3.12 Fixing
> 11.6.3.2 The first phase
>
> An *unfixed* type variable `Xᵢ` with a set of bounds is *fixed* as follows:
> For each of the method arguments `Eᵢ`:
>
> - The set of *candidate types* `Uₑ` starts out as the set of all types in the set of bounds for `Xᵢ` **where types inferred from collection literal element types are ignored if there are any types that were not inferred from collection literal element types**.
> - **If `Eᵢ` is a *collection literal*, a *collection element type inference* is made *from* `Eᵢ` *to* the corresponding *parameter type* `Tᵢ`.**
> - ...
>
A *collection element type inference* is made *from* a expression `E` *to* a type `T` as follows:
- If `E` is a collection literal with elements `Eᵢ`, and `T` is a *single-dimensional array type* `Tₑ[]` or `T` is [*collection type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/statements.md#1295-the-foreach-statement) with an [*iteration type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/statements.md#1295-the-foreach-statement) `Tₑ`, then for each `Eᵢ`:
- If `Eᵢ` is a *collection literal*, a *collection element type inference* is made *from* `Eᵢ` *to* `Tₑ`.
- Otherwise, if `Eᵢ` has type `Uᵢ` then a *lower-bound inference* is made *from* `Uᵢ` *to* `Tₑ`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this say something like "for each Ei.."?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"for each Ei" is included two lines above.

Copy link
Contributor

@RikkiGibson RikkiGibson Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I was thrown off by the mis-formatting from GitHub.

- Otherwise, no inferences are made for `Eᵢ`.

## Extension methods
```c#
Expand All @@ -329,7 +322,7 @@ var ia = [4].AsImmutableArray(); // AsImmutableArray<int>(ImmutableArray<int>)
static ImmutableArray<T> AsImmutableArray<T>(this ImmutableArray<T> arg) => arg;
```

The existing [*extension method invocation*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#11783-extension-method-invocations) rules are **updated** to include conversions from collection literal expressions.
The [*extension method invocation*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#11783-extension-method-invocations) rules are **updated** to include conversions from collection literal expressions.

> 11.7.8.3 Extension method invocations
>
Expand Down