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-7839] Protocol Restricting Class Inheritance Cannot Consistently Access Valid Instance Properties #50375

Closed
bscothern opened this issue Jun 1, 2018 · 1 comment
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@bscothern
Copy link

Previous ID SR-7839
Radar None
Original Reporter @bscothern
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

macOS 10.13.4 – Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2

Ubuntu 16.04 (on arm) – Swift version 4.1.1 (swift-4.1.1-RELEASE)

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

md5: 311941431a009031cc91fdaf1bd8655f

duplicates:

  • SR-6000 Let protocols inherit from class types

Issue Description:

When restricting a protocol to require inheritance from a specified class like so:

protocol SomeProtocol where Self: SomeClass {{{}}}

You are unable to access many properties of SomeClass outside of the same file as the declaration of SomeProtocol. I have experienced this on all apple platforms and ubuntu 16.04.

For example if you have the following files (which are attached and organized as a swift package) you will see all of the issues.

protocol SomeProtocol where Self: SomeClass {
}

extension SomeProtocol {
    func accessSomeClassProperty1FromSomeProtocolExtension() -> String {
        return Self.property1
    }
    
    func accessSomeClassProperty2FromSomeProtocolExtension() -> String {
        return property2
    }
}
class SomeClass {
    class var property1: String {
        return "1"
    }
    
    var property2: String {
        return "2"
    }
}
class Implementation: SomeClass, SomeProtocol {
}
func getSomeProtocol() -> SomeProtocol {
    return Implementation()
}

let someProtocol = getSomeProtocol()

print("=== Direct access through Implementation class/instance ===")
print("From Implementation class: " + Implementation.property1)
print("From new Implementation: " + Implementation().property2)

print("=== Access through SomeProtocol extension on Implementation instance ===")
print("From new Implementation: " + Implementation().accessSomeClassProperty1FromSomeProtocolExtension())
print("From new Implementation: " + Implementation().accessSomeClassProperty2FromSomeProtocolExtension())

print("=== Access through SomeProtocol extension on SomeProtocol instance ===")
print("From getSomeProtocol: " + someProtocol.accessSomeClassProperty1FromSomeProtocolExtension())
// This will crash
//print("From getSomeProtocol: " + someProtocol.accessSomeClassProperty2FromSomeProtocolExtension())

print("=== Access through Implementation instance through SomeProtocol instance ===")
// This case doesn't compile
//print("From getSomeProtocol: " + someProtocol.property2)
print("From getSomeProtocol as Implementation: " + (someProtocol as! SomeClass).property2)

For convenience here are the actual error outputs I have experienced.

Apple platforms:

Run time crash:

[1]    73553 bus error  swift run

Build failure:

swift build
Compile Swift Module 'ProtocolInheritClass' (4 sources)
/Users/Braden/Documents/Teal/Programming/SwiftBugs/ProtocolInheritClass/Sources/ProtocolInheritClass/main.swift:22:34: error: value of type 'SomeProtocol' has no member 'property2'
print("From getSomeProtocol: " + someProtocol.property2)
                                 ^~~~~~~~~~~~ ~~~~~~~~~
'ProtocolInheritClass' /Users/Braden/Documents/Teal/Programming/SwiftBugs/ProtocolInheritClass: warning: Ignoring duplicate product 'ProtocolInheritClass' 
error: terminated(1): /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/Braden/Documents/Teal/Programming/SwiftBugs/ProtocolInheritClass/.build/debug.yaml main output:

Ubuntu 16.04:

Build Failure

swift build
Compile Swift Module 'ProtocolInheritClass' (4 sources)
TYPE MISMATCH IN ARGUMENT 0 OF APPLY AT expression at [/home/tealdrone/ProtocolInheritClass/Sources/ProtocolInheritClass/main.swift:16:34 - line:16:97] RangeText="someProtocol.accessSomeClassProperty1FromSomeProtocolExtension()"
  argument value:   %227 = open_existential_addr immutable_access %3 : $*SomeProtocol to $*@opened("C95DABBC-6526-11E8-B001-00044B660A48") SomeProtocol
  parameter type: $@opened("C95DABBC-6526-11E8-B001-00044B660A48") SomeProtocol
/usr/bin/swift[0x3912d70]
Stack dump:
0.  Program arguments: /usr/bin/swift -frontend -c /home/tealdrone/ProtocolInheritClass/Sources/ProtocolInheritClass/Implementation.swift -primary-file /home/tealdrone/ProtocolInheritClass/Sources/ProtocolInheritClass/main.swift /home/tealdrone/ProtocolInheritClass/Sources/ProtocolInheritClass/SomeClass.swift /home/tealdrone/ProtocolInheritClass/Sources/ProtocolInheritClass/SomeProtocol.swift -target aarch64-unknown-linux -Xllvm -aarch64-use-tbi -disable-objc-interop -sdk / -I /home/tealdrone/ProtocolInheritClass/.build/aarch64-unknown-linux/debug -enable-testing -g -module-cache-path /home/tealdrone/ProtocolInheritClass/.build/aarch64-unknown-linux/debug/ModuleCache -swift-version 4 -Onone -D SWIFT_PACKAGE -color-diagnostics -emit-module-doc-path /home/tealdrone/ProtocolInheritClass/.build/aarch64-unknown-linux/debug/ProtocolInheritClass.build/main~partial.swiftdoc -module-name ProtocolInheritClass -emit-module-path /home/tealdrone/ProtocolInheritClass/.build/aarch64-unknown-linux/debug/ProtocolInheritClass.build/main~partial.swiftmodule -emit-dependencies-path /home/tealdrone/ProtocolInheritClass/.build/aarch64-unknown-linux/debug/ProtocolInheritClass.build/main.d -emit-reference-dependencies-path /home/tealdrone/ProtocolInheritClass/.build/aarch64-unknown-linux/debug/ProtocolInheritClass.build/main.swiftdeps -num-threads 4 -o /home/tealdrone/ProtocolInheritClass/.build/aarch64-unknown-linux/debug/ProtocolInheritClass.build/main.swift.o 
<unknown>:0: error: unable to execute command: Aborted
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)
'ProtocolInheritClass' /home/tealdrone/ProtocolInheritClass: warning: Ignoring duplicate product 'ProtocolInheritClass' 
error: terminated(1): /usr/bin/swift-build-tool -f /home/tealdrone/ProtocolInheritClass/.build/debug.yaml main output:
@belkadan
Copy link
Contributor

belkadan commented Jun 1, 2018

@slavapestov, sounds like yet another manifestation of the other bugs we have on this?

@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
Projects
None yet
Development

No branches or pull requests

2 participants