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-8349] CustomStringConvertible and rethrows in Playgrounds #66

Closed
swift-ci opened this issue Jul 24, 2018 · 9 comments
Closed

[SR-8349] CustomStringConvertible and rethrows in Playgrounds #66

swift-ci opened this issue Jul 24, 2018 · 9 comments

Comments

@swift-ci
Copy link

Previous ID SR-8349
Radar rdar://problem/41460357
Original Reporter Tony Y. (JIRA User)
Type Bug
Status Closed
Resolution Done

Attachment: Download

Environment

macOS High Sierra

Xcode 9.4.1 (9F2000), Xcode 10 beta 1 to 6, Xcode 10 GM seed, and Xcode 10.0 (10A255)

Additional Detail from JIRA
Votes 0
Component/s Xcode Playground Support
Labels Bug, RunTimeCrash
Assignee Tony Y. (JIRA)
Priority Medium

md5: f44e7e48b8a3f02ecbd930690a6ba11c

Issue Description:

The following code crashes Xcode when run in Playground:

struct BuggyStruct {
    func bug(_ body: (Int) throws -> Void) rethrows { try body(0) }
}
extension BuggyStruct : CustomStringConvertible {
    var description: String {
        var text = "Bug"
        self.bug({ element in
            text += "\(element)"
        })
        return text
    }
}
do {
    print(BuggyStruct())
}

I got error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=2, address=0x7ffeeeb54ff8). The computed variable description was used many times (more than 1000 times as shown in the side bar of Playgrounds). When using throws instead of rethrows and adding try! at the throwing function self.bug(...), I got no error.

Swift Forums threads

@huonw
Copy link
Mannequin

huonw mannequin commented Jul 24, 2018

Reduced version:

struct WillCrash: CustomStringConvertible {
    var description: String {
        self
        return "some string"
    }
}
WillCrash()

@swift-ci
Copy link
Author

Comment by Tony Y. (JIRA)

The following code is appropriate, and will not crash.

struct WillNotCrash: CustomStringConvertible {
    var description: String {
        let _ = self
        return "some string"
    }
}
WillNotCrash()

@huonw Is the same the bug which causes BuggyStruct and WillCrash to crash?

@huonw
Copy link
Mannequin

huonw mannequin commented Jul 25, 2018

What do you mean by "appropriate"? Even if some code doesn't "make sense" for a real-world application, it can still be an example of the bug, and our suspicious is that those two are the same bug.

@swift-ci
Copy link
Author

Comment by Tony Y. (JIRA)

I can understand that `self` in WillCrash calls the computed variable `description`. However, why does the 'rethrows' function call the computed variable `description`?

@swift-ci
Copy link
Author

Comment by Tony Y. (JIRA)

I got no error when inserting "let _ = " at "self.bug(...)" as "WillNotCrash".

BuggyStruct_NoCrash.playground.zip

@swift-ci
Copy link
Author

Comment by Tony Y. (JIRA)

Another code that will not crash:

BuggyStruct_NoCrash2.playground.zip

@swift-ci
Copy link
Author

swift-ci commented Sep 1, 2018

Comment by Connor Wakamo (JIRA)

I've created a pull request which prevents PlaygroundLogger from logging recursively: <https://github.com/apple/swift-xcode-playground-support/pull/29\>

@swift-ci
Copy link
Author

swift-ci commented Sep 7, 2018

Comment by Connor Wakamo (JIRA)

Merged <https://github.com/apple/swift-xcode-playground-support/pull/29\> into apple/swift-xcode-playground-support master with a change which addresses this issue.

@swift-ci
Copy link
Author

Comment by Tony Y. (JIRA)

I confirmed that the Xcode 10.1 beta 2 doesn't have this issue.

@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
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant