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

List-patterns: report error when no suitable length member #59475

Merged
merged 5 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 13 additions & 8 deletions src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,12 @@ private BoundListPattern BindListPattern(
BoundListPatternReceiverPlaceholder? receiverPlaceholder;
BoundListPatternIndexPlaceholder? argumentPlaceholder;

if (inputType.IsErrorType())
if (inputType.IsDynamic())
{
Error(diagnostics, ErrorCode.ERR_UnsupportedTypeForListPattern, node, inputType);
}

if (inputType.IsErrorType() || inputType.IsDynamic())
{
hasErrors = true;
elementType = inputType;
Expand Down Expand Up @@ -337,13 +342,9 @@ private bool IsCountableAndIndexable(SyntaxNode node, TypeSymbol inputType, out
private bool BindLengthAndIndexerForListPattern(SyntaxNode node, TypeSymbol inputType, uint inputValEscape, BindingDiagnosticBag diagnostics,
out BoundExpression indexerAccess, out BoundExpression lengthAccess, out BoundListPatternReceiverPlaceholder? receiverPlaceholder, out BoundListPatternIndexPlaceholder argumentPlaceholder)
{
bool hasErrors = false;
if (inputType.IsDynamic())
{
hasErrors |= true;
Error(diagnostics, ErrorCode.ERR_UnsupportedTypeForListPattern, node, inputType);
}
Debug.Assert(!inputType.IsDynamic());

bool hasErrors = false;
receiverPlaceholder = new BoundListPatternReceiverPlaceholder(node, GetValEscape(inputType, inputValEscape), inputType) { WasCompilerGenerated = true };
if (inputType.IsSZArray())
{
Expand All @@ -359,7 +360,11 @@ private bool BindLengthAndIndexerForListPattern(SyntaxNode node, TypeSymbol inpu
}
else
{
hasErrors |= !TryBindLengthOrCount(node, receiverPlaceholder, out lengthAccess, diagnostics);
Copy link
Member

@alrz alrz Feb 11, 2022

Choose a reason for hiding this comment

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

Surprised that this wasn't sufficient. Pretty sure we had tests with missing members. How this was still possible? #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.

The tests with missing/bad length all used an int indexer, so produced other errors. But when we have a System.Index indexer, we'd produce a bound tree with an error but no diagnostics.

if (!TryBindLengthOrCount(node, receiverPlaceholder, out lengthAccess, diagnostics))
{
hasErrors = true;
Error(diagnostics, ErrorCode.ERR_ListPatternRequiresLength, node, inputType);
}
}

var analyzedArguments = AnalyzedArguments.GetInstance();
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 @@ -6760,6 +6760,9 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_UnsupportedTypeForListPattern" xml:space="preserve">
<value>List patterns may not be used for a value of type '{0}'.</value>
</data>
<data name="ERR_ListPatternRequiresLength" xml:space="preserve">
<value>List patterns may not be used for a value of type '{0}'. No suitable 'Length' or 'Count' property was found.</value>
</data>
<data name="ERR_UnsupportedTypeForSlicePattern" xml:space="preserve">
<value>Slice patterns may not be used for a value of type '{0}'.</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 @@ -2020,6 +2020,7 @@ internal enum ErrorCode
ERR_StructHasInitializersAndNoDeclaredConstructor = 8983,
ERR_EncUpdateFailedDelegateTypeChanged = 8984,

ERR_ListPatternRequiresLength = 8985,
ERR_DiscardCannotBeNullChecked = 8990,
ERR_MustNullCheckInImplementation = 8991,
ERR_NonNullableValueTypeIsNullChecked = 8992,
Expand Down
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.

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -2319,6 +2319,9 @@ void M()
";
var expectedDiagnostics = new[]
{
// (8,31): error CS8985: List patterns may not be used for a value of type 'X'. No suitable 'Length' or 'Count' property was found.
// _ = /*<bind>*/this is []/*</bind>*/;
Diagnostic(ErrorCode.ERR_ListPatternRequiresLength, "[]").WithArguments("X").WithLocation(8, 31),
// (8,31): error CS0518: Predefined type 'System.Index' is not defined or imported
// _ = /*<bind>*/this is []/*</bind>*/;
Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "[]").WithArguments("System.Index").WithLocation(8, 31)
Expand Down
Loading