Skip to content

Commit

Permalink
Collection literals natural type
Browse files Browse the repository at this point in the history
  • Loading branch information
cston committed Mar 21, 2023
1 parent 02f55f6 commit eb7fcc3
Show file tree
Hide file tree
Showing 24 changed files with 863 additions and 280 deletions.
9 changes: 8 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,14 @@ private BoundExpression CheckValue(BoundExpression expr, BindValueKind valueKind
expr = BindIndexerDefaultArguments((BoundIndexerAccess)expr, valueKind, diagnostics);
break;

case BoundKind.UnconvertedObjectCreationExpression:
case BoundKind.UnconvertedObjectCreationExpression: // PROTOTYPE: What is the effect of commenting out this case? Add a corresponding test for collection literals (next case).
if (valueKind == BindValueKind.RValue)
{
return expr;
}
break;

case BoundKind.UnconvertedCollectionLiteralExpression:
if (valueKind == BindValueKind.RValue)
{
return expr;
Expand Down
239 changes: 175 additions & 64 deletions src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ internal BoundExpression BindToNaturalType(BoundExpression expression, BindingDi
result = RebindSimpleBinaryOperatorAsConverted(unconvertedBinaryOperator, diagnostics);
}
break;
case BoundUnconvertedCollectionLiteralExpression collectionLiteral:
result = ConvertCollectionLiteralExpressionToNaturalType(collectionLiteral, diagnostics);
break;
default:
result = expression;
break;
Expand Down Expand Up @@ -4490,7 +4493,10 @@ private BoundExpression BindCollectionLiteralExpression(CollectionCreationExpres
{
builder.Add(bindElement(element, diagnostics));
}
return new BoundUnconvertedCollectionLiteralExpression(syntax, builder.ToImmutableAndFree(), this);

var initializers = builder.ToImmutableAndFree();
var naturalType = InferCollectionLiteralNaturalType(initializers);
return new BoundUnconvertedCollectionLiteralExpression(syntax, initializers, this, type: naturalType);

BoundExpression bindElement(CollectionElementSyntax syntax, BindingDiagnosticBag diagnostics)
{
Expand Down Expand Up @@ -5510,7 +5516,7 @@ private BoundExpression BindUnexpectedComplexElementInitializer(InitializerExpre
}

private BoundExpression BindCollectionInitializerElementAddMethod(
ExpressionSyntax elementInitializer,
SyntaxNode elementInitializer,
ImmutableArray<BoundExpression> boundElementInitializerExpressions,
bool hasEnumerableInitializerType,
Binder collectionInitializerAddMethodBinder,
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/BoundTree/BoundExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ internal bool NeedsToBeConverted()
case BoundKind.UnconvertedConditionalOperator:
case BoundKind.DefaultLiteral:
case BoundKind.UnconvertedInterpolatedString:
case BoundKind.UnconvertedCollectionLiteralExpression:
return true;
case BoundKind.StackAllocArrayCreation:
// A BoundStackAllocArrayCreation is given a null type when it is in a
Expand Down
4 changes: 1 addition & 3 deletions src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1867,12 +1867,10 @@
</Node>

<!--
A target-typed collection literal expression.
An unconverted collection literal expression.
It does not survive past initial binding.
-->
<Node Name="BoundUnconvertedCollectionLiteralExpression" Base="BoundExpression">
<!-- Type is not significant for this node type; always null -->
<Field Name="Type" Type="TypeSymbol?" Override="true" Null="always"/>
<!-- Collection literal elements. -->
<Field Name="Initializers" Type="ImmutableArray&lt;BoundExpression&gt;"/>
<Field Name="Binder" Type="Binder" Null="disallow"/>
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -6789,6 +6789,9 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_CollectionLiteralElementNotImplemented" xml:space="preserve">
<value>Support for collection literal dictionary and spread elements has not been implemented.</value>
</data>
<data name="ERR_ImplicitlyTypedCollectionLiteralNoBestType" xml:space="preserve">
<value>No best type found for implicitly-typed collection literal.</value>
</data>
<data name="ERR_EqualityContractRequiresGetter" xml:space="preserve">
<value>Record equality contract property '{0}' must have a get accessor.</value>
</data>
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2190,6 +2190,7 @@ internal enum ErrorCode
ERR_CollectionLiteralTargetTypeNotConstructible = 9500, // PROTOTYPE: Update error numbers.
ERR_ExpressionTreeContainsCollectionLiteral = 9501,
ERR_CollectionLiteralElementNotImplemented = 9502, // PROTOTYPE: Temporary error until feature has been implemented.
ERR_ImplicitlyTypedCollectionLiteralNoBestType = 9503,

#endregion

Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2310,6 +2310,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_CollectionLiteralTargetTypeNotConstructible:
case ErrorCode.ERR_ExpressionTreeContainsCollectionLiteral:
case ErrorCode.ERR_CollectionLiteralElementNotImplemented:
case ErrorCode.ERR_ImplicitlyTypedCollectionLiteralNoBestType:
return false;
default:
// NOTE: All error codes must be explicitly handled in this switch statement
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eb7fcc3

Please sign in to comment.