Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid IL is emitted for a conditional access of off a constrained generic type parameter #23351

Closed
AlekseyTs opened this issue Nov 22, 2017 · 1 comment
Assignees
Labels
Area-Compilers Bug Language-VB Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented

Comments

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Nov 22, 2017

Compile and run the following program:

Module Module1
    Sub Main()
        Dim obj1 As New MyObject1 With {.MyDate = CDate("2017-11-13T14:25:00Z")}
        Dim obj2 As New MyObject2(Of MyObject1)(obj1)

        System.Console.WriteLine(obj1.MyDate.Ticks)
        System.Console.WriteLine(obj2.CurrentDate.Value.Ticks)
    End Sub
End Module

Public MustInherit Class MyBaseObject1
    Property MyDate As Date
End Class

Public Class MyObject1
    Inherits MyBaseObject1
End Class

Public Class MyObject2(Of MyObjectType As {MyBaseObject1, New})
    Public Sub New(obj As MyObjectType)
        m_CurrentObject1 = obj
    End Sub

    Private m_CurrentObject1 As MyObjectType = Nothing
    Public ReadOnly Property CurrentObject1 As MyObjectType
        Get
            Return m_CurrentObject1
        End Get
    End Property
    Public ReadOnly Property CurrentDate As Date?
        Get
            Return CurrentObject1?.MyDate
        End Get
    End Property
End Class

Observed:

636461511000000000
2886802138464272

Also, PEVerify fails

[IL]: Error: MyObject2`1[MyObjectType]::get_CurrentDate][offset 0x00000021] The 'this' argument to a constrained call must have ByRef type.

Expected:

636461511000000000
636461511000000000

PEVerify succeeds.

@AlekseyTs
Copy link
Contributor Author

The workaround is to explicitly cast the receiver to the type of the constraint:

DirectCast(CurrentObject1, MyBaseObject1)?.MyDate

AlekseyTs added a commit to AlekseyTs/roslyn that referenced this issue Nov 22, 2017
@AlekseyTs AlekseyTs added the Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented label Nov 22, 2017
AlekseyTs added a commit that referenced this issue Nov 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Language-VB Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Projects
None yet
Development

No branches or pull requests

1 participant