未验证 提交 ef0b1d10 编写于 作者: A AlekseyTs 提交者: GitHub

Ensure correct binder is used to bind constructor initializers for synthesized...

Ensure correct binder is used to bind constructor initializers for synthesized parameter-less and copy constructors in records. (#44910)

Fixes #44898.
上级 9b82fcd1
......@@ -132,7 +132,7 @@ internal Binder GetBinder(SyntaxNode node, int position, CSharpSyntaxNode member
internal InMethodBinder GetRecordConstructorInMethodBinder(SynthesizedRecordConstructor constructor)
{
TypeDeclarationSyntax typeDecl = constructor.GetSyntax();
RecordDeclarationSyntax typeDecl = constructor.GetSyntax();
var extraInfo = NodeUsage.ConstructorBodyOrInitializer;
var key = BinderFactoryVisitor.CreateBinderCacheKey(typeDecl, extraInfo);
......@@ -149,7 +149,7 @@ internal InMethodBinder GetRecordConstructorInMethodBinder(SynthesizedRecordCons
return (InMethodBinder)resultBinder;
}
internal Binder GetInRecordBodyBinder(TypeDeclarationSyntax typeDecl)
internal Binder GetInRecordBodyBinder(RecordDeclarationSyntax typeDecl)
{
BinderFactoryVisitor visitor = _binderFactoryVisitorPool.Allocate();
visitor.Initialize(position: typeDecl.SpanStart, memberDeclarationOpt: null, memberOpt: null);
......
......@@ -1867,8 +1867,17 @@ private static void ReportCtorInitializerCycles(MethodSymbol method, BoundExpres
// The constructor is implicit. We need to get the binder for the body
// of the enclosing class.
CSharpSyntaxNode containerNode = constructor.GetNonNullSyntaxNode();
SyntaxToken bodyToken = GetImplicitConstructorBodyToken(containerNode);
outerBinder = compilation.GetBinderFactory(containerNode.SyntaxTree).GetBinder(containerNode, bodyToken.Position);
BinderFactory binderFactory = compilation.GetBinderFactory(containerNode.SyntaxTree);
if (containerNode is RecordDeclarationSyntax recordDecl)
{
outerBinder = binderFactory.GetInRecordBodyBinder(recordDecl);
}
else
{
SyntaxToken bodyToken = GetImplicitConstructorBodyToken(containerNode);
outerBinder = binderFactory.GetBinder(containerNode, bodyToken.Position);
}
}
else
{
......
......@@ -1185,6 +1185,134 @@ public static void Main()
);
}
[Fact]
public void AccessibilityOfBaseCtor_01()
{
var src = @"
using System;
record Base
{
protected Base(int X, int Y)
{
Console.WriteLine(X);
Console.WriteLine(Y);
}
public Base() {}
public static void Main()
{
var c = new C(1, 2);
}
}
record C(int X, int Y) : Base(X, Y);
";
CompileAndVerify(src, expectedOutput: @"
1
2
");
}
[Fact]
public void AccessibilityOfBaseCtor_02()
{
var src = @"
using System;
record Base
{
protected Base(int X, int Y)
{
Console.WriteLine(X);
Console.WriteLine(Y);
}
public Base() {}
public static void Main()
{
var c = new C(1, 2);
}
}
record C(int X, int Y) : Base(X, Y) {}
";
CompileAndVerify(src, expectedOutput: @"
1
2
");
}
[Fact]
[WorkItem(44898, "https://github.com/dotnet/roslyn/issues/44898")]
public void AccessibilityOfBaseCtor_03()
{
var src = @"
abstract record A
{
protected A() {}
protected A(A x) {}
};
record B(object P) : A;
";
var comp = CreateCompilation(src);
comp.VerifyEmitDiagnostics();
}
[Fact]
[WorkItem(44898, "https://github.com/dotnet/roslyn/issues/44898")]
public void AccessibilityOfBaseCtor_04()
{
var src = @"
abstract record A
{
protected A() {}
protected A(A x) {}
};
record B(object P) : A {}
";
var comp = CreateCompilation(src);
comp.VerifyDiagnostics();
}
[Fact]
[WorkItem(44898, "https://github.com/dotnet/roslyn/issues/44898")]
public void AccessibilityOfBaseCtor_05()
{
var src = @"
abstract record A
{
protected A() {}
protected A(A x) {}
};
record B : A;
";
var comp = CreateCompilation(src);
comp.VerifyEmitDiagnostics();
}
[Fact]
[WorkItem(44898, "https://github.com/dotnet/roslyn/issues/44898")]
public void AccessibilityOfBaseCtor_06()
{
var src = @"
abstract record A
{
protected A() {}
protected A(A x) {}
};
record B : A {}
";
var comp = CreateCompilation(src);
comp.VerifyDiagnostics();
}
[Fact]
public void WithExprNestedErrors()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册