Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-14564] "\(Self.self)" causes issue on Xcode 12.5 when Swift is optimized #398

Closed
swift-ci opened this issue May 3, 2021 · 5 comments
Labels
bug Something isn't working

Comments

@swift-ci
Copy link
Contributor

swift-ci commented May 3, 2021

Previous ID SR-14564
Radar rdar://problem/77465728
Original Reporter matteolongeri_ovolab (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s Compiler, SwiftSyntax
Labels Bug
Assignee None
Priority Medium

md5: 4cc049e4929f10d973d6a2409610d84f

Issue Description:

Using Xcode 12.5 I have identified an issue with this instruction "(Self.self)" when it runs on a build created setting SWIFT_OPTIMIZATION_LEVEL to -O (Optimize for speed).

The instruction above is executed in a superclass (subclass of UIView) when it to load a NIB:

Bundle.main.loadNibNamed("\(Self.self)", owner: self, options: nil)

Instead of getting the right subclass name, it simply returns "Self" and this generate the following error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: '...' with name 'Self''

This never happens before with previous versions of Xcode, and it still does not happen when the build is generate without using optimization -Onone.

To fix it I have changed "(Self.self)" with "(type(of: self))". This way it works, but I think that it should be investigate why the original version causes that kind of issue when Swift is optimized.

Forcing optimization in Debug, I was able to reproduce the issue. I have also tried to insert a breakpoint before the crash, trying to execute the instruction in Console with po command, but it works as expected, printing the right class name.

@swift-ci
Copy link
Contributor Author

swift-ci commented May 3, 2021

Comment by Matteo Longeri (JIRA)

UPDATE: To be sure that this issue is directly related to Xcode 12.5 I have created a simple project, defining:

class MyClass {

    var classname: String {
        "\(Self.self)"
    }

}

and calling it from the ViewController:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let c = MyClass()
        print(c.classname)
    }

}

When I activate -O optimization in Debug, the Console prints: Self.

Just to confirm that this issue is not directly related to the app I am working on, but it is reproducible in any context.

Matteo

@typesanitizer
Copy link

@swift-ci create

@eeckstein
Copy link
Member

Thanks for reporting. This is already fixed in Swift 5.5: apple/swift#36717

@swift-ci
Copy link
Contributor Author

Comment by Matteo Longeri (JIRA)

Thanks for your answer @eeckstein!

Best regards,

Matteo

@swift-ci
Copy link
Contributor Author

Comment by Andrey Yakushev (JIRA)

Issue appears only in classes. It's fine to write

"\(Self.self)"

inside structs.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 9, 2022
adevress pushed a commit to adevress/swift-syntax that referenced this issue Jan 14, 2024
Don't transform `Optional<T>` -> `T?` if it would cause a performance issue
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants