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-12984] Imported Protocol Conformance Leaks into Other Files #55430

Open
swift-ci opened this issue Jun 11, 2020 · 1 comment
Open

[SR-12984] Imported Protocol Conformance Leaks into Other Files #55430

swift-ci opened this issue Jun 11, 2020 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12984
Radar None
Original Reporter Matt Rips (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 27233772a9be23a732852d1bad7b6b18

Issue Description:

If a protocol conformance is imported into one file of a module, that conformance leaks into other files of the same module. That leaky behavior violates the rule that the visibility of an import is confined to the file in which the import is stated.

// Module A
public struct X {
    public let value: Int
    public init(_ value: Int) { self.value = value }
}

// Module B
import A
extension X: Equatable {
    public static func ==(lhs: X, rhs: X) -> Bool { lhs.value == rhs.value }
}

// Module C - FileOne.swift
import A
import B
public func first() { print(X(1) == X(1)) }
    // This compiles because Equatable conformance
    // is imported from A.

// Module C - FileTwo.swift
import A
public func second() { print(X(1) == X(2)) }
    // This should not compile because conformance 
    // to Equatable is not declared.
    // But, it does compile because the Equatable 
    // conformance imported in FileOne somehow is 
    // visible in FileTwo.

This issue leads to other seemingly incorrect behavior.

(A) INDETERMINATE DISPATCH. If multiple conflicting conformances are imported into FileOne (e.g., one from Module A and another from some other module), then, in FileTwo, if the protocol requirement is accessed, the compiler seems to pick one of the conflicting conformances. It is not clear how the compiler chooses which to call.

(B) INABILITY TO DECLARE CONFORMANCE. If a conformance is imported into FileOne, then, in FileTwo, it is not possible to declare a different version of the conformance for use within Module B. The compiler raises a redundancy error, and points at the conformance provided by A.Array<Int>.

NOTE: I cannot find formal documentation of the rule-of-import-visibility to which I refer. That inability to find documentation may be due to a lack of diligence on my part or a documentation bug. Alternatively, that inability may be evidence that the rule does not actually exist. If the rule does not exist, then, I believe Swift needs a formal rule to govern the scope of import visibility.

@swift-ci
Copy link
Collaborator Author

Comment by Matthew Rips (JIRA)

I've changed the example code to a more realistic example that actually does (but shouldn't) compile.

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

No branches or pull requests

1 participant