Skip to content

Commit

Permalink
Determinism fixes for AnonymousTypes in VB (#49467)
Browse files Browse the repository at this point in the history
Related to #48417.
  • Loading branch information
AlekseyTs authored Nov 19, 2020
1 parent bd7a354 commit 8fe41a0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
returnType)

For i = 0 To parameterDescriptors.Length - 2
parameters.Add(New AnonymousDelegateParameterSymbol(delegateInvoke,
Me.TypeParameters(i),
i,
parameterDescriptors(i).IsByRef,
parameterDescriptors(i).Name,
i))
parameters.Add(New AnonymousTypeOrDelegateParameterSymbol(delegateInvoke,
Me.TypeParameters(i),
i,
parameterDescriptors(i).IsByRef,
parameterDescriptors(i).Name,
i))
Next

delegateInvoke.SetParameters(parameters.ToImmutable())
Expand All @@ -71,8 +71,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols

delegateCtor.SetParameters(
ImmutableArray.Create(Of ParameterSymbol)(
New AnonymousDelegateParameterSymbol(delegateCtor, manager.System_Object, 0, False, StringConstants.DelegateConstructorInstanceParameterName),
New AnonymousDelegateParameterSymbol(delegateCtor, manager.System_IntPtr, 1, False, StringConstants.DelegateConstructorMethodParameterName)
New AnonymousTypeOrDelegateParameterSymbol(delegateCtor, manager.System_Object, 0, False, StringConstants.DelegateConstructorInstanceParameterName),
New AnonymousTypeOrDelegateParameterSymbol(delegateCtor, manager.System_IntPtr, 1, False, StringConstants.DelegateConstructorMethodParameterName)
))

Dim delegateBeginInvoke As SynthesizedDelegateMethodSymbol
Expand All @@ -93,12 +93,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols

For i = 0 To delegateInvoke.ParameterCount - 1
Dim parameter As ParameterSymbol = delegateInvoke.Parameters(i)
parameters.Add(New AnonymousDelegateParameterSymbol(delegateBeginInvoke, parameter.Type, i, parameter.IsByRef(), parameter.Name, i))
parameters.Add(New AnonymousTypeOrDelegateParameterSymbol(delegateBeginInvoke, parameter.Type, i, parameter.IsByRef(), parameter.Name, i))
Next

parameters.Add(New AnonymousDelegateParameterSymbol(delegateBeginInvoke, manager.System_AsyncCallback, i, False, StringConstants.DelegateMethodCallbackParameterName))
parameters.Add(New AnonymousTypeOrDelegateParameterSymbol(delegateBeginInvoke, manager.System_AsyncCallback, i, False, StringConstants.DelegateMethodCallbackParameterName))
i += 1
parameters.Add(New AnonymousDelegateParameterSymbol(delegateBeginInvoke, manager.System_Object, i, False, StringConstants.DelegateMethodInstanceParameterName))
parameters.Add(New AnonymousTypeOrDelegateParameterSymbol(delegateBeginInvoke, manager.System_Object, i, False, StringConstants.DelegateMethodInstanceParameterName))
delegateBeginInvoke.SetParameters(parameters.ToImmutable())
parameters.Clear()

Expand All @@ -112,12 +112,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Dim parameter As ParameterSymbol = delegateInvoke.Parameters(i)

If parameter.IsByRef Then
parameters.Add(New AnonymousDelegateParameterSymbol(delegateEndInvoke, parameter.Type, ordinal, parameter.IsByRef(), parameter.Name, i))
parameters.Add(New AnonymousTypeOrDelegateParameterSymbol(delegateEndInvoke, parameter.Type, ordinal, parameter.IsByRef(), parameter.Name, i))
ordinal += 1
End If
Next

parameters.Add(New AnonymousDelegateParameterSymbol(delegateEndInvoke, manager.System_IAsyncResult, ordinal, False, StringConstants.DelegateMethodResultParameterName))
parameters.Add(New AnonymousTypeOrDelegateParameterSymbol(delegateEndInvoke, manager.System_IAsyncResult, ordinal, False, StringConstants.DelegateMethodResultParameterName))
delegateEndInvoke.SetParameters(parameters.ToImmutable())

_members = ImmutableArray.Create(delegateCtor, delegateBeginInvoke, delegateEndInvoke, delegateInvoke)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols

Private ReadOnly _typeParameters As ImmutableArray(Of TypeParameterSymbol)
Private _adjustedPropertyNames As LocationAndNames
#If DEBUG Then
Private _locationAndNamesAreLocked As Boolean
#End If

''' <summary>
''' The key of the anonymous type descriptor used for this type template
Expand Down Expand Up @@ -315,6 +318,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols

Public ReadOnly Property SmallestLocation As Location
Get
#If DEBUG Then
_locationAndNamesAreLocked = True
#End If
Return Me._adjustedPropertyNames.Location
End Get
End Property
Expand All @@ -335,12 +341,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
' to set it ('location' in type descriptor is bigger that the one in m_adjustedPropertyNames)
Dim currentAdjustedNames As LocationAndNames = Me._adjustedPropertyNames
If currentAdjustedNames IsNot Nothing AndAlso
Me.Manager.Compilation.CompareSourceLocations(currentAdjustedNames.Location, newLocation) < 0 Then
Me.Manager.Compilation.CompareSourceLocations(currentAdjustedNames.Location, newLocation) <= 0 Then

' The template's adjusted property names do not need to be changed
Exit Sub
End If

#If DEBUG Then
Debug.Assert(Not _locationAndNamesAreLocked)
#End If

Dim newAdjustedNames As New LocationAndNames(typeDescr)

If Interlocked.CompareExchange(Me._adjustedPropertyNames, newAdjustedNames, currentAdjustedNames) Is currentAdjustedNames Then
Expand All @@ -351,6 +361,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
End Sub

Friend Function GetAdjustedName(index As Integer) As String
#If DEBUG Then
_locationAndNamesAreLocked = True
#End If
Dim names = Me._adjustedPropertyNames
Debug.Assert(names IsNot Nothing)
Debug.Assert(names.Names.Length > index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Partial Friend NotInheritable Class AnonymousTypeManager

Private NotInheritable Class AnonymousDelegateParameterSymbol
Private NotInheritable Class AnonymousTypeOrDelegateParameterSymbol
Inherits SynthesizedParameterSymbol

Public ReadOnly CorrespondingInvokeParameter As Integer
Public ReadOnly CorrespondingInvokeParameterOrProperty As Integer

Public Sub New(
container As SynthesizedDelegateMethodSymbol,
container As MethodSymbol,
type As TypeSymbol, ordinal As Integer,
isByRef As Boolean,
name As String,
Optional correspondingInvokeParameter As Integer = -1
Optional correspondingInvokeParameterOrProperty As Integer = -1
)
MyBase.New(container, type, ordinal, isByRef, name)
Me.CorrespondingInvokeParameter = correspondingInvokeParameter
Me.CorrespondingInvokeParameterOrProperty = correspondingInvokeParameterOrProperty
End Sub

Public Overrides ReadOnly Property MetadataName As String
Get
If CorrespondingInvokeParameter <> -1 Then
Return DirectCast(_container.ContainingSymbol, AnonymousDelegateTemplateSymbol).GetAdjustedName(CorrespondingInvokeParameter)
If CorrespondingInvokeParameterOrProperty <> -1 Then
Return DirectCast(_container.ContainingSymbol, AnonymousTypeOrDelegateTemplateSymbol).GetAdjustedName(CorrespondingInvokeParameterOrProperty)
End If

Return MyBase.MetadataName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Dim paramsArr = New ParameterSymbol(fieldsCount - 1) {}
For index = 0 To fieldsCount - 1
Dim [property] As PropertySymbol = container.Properties(index)
paramsArr(index) = New SynthesizedParameterSimpleSymbol(Me, [property].Type, index, [property].Name)
paramsArr(index) = New AnonymousTypeOrDelegateParameterSymbol(Me, [property].Type, index, isByRef:=False, [property].Name, correspondingInvokeParameterOrProperty:=index)
Next
Me._parameters = paramsArr.AsImmutableOrNull()
End Sub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,11 @@ End Module
Assert.Equal("$" & propName, field.Name)
Assert.Equal("$" & propName, field.MetadataName)
Assert.Equal(Accessibility.Private, field.DeclaredAccessibility)

Dim parameter = type.Constructors.Single().Parameters(0)
Assert.Equal(propType, parameter.Type)
Assert.Equal(propName, parameter.Name)
Assert.Equal(propName, parameter.MetadataName)
End Sub

Private Shared Sub CheckMethod(m As ModuleSymbol, method As MethodSymbol,
Expand All @@ -702,6 +707,7 @@ End Module

Assert.NotNull(method)
Assert.Equal(name, method.Name)
Assert.Equal(name, method.MetadataName)
Assert.Equal(paramCount, method.ParameterCount)

If isSub Then
Expand Down

0 comments on commit 8fe41a0

Please sign in to comment.