diff --git a/docs/compilers/CSharp/Warnversion Warning Waves.md b/docs/compilers/CSharp/Warnversion Warning Waves.md
index 19482f55822bd..164173ad21624 100644
--- a/docs/compilers/CSharp/Warnversion Warning Waves.md
+++ b/docs/compilers/CSharp/Warnversion Warning Waves.md
@@ -19,4 +19,5 @@ The table below describes all of the warnings controlled by warning levels `5` o
| Warning ID | warning level | Description |
|------------|---------|-------------|
+| CS7023 | 5 | [A static type is used in an 'is' or 'as' expression](https://github.com/dotnet/roslyn/issues/30198) |
| CS8073 | 5 | [Expression always true (or false) when comparing a struct to null](https://github.com/dotnet/roslyn/issues/45744) |
diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs
index 188921156b4b4..d9ffbc692ed38 100644
--- a/src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs
+++ b/src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs
@@ -2875,12 +2875,11 @@ private bool IsOperatorErrors(CSharpSyntaxNode node, TypeSymbol operandType, Bou
// The native compiler allows "x is C" where C is a static class. This
// is strictly illegal according to the specification (see the section
// called "Referencing Static Class Types".) To retain compatibility we
- // allow it, but when /feature:strict is enabled we break with the native
- // compiler and turn this into an error, as it should be.
- if (targetType.IsStatic && Compilation.FeatureStrictEnabled)
+ // allow it, but when /warn:5 or higher we break with the native
+ // compiler and turn this into a warning.
+ if (targetType.IsStatic)
{
- Error(diagnostics, ErrorCode.ERR_StaticInAsOrIs, node, targetType);
- return true;
+ Error(diagnostics, ErrorCode.WRN_StaticInAsOrIs, node, targetType);
}
if ((object)operandType != null && operandType.IsPointerOrFunctionPointer() || targetType.IsPointerOrFunctionPointer())
@@ -3401,12 +3400,11 @@ private BoundExpression BindAsOperator(BinaryExpressionSyntax node, DiagnosticBa
// a static type C to be an expression of type C.
// It also allows "someObject as C" if "someObject"
// is of type object. To retain compatibility we
- // allow it, but when /feature:strict is enabled we break with the native
- // compiler and turn this into an error, as it should be.
- if (targetType.IsStatic && Compilation.FeatureStrictEnabled)
+ // allow it, but when /warn:5 or higher we break with the native
+ // compiler and turn this into a warning.
+ if (targetType.IsStatic)
{
- Error(diagnostics, ErrorCode.ERR_StaticInAsOrIs, node, targetType);
- return new BoundAsOperator(node, operand, typeExpression, Conversion.NoConversion, resultType, hasErrors: true);
+ Error(diagnostics, ErrorCode.WRN_StaticInAsOrIs, node, targetType);
}
if (operand.IsLiteralNull())
diff --git a/src/Compilers/CSharp/Portable/CSharpResources.resx b/src/Compilers/CSharp/Portable/CSharpResources.resx
index 951400c104af2..40a4ac531da02 100644
--- a/src/Compilers/CSharp/Portable/CSharpResources.resx
+++ b/src/Compilers/CSharp/Portable/CSharpResources.resx
@@ -3896,9 +3896,12 @@ You should consider suppressing the warning only if you're sure that you don't w
The entry point of the program is global code; ignoring entry point
-
+
The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+
Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'
diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
index 4f6329fd8dc4e..d62fc03c91a94 100644
--- a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
+++ b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
@@ -1166,7 +1166,7 @@ internal enum ErrorCode
ERR_YieldNotAllowedInScript = 7020,
ERR_NamespaceNotAllowedInScript = 7021,
WRN_MainIgnored = 7022,
- ERR_StaticInAsOrIs = 7023,
+ WRN_StaticInAsOrIs = 7023,
ERR_InvalidDelegateType = 7024,
ERR_BadVisEventType = 7025,
ERR_GlobalAttributesNotAllowed = 7026,
diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs b/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
index 541d22d07830a..224457267b0e4 100644
--- a/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
+++ b/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
@@ -217,6 +217,7 @@ internal static int GetWarningLevel(ErrorCode code)
switch (code)
{
case ErrorCode.WRN_NubExprIsConstBool2:
+ case ErrorCode.WRN_StaticInAsOrIs:
// Warning level 5 is exclusively for warnings introduced in the compiler
// shipped with dotnet 5 (C# 9) and that can be reported for pre-existing code.
return 5;
diff --git a/src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs b/src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs
index 7b92d1666778a..46d4c14f1f9ce 100644
--- a/src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs
+++ b/src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs
@@ -152,6 +152,7 @@ public static bool IsWarning(ErrorCode code)
case ErrorCode.WRN_CallerFilePathParamForUnconsumedLocation:
case ErrorCode.WRN_CallerMemberNameParamForUnconsumedLocation:
case ErrorCode.WRN_MainIgnored:
+ case ErrorCode.WRN_StaticInAsOrIs:
case ErrorCode.WRN_DelaySignButNoKey:
case ErrorCode.WRN_InvalidVersionFormat:
case ErrorCode.WRN_CallerFilePathPreferredOverCallerMemberName:
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
index 2572dff9b0052..ccf6f2f9bd85a 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
@@ -2399,6 +2399,16 @@
Metoda označená jako [DoesNotReturn] by neměla vracet hodnotu
+
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+ The second operand of an 'is' or 'as' operator may not be a static type
+
+ The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.
@@ -8272,11 +8282,6 @@ Potlačení upozornění zvažte jenom v případě, když určitě nechcete če
Vstupním bodem programu je globální kód skriptu. Vstupní bod se ignoruje.
-
- The second operand of an 'is' or 'as' operator may not be static type '{0}'
- Druhý operand operátoru is nebo as nesmí být statického typu {0}.
-
- Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'Nekonzistentní dostupnost: Typ události {1} je míň dostupný než událost {0}.
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
index 8decafc5d2dd9..919d2583a78f5 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
@@ -2399,6 +2399,16 @@
Eine mit [DoesNotReturn] gekennzeichnete Methode darf nicht zurückgegeben werden.
+
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+ The second operand of an 'is' or 'as' operator may not be a static type
+
+ The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.
@@ -8272,11 +8282,6 @@ Sie sollten das Unterdrücken der Warnung nur in Betracht ziehen, wenn Sie siche
Der Einstiegspunkt des Programms ist globaler Skriptcode; der Einstiegspunkt wird ignoriert.
-
- The second operand of an 'is' or 'as' operator may not be static type '{0}'
- Der zweite Operand eines is- oder as-Operators darf nicht den statischen Typ "{0}" aufweisen.
-
- Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'Inkonsistenter Zugriff: Ereignistyp "{1}" ist weniger zugreifbar als Ereignis "{0}".
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
index c9531bcd3d773..f0a9b49987322 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
@@ -2399,6 +2399,16 @@
Un método marcado [DoesNotReturn] no debe devolver.
+
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+ The second operand of an 'is' or 'as' operator may not be a static type
+
+ The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.
@@ -8272,11 +8282,6 @@ Considere la posibilidad de suprimir la advertencia solo si tiene la seguridad d
El punto de entrada del programa es código de script global. Ignorando punto de entrada
-
- The second operand of an 'is' or 'as' operator may not be static type '{0}'
- El segundo operando de un operador 'is' o 'as' no puede ser el tipo estático '{0}'
-
- Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'Incoherencia de accesibilidad: el tipo de evento '{1}' es menos accesible que el evento '{0}'
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
index af68a2dc8f517..88856e9b8fa9c 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
@@ -2399,6 +2399,16 @@
Une méthode marquée [DoesNotReturn] ne doit pas être retournée.
+
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+ The second operand of an 'is' or 'as' operator may not be a static type
+
+ The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.
@@ -8272,11 +8282,6 @@ Supprimez l'avertissement seulement si vous êtes sûr de ne pas vouloir attendr
Le point d'entrée du programme est du code de script global ; ce point d'entrée est ignoré
-
- The second operand of an 'is' or 'as' operator may not be static type '{0}'
- Le second opérande d'un opérateur 'is' ou 'as' ne peut pas être du type static '{0}'
-
- Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'Accessibilité incohérente : le type d'événement '{1}' est moins accessible que l'événement '{0}'
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
index eb2bec728c33f..588f7e50bb207 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
@@ -2399,6 +2399,16 @@
Un metodo contrassegnato con [DoesNotReturn] non deve essere restituito.
+
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+ The second operand of an 'is' or 'as' operator may not be a static type
+
+ The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.
@@ -8272,11 +8282,6 @@ Come procedura consigliata, è consigliabile attendere sempre la chiamata.
Il punto di ingresso del programma è codice script globale. Il punto di ingresso verrà ignorato
-
- The second operand of an 'is' or 'as' operator may not be static type '{0}'
- Il secondo operando di un operatore 'is' o 'as' non può essere di tipo statico '{0}'
-
- Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'Accessibilità incoerente: il tipo di evento '{1}' è meno accessibile di '{0}'
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
index 6f3b68e438910..2c7e257e7a64f 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
@@ -2399,6 +2399,16 @@
[DoesNotReturn] とマークされたメソッドの返却禁止。
+
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+ The second operand of an 'is' or 'as' operator may not be a static type
+
+ The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.
@@ -8272,11 +8282,6 @@ You should consider suppressing the warning only if you're sure that you don't w
プログラムのエントリ ポイントは、グローバル スクリプト コードです。エントリ ポイントを無視します
-
- The second operand of an 'is' or 'as' operator may not be static type '{0}'
- is' または 'as' 演算子の 2 番目のオペランドはスタティック型 '{0}' にすることはできません。
-
- Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'アクセシビリティに一貫性がありません。イベント型 '{1}' のアクセシビリティはイベント '{0}' よりも低く設定されています。
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
index 5a575ab3b52a5..f6ec0be93a944 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
@@ -2399,6 +2399,16 @@
[DoesNotReturn]으로 표시된 메서드는 반환하지 않아야 합니다.
+
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+ The second operand of an 'is' or 'as' operator may not be a static type
+
+ The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.
@@ -8272,11 +8282,6 @@ You should consider suppressing the warning only if you're sure that you don't w
프로그램의 진입점이 전역 스크립트 코드이며 진입점을 무시함
-
- The second operand of an 'is' or 'as' operator may not be static type '{0}'
- is' 또는 'as' 연산자의 두 번째 피연산자는 '{0}' 정적 형식일 수 없습니다.
-
- Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'일관성 없는 액세스 가능성: '{1}' 이벤트 형식이 '{0}' 이벤트보다 액세스하기 어렵습니다.
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
index b3ab565660228..84413b0a1fa29 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
@@ -2399,6 +2399,16 @@
Metoda oznaczona [DoesNotReturn] nie powinna zwracać wartości.
+
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+ The second operand of an 'is' or 'as' operator may not be a static type
+
+ The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.
@@ -8272,11 +8282,6 @@ Pominięcie ostrzeżenia należy wziąć pod uwagę tylko w sytuacji, gdy na pew
Punkt wejścia programu to kod skryptu globalnego. Punkt wejścia został zignorowany
-
- The second operand of an 'is' or 'as' operator may not be static type '{0}'
- Drugi operand operatora „is” lub „as” nie może być typem statycznym „{0}”
-
- Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'Niespójność dostępności: typ zdarzenia „{1}” jest mniej dostępny niż zdarzenie „{0}”
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
index b7277640135fe..46ed5e788289a 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
@@ -2397,6 +2397,16 @@
Um método marcado como [DoesNotReturn] não deve ser retornado.
+
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+ The second operand of an 'is' or 'as' operator may not be a static type
+
+ The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.
@@ -8270,11 +8280,6 @@ Você pode suprimir o aviso se tiver certeza de que não vai querer aguardar a c
O ponto de entrada do programa é o código de script global; ignorando o ponto de entrada
-
- The second operand of an 'is' or 'as' operator may not be static type '{0}'
- O segundo operando de um operador "is" ou "as" não pode ser do tipo estático "{0}"
-
- Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'Acessibilidade inconsistente: tipo de evento "{1}" é menos acessível do que o evento "{0}"
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
index 019f2002801c6..653c8c3587ea7 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
@@ -2399,6 +2399,16 @@
Метод, помеченный [DoesNotReturn], не должен возвращать значение
+
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+ The second operand of an 'is' or 'as' operator may not be a static type
+
+ The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.
@@ -8272,11 +8282,6 @@ You should consider suppressing the warning only if you're sure that you don't w
Точка входа в программе является глобальным кодом скрипта; выполняется пропуск точки входа
-
- The second operand of an 'is' or 'as' operator may not be static type '{0}'
- Второй операнд оператора "is" или "as" не может быть статического типа "{0}".
-
- Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'Несогласованность по доступности: доступность типа события "{1}" ниже доступности события "{0}"
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
index 19e6deb91ea52..07900171f57cc 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
@@ -2399,6 +2399,16 @@
[DoesNotReturn] olarak işaretlenen bir yöntem, döndürmemelidir.
+
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+ The second operand of an 'is' or 'as' operator may not be a static type
+
+ The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.
@@ -8272,11 +8282,6 @@ Yalnızca asenkron çağrının tamamlanmasını beklemek istemediğinizden ve
Programın giriş noktası genel betik kodudur, giriş noktası yoksayılıyor
-
- The second operand of an 'is' or 'as' operator may not be static type '{0}'
- Bir 'is' veya 'as' işlecinin ikinci işleneni '{0}' statik türü olmayabilir
-
- Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'Tutarsız erişilebilirlik: '{1}' olay türü, '{0}' olayından daha az erişilebilir
diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
index bf5adb8e3c91a..7652719ed608a 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
@@ -2399,6 +2399,16 @@
不应返回标记为 [DoesNotReturn] 的方法。
+
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+ The second operand of an 'is' or 'as' operator may not be a static type
+
+ The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.
@@ -8272,11 +8282,6 @@ You should consider suppressing the warning only if you're sure that you don't w
程序的入口点是全局脚本代码;正在忽略入口点
-
- The second operand of an 'is' or 'as' operator may not be static type '{0}'
- “is”或“as”运算符的第二个操作数不能是静态类型“{0}”
-
- Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'可访问性不一致: 事件类型“{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 3907e81058954..c94c873cf9fc6 100644
--- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
+++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
@@ -2399,6 +2399,16 @@
標記 [DoesNotReturn] 的方法不應傳回。
+
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+ The second operand of an 'is' or 'as' operator may not be static type '{0}'
+
+
+
+ The second operand of an 'is' or 'as' operator may not be a static type
+ The second operand of an 'is' or 'as' operator may not be a static type
+
+ The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{0}' is not covered.
@@ -8272,11 +8282,6 @@ You should consider suppressing the warning only if you're sure that you don't w
程式的進入點是全域指令碼; 將忽略進入點
-
- The second operand of an 'is' or 'as' operator may not be static type '{0}'
- is' 或 'as' 運算子的第二個運算元不可為靜態類型 '{0}'
-
- Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'不一致的存取範圍: 事件類型 '{1}' 比事件 '{0}' 的存取範圍小
diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/WarningVersionTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/WarningVersionTests.cs
index dad55fb13c8c1..66bb567960808 100644
--- a/src/Compilers/CSharp/Test/Semantic/Semantics/WarningVersionTests.cs
+++ b/src/Compilers/CSharp/Test/Semantic/Semantics/WarningVersionTests.cs
@@ -47,5 +47,33 @@ struct S
CreateCompilation(source, options: TestOptions.ReleaseDll.WithWarningLevel(5)).VerifyDiagnostics(whenWave5);
CreateCompilation(source, options: TestOptions.ReleaseDll.WithWarningLevel(6)).VerifyDiagnostics(whenWave5);
}
+
+ [Fact]
+ public void WRN_StaticInAsOrIs()
+ {
+ var source = @"
+class Program
+{
+ public static void M(object o)
+ {
+ if (o is SC)
+ _ = o as SC;
+ }
+}
+static class SC { }
+";
+ var whenWave5 = new[]
+ {
+ // (6,13): warning CS7023: The second operand of an 'is' or 'as' operator may not be static type 'SC'
+ // if (o is SC)
+ Diagnostic(ErrorCode.WRN_StaticInAsOrIs, "o is SC").WithArguments("SC").WithLocation(6, 13),
+ // (7,17): warning CS7023: The second operand of an 'is' or 'as' operator may not be static type 'SC'
+ // _ = o as SC;
+ Diagnostic(ErrorCode.WRN_StaticInAsOrIs, "o as SC").WithArguments("SC").WithLocation(7, 17)
+ };
+ CreateCompilation(source).VerifyDiagnostics();
+ CreateCompilation(source, options: TestOptions.ReleaseDll.WithWarningLevel(4)).VerifyDiagnostics();
+ CreateCompilation(source, options: TestOptions.ReleaseDll.WithWarningLevel(5)).VerifyDiagnostics(whenWave5);
+ }
}
}
diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs
index 47f134f593113..fcaee99b2c2a7 100644
--- a/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs
+++ b/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs
@@ -12887,9 +12887,10 @@ static void M(object o)
[Fact]
public void CS7023ERR_StaticInIsAsOrIs()
{
- // BREAKING CHANGE: The C# specification states that it is always illegal
- // BREAKING CHANGE: to use a static type with "is" and "as". The native
- // BREAKING CHANGE: compiler allows it in some cases; Roslyn does not.
+ // The C# specification states that it is always illegal
+ // to use a static type with "is" and "as". The native
+ // compiler allows it in some cases; Roslyn gives a warning
+ // at level '/warn:5' or higher.
var text = @"
static class C
@@ -12910,55 +12911,63 @@ static void M(object o)
}
}
";
- var regularComp = CreateCompilation(text);
-
- // these diagnostics correspond to those produced by the native compiler.
- regularComp.VerifyDiagnostics(
+ var strictDiagnostics = new[]
+ {
+ // (6,11): warning CS7023: The second operand of an 'is' or 'as' operator may not be static type 'C'
+ // M(o as C); // legal in native
+ Diagnostic(ErrorCode.WRN_StaticInAsOrIs, "o as C").WithArguments("C").WithLocation(6, 11),
+ // (7,11): warning CS7023: The second operand of an 'is' or 'as' operator may not be static type 'C'
+ // M(new object() as C); // legal in native
+ Diagnostic(ErrorCode.WRN_StaticInAsOrIs, "new object() as C").WithArguments("C").WithLocation(7, 11),
+ // (8,11): warning CS7023: The second operand of an 'is' or 'as' operator may not be static type 'C'
+ // M(null as C); // legal in native
+ Diagnostic(ErrorCode.WRN_StaticInAsOrIs, "null as C").WithArguments("C").WithLocation(8, 11),
+ // (9,11): warning CS7023: The second operand of an 'is' or 'as' operator may not be static type 'C'
+ // M(1 as C);
+ Diagnostic(ErrorCode.WRN_StaticInAsOrIs, "1 as C").WithArguments("C").WithLocation(9, 11),
// (9,11): error CS0039: Cannot convert type 'int' to 'C' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
// M(1 as C);
Diagnostic(ErrorCode.ERR_NoExplicitBuiltinConv, "1 as C").WithArguments("int", "C").WithLocation(9, 11),
+ // (10,11): warning CS7023: The second operand of an 'is' or 'as' operator may not be static type 'C'
+ // M("a" as C);
+ Diagnostic(ErrorCode.WRN_StaticInAsOrIs, @"""a"" as C").WithArguments("C").WithLocation(10, 11),
// (10,11): error CS0039: Cannot convert type 'string' to 'C' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
// M("a" as C);
Diagnostic(ErrorCode.ERR_NoExplicitBuiltinConv, @"""a"" as C").WithArguments("string", "C").WithLocation(10, 11),
+ // (12,11): warning CS7023: The second operand of an 'is' or 'as' operator may not be static type 'C'
+ // M(o is C); // legal in native, no warning
+ Diagnostic(ErrorCode.WRN_StaticInAsOrIs, "o is C").WithArguments("C").WithLocation(12, 11),
+ // (13,11): warning CS7023: The second operand of an 'is' or 'as' operator may not be static type 'C'
+ // M(new object() is C); // legal in native, no warning
+ Diagnostic(ErrorCode.WRN_StaticInAsOrIs, "new object() is C").WithArguments("C").WithLocation(13, 11),
+ // (14,11): warning CS7023: The second operand of an 'is' or 'as' operator may not be static type 'C'
+ // M(null is C); // legal in native, warns
+ Diagnostic(ErrorCode.WRN_StaticInAsOrIs, "null is C").WithArguments("C").WithLocation(14, 11),
// (14,11): warning CS0184: The given expression is never of the provided ('C') type
// M(null is C); // legal in native, warns
Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "null is C").WithArguments("C").WithLocation(14, 11),
+ // (15,11): warning CS7023: The second operand of an 'is' or 'as' operator may not be static type 'C'
+ // M(1 is C); // legal in native, warns
+ Diagnostic(ErrorCode.WRN_StaticInAsOrIs, "1 is C").WithArguments("C").WithLocation(15, 11),
// (15,11): warning CS0184: The given expression is never of the provided ('C') type
// M(1 is C); // legal in native, warns
Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "1 is C").WithArguments("C").WithLocation(15, 11),
+ // (16,11): warning CS7023: The second operand of an 'is' or 'as' operator may not be static type 'C'
+ // M("a" is C); // legal in native, warns
+ Diagnostic(ErrorCode.WRN_StaticInAsOrIs, @"""a"" is C").WithArguments("C").WithLocation(16, 11),
// (16,11): warning CS0184: The given expression is never of the provided ('C') type
// M("a" is C); // legal in native, warns
Diagnostic(ErrorCode.WRN_IsAlwaysFalse, @"""a"" is C").WithArguments("C").WithLocation(16, 11)
- );
-
- // in strict mode we also diagnose "is" and "as" operators with a static type.
- var strictComp = CreateCompilation(text, parseOptions: TestOptions.Regular.WithStrictFeature());
- strictComp.VerifyDiagnostics(
- // In the native compiler these three produce no errors.
-
- Diagnostic(ErrorCode.ERR_StaticInAsOrIs, "o as C").WithArguments("C"),
- Diagnostic(ErrorCode.ERR_StaticInAsOrIs, "new object() as C").WithArguments("C"),
- Diagnostic(ErrorCode.ERR_StaticInAsOrIs, "null as C").WithArguments("C"),
-
- // In the native compiler these two produce:
- // error CS0039: Cannot convert type 'int' to 'C' via a reference conversion, boxing conversion,
- // unboxing conversion, wrapping conversion, or null type conversion
-
- Diagnostic(ErrorCode.ERR_StaticInAsOrIs, "1 as C").WithArguments("C"),
- Diagnostic(ErrorCode.ERR_StaticInAsOrIs, "\"a\" as C").WithArguments("C"),
-
- // In the native compiler these two produce no errors:
-
- Diagnostic(ErrorCode.ERR_StaticInAsOrIs, "o is C").WithArguments("C"),
- Diagnostic(ErrorCode.ERR_StaticInAsOrIs, "new object() is C").WithArguments("C"),
+ };
- // In the native compiler these three produce:
- // warning CS0184: The given expression is never of the provided ('C') type
+ // in /warn:5 we diagnose "is" and "as" operators with a static type.
+ var strictComp = CreateCompilation(text, options: TestOptions.ReleaseDll.WithWarningLevel(5));
+ strictComp.VerifyDiagnostics(strictDiagnostics);
- Diagnostic(ErrorCode.ERR_StaticInAsOrIs, "null is C").WithArguments("C"),
- Diagnostic(ErrorCode.ERR_StaticInAsOrIs, "1 is C").WithArguments("C"),
- Diagnostic(ErrorCode.ERR_StaticInAsOrIs, "\"a\" is C").WithArguments("C")
- );
+ // these rest of the diagnostics correspond to those produced by the native compiler.
+ var regularDiagnostics = strictDiagnostics.Where(d => !d.Code.Equals((int)ErrorCode.WRN_StaticInAsOrIs)).ToArray();
+ var regularComp = CreateCompilation(text);
+ regularComp.VerifyDiagnostics(regularDiagnostics);
}
[Fact]
diff --git a/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs b/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs
index 6279125878e90..ae3473b0a3d96 100644
--- a/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs
+++ b/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs
@@ -331,6 +331,7 @@ public void WarningLevel_2()
Assert.Equal(4, ErrorFacts.GetWarningLevel(errorCode));
break;
case ErrorCode.WRN_NubExprIsConstBool2:
+ case ErrorCode.WRN_StaticInAsOrIs:
Assert.Equal(5, ErrorFacts.GetWarningLevel(errorCode));
break;
default: