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-7125] Swift 4.1 Xcode 9.3b4 regression #49673

Closed
keith opened this issue Mar 5, 2018 · 10 comments
Closed

[SR-7125] Swift 4.1 Xcode 9.3b4 regression #49673

keith opened this issue Mar 5, 2018 · 10 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.1 type checker Area → compiler: Semantic analysis

Comments

@keith
Copy link
Collaborator

keith commented Mar 5, 2018

Previous ID SR-7125
Radar rdar://problem/38159133
Original Reporter @keith
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 9.3b4

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 4.1Regression, TypeChecker
Assignee @xedin
Priority Medium

md5: c85e0d6950ba1dfcfa34d061adc53271

Issue Description:

This code compiles successfully in Xcode 9.2 with Swift 4:

protocol Bar {}
class Superclass {}

class SubclassA: Superclass, Bar {}
class SubclassB: Superclass, Bar {}

class Foo {
    var a: SubclassA? { return nil }
    var b: SubclassB? { return nil }

    func bar() {
        let bars: [Bar] = [
            self.a,
            self.b,
        ].flatMap { $0 }
    }
}

With Swift 4.1 this fails with this error:

foo.swift:15:11: error: 'flatMap' produces '[String]', not the expected contextual result type '[Bar]'
        ].flatMap { $0 }

If I change the `flatMap` to the new `compactMap` I get a slightly different error:

foo.swift:15:27: error: cannot convert value of type 'Superclass?' to closure result type 'Bar?'
        ].compactMap { $0 }

Changing it to this works as expected:

let bars: [Bar] = [
    self.a,
    self.b,
].compactMap { $0 as? Bar }

This is new code in our codebase since Xcode 9.3b3, so I'm not sure if this is new in beta 4 or not.

@jckarter
Copy link
Member

jckarter commented Mar 5, 2018

cc @rudkx, @xedin. It seems to me this has a legitimate reason for compiling previously, and still should be able to compile using `compactMap`. Working backward from the contextual result type `[Bar]`, we should be able to infer `[Bar?]` for the array literal type, which is a supertype of all of the elements of the array literal.

@jckarter
Copy link
Member

jckarter commented Mar 5, 2018

@swift-ci create

@jckarter
Copy link
Member

jckarter commented Mar 5, 2018

Does it also work if you use $0 as Bar? to give explicit type information to the closure body?

@keith
Copy link
Collaborator Author

keith commented Mar 5, 2018

It does compile with that in the closure body, that's the last example above. It also compiles with this:

let bars: [Bar] = [
    self.a as Bar?,
    self.b as Bar?,
].compactMap { $0 }

@jckarter
Copy link
Member

jckarter commented Mar 5, 2018

Sorry, I read that you had as? Bar in the closure body in your last example, which is slightly different from as Bar?.

@keith
Copy link
Collaborator Author

keith commented Mar 5, 2018

Ah sorry I misread that! No that fails with this error:

bar.swift:15:27: error: 'Superclass?' is not convertible to 'Bar?'; did you mean to use 'as!' to force downcast?
        ].compactMap { $0 as Bar? }
                       ~~~^~~~~~~
                          as!

@xedin
Copy link
Member

xedin commented Mar 5, 2018

@rudkx It seems like we do something very wrong here since with `$0 as? Bar` the result of the closure is inferred to be `Superclass` and `$0` to be `Superclass?` instead of `Bar` and `Bar?`. I'm going to take a look, it might be an easy fix.

@xedin
Copy link
Member

xedin commented Mar 5, 2018

@swift-ci create

@xedin
Copy link
Member

xedin commented Mar 7, 2018

The problem should be resolved by #15009 @keith, thanks for reporting and you can verify the fix by using next available nightly snapshot.

@keith
Copy link
Collaborator Author

keith commented Mar 10, 2018

@xedin I tested the master snapshot from 3/8 and it does compile now. Thanks! What's the process for getting that fixed in the 4.1 branch?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 regression swift 4.1 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants