Skip to content

Commit 838874b

Browse files
author
Neal Gafter
authored
Diagnose a static type used in an 'is' or 'as' expression. (#46240)
Fixes #30198
1 parent a842aba commit 838874b

22 files changed

+219
-112
lines changed

docs/compilers/CSharp/Warnversion Warning Waves.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ The table below describes all of the warnings controlled by warning levels `5` o
1919

2020
| Warning ID | warning level | Description |
2121
|------------|---------|-------------|
22+
| CS7023 | 5 | [A static type is used in an 'is' or 'as' expression](https://github.com/dotnet/roslyn/issues/30198) |
2223
| CS8073 | 5 | [Expression always true (or false) when comparing a struct to null](https://github.com/dotnet/roslyn/issues/45744) |

src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,12 +2875,11 @@ private bool IsOperatorErrors(CSharpSyntaxNode node, TypeSymbol operandType, Bou
28752875
// The native compiler allows "x is C" where C is a static class. This
28762876
// is strictly illegal according to the specification (see the section
28772877
// called "Referencing Static Class Types".) To retain compatibility we
2878-
// allow it, but when /feature:strict is enabled we break with the native
2879-
// compiler and turn this into an error, as it should be.
2880-
if (targetType.IsStatic && Compilation.FeatureStrictEnabled)
2878+
// allow it, but when /warn:5 or higher we break with the native
2879+
// compiler and turn this into a warning.
2880+
if (targetType.IsStatic)
28812881
{
2882-
Error(diagnostics, ErrorCode.ERR_StaticInAsOrIs, node, targetType);
2883-
return true;
2882+
Error(diagnostics, ErrorCode.WRN_StaticInAsOrIs, node, targetType);
28842883
}
28852884

28862885
if ((object)operandType != null && operandType.IsPointerOrFunctionPointer() || targetType.IsPointerOrFunctionPointer())
@@ -3401,12 +3400,11 @@ private BoundExpression BindAsOperator(BinaryExpressionSyntax node, DiagnosticBa
34013400
// a static type C to be an expression of type C.
34023401
// It also allows "someObject as C" if "someObject"
34033402
// is of type object. To retain compatibility we
3404-
// allow it, but when /feature:strict is enabled we break with the native
3405-
// compiler and turn this into an error, as it should be.
3406-
if (targetType.IsStatic && Compilation.FeatureStrictEnabled)
3403+
// allow it, but when /warn:5 or higher we break with the native
3404+
// compiler and turn this into a warning.
3405+
if (targetType.IsStatic)
34073406
{
3408-
Error(diagnostics, ErrorCode.ERR_StaticInAsOrIs, node, targetType);
3409-
return new BoundAsOperator(node, operand, typeExpression, Conversion.NoConversion, resultType, hasErrors: true);
3407+
Error(diagnostics, ErrorCode.WRN_StaticInAsOrIs, node, targetType);
34103408
}
34113409

34123410
if (operand.IsLiteralNull())

src/Compilers/CSharp/Portable/CSharpResources.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3896,9 +3896,12 @@ You should consider suppressing the warning only if you're sure that you don't w
38963896
<data name="WRN_MainIgnored_Title" xml:space="preserve">
38973897
<value>The entry point of the program is global code; ignoring entry point</value>
38983898
</data>
3899-
<data name="ERR_StaticInAsOrIs" xml:space="preserve">
3899+
<data name="WRN_StaticInAsOrIs" xml:space="preserve">
39003900
<value>The second operand of an 'is' or 'as' operator may not be static type '{0}'</value>
39013901
</data>
3902+
<data name="WRN_StaticInAsOrIs_Title" xml:space="preserve">
3903+
<value>The second operand of an 'is' or 'as' operator may not be a static type</value>
3904+
</data>
39023905
<data name="ERR_BadVisEventType" xml:space="preserve">
39033906
<value>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</value>
39043907
</data>

src/Compilers/CSharp/Portable/Errors/ErrorCode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ internal enum ErrorCode
11661166
ERR_YieldNotAllowedInScript = 7020,
11671167
ERR_NamespaceNotAllowedInScript = 7021,
11681168
WRN_MainIgnored = 7022,
1169-
ERR_StaticInAsOrIs = 7023,
1169+
WRN_StaticInAsOrIs = 7023,
11701170
ERR_InvalidDelegateType = 7024,
11711171
ERR_BadVisEventType = 7025,
11721172
ERR_GlobalAttributesNotAllowed = 7026,

src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ internal static int GetWarningLevel(ErrorCode code)
217217
switch (code)
218218
{
219219
case ErrorCode.WRN_NubExprIsConstBool2:
220+
case ErrorCode.WRN_StaticInAsOrIs:
220221
// Warning level 5 is exclusively for warnings introduced in the compiler
221222
// shipped with dotnet 5 (C# 9) and that can be reported for pre-existing code.
222223
return 5;

src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,6 +2416,16 @@
24162416
<target state="translated">Metoda označená jako [DoesNotReturn] by se neměla ukončit standardním způsobem</target>
24172417
<note />
24182418
</trans-unit>
2419+
<trans-unit id="WRN_StaticInAsOrIs">
2420+
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
2421+
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
2422+
<note />
2423+
</trans-unit>
2424+
<trans-unit id="WRN_StaticInAsOrIs_Title">
2425+
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
2426+
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
2427+
<note />
2428+
</trans-unit>
24192429
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
24202430
<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>
24212431
<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
82898299
<target state="translated">Vstupním bodem programu je globální kód. Vstupní bod se ignoruje</target>
82908300
<note />
82918301
</trans-unit>
8292-
<trans-unit id="ERR_StaticInAsOrIs">
8293-
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
8294-
<target state="translated">Druhý operand operátoru is nebo as nesmí být statického typu {0}.</target>
8295-
<note />
8296-
</trans-unit>
82978302
<trans-unit id="ERR_BadVisEventType">
82988303
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
82998304
<target state="translated">Nekonzistentní dostupnost: Typ události {1} je míň dostupný než událost {0}.</target>

src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,6 +2416,16 @@
24162416
<target state="translated">Eine mit [DoesNotReturn] gekennzeichnete Methode darf nicht zurückgegeben werden.</target>
24172417
<note />
24182418
</trans-unit>
2419+
<trans-unit id="WRN_StaticInAsOrIs">
2420+
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
2421+
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
2422+
<note />
2423+
</trans-unit>
2424+
<trans-unit id="WRN_StaticInAsOrIs_Title">
2425+
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
2426+
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
2427+
<note />
2428+
</trans-unit>
24192429
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
24202430
<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>
24212431
<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
82898299
<target state="translated">Der Einstiegspunkt des Programms ist globaler Code. Der Einstiegspunkt wird ignoriert.</target>
82908300
<note />
82918301
</trans-unit>
8292-
<trans-unit id="ERR_StaticInAsOrIs">
8293-
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
8294-
<target state="translated">Der zweite Operand eines is- oder as-Operators darf nicht den statischen Typ "{0}" aufweisen.</target>
8295-
<note />
8296-
</trans-unit>
82978302
<trans-unit id="ERR_BadVisEventType">
82988303
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
82998304
<target state="translated">Inkonsistenter Zugriff: Ereignistyp "{1}" ist weniger zugreifbar als Ereignis "{0}".</target>

src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,6 +2416,16 @@
24162416
<target state="translated">Un método marcado como [DoesNotReturn] no debe devolver nada.</target>
24172417
<note />
24182418
</trans-unit>
2419+
<trans-unit id="WRN_StaticInAsOrIs">
2420+
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
2421+
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
2422+
<note />
2423+
</trans-unit>
2424+
<trans-unit id="WRN_StaticInAsOrIs_Title">
2425+
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
2426+
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
2427+
<note />
2428+
</trans-unit>
24192429
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
24202430
<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>
24212431
<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
82898299
<target state="translated">El punto de entrada del programa es código global; se ignora el punto de entrada.</target>
82908300
<note />
82918301
</trans-unit>
8292-
<trans-unit id="ERR_StaticInAsOrIs">
8293-
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
8294-
<target state="translated">El segundo operando de un operador 'is' o 'as' no puede ser el tipo estático '{0}'</target>
8295-
<note />
8296-
</trans-unit>
82978302
<trans-unit id="ERR_BadVisEventType">
82988303
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
82998304
<target state="translated">Incoherencia de accesibilidad: el tipo de evento '{1}' es menos accesible que el evento '{0}'</target>

src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,6 +2416,16 @@
24162416
<target state="translated">Une méthode marquée [DoesNotReturn] ne doit pas être retournée.</target>
24172417
<note />
24182418
</trans-unit>
2419+
<trans-unit id="WRN_StaticInAsOrIs">
2420+
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
2421+
<target state="new">The second operand of an 'is' or 'as' operator may not be static type '{0}'</target>
2422+
<note />
2423+
</trans-unit>
2424+
<trans-unit id="WRN_StaticInAsOrIs_Title">
2425+
<source>The second operand of an 'is' or 'as' operator may not be a static type</source>
2426+
<target state="new">The second operand of an 'is' or 'as' operator may not be a static type</target>
2427+
<note />
2428+
</trans-unit>
24192429
<trans-unit id="WRN_SwitchExpressionNotExhaustive">
24202430
<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>
24212431
<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
82898299
<target state="translated">Le point d'entrée du programme est du code global ; ce point d'entrée est ignoré</target>
82908300
<note />
82918301
</trans-unit>
8292-
<trans-unit id="ERR_StaticInAsOrIs">
8293-
<source>The second operand of an 'is' or 'as' operator may not be static type '{0}'</source>
8294-
<target state="translated">Le second opérande d'un opérateur 'is' ou 'as' ne peut pas être du type static '{0}'</target>
8295-
<note />
8296-
</trans-unit>
82978302
<trans-unit id="ERR_BadVisEventType">
82988303
<source>Inconsistent accessibility: event type '{1}' is less accessible than event '{0}'</source>
82998304
<target state="translated">Accessibilité incohérente : le type d'événement '{1}' est moins accessible que l'événement '{0}'</target>

0 commit comments

Comments
 (0)