2

I have some Swift code that was written in Swift 3.2, and I just attempted to use the automatic syntax converter in Xcode 9.0 to update to Swift 4.0.

It actually found nothing that needed to be converted, but during the code analysis when the compiler compiles the project, the compiler crashes with a segmentation fault. I can't share the full source code, so I created a simple example which demonstrates the same issue, and confirmed that this also produces a crash in a newly-created Swift project.

The compiler crash only appears when joining values together using string interpolation. To reproduce:

  1. Create a new Xcode project, and set Swift version to 3.2
  2. Create a new Swift class
  3. Paste the following code into the new class:

    public class SomeClass: NSObject {
        static let defaultStringPrefix = "abc"
        var a: String = ""
        var b: String = ""
        var c: String = ""
    
        lazy var concatStr: String = {
            let str = "\(defaultStringPrefix)/\(self.a)/\(self.b)/\(self.c)/somepage.html"
            return str
        }()
    
    }
    
  4. Attempt to perform the Swift 3.2 to Swift 4.0 conversion -> This will fail and a segfault will appear with the following error:

Command failed due to Segmentation fault: 11 Stack dump: 0....(a huge load of arguments to the swift compiler) 1. While type-checking getter for concatStr at /Users/craigrouse/mobiledev/test/test/SomeClass.swift:17:14 2. While type-checking declaration 0x7ff292a86400 in module 'test' 3. While type-checking expression at [/Users/craigrouse/mobiledev/test/test/SomeClass.swift:17:34 - line:20:7] RangeText="{ let str = "\(a)/\(b)/\(c)/somepage.html" return str }()"

I've tried various versions of this, and if I remove the string interpolation, the problem goes away. Obviously I could avoid using the auto conversion, and set the Swift version to 4 manually in the project settings, but I'm sure others will encounter this mysterious error, and I'd like to find out what's causing it. Is there a problem with my syntax?

Thanks.

CPR
  • 753
  • 8
  • 19
  • You haven't defined your variables types. You need to specify `a: String` – Leo Dabus Sep 25 '17 at 12:40
  • @LeoDabus thanks for your comments. The original code did have the variable types defined correctly. I have edited the sample code to reflect this. This code still causes a segfault in the Swift compiler. – CPR Sep 25 '17 at 12:52
  • 1
    you have declared your defaultStringPrefix variable as static so you need to call `SomeClass.defaultStringPrefix` – Leo Dabus Sep 25 '17 at 12:55
  • 1
    @LeoDabus thanks so much - this solved the problem. It's code I inherited from someone else, and I hadn't spotted that. Shame the compiler doesn't give more useful output - a compiler crash is not what I would have expected here. – CPR Sep 25 '17 at 12:57
  • 1
    When you click on the error in the Issue Navigator (⌘5) to get the detailed view you will see above the stack trace: *error: static member 'defaultStringPrefix' cannot be used on instance of type 'SomeClass'*. This is a clear description of the error. – vadian Sep 25 '17 at 13:17
  • True @vadian, although surely this should be detected as an error by the IDE before the point where you actually compile the code? Feels Xcode should catch this before compile time. I hadn't spotted that error in the sample code, but I've re-checked the original code, and the error does not show up - just all the unintelligible stuff from the Swift compiler stack trace. – CPR Sep 25 '17 at 14:38

0 Answers0