Skip to content

Sema: Restore old behavior of generic parameters with associated type inference #71548

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

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Feb 11, 2024

This effectively reverts d0bd026, so
we now look for abstract type witnesses before generic parameters.

In particular, this means we again prefer the default type witness
over a generic parameter if nothing else forces it to be a generic
parameter:

protocol P { associatedtype A = Int }
struct S<T>: P {}
// S.T is always Int

Fixing this properly requires modeling generic parameter bindings as
disjunctions, which is a more disruptive change than I want to take
for now.

Fixes rdar://problem/122587432.

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

… inference

This effectively reverts d0bd026, so
we now look for abstract type witnesses before generic parameters.

In particular, this means we again prefer the default type witness
over a generic parameter if nothing else forces it to be a generic
parameter:

    protocol P { associatedtype A = Int }
    struct S<T>: P {}
    // S.T is always Int

Fixing this properly requires modeling generic parameter bindings as
disjunctions, which is a more disruptive change than I want to take
for now.

Fixes rdar://problem/122587432.
@slavapestov slavapestov force-pushed the assoc-type-regression-fixes-3 branch from ee2d67f to f95109d Compare February 12, 2024 02:17
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov slavapestov marked this pull request as ready for review February 12, 2024 02:19
@slavapestov slavapestov merged commit d8b5b88 into swiftlang:main Feb 12, 2024
Comment on lines +2 to +11
// RUN: %target-typecheck-verify-swift -disable-experimental-associated-type-inference

protocol P {
associatedtype A = Int
}

struct S<A>: P {}

let x: String.Type = S<String>.A.self
// This is unfortunate but it is the behavior of Swift 5.10.
let x: Int.Type = S<String>.A.self
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would looking for generic parameters before defaults but after same-type requirements not fix this at least?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe, but I would rather fix this in one shot by building a disjunction containing the default associated type and generic parameter, if any, in inferTypeWitnessesViaAssociatedType(), and having the solver walk through the solution space. This first requires better ranking rules and a smarter solver that won't go exponential as easily.

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.

2 participants