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-1576] Swift 3 can't subscript PHFetchResult #44185

Open
mattneub opened this issue May 21, 2016 · 15 comments
Open

[SR-1576] Swift 3 can't subscript PHFetchResult #44185

mattneub opened this issue May 21, 2016 · 15 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software

Comments

@mattneub
Copy link

Previous ID SR-1576
Radar rdar://problem/26632701
Original Reporter @mattneub
Type Bug
Environment

Toolchain: Swift Development Snapshot 2016-05-09 (a)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash
Assignee @jckarter
Priority Medium

md5: b0225e3be4ccb6e322b1c4940f69ccb9

relates to:

  • SR-3328 Cannot extend PHFetchResult to conform to Sequence in Swift 3

Issue Description:

This crashes the compiler (Command failed due to signal: Illegal instruction: 4):

        let result = PHCollectionList.fetchCollectionLists(with:
            .momentList, subtype: .momentListYear, options: nil)
        for ix in 0..<result.count {
            let list = result[ix]
        }

The problem is the attempt to subscript result. A workaround is:

            let list = result.object(at:ix)

But this should not be necessary.

This may be related to SR-1522.

Another possibly related issue is that I was previously working around the fact that you can't use for...in with PHFetchResult by making it adopt SequenceType. The Swift 3 equivalent of my code would be:

extension PHFetchResult: Sequence {
    public func makeIterator() -> NSFastEnumerationIterator {
        return NSFastEnumerationIterator(self)
    }
}

But that, too, crashes the compiler.

@jckarter
Copy link
Member

This smells like a bug we encountered dispatching subscript operators from generic classes on AnyObject. I have a potential fix here:

#2686 (master)
#2687 (3.0 preview 1)

@jckarter
Copy link
Member

jckarter commented Jun 3, 2016

@mattneub Thanks for following up! I encountered a similar problem locally while testing (tracked internally as rdar://problem/26602097), where associated types that refer to the ObjC class's generic parameters would cause a crash. If you can make it adopt `Sequence` with `AnyObject` as its element type, that might work around the problem, even though it's of course not ideal.

@mattneub
Copy link
Author

mattneub commented Jun 3, 2016

@jckarter Unfortunately this is not as fixed as I thought; in one project, I can subscript PHFetchResult, but in another, I can't.

@jckarter
Copy link
Member

jckarter commented Jun 3, 2016

@mattneub Are you building both with the same toolchain? Is there any difference in how PHFetchResult is used—maybe one uses it in a generic context, while the other binds it to a specific parameter type?

@mattneub
Copy link
Author

mattneub commented Jun 3, 2016

@jckarter It's weird. If I say

        let asset = self.photos[ix]
        dvc.asset = asset

everything is fine. If I try to condense that into one line

        dvc.asset = self.photos[ix]

the compiler crashes.

@mattneub
Copy link
Author

mattneub commented Jun 3, 2016

@jckarter Okay, I see what the difference is. `dvc.asset` is an Optional. I can work around this by saying dvc.asset = Optional(self.photos[ix]). But I probably shouldn't have to.

@jckarter
Copy link
Member

jckarter commented Jun 3, 2016

Definitely not. Thanks for narrowing it down.

@mattneub
Copy link
Author

mattneub commented Jun 4, 2016

@jckarter Here's a test case. This crashes the compiler on my machine:

import UIKit
import Photos

class DataViewController : UIViewController {
    var asset : PHAsset!
}

class ModelController: NSObject {
    var photos : PHFetchResult<PHAsset>!
    func test(ix:Int) {
        let d = DataViewController()
        d.asset = self.photos[0] // workaround is: d.asset = Optional(self.photos[0])
    }
}

@jckarter
Copy link
Member

jckarter commented Jun 4, 2016

Great test case. Is it OK for me to use that as a release note until we fix the bug?

@mattneub
Copy link
Author

mattneub commented Jun 7, 2016

@jckarter Of course - no need to ask. I'm just happy that I found a workaround. 🙂

@jckarter
Copy link
Member

jckarter commented Aug 9, 2016

Trying a fix: #4128

@mattneub
Copy link
Author

@jckarter I still can't find a way to make PHFetchResult respond to `for...in`, even though it adopts NSFastEnumeration in Objective-C. Should I be posting that as a separate bug?

@jckarter
Copy link
Member

@mattneub It would need to conform to Sequence to be usable with for...in. It should be possible to extend the class to conform, though due to other limitations with ObjC generics it may have to use AnyObject as its element type to do so.

@mattneub
Copy link
Author

@jckarter Understood, but the fact that `for...in` can be used in Objective-C but not in Swift is kind of dreadful. I used to be able to work around this by making PHFetchResult conform to SequenceType (as it was called in those days), but now I don't know how to do that, because of the generic. And even in the old days, I shouldn't have had to work around it myself, I feel. Nothing is more natural than to want to cycle thru your fetch results, and the `enumerate` methods are unSwifty.

@an0
Copy link
Mannequin

an0 mannequin commented Dec 4, 2016

@jckarter you mentioned "it may have to use AnyObject as its element type to do so". But how exactly? Can you show me the code? Thanks.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software
Projects
None yet
Development

No branches or pull requests

3 participants