Skip to content

Sema: Accept tautological assoc type inference candidates when same-typed. #7875

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

Merged

Conversation

jckarter
Copy link
Contributor

@jckarter jckarter commented Mar 2, 2017

X := Self.X is still interesting information if it came from a witness candidate in a context where X == Y and we can infer a concrete witness for Y, for example:

protocol SameTypedDefault {
    associatedtype X
    associatedtype Y
    static var x: X { get }
    static var y: Y { get }
}
extension SameTypedDefault where Y == X {
    static var x: X {
        return y
    }
}

struct UsesSameTypedDefault: SameTypedDefault {
    static var y: Int {
        return 0
    }
}

Defer checking whether such a witness candidate satisfies associated type requirements until we consider a complete solution system, since ConformingType.X isn't a valid type until we've settled on a witness for X, and its abilities depend on what we choose for Y. Fixes SR-4143.

@jckarter
Copy link
Contributor Author

jckarter commented Mar 2, 2017

@swift-ci Please test

@jckarter
Copy link
Contributor Author

jckarter commented Mar 2, 2017

@DougGregor or @slavapestov, does this look OK? Is there a better way to ask a generic environment whether an associated type is same-typed to another one than trawling the requirements?

dmt->getAssocType());
for (auto &reqt : witness->getDeclContext()
->getGenericSignatureOfContext()
->getCanonicalSignature()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getCanonicalSignature() won't change the signature, it just canonicalizes the types themselves; you don't need that here

@swift-ci
Copy link
Contributor

swift-ci commented Mar 2, 2017

Build failed
Jenkins build - Swift Test Linux Platform
Git Commit - 4addc8f9bafdd7c81499bccb8d82001391fc3cb7
Test requested by - @jckarter

// If this associated type is same-typed to another associated type
// on `Self`, then it may still be an interesting candidate if we find
// an answer for that other type.
if (witness->getDeclContext()->getAsProtocolExtensionContext()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to check that two specific associated types are equal, you can map both into context and compare archetypes for pointer equality.

To see if an associated type is equal to some other associated type, the only way that's slightly saner than yours is to get the canonical generic signature builder for the signature, and ask it for the equivalence class of the associated type. But I don't know if the right APIs are public or sane.

So what you have here might be your best bet.

auto selfTy = witness->getDeclContext()->getSelfInterfaceType();
auto selfAssocTy = DependentMemberType::get(selfTy,
dmt->getAssocType());
for (auto &reqt : witness->getDeclContext()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like factoring out 'witness->getDeclContext()' and 'dc->getGenericSignatureOfContext()' would make this code easier to read.

…yped.

`X := Self.X` is still interesting information if it came from a witness candidate in a context where `X == Y` and we can infer a concrete witness for Y, for example:

```swift
protocol SameTypedDefault {
    associatedtype X
    associatedtype Y
    static var x: X { get }
    static var y: Y { get }
}
extension SameTypedDefault where Y == X {
    static var x: X {
        return y
    }
}

struct UsesSameTypedDefault: SameTypedDefault {
    static var y: Int {
        return 0
    }
}
```

Defer checking whether such a witness candidate satisfies associated type requirements until we consider a complete solution system, since `ConformingType.X` isn't a valid type until we've settled on a witness for X, and its abilities depend on what we choose for Y. Fixes SR-4143.
@jckarter jckarter force-pushed the assoc-type-inference-same-type-candidate branch from 4addc8f to cdc3c1b Compare March 2, 2017 22:31
@jckarter
Copy link
Contributor Author

jckarter commented Mar 2, 2017

@swift-ci Please test

1 similar comment
@jckarter
Copy link
Contributor Author

jckarter commented Mar 2, 2017

@swift-ci Please test

@jckarter
Copy link
Contributor Author

jckarter commented Mar 2, 2017

@slavapestov Thanks, I updated the patch in response to your feedback. How's this look? Reasonable for 3.1?

@slavapestov
Copy link
Contributor

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants