提交 e668f29d 编写于 作者: A Andy Gocke 提交者: GitHub

Add tests for default parameters in local functions (#16784)

Fixes #16352
上级 f3717ae2
......@@ -30,6 +30,80 @@ public static IMethodSymbol FindLocalFunction(this CommonTestBase.CompilationVer
[CompilerTrait(CompilerFeature.LocalFunctions)]
public class CodeGenLocalFunctionTests : CSharpTestBase
{
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/16783")]
[WorkItem(16783, "https://github.com/dotnet/roslyn/issues/16783")]
public void GenericDefaultParams()
{
CompileAndVerify(@"
using System;
class C
{
public void M()
{
void Local<T>(T t = default(T))
{
Console.WriteLine(t);
}
Local<int>();
}
}
class C2
{
public static void Main()
{
new C().M();
}
}", expectedOutput: "0");
}
[Fact]
public void GenericCaptureDefaultParams()
{
CompileAndVerify(@"
using System;
class C<T>
{
public void M()
{
void Local(T t = default(T))
{
Console.WriteLine(t);
}
Local();
}
}
class C2
{
public static void Main()
{
new C<int>().M();
}
}", expectedOutput: "0");
}
[Fact]
public void NameofRecursiveDefaultParameter()
{
var comp = CreateCompilationWithMscorlib(@"
using System;
class C
{
public static void Main()
{
void Local(string s = nameof(Local))
{
Console.WriteLine(s);
}
Local();
}
}", options: TestOptions.ReleaseExe);
comp.VerifyDiagnostics();
comp.DeclarationDiagnostics.Verify();
CompileAndVerify(comp, expectedOutput: "Local");
}
[Fact]
[WorkItem(16399, "https://github.com/dotnet/roslyn/issues/16399")]
public void RecursiveGenericLocalFunctionIterator()
......
......@@ -30,6 +30,72 @@ internal void VerifyDiagnostics(string source, CSharpCompilationOptions options,
[CompilerTrait(CompilerFeature.LocalFunctions)]
public class LocalFunctionTests : LocalFunctionsTestBase
{
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/16451")]
[WorkItem(16451, "https://github.com/dotnet/roslyn/issues/16451")]
public void RecursiveDefaultParameter()
{
var comp = CreateCompilationWithMscorlib(@"
class C
{
public static void Main()
{
int Local(int j = Local()) => 0;
Local();
}
}");
comp.VerifyDiagnostics(
// (6,27): error CS1736: Default parameter value for 'j' must be a compile-time constant
// int Local(int j = Local()) => 0;
Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "Local()").WithArguments("j").WithLocation(6, 27));
comp.DeclarationDiagnostics.Verify();
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/16451")]
[WorkItem(16451, "https://github.com/dotnet/roslyn/issues/16451")]
public void RecursiveDefaultParameter2()
{
var comp = CreateCompilationWithMscorlib(@"
using System;
class C
{
void M()
{
int Local(Action a = Local) => 0;
Local();
}
}");
comp.VerifyDiagnostics(
// (7,30): error CS1736: Default parameter value for 'a' must be a compile-time constant
// int Local(Action a = Local) => 0;
Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "Local").WithArguments("a").WithLocation(7, 30));
comp.DeclarationDiagnostics.Verify();
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/16451")]
[WorkItem(16451, "https://github.com/dotnet/roslyn/issues/16451")]
public void MutuallyRecursiveDefaultParameters()
{
var comp = CreateCompilationWithMscorlib(@"
class C
{
void M()
{
int Local1(int p = Local2()) => 0;
int Local2(int p = Local1()) => 0;
Local1();
Local2();
}
}");
comp.VerifyDiagnostics(
// (6,28): error CS1736: Default parameter value for 'p' must be a compile-time constant
// int Local1(int p = Local2()) => 0;
Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "Local2()").WithArguments("p").WithLocation(6, 28),
// (7,28): error CS1736: Default parameter value for 'p' must be a compile-time constant
// int Local2(int p = Local1()) => 0;
Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "Local1()").WithArguments("p").WithLocation(7, 28));
comp.DeclarationDiagnostics.Verify();
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/16652")]
public void FetchLocalFunctionSymbolThroughLocal()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册