RLovelett/langserver-swift

unexpectedly found nil while unwrapping an Optional value

aaroncrespo opened this issue · 3 comments

Running the language server:

import Foundation
struct Foo {
  var i = 0
}
struct Bar {
  var k: Foo

  func test() {
      k.
       _^_
  }
}
fatal error: unexpectedly found nil while unwrapping an Optional value
Current stack trace:
0    libswiftCore.dylib                 0x000000010205cce0 swift_reportError + 132
1    libswiftCore.dylib                 0x000000010207a090 _swift_stdlib_reportFatalError + 61
2    libswiftCore.dylib                 0x0000000101e700c0 specialized specialized StaticString.withUTF8Buffer<A> ((UnsafeBufferPointer<UInt8>) -> A) -> A + 355
3    libswiftCore.dylib                 0x0000000101fec230 partial apply for (_fatalErrorMessage(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> Never).(closure #2) + 109
4    libswiftCore.dylib                 0x0000000101e700c0 specialized specialized StaticString.withUTF8Buffer<A> ((UnsafeBufferPointer<UInt8>) -> A) -> A + 355
5    libswiftCore.dylib                 0x0000000101fa43f0 specialized _fatalErrorMessage(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> Never + 96
6    LanguageServer                     0x0000000101873e70 handle(Request) -> Response + 2030
7    LanguageServer                     0x0000000101871950 (closure #1) + 2132
8    LanguageServer                     0x00000001018733f0 thunk + 73
9    Foundation                         0x00007fff82232bfe -[__NSObserver _doit:] + 304
10   CoreFoundation                     0x00007fff80806a60 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
11   CoreFoundation                     0x00007fff808067c0 _CFXRegistrationPost + 427
12   CoreFoundation                     0x00007fff808066a0 ___CFXNotificationPost_block_invoke + 50
13   CoreFoundation                     0x00007fff807c3640 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1827
14   CoreFoundation                     0x00007fff807c2b40 _CFXNotificationPost + 604
15   Foundation                         0x00007fff821e99f5 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
16   Foundation                         0x00007fff822a6a7c _performFileHandleSource + 1144
17   CoreFoundation                     0x00007fff80810970 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
18   CoreFoundation                     0x00007fff807f1850 __CFRunLoopDoSources0 + 557
19   CoreFoundation                     0x00007fff807f0bd0 __CFRunLoopRun + 934
20   CoreFoundation                     0x00007fff807f07d0 CFRunLoopRunSpecific + 420
21   Foundation                         0x00007fff822054fd -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 277
22   Foundation                         0x00007fff8220549e -[NSRunLoop(NSRunLoop) run] + 76
23   LanguageServer                     0x00000001018713d0 main + 1035
24   libdyld.dylib                      0x00007fff95d59254 start + 1
[Info  - 3:56:26 PM] Connection to server got closed. Server will restart.

This is one of those things I've got to do: remove all the IUOs.

While I try and replicate this locally and test. I thought I might share this. It might help you come up with a fix for the issue while I try and work it from my end.

Thanks for the report @aaroncrespo!

Still working on this. I've at least narrowed this down to what it should be directly from SourceKit. This is the expected desired input and output.

{
    key.request: source.request.codecomplete,
    key.sourcetext: "import Foundation\nstruct Foo {\n  var i = 0\n}\nstruct Bar {\n  var k: Foo\n\n  func test() {\n      k.\n  }\n}\n",
    key.offset: 96,
    key.compilerargs: ["<input>"]
}
{
  key.results: [
    {
      key.kind: source.lang.swift.decl.var.instance,
      key.name: "i",
      key.sourcetext: "i",
      key.description: "i",
      key.typename: "Int",
      key.context: source.codecompletion.context.thisclass,
      key.num_bytes_to_erase: 0,
      key.associated_usrs: "s:vV4main3Foo1iSi",
      key.modulename: "main"
    }
  ]
}

@aaroncrespo I'm going to call this one done because I can no longer replicate it locally.

screen shot 2017-03-16 at 9 33 21 pm

I'm not entirely sure which one of the recent two commits, 9ef2278 or fea20f5, did it but my guess is 9ef2278.

Either way I've added a bunch of new logging on macOS 10.12 for the unified logging system. Hopefully if you do experience this again it'll help you to provide me with more clues.

Please re-open if you can still replicate it on master.