Skip to content

[Sema] Attempt diagnose generic arg ambiguity if all solutions produce generic arg mismatch #41506

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3697,7 +3697,8 @@ static bool diagnoseConflictingGenericArguments(ConstraintSystem &cs,
solution.Fixes, [](const ConstraintFix *fix) -> bool {
return fix->getKind() == FixKind::AllowArgumentTypeMismatch ||
fix->getKind() == FixKind::AllowFunctionTypeMismatch ||
fix->getKind() == FixKind::AllowTupleTypeMismatch;
fix->getKind() == FixKind::AllowTupleTypeMismatch ||
fix->getKind() == FixKind::GenericArgumentsMismatch;
});
});

Expand All @@ -3711,24 +3712,27 @@ static bool diagnoseConflictingGenericArguments(ConstraintSystem &cs,
genericParams;
// Consider only representative type variables shared across
// all of the solutions.
for (auto *typeVar : cs.getTypeVariables()) {
if (auto *GP = typeVar->getImpl().getGenericParameter()) {
auto *locator = typeVar->getImpl().getLocator();
auto *repr = cs.getRepresentative(typeVar);
// If representative is another generic parameter let's
// use its generic parameter type instead of originator's,
// but it's possible that generic parameter is equated to
// some other type e.g.
//
// func foo<T>(_: T) -> T {}
//
// In this case when reference to function `foo` is "opened"
// type variable representing `T` would be equated to
// type variable representing a result type of the reference.
if (auto *reprGP = repr->getImpl().getGenericParameter())
GP = reprGP;
for (auto &solution : solutions) {
for (auto &typeBinding : solution.typeBindings) {
auto *typeVar = typeBinding.first;
if (auto *GP = typeVar->getImpl().getGenericParameter()) {
auto *locator = typeVar->getImpl().getLocator();
auto *repr = cs.getRepresentative(typeVar);
// If representative is another generic parameter let's
// use its generic parameter type instead of originator's,
// but it's possible that generic parameter is equated to
// some other type e.g.
//
// func foo<T>(_: T) -> T {}
//
// In this case when reference to function `foo` is "opened"
// type variable representing `T` would be equated to
// type variable representing a result type of the reference.
if (auto *reprGP = repr->getImpl().getGenericParameter())
GP = reprGP;

genericParams[repr] = {GP, getLoc(locator->getAnchor())};
genericParams[repr] = {GP, getLoc(locator->getAnchor())};
}
}
}

Expand All @@ -3743,6 +3747,14 @@ static bool diagnoseConflictingGenericArguments(ConstraintSystem &cs,
llvm::SmallSetVector<Type, 4> arguments;
for (const auto &solution : solutions) {
auto type = solution.typeBindings.lookup(typeVar);
// Type variables gathered from a solution's type binding context may not
// exist in another given solution because each solution could have a
// different set of overload choices picked, which implies that some of
// the generic parameters, and type variables that represent them, would
// be unique to that solution.
if (!type)
continue;

// Contextual opaque result type is uniquely identified by
// declaration it's associated with, so we have to compare
// declarations instead of using pointer equality on such types.
Expand Down
11 changes: 5 additions & 6 deletions test/Constraints/casts_swift6.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ func test_compatibility_coercions(_ arr: [Int], _ optArr: [Int]?, _ dict: [Strin
// expected-note@-1 {{arguments to generic parameter 'Element' ('Int' and 'String') are expected to be equal}}

// Make sure we error on the following in Swift 6 mode.

// FIXME: Bad diagnostics (SR-15843)
_ = id(arr) as [String] // expected-error {{type of expression is ambiguous without more context}}
_ = (arr ?? []) as [String] // expected-error {{type of expression is ambiguous without more context}}
_ = (arr ?? [] ?? []) as [String] // expected-error {{type of expression is ambiguous without more context}}
_ = (optArr ?? []) as [String] // expected-error {{type of expression is ambiguous without more context}}
_ = id(arr) as [String] // expected-error {{conflicting arguments to generic parameter 'T' ('[Int]' vs. '[String]')}}
_ = (arr ?? []) as [String] // expected-error {{conflicting arguments to generic parameter 'T' ('[String]' vs. '[Int]')}}
_ = (arr ?? [] ?? []) as [String] // expected-error {{conflicting arguments to generic parameter 'T' ('[String]' vs. '[Int]')}}
// expected-error@-1{{conflicting arguments to generic parameter 'T' ('[String]' vs. '[Int]')}}
_ = (optArr ?? []) as [String] // expected-error {{conflicting arguments to generic parameter 'T' ('[Int]' vs. '[String]'}}

_ = (arr ?? []) as [String]? // expected-error {{'[Int]' is not convertible to '[String]?'}}
// expected-note@-1 {{did you mean to use 'as!' to force downcast?}}
Expand Down