Skip to content

Commit

Permalink
Allow generic type parameters to be the same type in method generation (
Browse files Browse the repository at this point in the history
#11151)

Previouly we assumed that the type parameters in a method would not be of
the same TypeSymbol when generating a method and put them in a dictionary.
This causes exceptions when attempting to add to the dictionary and the
TypeSymbol is already added as a key. Now we use a list of instead of a
dictionary.

Fixes #10004
  • Loading branch information
jmarolf committed May 10, 2016
1 parent 39840b4 commit 88ffc16
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2879,6 +2879,46 @@ private static void OnChanged(object sender, EventArgs e)
}");
}

[WorkItem(10004, "https://github.com/dotnet/roslyn/issues/10004")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)]
public async Task TestGenerateMethodWithMultipleOfSameGenericType()
{
await TestAsync(
@"using System;
public class C
{
}
public static class Ex
{
public static T M1<T>(this T t) where T : C
{
return [|t.M<T, T>()|];
}
}
",
@"using System;
public class C
{
internal T2 M<T1, T2>()
where T1 : C
where T2 : C
{
}
}
public static class Ex
{
public static T M1<T>(this T t) where T : C
{
return t.M<T, T>();
}
}
");
}

public class GenerateConversionTest : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
{
internal override Tuple<DiagnosticAnalyzer, CodeFixProvider> CreateDiagnosticProviderAndFixer(Workspace workspace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,38 @@ NewLines("Imports System \n Imports System.Collections.Generic \n Module Program
NewLines("Imports System \n Imports System.Collections.Generic \n Module Program \n Sub M() \n Dim x = New Dictionary ( Of Integer , Boolean ) From { { 1, T() } } \n End Sub \n Private Function T() As Boolean \n Throw New NotImplementedException() \n End Function \n End Module"))
End Function

<WorkItem(10004, "https://github.com/dotnet/roslyn/issues/10004")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateMethod)>
Public Async Function TestGenerateMethodWithMultipleOfSameGenericType() As Task
Await TestAsync(
<text>
Namespace TestClasses
Public Class C
End Class

Module Ex
Public Function M(Of T As C)(a As T) As T
Return [|a.Test(Of T, T)()|]
End Function
End Module
End Namespace
</text>.Value.Replace(vbLf, vbCrLf),
<text>
Namespace TestClasses
Public Class C
Friend Function Test(Of T1 As C, T2 As C)() As T2
End Function
End Class

Module Ex
Public Function M(Of T As C)(a As T) As T
Return a.Test(Of T, T)()
End Function
End Module
End Namespace
</text>.Value.Replace(vbLf, vbCrLf))
End Function

Public Class GenerateConversionTests
Inherits AbstractVisualBasicDiagnosticProviderBasedUserDiagnosticTest

Expand Down Expand Up @@ -2484,7 +2516,6 @@ Class Digit
End Class
</text>.Value.Replace(vbLf, vbCrLf), compareTokens:=False)
End Function

End Class
End Class
End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static IMethodSymbol RenameTypeParameters(this IMethodSymbol method, ILis
var mapping = new Dictionary<ITypeSymbol, ITypeSymbol>();
for (int i = 0; i < method.TypeParameters.Length; i++)
{
mapping.Add(method.TypeParameters[i], updatedTypeParameters[i]);
mapping[method.TypeParameters[i]] = updatedTypeParameters[i];
}

return CodeGenerationSymbolFactory.CreateMethodSymbol(
Expand Down Expand Up @@ -131,7 +131,7 @@ public static IMethodSymbol RenameParameters(this IMethodSymbol method, IList<st
typeParameter.Ordinal);

newTypeParameters.Add(newTypeParameter);
mapping.Add(typeParameter, newTypeParameter);
mapping[typeParameter] = newTypeParameter;
}

// Now we update the constraints.
Expand Down

0 comments on commit 88ffc16

Please sign in to comment.