Skip to content

[SR-12133] Code compatibility regression in Swift 5.2 when using a type named "Self" #54568

@JaviSoto

Description

@JaviSoto
Previous ID SR-12133
Radar rdar://problem/59216636
Original Reporter @JaviSoto
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 11.4 Beta 1 in Swift 5 mode

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: aaf8d75237edc83c5ece6de2caeb53e9

Issue Description:

See the following code:

struct OuterType {
    struct `Self` {
        let string: String  
    }
    
    var foo: `Self`? {
        let optional: String? = "foo"
        return optional.map { `Self`(string: $0) }
    }
}

Untitled 3.swift:10:29: error: cannot convert value of type 'OuterType.`Self`' to closure result type 'OuterType?'
return optional.flatMap
Unknown macro: { `Self`(string}
^~~~~~~~~~~~~~~~~~

This compiles with Swift 5.1. The var declaration understands what type we're referring to (the nested `Self` type), but the closure code thinks that we're instantiating `Self` (aka `OuterType`)

If we change the code in the var to this, it compiles, even though it shouldn't:

var foo: `Self`? {
    // This shouldn't compile, because `self` is the wrong type.
    return self
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions