Browse Source

Fix #2283: Disallow all implicit conversions in switch on string.

pull/2301/head
Siegfried Pammer 4 years ago
parent
commit
f29205448b
  1. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs
  2. 4
      ICSharpCode.Decompiler/CSharp/StatementBuilder.cs

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs

@ -411,7 +411,9 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -411,7 +411,9 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public static string SwitchOverImplicitString(ImplicitString s)
{
switch (s)
// we emit an explicit cast, because the rules used by the C# compiler are counter-intuitive:
// The C# compiler does *not* take the type of the switch labels into account at all.
switch ((string)s)
{
case "First case":
return "Text1";

4
ICSharpCode.Decompiler/CSharp/StatementBuilder.cs

@ -205,7 +205,9 @@ namespace ICSharpCode.Decompiler.CSharp @@ -205,7 +205,9 @@ namespace ICSharpCode.Decompiler.CSharp
.ConvertTo(
typeSystem.FindType(KnownTypeCode.String),
exprBuilder,
allowImplicitConversion: true
// switch statement does support implicit conversions in general, however, the rules are
// not very intuitive and in order to prevent bugs, we emit an explicit cast.
allowImplicitConversion: false
);
type = exprBuilder.compilation.FindType(KnownTypeCode.String);
}

Loading…
Cancel
Save