提交 99239f9f 编写于 作者: C ChuckStoner

Update assert in TypeUnification.CanUnify for substitutions from type...

Update assert in TypeUnification.CanUnify for substitutions from type parameter to type parameter (changeset 1354882)
上级 7509b3c3
......@@ -3,8 +3,6 @@
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.CSharp
{
......@@ -18,11 +16,29 @@ public static bool CanUnify(TypeSymbol t1, TypeSymbol t2)
{
MutableTypeMap substitution = null;
bool result = CanUnifyHelper(t1, t2, ref substitution);
Debug.Assert(!result || (substitution == null && t1 == t2) ||
substitution.SubstituteType(t1) == substitution.SubstituteType(t2));
#if DEBUG
Debug.Assert(!result ||
SubstituteAllTypeParameters(substitution, t1) == SubstituteAllTypeParameters(substitution, t2));
#endif
return result;
}
#if DEBUG
private static TypeSymbol SubstituteAllTypeParameters(AbstractTypeMap substitution, TypeSymbol type)
{
if (substitution != null)
{
TypeSymbol previous;
do
{
previous = type;
type = substitution.SubstituteType(type);
} while (type != previous);
}
return type;
}
#endif
/// <summary>
/// Determine whether there is any substitution of type parameters that will
/// make two types identical.
......
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.CSharp.UnitTests
......@@ -397,6 +396,24 @@ public class M<U>
AssertCanUnify(type6, type6);
}
[WorkItem(1042692)]
[Fact]
public void SubstituteWithOtherTypeParameter()
{
var text =
@"interface IA<T, U>
{
}
interface IB<T, U> : IA<U, object>, IA<T, U>
{
}";
var comp = CreateCompilationWithMscorlib(text);
var type = comp.GetMember<NamedTypeSymbol>("IB");
AssertCanUnify(type.Interfaces[0], type.Interfaces[1]);
DiagnosticsUtils.VerifyErrorCodes(comp.GetDiagnostics(),
new ErrorDescription { Code = (int)ErrorCode.ERR_UnifyingInterfaceInstantiations, Line = 4, Column = 11 });
}
private static void AssertCanUnify(TypeSymbol t1, TypeSymbol t2)
{
Assert.True(TypeUnification.CanUnify(t1, t2), string.Format("{0} vs {1}", t1, t2));
......
' Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Diagnostics
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic
......@@ -19,11 +17,26 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Dim substitution As TypeSubstitution = Nothing
Dim result As Boolean = CanUnifyHelper(containingGenericType, t1, t2, substitution)
Debug.Assert(Not result OrElse (substitution Is Nothing AndAlso t1 = t2) OrElse
t1.InternalSubstituteTypeParameters(substitution) = t2.InternalSubstituteTypeParameters(substitution))
#If DEBUG Then
Debug.Assert(Not result OrElse
SubstituteAllTypeParameters(substitution, t1) = SubstituteAllTypeParameters(substitution, t2))
#End If
Return result
End Function
#If DEBUG Then
Private Shared Function SubstituteAllTypeParameters(substitution As TypeSubstitution, type As TypeSymbol) As TypeSymbol
If substitution IsNot Nothing Then
Dim previous As TypeSymbol
Do
previous = type
type = type.InternalSubstituteTypeParameters(substitution)
Loop While previous <> type
End If
Return type
End Function
#End If
''' <summary>
''' Determine whether there is any substitution of type parameters that will
''' make two types identical.
......
' Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Collections.Immutable
Imports System.IO
Imports System.Threading
Imports System.Xml.Linq
Imports Microsoft.CodeAnalysis.Test.Utilities
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Imports Microsoft.CodeAnalysis.VisualBasic.UnitTests.Symbols
Imports Roslyn.Test.Utilities
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols.Metadata.PE
......@@ -15897,6 +15893,27 @@ BC32120: Cannot inherit interface 'interfaceA(Of t2)' because it could be identi
CompilationUtils.AssertTheseDeclarationDiagnostics(compilation1, expectedErrors1)
End Sub
<WorkItem(1042692)>
<Fact()>
Public Sub BC32120ERR_InterfaceUnifiesWithInterface2_SubstituteWithOtherTypeParameter()
Dim compilation1 = CompilationUtils.CreateCompilationWithMscorlib(
<compilation>
<file name="a.vb"><![CDATA[
Interface IA(Of T, U)
End Interface
Interface IB(Of T, U)
Inherits IA(Of U, Object), IA(Of T, U)
End Interface
]]></file>
</compilation>)
compilation1.AssertTheseDeclarationDiagnostics(
<errors><![CDATA[
BC32120: Cannot inherit interface 'IA(Of T, U)' because it could be identical to interface 'IA(Of U, Object)' for some type arguments.
Inherits IA(Of U, Object), IA(Of T, U)
~~~~~~~~~~~
]]></errors>)
End Sub
<Fact(), WorkItem(543726, "DevDiv")>
Public Sub BC32121ERR_BaseUnifiesWithInterfaces3()
Dim compilation1 = CompilationUtils.CreateCompilationWithMscorlib(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册