未验证 提交 838874b1 编写于 作者: N Neal Gafter 提交者: GitHub

Diagnose a static type used in an 'is' or 'as' expression. (#46240)

Fixes #30198
上级 a842aba2
......@@ -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) |
......@@ -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())
......
......@@ -3896,9 +3896,12 @@ You should consider suppressing the warning only if you're sure that you don't w
<data name="WRN_MainIgnored_Title" xml:space="preserve">
<value>The entry point of the program is global code; ignoring entry point</value>
</data>
<data name="ERR_StaticInAsOrIs" xml:space="preserve">
<data name="WRN_StaticInAsOrIs" xml:space="preserve">
<value>The second operand of an 'is' or 'as' operator may not be static type '{0}'</value>
</data>
<data name="WRN_StaticInAsOrIs_Title" xml:space="preserve">
<value>The second operand of an 'is' or 'as' operator may not be a static type</value>
</data>
<data name="ERR_BadVisEventType" xml:space="preserve">
<value>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</value>
</data>
......
......@@ -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,
......
......@@ -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;
......
......@@ -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:
......
......@@ -2416,6 +2416,16 @@
<target state="translated">Metoda označená jako [DoesNotReturn] by se neměla ukončit standardním způsobem</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs_Title">
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
<note />
</trans-unit>
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
<source>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.</source>
<target state="translated">Výraz switch nezachycuje všechny možné hodnoty vstupního typu (není úplný). Nezachycuje například vzor {0}.</target>
......@@ -8289,11 +8299,6 @@ Potlačení upozornění zvažte jenom v případě, když určitě nechcete če
<target state="translated">Vstupním bodem programu je globální kód. Vstupní bod se ignoruje</target>
<note />
</trans-unit>
<trans-unit id="ERR_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="translated">Druhý operand operátoru is nebo as nesmí být statického typu {0}.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadVisEventType">
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
<target state="translated">Nekonzistentní dostupnost: Typ události {1} je míň dostupný než událost {0}.</target>
......
......@@ -2416,6 +2416,16 @@
<target state="translated">Eine mit [DoesNotReturn] gekennzeichnete Methode darf nicht zurückgegeben werden.</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs_Title">
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
<note />
</trans-unit>
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
<source>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.</source>
<target state="translated">Der switch-Ausdruck verarbeitet nicht alle möglichen Werte des zugehörigen Eingabetyps (nicht umfassender Ausdruck). Das Muster "{0}" wird beispielsweise nicht abgedeckt.</target>
......@@ -8289,11 +8299,6 @@ Sie sollten das Unterdrücken der Warnung nur in Betracht ziehen, wenn Sie siche
<target state="translated">Der Einstiegspunkt des Programms ist globaler Code. Der Einstiegspunkt wird ignoriert.</target>
<note />
</trans-unit>
<trans-unit id="ERR_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="translated">Der zweite Operand eines is- oder as-Operators darf nicht den statischen Typ "{0}" aufweisen.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadVisEventType">
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
<target state="translated">Inkonsistenter Zugriff: Ereignistyp "{1}" ist weniger zugreifbar als Ereignis "{0}".</target>
......
......@@ -2416,6 +2416,16 @@
<target state="translated">Un método marcado como [DoesNotReturn] no debe devolver nada.</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs_Title">
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
<note />
</trans-unit>
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
<source>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.</source>
<target state="translated">La expresión switch no controla todos los valores posibles de su tipo de entrada (no es exhaustivo). Por ejemplo, el patrón "{0}" no está incluido.</target>
......@@ -8289,11 +8299,6 @@ Considere la posibilidad de suprimir la advertencia solo si tiene la seguridad d
<target state="translated">El punto de entrada del programa es código global; se ignora el punto de entrada.</target>
<note />
</trans-unit>
<trans-unit id="ERR_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="translated">El segundo operando de un operador 'is' o 'as' no puede ser el tipo estático '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadVisEventType">
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
<target state="translated">Incoherencia de accesibilidad: el tipo de evento '{1}' es menos accesible que el evento '{0}'</target>
......
......@@ -2416,6 +2416,16 @@
<target state="translated">Une méthode marquée [DoesNotReturn] ne doit pas être retournée.</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs_Title">
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
<note />
</trans-unit>
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
<source>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.</source>
<target state="translated">L'expression switch ne prend pas en charge toutes les valeurs possibles de son type d'entrée (elle n'est pas exhaustive). Par exemple, le modèle '{0}' n'est pas couvert.</target>
......@@ -8289,11 +8299,6 @@ Supprimez l'avertissement seulement si vous êtes sûr de ne pas vouloir attendr
<target state="translated">Le point d'entrée du programme est du code global ; ce point d'entrée est ignoré</target>
<note />
</trans-unit>
<trans-unit id="ERR_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="translated">Le second opérande d'un opérateur 'is' ou 'as' ne peut pas être du type static '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadVisEventType">
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
<target state="translated">Accessibilité incohérente : le type d'événement '{1}' est moins accessible que l'événement '{0}'</target>
......
......@@ -2416,6 +2416,16 @@
<target state="translated">Un metodo contrassegnato con [DoesNotReturn] non deve essere terminare normalmente.</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs_Title">
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
<note />
</trans-unit>
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
<source>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.</source>
<target state="translated">L'espressione switch non gestisce tutti i possibili valori del relativo tipo di input (non è esaustiva). Ad esempio, il criterio '{0}' non è coperto.</target>
......@@ -8289,11 +8299,6 @@ Come procedura consigliata, è consigliabile attendere sempre la chiamata.
<target state="translated">Il punto di ingresso del programma è codice globale. Il punto di ingresso verrà ignorato</target>
<note />
</trans-unit>
<trans-unit id="ERR_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="translated">Il secondo operando di un operatore 'is' o 'as' non può essere di tipo statico '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadVisEventType">
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
<target state="translated">Accessibilità incoerente: il tipo di evento '{1}' è meno accessibile di '{0}'</target>
......
......@@ -2416,6 +2416,16 @@
<target state="translated">[DoesNotReturn] とマークされたメソッドの返却禁止。</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs_Title">
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
<note />
</trans-unit>
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
<source>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.</source>
<target state="translated">この switch 式では入力型の可能な値がすべて扱われるわけではありません (すべてが網羅されているわけではありません)。たとえば、パターン '{0}' がカバーされていません。</target>
......@@ -8289,11 +8299,6 @@ You should consider suppressing the warning only if you're sure that you don't w
<target state="translated">プログラムのエントリ ポイントがグローバル コード、エントリ ポイントを無視</target>
<note />
</trans-unit>
<trans-unit id="ERR_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="translated">is' または 'as' 演算子の 2 番目のオペランドはスタティック型 '{0}' にすることはできません。</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadVisEventType">
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
<target state="translated">アクセシビリティに一貫性がありません。イベント型 '{1}' のアクセシビリティはイベント '{0}' よりも低く設定されています。</target>
......
......@@ -2416,6 +2416,16 @@
<target state="translated">[DoesNotReturn]으로 표시된 메서드는 반환하지 않아야 합니다.</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs_Title">
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
<note />
</trans-unit>
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
<source>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.</source>
<target state="translated">switch 식에서 입력 형식의 가능한 값을 모두 처리하지는 않습니다(전체 아님). 예를 들어 '{0}' 패턴은 포함되지 않습니다.</target>
......@@ -8289,11 +8299,6 @@ You should consider suppressing the warning only if you're sure that you don't w
<target state="translated">프로그램의 진입점이 전역 코드이며 진입점을 무시함</target>
<note />
</trans-unit>
<trans-unit id="ERR_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="translated">is' 또는 'as' 연산자의 두 번째 피연산자는 '{0}' 정적 형식일 수 없습니다.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadVisEventType">
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
<target state="translated">일관성 없는 액세스 가능성: '{1}' 이벤트 형식이 '{0}' 이벤트보다 액세스하기 어렵습니다.</target>
......
......@@ -2416,6 +2416,16 @@
<target state="translated">Metoda z oznaczeniem [DoesNotReturn] nie powinna zwracać wartości.</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs_Title">
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
<note />
</trans-unit>
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
<source>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.</source>
<target state="translated">Wyrażenie switch nie obsługuje wszystkich możliwych wartości typu wejściowego (nie jest wyczerpujące). Na przykład nie jest uwzględniony wzorzec „{0}”.</target>
......@@ -8289,11 +8299,6 @@ Pominięcie ostrzeżenia należy wziąć pod uwagę tylko w sytuacji, gdy na pew
<target state="translated">Punkt wejścia programu to kod globalny; punkt wejścia jest ignorowany</target>
<note />
</trans-unit>
<trans-unit id="ERR_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="translated">Drugi operand operatora „is” lub „as” nie może być typem statycznym „{0}”</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadVisEventType">
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
<target state="translated">Niespójność dostępności: typ zdarzenia „{1}” jest mniej dostępny niż zdarzenie „{0}”</target>
......
......@@ -2415,6 +2415,16 @@
<target state="translated">Um método marcado como [DoesNotReturn] não deve ser retornado.</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs_Title">
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
<note />
</trans-unit>
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
<source>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.</source>
<target state="translated">A expressão switch não manipula todos os valores possíveis do tipo de entrada (ela não é exaustiva). Por exemplo, o padrão '{0}' não é coberto.</target>
......@@ -8288,11 +8298,6 @@ Você pode suprimir o aviso se tiver certeza de que não vai querer aguardar a c
<target state="translated">O ponto de entrada do programa é o código global. Ignorando o ponto de entrada</target>
<note />
</trans-unit>
<trans-unit id="ERR_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="translated">O segundo operando de um operador "is" ou "as" não pode ser do tipo estático "{0}"</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadVisEventType">
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
<target state="translated">Acessibilidade inconsistente: tipo de evento "{1}" é menos acessível do que o evento "{0}"</target>
......
......@@ -2416,6 +2416,16 @@
<target state="translated">Метод, помеченный [DoesNotReturn], не должен возвращать значение.</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs_Title">
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
<note />
</trans-unit>
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
<source>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.</source>
<target state="translated">Выражение switch не обрабатывает все возможные типы входных значений (не является исчерпывающим). Например, шаблон "{0}" не охвачен.</target>
......@@ -8289,11 +8299,6 @@ You should consider suppressing the warning only if you're sure that you don't w
<target state="translated">Точкой входа программы является глобальный код; игнорируется точка входа</target>
<note />
</trans-unit>
<trans-unit id="ERR_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="translated">Второй операнд оператора "is" или "as" не может быть статического типа "{0}".</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadVisEventType">
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
<target state="translated">Несогласованность по доступности: доступность типа события "{1}" ниже доступности события "{0}"</target>
......
......@@ -2416,6 +2416,16 @@
<target state="translated">[DoesNotReturn] olarak işaretlenen bir metot, değer döndürmemelidir.</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs_Title">
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
<note />
</trans-unit>
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
<source>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.</source>
<target state="translated">Switch ifadesi kendi giriş türünün tüm olası değerlerini işlemiyor (tam kapsamlı değil). Örneğin, '{0}' deseni kapsanmıyor.</target>
......@@ -8289,11 +8299,6 @@ Yalnızca asenkron çağrının tamamlanmasını beklemek istemediğinizden ve
<target state="translated">Programın giriş noktası genel koddur, giriş noktası yoksayılıyor</target>
<note />
</trans-unit>
<trans-unit id="ERR_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="translated">Bir 'is' veya 'as' işlecinin ikinci işleneni '{0}' statik türü olmayabilir</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadVisEventType">
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
<target state="translated">Tutarsız erişilebilirlik: '{1}' olay türü, '{0}' olayından daha az erişilebilir</target>
......
......@@ -2416,6 +2416,16 @@
<target state="translated">不应返回标记为 [DoesNotReturn] 的方法。</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs_Title">
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
<note />
</trans-unit>
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
<source>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.</source>
<target state="translated">switch 表达式不处理其输入类型的所有可能的值(它不是穷举)。例如,模式“{0}”未包含在内。</target>
......@@ -8289,11 +8299,6 @@ You should consider suppressing the warning only if you're sure that you don't w
<target state="translated">程序的入口点是全局代码;将忽略此入口点</target>
<note />
</trans-unit>
<trans-unit id="ERR_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="translated">“is”或“as”运算符的第二个操作数不能是静态类型“{0}”</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadVisEventType">
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
<target state="translated">可访问性不一致: 事件类型“{1}”的可访问性低于事件“{0}”</target>
......
......@@ -2416,6 +2416,16 @@
<target state="translated">標記 [DoesNotReturn] 的方法不應傳回。</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
<note />
</trans-unit>
<trans-unit id="WRN_StaticInAsOrIs_Title">
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
<note />
</trans-unit>
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
<source>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.</source>
<target state="translated">switch 運算式未處理其輸入類型的所有可能值 (未徹底處理)。例如,未涵蓋模式 '{0}'。</target>
......@@ -8289,11 +8299,6 @@ You should consider suppressing the warning only if you're sure that you don't w
<target state="translated">程式的進入點是全域程式碼; 將忽略進入點</target>
<note />
</trans-unit>
<trans-unit id="ERR_StaticInAsOrIs">
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
<target state="translated">is' 或 'as' 運算子的第二個運算元不可為靜態類型 '{0}'</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadVisEventType">
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
<target state="translated">不一致的存取範圍: 事件類型 '{1}' 比事件 '{0}' 的存取範圍小</target>
......
......@@ -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);
}
}
}
......@@ -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]
......
......@@ -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:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册