{"action":"opened","issue":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/4856","labels_url":"https://api.github.com/repos/dotnet/roslyn/issues/4856/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/roslyn/issues/4856/comments","events_url":"https://api.github.com/repos/dotnet/roslyn/issues/4856/events","html_url":"https://github.com/dotnet/roslyn/issues/4856","id":103621642,"number":4856,"title":"VB closure not capturing me reference, causes runtime null reference","user":{"login":"liqdfire","id":328512,"avatar_url":"https://avatars.githubusercontent.com/u/328512?v=3","gravatar_id":"","url":"https://api.github.com/users/liqdfire","html_url":"https://github.com/liqdfire","followers_url":"https://api.github.com/users/liqdfire/followers","following_url":"https://api.github.com/users/liqdfire/following{/other_user}","gists_url":"https://api.github.com/users/liqdfire/gists{/gist_id}","starred_url":"https://api.github.com/users/liqdfire/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/liqdfire/subscriptions","organizations_url":"https://api.github.com/users/liqdfire/orgs","repos_url":"https://api.github.com/users/liqdfire/repos","events_url":"https://api.github.com/users/liqdfire/events{/privacy}","received_events_url":"https://api.github.com/users/liqdfire/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-08-28T00:26:48Z","updated_at":"2015-08-28T00:26:48Z","closed_at":null,"body":"### Known conditions required to create\r\n*  Must be in constructor\r\n*  Must reference class level variable\r\n*  Must be inside some type of control or flow statement\r\n    * If block, for each, switch case etc...\r\n*  Must capture a variable inside the control or flow block\r\n</br></br>\r\n\r\nI walked through the roslyn code, it appears to me that because of the control / flow block and the closure variable captures, the compiler is creating a lambda frame. When the [LambdaRewriter](../blob/master/src/Compilers/VisualBasic/Portable/Lowering/LambdaRewriter/LambdaRewriter.vb) goes to introduce the frame into the tree, it sees the method as being a constructor, and sets the bound me expression for deferred initialization. However, when the bound calls are visited it never initializes the deferred bound me reference because it sees the top level method as a constructor; therefore, it does not get added to the syntax tree. When in the method IntroduceFrame if I change the code flow to initialize immediately instead of deferred it compiles correctly. \r\n\r\n##### Small snippet that repos the bug\r\n``` vb\r\nImports System.Collections.Generic\r\nPublic Class Processor\r\n    Private _units As New List(Of Integer)\r\n\r\n    Public Sub New()\r\n        Dim source = New List(Of Integer) From {0}\r\n        If True Then\r\n            Dim namedList1 = New List(Of Integer) From {1, 2}\r\n            source.ForEach(Sub(listItem)\r\n                               listItem += namedList1.Count\r\n                               _units.Add(listItem)\r\n                           End Sub)\r\n        End If\r\n    End Sub\r\nEnd Class\r\n```\r\n</br>\r\n\r\n##### Screen shot showing memory value of \"ME\" prior to entering the closure\r\n![ME reference before closure entry](https://cloud.githubusercontent.com/assets/328512/9535600/1a9d24cc-4cf1-11e5-8352-60f0a4c69d5e.png)\r\n</br>\r\n\r\n##### Screen shot showing memory value of \"ME\" after entering the closure\r\n![ME reference after closure entry](https://cloud.githubusercontent.com/assets/328512/9535997/12b7d87a-4cf5-11e5-8e9f-cd084b6b1788.png)\r\n</br>\r\n\r\n##### Reflector decompilation showing Closure field $VB$Me never being set\r\n``` vb\r\nPublic Class Processor\r\n    ' Methods\r\n    Public Sub New()\r\n        Dim list As New List(Of Integer) From { 0 }\r\n        Dim e$__- As New _Closure$__1-0 With { _\r\n            .$VB$Local_namedList1 = 1 _\r\n        }\r\n        list.ForEach(New Action(Of Integer)(AddressOf e$__-._Lambda$__0))\r\n    End Sub\r\n\r\n\r\n    ' Fields\r\n    Private _units As List(Of Integer) = New List(Of Integer)\r\n\r\n    ' Nested Types\r\n    <CompilerGenerated> _\r\n    Friend NotInheritable Class _Closure$__1-0\r\n        ' Methods\r\n        Friend Sub _Lambda$__0(ByVal listItem As Integer)\r\n            listItem = (listItem + Me.$VB$Local_namedList1)\r\n            Me.$VB$Me._units.Add(listItem)\r\n        End Sub\r\n\r\n\r\n        ' Fields\r\n        Public $VB$Local_namedList1 As Integer\r\n        Public $VB$Me As Processor\r\n    End Class\r\nEnd Class\r\n```\r\n</br>\r\n\r\n##### Bound me expression being built in method IntroduceFrame, set as deferred initialization\r\n![Bound Me Expression](https://cloud.githubusercontent.com/assets/328512/9536418/6f027550-4cf9-11e5-99b7-bde6269aaecf.png)\r\n"}}