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

Warn that a user-defined API on an inline array type won’t be used by the language for an element access expression #69064

Merged
merged 3 commits into from
Jul 20, 2023
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
15 changes: 10 additions & 5 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9252,11 +9252,7 @@ candidate is PropertySymbol property &&
if (!candidate.IsStatic &&
IsAccessible(candidate, syntax, diagnostics) &&
candidate is MethodSymbol method &&
method.OriginalDefinition is var original &&
!original.ReturnsVoid &&
original.ParameterCount == 2 &&
original.Parameters[0] is { Type.SpecialType: SpecialType.System_Int32, RefKind: RefKind.None } &&
original.Parameters[1] is { Type.SpecialType: SpecialType.System_Int32, RefKind: RefKind.None })
MethodHasValidSliceSignature(method))
{
makeCall(syntax, receiver, method, out indexerOrSliceAccess, out argumentPlaceholders);
lookupResult.Free();
Expand Down Expand Up @@ -9295,6 +9291,15 @@ void makeCall(SyntaxNode syntax, BoundExpression receiver, MethodSymbol method,
}
}

internal static bool MethodHasValidSliceSignature(MethodSymbol method)
{
return method.OriginalDefinition is var original &&
!original.ReturnsVoid &&
original.ParameterCount == 2 &&
original.Parameters[0] is { Type.SpecialType: SpecialType.System_Int32, RefKind: RefKind.None } &&
original.Parameters[1] is { Type.SpecialType: SpecialType.System_Int32, RefKind: RefKind.None };
}

private bool TryBindLengthOrCount(
SyntaxNode syntax,
BoundValuePlaceholderBase receiverPlaceholder,
Expand Down
18 changes: 18 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -7682,4 +7682,22 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_InlineArrayUnsupportedElementFieldModifier" xml:space="preserve">
<value>Inline array element field cannot be declared as required, readonly, volatile, or as a fixed size buffer.</value>
</data>
<data name="WRN_InlineArrayIndexerNotUsed" xml:space="preserve">
<value>Inline array indexer will not be used for element access expression.</value>
</data>
<data name="WRN_InlineArrayIndexerNotUsed_Title" xml:space="preserve">
<value>Inline array indexer will not be used for element access expression.</value>
</data>
<data name="WRN_InlineArraySliceNotUsed" xml:space="preserve">
<value>Inline array 'Slice' method will not be used for element access expression.</value>
</data>
<data name="WRN_InlineArraySliceNotUsed_Title" xml:space="preserve">
<value>Inline array 'Slice' method will not be used for element access expression.</value>
</data>
<data name="WRN_InlineArrayConversionOperatorNotUsed" xml:space="preserve">
<value>Inline array conversion operator will not be used for conversion from expression of the declaring type.</value>
</data>
<data name="WRN_InlineArrayConversionOperatorNotUsed_Title" xml:space="preserve">
<value>Inline array conversion operator will not be used for conversion from expression of the declaring type.</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2243,6 +2243,9 @@ internal enum ErrorCode
WRN_PrimaryConstructorParameterIsShadowedAndNotPassedToBase = 9179,

ERR_InlineArrayUnsupportedElementFieldModifier = 9180,
WRN_InlineArrayIndexerNotUsed = 9181,
WRN_InlineArraySliceNotUsed = 9182,
WRN_InlineArrayConversionOperatorNotUsed = 9183,

#endregion

Expand Down
6 changes: 6 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@ internal static int GetWarningLevel(ErrorCode code)
case ErrorCode.WRN_NullabilityMismatchInParameterTypeOnInterceptor:
case ErrorCode.WRN_CapturedPrimaryConstructorParameterInFieldInitializer:
case ErrorCode.WRN_PrimaryConstructorParameterIsShadowedAndNotPassedToBase:
case ErrorCode.WRN_InlineArrayIndexerNotUsed:
case ErrorCode.WRN_InlineArraySliceNotUsed:
case ErrorCode.WRN_InlineArrayConversionOperatorNotUsed:
return 1;
default:
return 0;
Expand Down Expand Up @@ -2363,6 +2366,9 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_CollectionLiteralNoTargetType:
case ErrorCode.WRN_PrimaryConstructorParameterIsShadowedAndNotPassedToBase:
case ErrorCode.ERR_InlineArrayUnsupportedElementFieldModifier:
case ErrorCode.WRN_InlineArrayIndexerNotUsed:
case ErrorCode.WRN_InlineArraySliceNotUsed:
case ErrorCode.WRN_InlineArrayConversionOperatorNotUsed:
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.

Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,50 @@ protected override void AfterMembersCompletedChecks(BindingDiagnosticBag diagnos
{
diagnostics.Add(ErrorCode.ERR_InlineArrayUnsupportedElementFieldModifier, elementField.TryGetFirstLocation() ?? GetFirstLocation());
}

if (TryGetInlineArrayElementField() is { TypeWithAnnotations: var elementType })
{
NamedTypeSymbol? index = null;
NamedTypeSymbol? range = null;

foreach (PropertySymbol indexer in Indexers)
{
if (indexer.Parameters is [{ Type: { } type }] &&
(type.SpecialType == SpecialType.System_Int32 ||
type.Equals(index ??= DeclaringCompilation.GetWellKnownType(WellKnownType.System_Index), TypeCompareKind.AllIgnoreOptions) ||
type.Equals(range ??= DeclaringCompilation.GetWellKnownType(WellKnownType.System_Range), TypeCompareKind.AllIgnoreOptions)))
{
diagnostics.Add(ErrorCode.WRN_InlineArrayIndexerNotUsed, indexer.TryGetFirstLocation() ?? GetFirstLocation());
}
}

foreach (var slice in GetMembers(WellKnownMemberNames.SliceMethodName).OfType<MethodSymbol>())
{
if (Binder.MethodHasValidSliceSignature(slice))
{
diagnostics.Add(ErrorCode.WRN_InlineArraySliceNotUsed, slice.TryGetFirstLocation() ?? GetFirstLocation());
break;
}
}

NamedTypeSymbol? span = null;
NamedTypeSymbol? readOnlySpan = null;

foreach (var conversion in GetMembers().OfType<SourceUserDefinedConversionSymbol>())
{
TypeSymbol returnType = conversion.ReturnType;
TypeSymbol returnTypeOriginalDefinition = returnType.OriginalDefinition;

if (conversion.ParameterCount == 1 &&
conversion.Parameters[0].Type.Equals(this, TypeCompareKind.AllIgnoreOptions) &&
(returnTypeOriginalDefinition.Equals(span ??= DeclaringCompilation.GetWellKnownType(WellKnownType.System_Span_T), TypeCompareKind.AllIgnoreOptions) ||
returnTypeOriginalDefinition.Equals(readOnlySpan ??= DeclaringCompilation.GetWellKnownType(WellKnownType.System_ReadOnlySpan_T), TypeCompareKind.AllIgnoreOptions)) &&
Conversions.HasIdentityConversion(((NamedTypeSymbol)returnTypeOriginalDefinition).Construct(ImmutableArray.Create(elementType)), returnType))
Copy link
Member

@jjonescz jjonescz Jul 17, 2023

Choose a reason for hiding this comment

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

Conversions.HasIdentityConversion(((NamedTypeSymbol)returnTypeOriginalDefinition).Construct(ImmutableArray.Create(elementType)), returnType)

What's this check for? #Resolved

Copy link
Contributor Author

@AlekseyTs AlekseyTs Jul 17, 2023

Choose a reason for hiding this comment

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

What's this check for?

This checks that the return type of the conversion is indeed something that conflicts with built-in conversions.
For example, in UserDefinedConversion_Warning_03 it does not.

{
diagnostics.Add(ErrorCode.WRN_InlineArrayConversionOperatorNotUsed, conversion.TryGetFirstLocation() ?? GetFirstLocation());
}
}
}
}
else
{
Expand Down
30 changes: 30 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.

30 changes: 30 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.

30 changes: 30 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.

30 changes: 30 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.

Loading