提交 07e1b309 编写于 作者: A AlekseyTs

Allow expression variables in lambdas within constructor and field initializers.

Fixes #16935.
上级 2c505555
......@@ -2200,7 +2200,7 @@ private BoundExpression BindOutVariableArgument(DeclarationExpressionSyntax decl
SourceLocalSymbol localSymbol = this.LookupLocal(designation.Identifier);
if ((object)localSymbol != null)
{
if (InConstructorInitializer || InFieldInitializer)
if ((InConstructorInitializer || InFieldInitializer) && ContainingMemberOrLambda.ContainingSymbol.Kind == SymbolKind.NamedType)
{
Error(diagnostics, ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, declarationExpression);
}
......
......@@ -292,7 +292,7 @@ internal BoundExpression ConvertPatternExpression(TypeSymbol inputType, CSharpSy
if (localSymbol != (object)null)
{
if (InConstructorInitializer || InFieldInitializer)
if ((InConstructorInitializer || InFieldInitializer) && ContainingMemberOrLambda.ContainingSymbol.Kind == SymbolKind.NamedType)
{
Error(diagnostics, ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, node);
}
......
......@@ -5273,6 +5273,39 @@ static bool TakeOutParam(int y, out int x)
#endif
}
[Fact]
[WorkItem(16935, "https://github.com/dotnet/roslyn/issues/16935")]
public void FieldInitializers_04()
{
var source =
@"
public class X
{
public static void Main()
{
System.Console.WriteLine(Test1());
}
static System.Func<bool> Test1 = () => TakeOutParam(1, out int x1) && Dummy(x1);
static bool Dummy(int x)
{
System.Console.WriteLine(x);
return true;
}
static bool TakeOutParam(int y, out int x)
{
x = y;
return true;
}
}
";
var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular);
CompileAndVerify(compilation, expectedOutput: @"1
True");
}
[Fact]
public void Scope_Fixed_01()
{
......@@ -10688,6 +10721,39 @@ static bool TakeOutParam(int y, out int x)
#endif
}
[Fact]
[WorkItem(16935, "https://github.com/dotnet/roslyn/issues/16935")]
public void PropertyInitializers_02()
{
var source =
@"
public class X
{
public static void Main()
{
System.Console.WriteLine(Test1());
}
static System.Func<bool> Test1 {get;} = () => TakeOutParam(1, out int x1) && Dummy(x1);
static bool Dummy(int x)
{
System.Console.WriteLine(x);
return true;
}
static bool TakeOutParam(int y, out int x)
{
x = y;
return true;
}
}
";
var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular);
CompileAndVerify(compilation, expectedOutput: @"1
True");
}
[Fact]
public void Scope_Query_01()
{
......@@ -17720,6 +17786,56 @@ class Test3 : Test2
VerifyModelForOutVar(model, x1Decl);
}
[Fact]
[WorkItem(16935, "https://github.com/dotnet/roslyn/issues/16935")]
public void ConstructorInitializers_05()
{
var text = @"
public class Cls
{
public static void Main()
{
Do(new Test2());
}
static void Do(object x){}
public static object Test1(out int x)
{
x = 123;
return null;
}
class Test2
{
Test2(System.Func<object> x)
{
System.Console.WriteLine(x());
}
public Test2()
: this(() =>
{
Test1(out var x1);
return x1;
})
{}
}
}";
var compilation = CreateCompilationWithMscorlib(text,
options: TestOptions.ReleaseExe,
parseOptions: TestOptions.Regular);
CompileAndVerify(compilation, expectedOutput: @"123").VerifyDiagnostics();
var tree = compilation.SyntaxTrees.Single();
var model = compilation.GetSemanticModel(tree);
var x1Decl = GetOutVarDeclaration(tree, "x1");
var x1Ref = GetReference(tree, "x1");
VerifyModelForOutVar(model, x1Decl, x1Ref);
}
[Fact]
public void SimpleVar_14()
{
......@@ -22658,7 +22774,7 @@ static bool TakeOutParam(bool y, out bool x)
}
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/16935")]
[Fact]
[WorkItem(16935, "https://github.com/dotnet/roslyn/issues/16935")]
public void GlobalCode_Lambda_02()
{
......@@ -1088,6 +1088,33 @@ static bool Dummy(System.Func<int> x)
#endif
}
[Fact]
[WorkItem(16935, "https://github.com/dotnet/roslyn/issues/16935")]
public void FieldInitializers_04()
{
var source =
@"
public class X
{
public static void Main()
{
System.Console.WriteLine(Test1());
}
static System.Func<bool> Test1 = () => 1 is int x1 && Dummy(x1);
static bool Dummy(int x)
{
System.Console.WriteLine(x);
return true;
}
}
";
var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe);
CompileAndVerify(compilation, expectedOutput: @"1
True");
}
[Fact]
public void PropertyInitializers_01()
{
......@@ -1121,6 +1148,34 @@ static bool Dummy(int x)
);
#endif
}
[Fact]
[WorkItem(16935, "https://github.com/dotnet/roslyn/issues/16935")]
public void PropertyInitializers_02()
{
var source =
@"
public class X
{
public static void Main()
{
System.Console.WriteLine(Test1());
}
static System.Func<bool> Test1 {get;} = () => 1 is int x1 && Dummy(x1);
static bool Dummy(int x)
{
System.Console.WriteLine(x);
return true;
}
}
";
var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe);
CompileAndVerify(compilation, expectedOutput: @"1
True");
}
[Fact]
public void ConstructorInitializers_01()
{
......@@ -1179,6 +1234,54 @@ public C(object b)
#endif
}
[Fact]
[WorkItem(16935, "https://github.com/dotnet/roslyn/issues/16935")]
public void ConstructorInitializers_02()
{
var source =
@"
public class X
{
public static void Main()
{
var x = new D();
}
}
class D : C
{
public D(System.Func<bool> o) : base(() => 2 is int x1 && Dummy(x1))
{
System.Console.WriteLine(o());
}
public D() : this(() => 1 is int x1 && Dummy(x1))
{
}
static bool Dummy(int x)
{
System.Console.WriteLine(x);
return true;
}
}
class C
{
public C(System.Func<bool> b)
{
System.Console.WriteLine(b());
}
}
";
var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe);
CompileAndVerify(compilation, expectedOutput:
@"2
True
1
True");
}
[Fact]
public void Switch_01()
{
......
......@@ -7196,7 +7196,7 @@ public void GlobalCode_Lambda_01()
}
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/16935")]
[Fact]
[WorkItem(16935, "https://github.com/dotnet/roslyn/issues/16935")]
public void GlobalCode_Lambda_02()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册