Skip to content

[SR-9975] Protocol Extension Bugs #52379

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

Open
swift-ci opened this issue Feb 22, 2019 · 2 comments
Open

[SR-9975] Protocol Extension Bugs #52379

swift-ci opened this issue Feb 22, 2019 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself

Comments

@swift-ci
Copy link
Contributor

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"

@belkadan
Copy link
Contributor

#2 and #3 are tracked elsewhere (I'll find them in a minute), but #1 seems valid!

@belkadan
Copy link
Contributor

Okay, #2 is rdar://problem/35633741 but doesn't seem to have an SR. #3 is SR-6689.

Let's use this report to track #1.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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 itself
Projects
None yet
Development

No branches or pull requests

2 participants