Skip to content

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

@keith

Description

@keith
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.

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfregressionswift 4.1type checkerArea → compiler: Semantic analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions