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-7516] Compiler accepts ambiguous and/or invalid use of associated types #50058

Open
jepers opened this issue Apr 24, 2018 · 5 comments
Open
Assignees

Comments

@jepers
Copy link

jepers commented Apr 24, 2018

Previous ID SR-7516
Radar None
Original Reporter @jepers
Type Bug
Status In Progress
Resolution
Environment

Xcode 9.3, default toolchain and recent development snapshots

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

md5: a723144527397c76606f258fbc351848

relates to:

  • SR-5440 Typealias in constrained extension misinterprets the where clause
  • SR-7217 Protocol composition with conflicting typealiases does not diagnose

Issue Description:

I'm not entirely sure what the intended behavior should be for the examples in the below demonstration program, I suppose the current behavior might be the result of more than one bug.

Perhaps the last example, S6, should be allowed but is currently not working as inteded?

Related forum post: https://forums.swift.org/t/whats-the-intended-behavior-here-associated-type-with-default-type-conditional-conformances/12094

This program compiles successfully with Xcode 9.3 default toolchain and recent development snapshots.

protocol P {
    associatedtype A = Int
}
protocol Q {
    associatedtype A = Bool
}

struct S1: P {
    var v: A // A is Int, as expected.
}

struct S2: P, Q {
    var v: A // A is Bool, but should be ambiguous?
}

struct S3: Q, P {
    var v: A // A is Int, but should be ambiguous?
}

struct S4<T> {
    var v: A // A is Int (Bool with recent snapshots), but should be undeclared?
}
extension S4: P where T == Double {}
extension S4: Q where T == Double {}

struct S5<T> {
    var v: A // A is Bool (Int with recent snapshots), but should be undeclared?
}
extension S5: Q where T == Double {}
extension S5: P where T == Double {}

struct S6<T>: P {
    var v: A // A is Bool, but should be ambiguos or: Bool only if T == Float, otherwise Int?
}
extension S6: Q where T == Float {}
@moiseev
Copy link
Mannequin

moiseev mannequin commented Apr 24, 2018

/cc @DougGregor, @huonw

@jepers
Copy link
Author

jepers commented Apr 24, 2018

At least the S4 and S5 examples seems to be related to: https://bugs.swift.org/browse/SR-5440
Example:

struct S<T> {
    var v: A // <-- Compiler happy, says A is String here.
             //       I assume it should be error: A is undeclared
}
extension S where T == Double {
    typealias A = String
}

@jepers
Copy link
Author

jepers commented Mar 27, 2020

Note that now, with Swift 5.2, Xcode 11.4, the following last part of the example no longer compiles and results in this error:

struct S6<T>: P {
    var v: A // ERROR: 'S6<T>.A' (aka 'Bool') requires the types 'T' and 'Float' be equivalent
}
extension S6: Q where T == Float {}

But the rest of the example still compiles.

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Mar 29, 2020

#30700 Fixes the less source-breaking part, that is, everything except for associated type resolution ambiguities like these:

struct S2: P, Q {
 var v: A // Bool or Int? Be it a value witness, the compiler would complain.
}

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Mar 29, 2020

I haven't seen a principled fix while working on this, so it is likely those now 'working' examples are either just a favorable combination of circumstances or the result of some change in validation that makes extensions be visited before primary declarations.

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

No branches or pull requests

2 participants