Skip to content

[SR-9975] Protocol Extension Bugs #52379

@swift-ci

Description

@swift-ci
Previous ID SR-9975
Radar rdar://problem/48315560
Original Reporter jesands (JIRA User)
Type Bug
Environment

Xcode Version 10.1 (10B61)
Apple Swift version 4.2.1

All examples run in a Swift Playground.

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

md5: d4d208ed9f32df731ab2823a18e0708c

Issue Description:

There are several issues surrounding protocols with default implementations in extensions. The first two here are definitely bugs, the third is arguably a bug.

1. Incorrect behavior for statically defined constants

protocol A {
    var a: Int { get }
}

extension A {
    var a: Int { 
        return 1
    }
}

class Implementation: A {
    static let a: Int = 2
    let a = Implementation.a
}

let implementation = Implementation()
print(implementation.a)
print(implementation.a as Int)
print((implementation as A).a as Int)

Expected output: "2 2 2"
Actual output: "2 2 1".
Note: Moving the static variable declaration to another class produces the expected output. Declaring the `let a` as `let a: Int` also produces the expected output.

2. Declaring uninitialized variables not caught by the compiler:

class Z {}

protocol A {
    var z: Z { get }
}

extension A {
    var z: Z {
        return Implementation().z
    }
}

struct Implementation: A {
    let z = globalZ
}

let globalZ = Implementation().z

Expectation: compiler error. Circular definition.
Actual error: "Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x0)."

3. Type based variable overloading

protocol A {
    var a: Float { get }
}

extension A {
    var a: Float {
        return 1
    }
}

class Implementation: A {
 let a = 2
}

let implementation = Implementation()
print(implementation.a)
print(implementation.a as Float)
print((implementation as A).a)

Expected output: Compiler error or "2.0 2.0 2.0".
Actual output: "2 1.0 1.0"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions