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

Fix a bug in conversion classification that improperly hides a switch...

Fix a bug in conversion classification that improperly hides a switch expression conversion when there is a built-in explicit conversion (#42339)

This bug results in the compiler crashing or producing bad code in some scenarios.
Fixes #42331
上级 86193f42
......@@ -100,7 +100,10 @@ public Conversion ClassifyImplicitConversionFromExpression(BoundExpression sourc
Conversion fastConversion = FastClassifyConversion(sourceType, destination);
if (fastConversion.Exists)
{
return fastConversion.IsImplicit ? fastConversion : Conversion.NoConversion;
if (fastConversion.IsImplicit)
{
return fastConversion;
}
}
else
{
......
......@@ -688,7 +688,7 @@ class Source2
}
[Fact]
public void TargetTypedSwitch_Overload()
public void TargetTypedSwitch_Overload_01()
{
var source = @"
using System;
......@@ -730,6 +730,53 @@ class Source2
var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput);
}
[Fact]
public void TargetTypedSwitch_Overload_02()
{
var source = @"
using System;
class Program
{
static void Main(string[] args)
{
bool b = true;
M(b switch { false => 1, true => 2 });
}
static void M(Int16 s) => Console.Write(nameof(Int16));
static void M(Int64 l) => Console.Write(nameof(Int64));
}
";
var expectedOutput = "Int16";
var compilation = CreateCompilation(source, options: TestOptions.DebugExe.WithNullableContextOptions(NullableContextOptions.Disable));
compilation.VerifyDiagnostics(
);
var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput);
}
[Fact]
public void TargetTypedSwitch_Overload_03()
{
var source = @"
using System;
class Program
{
static void Main(string[] args)
{
bool b = true;
M(b switch { false => 1, true => 2 });
}
static void M(Int16 s) => Console.Write(nameof(Int16));
}
";
var expectedOutput = "Int16";
var compilation = CreateCompilation(source, options: TestOptions.DebugExe.WithNullableContextOptions(NullableContextOptions.Disable));
compilation.VerifyDiagnostics(
);
var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput);
}
[Fact]
public void TargetTypedSwitch_Lambda_01()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册