提交 b3611be6 编写于 作者: J Jason Malinowski

Fix bug where constructor cycle detection would throw IndexOutOfBoundsException

The problem here was when we do cycle detection, we tried to look
at the source of the candidate constructor that we are looking at
delegating to. If we are in a cross-language case, the target might
be from metadata via the skeleton reference and we don't have source.
Rather than fixing the code that assumes it's a source method, I'm
making a change that says if the constructor you're delegating to is
in the base type, then we can't create a cycle and can skip the check
entirely.
上级 e0487a73
......@@ -1866,6 +1866,44 @@ End Class",
End Class")
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)>
Public Async Function TestDelegateConstructorCrossLanguageCycleAvoidance() As Task
Await TestInRegularAndScriptAsync(
<Workspace>
<Project Language="C#" Name="CSharpProject" CommonReferences="true">
<Document>
public class BaseType
{
public BaseType(int x, int y) { }
}
</Document>
</Project>
<Project Language="Visual Basic" CommonReferences="true">
<ProjectReference>CSharpProject</ProjectReference>
<Document>
Public Class B
Inherits BaseType
Public Sub New(a As Integer)
[|Me.New(a, 1)|]
End Sub
End Class</Document>
</Project>
</Workspace>.ToString(),
"
Public Class B
Inherits BaseType
Public Sub New(a As Integer)
Me.New(a, 1)
End Sub
Public Sub New(x As Integer, y As Integer)
MyBase.New(x, y)
End Sub
End Class")
End Function
<WorkItem(49850, "https://github.com/dotnet/roslyn/issues/49850")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateConstructor)>
Public Async Function TestDelegateConstructorCrossLanguage() As Task
......
......@@ -57,6 +57,10 @@ protected bool WillCauseConstructorCycle(State state, SemanticDocument document,
if (currentConstructor.Equals(delegatedConstructor))
return true;
// Delegating to a constructor in the base type can't cause a cycle
if (!delegatedConstructor.ContainingType.Equals(currentConstructor.ContainingType))
return false;
// We need ensure that delegating constructor won't cause circular dependency.
// The chain of dependency can not exceed the number for constructors
var constructorsCount = delegatedConstructor.ContainingType.InstanceConstructors.Length;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册