diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
index f1816ef59e88b..cc8f364c8de0b 100644
--- a/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
+++ b/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
@@ -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();
@@ -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,
diff --git a/src/Compilers/CSharp/Portable/CSharpResources.resx b/src/Compilers/CSharp/Portable/CSharpResources.resx
index 5dc4bb13443e6..bfb19b064a5fa 100644
--- a/src/Compilers/CSharp/Portable/CSharpResources.resx
+++ b/src/Compilers/CSharp/Portable/CSharpResources.resx
@@ -7682,4 +7682,22 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
Inline array element field cannot be declared as required, readonly, volatile, or as a fixed size buffer.
+
+ Inline array indexer will not be used for element access expression.
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
\ No newline at end of file
diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
index b4732b84b5059..54b6c3999a3c1 100644
--- a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
+++ b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
@@ -2243,6 +2243,9 @@ internal enum ErrorCode
WRN_PrimaryConstructorParameterIsShadowedAndNotPassedToBase = 9179,
ERR_InlineArrayUnsupportedElementFieldModifier = 9180,
+ WRN_InlineArrayIndexerNotUsed = 9181,
+ WRN_InlineArraySliceNotUsed = 9182,
+ WRN_InlineArrayConversionOperatorNotUsed = 9183,
#endregion
diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs b/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
index f4dac2b80f872..3e1f560edf292 100644
--- a/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
+++ b/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
@@ -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;
@@ -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
diff --git a/src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs b/src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs
index b8fdf8dba05c0..9f8683570ab1e 100644
--- a/src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs
+++ b/src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs
@@ -320,6 +320,9 @@ public static bool IsWarning(ErrorCode code)
case ErrorCode.WRN_NullabilityMismatchInReturnTypeOnInterceptor:
case ErrorCode.WRN_NullabilityMismatchInParameterTypeOnInterceptor:
case ErrorCode.WRN_PrimaryConstructorParameterIsShadowedAndNotPassedToBase:
+ case ErrorCode.WRN_InlineArrayIndexerNotUsed:
+ case ErrorCode.WRN_InlineArraySliceNotUsed:
+ case ErrorCode.WRN_InlineArrayConversionOperatorNotUsed:
return true;
default:
return false;
diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamedTypeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamedTypeSymbol.cs
index 311cd479eb6f7..fb53c78a572c4 100644
--- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamedTypeSymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceNamedTypeSymbol.cs
@@ -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())
+ {
+ 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())
+ {
+ 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))
+ {
+ diagnostics.Add(ErrorCode.WRN_InlineArrayConversionOperatorNotUsed, conversion.TryGetFirstLocation() ?? GetFirstLocation());
+ }
+ }
+ }
}
else
{
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
index 2f67ffd2fe6ab..ca900a502a84b 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
@@ -2467,6 +2467,36 @@
Použití proměnné v tomto kontextu může vystavit odkazované proměnné mimo rozsah jejich oboru.
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+ Zachycuje se volání {0} s zachycovačem {1}, ale podpisy se neshodují.
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
index 6dcde332041b9..f37e5f17b75f2 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
@@ -2467,6 +2467,36 @@
Die Verwendung der Variablen in diesem Kontext kann dazu führen, dass referenzierte Variablen außerhalb ihres Deklarationsbereichs verfügbar gemacht werden.
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+ Abfangen eines Aufrufs von "{0}" mit Interceptor "{1}", aber die Signaturen stimmen nicht überein.
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
index 4c5dd570c72b1..650e58162ae80 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
@@ -2467,6 +2467,36 @@
Usar la variable en este contexto puede exponer variables a las que se hace referencia fuera de su ámbito de declaración
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+ Intercepción de una llamada a '{0}' con el interceptor '{1}', pero las firmas no coinciden.
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
index 8a78be23742ac..c46f8ad4fdf00 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
@@ -2467,6 +2467,36 @@
Utiliser la variable dans ce contexte peut exposer des variables de référence en dehors de leur étendue de déclaration
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+ Interception d'un appel à '{0}' avec l'intercepteur '{1}', mais les signatures ne correspondent pas.
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
index 0b3f03c8215f9..740293753934a 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
@@ -2467,6 +2467,36 @@
L'uso di variabili in questo contesto potrebbe esporre le variabili a cui si fa riferimento al di fuori dell'ambito della dichiarazione
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+ Intercettazione di una chiamata a '{0}' con l'intercettore '{1}', ma le firme non corrispondono.
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
index de98e5b6ccabe..2c2762b3958e6 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
@@ -2467,6 +2467,36 @@
このコンテキストでの変数の使用は、参照される変数が宣言のスコープ外に公開される可能性があります
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+ インターセプター '{1}' を使用して'{0}' への呼び出しをインターセプトしていますが、署名が一致しません。
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
index c70970921114f..a9424d06530a2 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
@@ -2467,6 +2467,36 @@
이 컨텍스트에서 변수를 사용하면 선언 범위 외부에서 참조된 변수가 노출될 수 있습니다.
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+ '{1}' 인터셉터로 '{0}'에 대한 호출을 가로채지만 서명이 일치하지 않습니다.
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
index c86a98cff9ea6..27f2ead1d61a5 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
@@ -2467,6 +2467,36 @@
Nie można używać zmiennej w tym kontekście, ponieważ może uwidaczniać odwoływane zmienne poza ich zakresem deklaracji
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+ Przechwytywanie wywołania do „{0}” za pomocą interceptora „{1}”, ale sygnatury nie są zgodne.
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
index d2599fc08c83c..759d6d68e0605 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
@@ -2467,6 +2467,36 @@
O uso de variável neste contexto pode expor variáveis referenciadas fora de seu escopo de declaração
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+ Interceptando uma chamada para '{0}' com interceptador '{1}', mas as assinaturas não coincidem.
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
index 0f03e0c6143f3..da6c8057b9b59 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
@@ -2467,6 +2467,36 @@
Использование переменной в этом контексте может представить ссылочные переменные за пределами области их объявления.
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+ Перехват вызова ' {0} ' с перехватчиком ' {1} ', но подписи не совпадают.
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
index 583d9566cb4d6..0c09ed70c5c61 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
@@ -2467,6 +2467,36 @@
Bu bağlamda değişken kullanımı, başvurulan değişkenleri bildirim kapsamının dışında gösterebilir.
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+ '{0}' hedefine olan çağrı '{1}' engelleyicisi ile engelleniyor, ancak imzalar eşleşmiyor.
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
index 9643ab23d2f19..62561e73f621a 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
@@ -2467,6 +2467,36 @@
在此上下文中使用变量可能会在变量声明范围以外公开所引用的变量
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+ 正在使用侦听器“{1}”截获对“{0}”的调用,但签名不匹配。
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
index a892581008629..dabf7c3d56aae 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
@@ -2467,6 +2467,36 @@
在此內容中使用變數,可能會將參考的變數公開在其宣告範圍外
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array conversion operator will not be used for conversion from expression of the declaring type.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array indexer will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+
+
+
+ Inline array 'Slice' method will not be used for element access expression.
+
+ 正在使用攔截器 '{1}' 攔截對 '{0}' 的呼叫,但簽章不相符。
diff --git a/src/Compilers/CSharp/Test/Emit2/Semantics/InlineArrayTests.cs b/src/Compilers/CSharp/Test/Emit2/Semantics/InlineArrayTests.cs
index 9b30efc0b910e..82a7316c7e918 100644
--- a/src/Compilers/CSharp/Test/Emit2/Semantics/InlineArrayTests.cs
+++ b/src/Compilers/CSharp/Test/Emit2/Semantics/InlineArrayTests.cs
@@ -7898,7 +7898,11 @@ public T this[int i]
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
- var verifier = CompileAndVerify(comp, expectedOutput: "111", verify: Verification.Fails).VerifyDiagnostics();
+ var verifier = CompileAndVerify(comp, expectedOutput: "111", verify: Verification.Fails).VerifyDiagnostics(
+ // (22,14): warning CS9181: Inline array indexer will not be used for element access expression.
+ // public T this[int i]
+ Diagnostic(ErrorCode.WRN_InlineArrayIndexerNotUsed, "this").WithLocation(22, 14)
+ );
verifier.VerifyIL("Program.M2",
@"
@@ -7986,7 +7990,10 @@ public T this[int i]
comp.VerifyDiagnostics(
// (14,37): error CS1913: Member '[^10]' cannot be initialized. It is not a field or property.
// static C M2() => new C() { F = {[^10] = 111} };
- Diagnostic(ErrorCode.ERR_MemberCannotBeInitialized, "[^10]").WithArguments("[^10]").WithLocation(14, 37)
+ Diagnostic(ErrorCode.ERR_MemberCannotBeInitialized, "[^10]").WithArguments("[^10]").WithLocation(14, 37),
+ // (22,14): warning CS9181: Inline array indexer will not be used for element access expression.
+ // public T this[int i]
+ Diagnostic(ErrorCode.WRN_InlineArrayIndexerNotUsed, "this").WithLocation(22, 14)
);
}
@@ -16476,7 +16483,11 @@ public struct Buffer10
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
- CompileAndVerify(comp, expectedOutput: "111", verify: Verification.Fails).VerifyDiagnostics();
+ CompileAndVerify(comp, expectedOutput: "111", verify: Verification.Fails).VerifyDiagnostics(
+ // (17,37): warning CS9183: Inline array conversion operator will not be used for conversion from expression of the declaring type.
+ // public static implicit operator System.ReadOnlySpan(Buffer10 x) => new[] { -111 };
+ Diagnostic(ErrorCode.WRN_InlineArrayConversionOperatorNotUsed, "System.ReadOnlySpan").WithLocation(17, 37)
+ );
}
[ConditionalFact(typeof(CoreClrOnly))]
@@ -17047,7 +17058,11 @@ public T this[int i]
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
- var verifier = CompileAndVerify(comp, expectedOutput: "0 111", verify: Verification.Fails).VerifyDiagnostics();
+ var verifier = CompileAndVerify(comp, expectedOutput: "0 111", verify: Verification.Fails).VerifyDiagnostics(
+ // (27,14): warning CS9181: Inline array indexer will not be used for element access expression.
+ // public T this[int i]
+ Diagnostic(ErrorCode.WRN_InlineArrayIndexerNotUsed, "this").WithLocation(27, 14)
+ );
verifier.VerifyIL("Program.M1",
@"
@@ -17117,7 +17132,11 @@ public T this[int i]
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
- var verifier = CompileAndVerify(comp, expectedOutput: "0 111", verify: Verification.Fails).VerifyDiagnostics();
+ var verifier = CompileAndVerify(comp, expectedOutput: "0 111", verify: Verification.Fails).VerifyDiagnostics(
+ // (27,14): warning CS9181: Inline array indexer will not be used for element access expression.
+ // public T this[int i]
+ Diagnostic(ErrorCode.WRN_InlineArrayIndexerNotUsed, "this").WithLocation(27, 14)
+ );
verifier.VerifyIL("Program.M1",
@"
@@ -17188,7 +17207,14 @@ public T this[int i]
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
- var verifier = CompileAndVerify(comp, expectedOutput: "0 111", verify: Verification.Fails).VerifyDiagnostics();
+ var verifier = CompileAndVerify(comp, expectedOutput: "0 111", verify: Verification.Fails).VerifyDiagnostics(
+ // (27,14): warning CS9181: Inline array indexer will not be used for element access expression.
+ // public T this[int i]
+ Diagnostic(ErrorCode.WRN_InlineArrayIndexerNotUsed, "this").WithLocation(27, 14),
+ // (34,29): warning CS9182: Inline array 'Slice' method will not be used for element access expression.
+ // public System.Span Slice(int start, int length) => throw null;
+ Diagnostic(ErrorCode.WRN_InlineArraySliceNotUsed, "Slice").WithLocation(34, 29)
+ );
verifier.VerifyIL("Program.M2",
@"
@@ -20835,5 +20861,435 @@ .locals init (int V_0)
comp = CreateCompilation(src + Buffer4Definition, targetFramework: TargetFramework.Net80, options: TestOptions.DebugExe);
CompileAndVerify(comp, expectedOutput: "-1 111 112 113 114").VerifyDiagnostics();
}
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedIndexer_Warning_01()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4
+{
+ private int _element0;
+
+ string this[int i] => ""int"";
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(b[0]);
+ }
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+
+ comp.VerifyDiagnostics(
+ // (7,12): warning CS9181: Inline array indexer will not be used for element access expression.
+ // string this[int i] => "int";
+ Diagnostic(ErrorCode.WRN_InlineArrayIndexerNotUsed, "this").WithLocation(7, 12)
+ );
+
+ CompileAndVerify(comp, expectedOutput: "0").VerifyDiagnostics(
+ // (7,12): warning CS9181: Inline array indexer will not be used for element access expression.
+ // string this[int i] => "int";
+ Diagnostic(ErrorCode.WRN_InlineArrayIndexerNotUsed, "this").WithLocation(7, 12)
+ );
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedIndexer_Warning_02()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4
+{
+ private int _element0;
+
+ string this[System.Index i] => ""index"";
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(b[(System.Index)0]);
+ }
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+
+ CompileAndVerify(comp, expectedOutput: "0").VerifyDiagnostics(
+ // (7,12): warning CS9181: Inline array indexer will not be used for element access expression.
+ // string this[System.Index i] => "index";
+ Diagnostic(ErrorCode.WRN_InlineArrayIndexerNotUsed, "this").WithLocation(7, 12)
+ );
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedIndexer_Warning_03()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4
+{
+ private int _element0;
+
+ string this[System.Range i] => ""range"";
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(b[..][0]);
+ }
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+
+ CompileAndVerify(comp, expectedOutput: "0", verify: Verification.Fails).VerifyDiagnostics(
+ // (7,12): warning CS9181: Inline array indexer will not be used for element access expression.
+ // string this[System.Range i] => "range";
+ Diagnostic(ErrorCode.WRN_InlineArrayIndexerNotUsed, "this").WithLocation(7, 12)
+ );
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedIndexer_Warning_04()
+ {
+ var src = @"
+Buffer4 b = default;
+System.Console.WriteLine(b[(nint)0]);
+
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4
+{
+ private int _element0;
+
+ public string this[nint i] => ""nint"";
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
+ CompileAndVerify(comp, expectedOutput: "nint").VerifyDiagnostics();
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedIndexer_Warning_05()
+ {
+ var src = @"
+Buffer4 b = default;
+System.Console.WriteLine(b[0]);
+
+[System.Runtime.CompilerServices.InlineArray(4)]
+ref struct Buffer4
+{
+ private ref int _element0;
+
+ public string this[int i] => ""int"";
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80);
+ CompileAndVerify(comp, expectedOutput: "int").VerifyDiagnostics();
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedIndexer_Warning_06()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4 : I1
+{
+ private int _element0;
+
+ int I1.this[int i] => throw null;
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(b[0]);
+ }
+}
+
+interface I1
+{
+ int this[int x] {get;}
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+ CompileAndVerify(comp, expectedOutput: "0").VerifyDiagnostics();
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedSlice_Warning_01()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4
+{
+ private int _element0;
+
+ int Length => 4;
+ string Slice(int i, int j) => ""int"";
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(b[..][0]);
+ }
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+
+ comp.VerifyDiagnostics(
+ // (8,12): warning CS9182: Inline array 'Slice' method will not be used for element access expression.
+ // string Slice(int i, int j) => "int";
+ Diagnostic(ErrorCode.WRN_InlineArraySliceNotUsed, "Slice").WithLocation(8, 12)
+ );
+
+ CompileAndVerify(comp, expectedOutput: "0", verify: Verification.Fails).VerifyDiagnostics(
+ // (8,12): warning CS9182: Inline array 'Slice' method will not be used for element access expression.
+ // string Slice(int i, int j) => "int";
+ Diagnostic(ErrorCode.WRN_InlineArraySliceNotUsed, "Slice").WithLocation(8, 12)
+ );
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedSlice_Warning_02()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4
+{
+ private int _element0;
+
+ int Length => 4;
+ string Slice(nint i, int j) => ""int"";
+ string Slice(int i, nint j) => ""int"";
+ string Slice(int i) => ""int"";
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(b[..][0]);
+ }
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+ CompileAndVerify(comp, expectedOutput: "0", verify: Verification.Fails).VerifyDiagnostics();
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedSlice_Warning_03()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4 : I1
+{
+ private int _element0;
+
+ int Length => 4;
+ string I1.Slice(int i, int j) => ""int"";
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(b[..][0]);
+ }
+}
+
+interface I1
+{
+ string Slice(int i, int j);
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+ CompileAndVerify(comp, expectedOutput: "0", verify: Verification.Fails).VerifyDiagnostics();
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedConversion_Warning_01()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4
+{
+ private int _element0;
+
+ public static implicit operator System.Span(Buffer4 b) => throw null;
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(((System.Span)b)[0]);
+ }
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+
+ comp.VerifyDiagnostics(
+ // (7,37): warning CS9183: Inline array conversion operator will not be used for conversion from expression of the declaring type.
+ // public static implicit operator System.Span(Buffer4 b) => throw null;
+ Diagnostic(ErrorCode.WRN_InlineArrayConversionOperatorNotUsed, "System.Span").WithLocation(7, 37)
+ );
+
+ CompileAndVerify(comp, expectedOutput: "0", verify: Verification.Fails).VerifyDiagnostics(
+ // (7,37): warning CS9183: Inline array conversion operator will not be used for conversion from expression of the declaring type.
+ // public static implicit operator System.Span(Buffer4 b) => throw null;
+ Diagnostic(ErrorCode.WRN_InlineArrayConversionOperatorNotUsed, "System.Span").WithLocation(7, 37)
+ );
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedConversion_Warning_02()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4
+{
+ private int _element0;
+
+ public static explicit operator System.ReadOnlySpan(in Buffer4 b) => throw null;
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(((System.ReadOnlySpan)b)[0]);
+ }
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+
+ comp.VerifyDiagnostics(
+ // (7,37): warning CS9183: Inline array conversion operator will not be used for conversion from expression of the declaring type.
+ // public static explicit operator System.ReadOnlySpan(in Buffer4 b) => throw null;
+ Diagnostic(ErrorCode.WRN_InlineArrayConversionOperatorNotUsed, "System.ReadOnlySpan").WithLocation(7, 37)
+ );
+
+ CompileAndVerify(comp, expectedOutput: "0", verify: Verification.Fails).VerifyDiagnostics(
+ // (7,37): warning CS9183: Inline array conversion operator will not be used for conversion from expression of the declaring type.
+ // public static explicit operator System.ReadOnlySpan(in Buffer4 b) => throw null;
+ Diagnostic(ErrorCode.WRN_InlineArrayConversionOperatorNotUsed, "System.ReadOnlySpan").WithLocation(7, 37)
+ );
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedConversion_Warning_03()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4
+{
+ private int _element0;
+
+ public static implicit operator System.ReadOnlySpan(Buffer4 b) => ""span"";
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(((System.ReadOnlySpan)b)[0]);
+ }
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+ CompileAndVerify(comp, expectedOutput: "s", verify: Verification.Fails).VerifyDiagnostics();
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedConversion_Warning_04()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4
+{
+ private int _element0;
+
+ public static implicit operator System.Span(Buffer4? b) => new [] {1, 2, 3, 4};
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(((System.Span)(Buffer4?)b)[0]);
+ }
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+ CompileAndVerify(comp, expectedOutput: "1", verify: Verification.Fails).VerifyDiagnostics();
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedConversion_Warning_05()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4
+{
+ private int _element0;
+
+ public static implicit operator System.Span?(Buffer4 b) => new [] {1, 2, 3, 4};
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(((System.Span?)b).Value[0]);
+ }
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+ comp.VerifyEmitDiagnostics(
+ // (7,37): error CS0306: The type 'Span' may not be used as a type argument
+ // public static implicit operator System.Span?(Buffer4 b) => new [] {1, 2, 3, 4};
+ Diagnostic(ErrorCode.ERR_BadTypeArgument, "System.Span?").WithArguments("System.Span").WithLocation(7, 37),
+ // (12,36): error CS0306: The type 'Span' may not be used as a type argument
+ // System.Console.WriteLine(((System.Span?)b).Value[0]);
+ Diagnostic(ErrorCode.ERR_BadTypeArgument, "System.Span?").WithArguments("System.Span").WithLocation(12, 36)
+ );
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedConversion_Warning_06()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4
+{
+ private int _element0;
+
+ public static implicit operator System.Span(Buffer4 b, int i) => new [] {1, 2, 3, 4};
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(((System.Span)b)[0]);
+ }
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+ comp.VerifyEmitDiagnostics(
+ // (7,53): error CS1019: Overloadable unary operator expected
+ // public static implicit operator System.Span(Buffer4 b, int i) => new [] {1, 2, 3, 4};
+ Diagnostic(ErrorCode.ERR_OvlUnaryOperatorExpected, "(Buffer4 b, int i)").WithLocation(7, 53)
+ );
+ }
+
+ [ConditionalFact(typeof(CoreClrOnly))]
+ public void UserDefinedConversion_Warning_07()
+ {
+ var src = @"
+[System.Runtime.CompilerServices.InlineArray(4)]
+struct Buffer4
+{
+ private int _element0;
+
+ public static implicit operator System.Span() => new [] {1, 2, 3, 4};
+
+ static void Main()
+ {
+ Buffer4 b = default;
+ System.Console.WriteLine(((System.Span)b)[0]);
+ }
+}
+";
+ var comp = CreateCompilation(src, targetFramework: TargetFramework.Net80, options: TestOptions.ReleaseExe);
+ comp.VerifyEmitDiagnostics(
+ // (7,53): error CS1019: Overloadable unary operator expected
+ // public static implicit operator System.Span() => new [] {1, 2, 3, 4};
+ Diagnostic(ErrorCode.ERR_OvlUnaryOperatorExpected, "()").WithLocation(7, 53)
+ );
+ }
}
}
diff --git a/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs b/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs
index 3789af11e2271..eadd162ebc585 100644
--- a/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs
+++ b/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs
@@ -309,6 +309,9 @@ public void WarningLevel_2()
case ErrorCode.WRN_NullabilityMismatchInParameterTypeOnInterceptor:
case ErrorCode.WRN_CapturedPrimaryConstructorParameterInFieldInitializer:
case ErrorCode.WRN_PrimaryConstructorParameterIsShadowedAndNotPassedToBase:
+ case ErrorCode.WRN_InlineArrayIndexerNotUsed:
+ case ErrorCode.WRN_InlineArraySliceNotUsed:
+ case ErrorCode.WRN_InlineArrayConversionOperatorNotUsed:
Assert.Equal(1, ErrorFacts.GetWarningLevel(errorCode));
break;
case ErrorCode.WRN_MainIgnored: