Using `Self!` and `Self?` as type in property declarations crashes compiler and static analyzer

The code below is correctly parsed by the static analyzer:

class FooBar {
    var bar: Self

    init(bar: Self) {
        self.bar = bar
    }
}

generating a meaningful warning:

`'Self' is only available in a protocol or as the result of a method in a class; did you mean 'FooBar'?

However, wrapping Self in Optional or in implicitly unwrapped Optional, like in the example below:

class FooBar {
    var foo: Self!
    var bar: Self?
}

generates an internal error:

An internal error occured. Source editor functionality is limited.

In addition, it makes the the frontend compiler crash with a Segmentation fault: 11.

Of course it shouldn't be used this way, but nevertheless I suppose it is not an intended behaviour.

swift --version
Apple Swift version 5.1.2 (swiftlang-1100.0.278 clang-1100.0.33.9)
Target: x86_64-apple-darwin19.0.0

Thanks for having a look.

A compiler crash is never intended, please file a report at bugs.swift.org.

It’s worth noting that var bar: Self would be sensible if the class were final, however it is still not allowed by the language.

3 Likes

I think a get only property is allowed on non-final classes still.

This doesn't crash on master. You will now get an error: stored property cannot have covariant 'Self' type

5 Likes
Terms of Service

Privacy Policy

Cookie Policy