Sunday, August 21, 2016

The Awesome Responsiveness of Swift.org

I encounter a bug in Swift 3

I had this cool idea to enhance CGPath, create an extension which would add a convenience init method that would take an SVG formatted path string. How neat would it be to be able to write
  let frogPath = CGPath(svgPath: "M 170 207C139 183 40 199 41 109A18 18 0 1 1 56 75Q67 53 91 45A18 18 0 1 1 127 40C170 29 193 62 212 173L225 110Q241 70 252 120L253 156 C253 180 265 223 235 214Q210 210 215 242 C222 265 161 249 125 248") 
and have a complicated curve available for display. (I should say this all relies on other code in my brand new, and very incomplete Scalar2D project)

So, I opened up a new source code file and tried:

public extension CGPath 
{
 public convenience init?(svgPath: String)
 {
  guard let mutableSelf = CGPath.pathFromSVGPath(svgPath) else
   {
   return nil
   }
   return mutableSelf.copy()
  }
}

Now the compiler didn't like this and told me I couldn't assign to self, and I couldn't figure out how to do what I wanted, so I turned to CGMutablePath, which I could mutate in the init method. I therefore tried


public extension CGMutablePath
{
 public convenience init?(svgPath: String)
 {
  self.init()
   if !self.addSVGPath(svgPath)
   {
   return nil
  }
 }
}
And the compiler promptly crashed. I had found a bug in the pre-release Swift 3 compiler.

I report the crash

In the olden days before Swift had been open sourced, if I could find the energy, I would find my way to the Apple Bug Reporter and enter my bug, never knowing if I was the first or the millionth person to encounter my problem. And it would sort of disappear, unless a request would come back sometime later for more information, or it was marked as a duplicate, or whatever vaguely unsatisfying outcomes would manifest.

For example, a bug I reported against Safari 8.0.6 in June of 2015, 21364634 Safari not Rendering SVG Gradients Properly, that has not been commented upon, nor fixed. But at least that is still open, most of my reported bugs have been archived, several with the rather insulting description of Insufficient Information, no they have plenty of information, with steps to reproduce and in one case even an Xcode project I took the trouble to generate. Others were tersely marked as duplicates, and one was operator error on my part when using MKPolygonRenderer.

But, this is the new era of open source Swift so, I made my way to bugs.swift.org  and discovered not some custom tool, but honest to goodness JIRA, the same tool I've used in many projects over the last several years. Wow, this was new. I could browse through the bugs and see if what I was seeing was already reported. Didn't seem to be. So, I opened up a new issue, simplified my steps to recreate to something you could type into the command line REPL and created my issue: SR-2338 Compiler Crash on Implementing Optional convenience init of CGMutablePath

The Awesomeness of Responsiveness

Within hours, someone with an Apple email address replicated the bug, and commented upon it in the JIRA issue for all to see. And a back and forth ensued amongst team members about what to do, where the bug came from, and possible solutions. My particular bug came from an initiative to give Core Graphics a more Swift-like object interface instead of the traditional C style interface. Nobody had considered the idea of testing if adding a convenience init method would work. Code and test cases were generated and a pull request was offered where anybody could comment on the pull request. When the resulting code wasn't right, an other approach was taken wherein the compiler just wouldn't allow you to extend Core Graphics objects this way, and a better fix would be delayed.

All of this was done in full view of the world, where anybody with an interest could contribute. And the final release of Swift 3, will be more reliable for it.