Xcode 10.2 : Auto-complete not always working inside closures

Xcode version : 10.2

Problem : Auto-complete inside closures not working consistently.


I am having a some issues with auto-complete in Xcode 10.2. Mostly for code completion inside closures. While code completion used to work well, before 10.2, it will now stop working inside closures if the expected return type (or something else) isn't correct.


Instead I get the following in the completion window when I had the "." after an object :


_ self


So for example, if I change the expected return type outside of the closure (say I am refactoring some of the code) and try to rework the code inside of the closure, I don't get code completion anymore (apart from the useless "_ self") for the objects inside the closure.


However (after guessing at the properties or methods, etc.) once I get the inside of the closure to return the expected type, it will work again -- which is somewhat useless at this point.


Another way to work around this is to start writing the closure from scratch again (😠).


This is very annoying and a major departure from previous behavior. Anyone else experiencing this ?

I'm too. ****!


Very similar issue happening to me!


Autocomplete only shows the useless _ self when I'm adding code to some of my closures... (In particular all closures of the "apply" extension as defined here: https://gist.github.com/kakajika/0bb3fd14f4afd5e5c2ec)



The funny thing is that once the property & its assignment are typed correctly, XCode highlights them, meaning it recognizes them as valid!... It just doesn't propose them in autocomplete 😝


I think you should go ask this over on forums.swift.org, where one of the compiler engineers can give you a definitive answer.


It's pretty well known that the Swift compiler is pretty bad at knowing what's going on when a closure is being "constructed" in the editor. It's especially believable in this case, because it can't suggest completions until it knows the type of "Self", and it doesn't know the type of Self until it looks up "UIStackView.apply(block:)", and it doesn't know that's what it should look up until it knows what the closure is. After all, there could be multiple methods that take a closure parameter.


You could imagine a more complex chain of reasoning in the compiler that would drive knowledge of the UIStackView type into the closure, but I think this would in general make the type inference part of the compiler much, much slower.


In a way, you're a victim of the generality of your own code pattern.


That's my theory about this. If you ask on the Swift forums and get a different answer, please let us know the truth back here too!

Xcode 10.2 : Auto-complete not always working inside closures
 
 
Q