Skip to content

[CSSimplify] Diagnose contextual mismatch between fully resolved depe… #61672

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 1 commit into from
Oct 25, 2022
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
17 changes: 17 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6343,6 +6343,23 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
if (desugar1->isEqual(desugar2))
return getTypeMatchSuccess();

if (shouldAttemptFixes()) {
if (!desugar1->hasTypeVariable() && !desugar2->hasTypeVariable()) {
auto *loc = getConstraintLocator(locator);

auto *fix =
loc->isLastElement<LocatorPathElt::TypeParameterRequirement>()
? fixRequirementFailure(*this, type1, type2, loc->getAnchor(),
loc->getPath())
: ContextualMismatch::create(*this, type1, type2, loc);

if (!fix || recordFix(fix))
return getTypeMatchFailure(locator);

return getTypeMatchSuccess();
}
}

// If one of the dependent member types has no type variables,
// this comparison is effectively illformed, because dependent
// member couldn't be simplified down to the actual type, and
Expand Down
13 changes: 13 additions & 0 deletions test/Constraints/generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -983,3 +983,16 @@ func testOverloadGenericVarFn() {
S<((String) -> Void)?>().foo?("")
S<((String) -> Void)?>().foo!("")
}

do {
func foo<T, U>(_: T, _: U) where T: Sequence, U: Sequence, T.Element == U.Element {}
// expected-note@-1 {{required by local function 'foo' where 'T' = 'Set<Int>.Type'}}
// expected-note@-2 {{required by local function 'foo' where 'U' = 'Array<String>.Type'}}
// expected-note@-3 {{where 'T.Element' = 'Set<Int>.Type.Element', 'U.Element' = 'Array<String>.Type.Element'}}

foo(Set<Int>, Array<String>)
// expected-error@-1 {{type 'Set<Int>.Type' cannot conform to 'Sequence'}}
// expected-error@-2 {{type 'Array<String>.Type' cannot conform to 'Sequence'}}
// expected-error@-3 {{local function 'foo' requires the types 'Set<Int>.Type.Element' and 'Array<String>.Type.Element' be equivalent}}
// expected-note@-4 2 {{only concrete types such as structs, enums and classes can conform to protocols}}
}