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

Dictionary expressions: initial binding and lowering support #76257

Merged
merged 17 commits into from
Dec 18, 2024

Conversation

cston
Copy link
Member

@cston cston commented Dec 4, 2024

Binding and lowering for IDictionary<K, V> and IReadOnlyDictionary<K, V> target types.

See proposals/dictionary-expressions.md

Relates to test plan #76310

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 4, 2024
@dotnet-policy-service dotnet-policy-service bot added the Needs API Review Needs to be reviewed by the API review council label Dec 4, 2024
@cston cston force-pushed the dictionary-binding branch from 5da632a to 74a425d Compare December 5, 2024 20:47
@cston cston force-pushed the dictionary-binding branch from 6b7dd35 to 767c1c9 Compare December 8, 2024 07:33
@cston cston force-pushed the dictionary-binding branch from 1fba2ac to 006323a Compare December 10, 2024 00:17
@cston cston marked this pull request as ready for review December 10, 2024 00:20
@cston cston requested a review from a team as a code owner December 10, 2024 00:20
@jcouv jcouv self-assigned this Dec 11, 2024
Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

Done with review pass (iteration 11)

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (iteration 14)

@cston cston requested a review from a team December 16, 2024 16:42
@jcouv jcouv removed Needs API Review Needs to be reviewed by the API review council untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 17, 2024
@@ -235,6 +259,8 @@ internal Conversion GetCollectionExpressionSpreadElementConversion(
{
return Conversion.NoConversion;
}
// This should be conversion from type rather than conversion
Copy link
Member

Choose a reason for hiding this comment

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

PROTOTYPE comment?

Copy link
Member Author

Choose a reason for hiding this comment

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

Using conversion from expression rather than conversion from type is a bug. The one case that I'm aware of where this is observable is when the spread element type is dynamic. Unfortunately, fixing this would be a breaking change for that case.

For instance, the following is currently allowed but would be an error when using conversion from type:

dynamic[] x = [1, 2, 3];
int[] y = [..x];

I could log a bug and reference that here if that seems useful.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've updated the comment to include the example.

{
string source = """
using System.Collections.Generic;
IDictionary<int, string> d = [1:"one"];
Copy link
Member

@333fred 333fred Dec 17, 2024

Choose a reason for hiding this comment

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

Consider testing var x = [1:"one"]; as well. #Resolved

Comment on lines +131 to +136
// (12,16): error CS9215: Collection expression type 'Dictionary<int, string>' must have an instance or extension method 'Add' that can be called with a single argument.
// return [1:"one", x, ..y];
Diagnostic(ErrorCode.ERR_CollectionExpressionMissingAdd, @"[1:""one"", x, ..y]").WithArguments("System.Collections.Generic.Dictionary<int, string>").WithLocation(12, 16),
// (12,17): error CS9268: Collection expression type 'Dictionary<int, string>' does not support key-value pair elements.
// return [1:"one", x, ..y];
Diagnostic(ErrorCode.ERR_CollectionExpressionKeyValuePairNotSupported, @"1:""one""").WithArguments("System.Collections.Generic.Dictionary<int, string>").WithLocation(12, 17));
Copy link
Member

@333fred 333fred Dec 17, 2024

Choose a reason for hiding this comment

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

I do not understand why there are errors here. Am I missing something about the feature? I would expect all of these to be fine. #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

This PR implements support for dictionary interface target types only. It does not include support for concrete types such as Dictionary<K, V>.

}

[Fact]
public void EvaluationOrder_01()
Copy link
Member

@333fred 333fred Dec 17, 2024

Choose a reason for hiding this comment

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

Consider some examples with complex sub-flow (like ternaries or switch expressions) in the key and value positions. #Resolved

@cston cston merged commit 5439dd5 into dotnet:features/dictionary-expressions Dec 18, 2024
24 checks passed
@cston cston deleted the dictionary-binding branch December 18, 2024 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants