Skip to content

Commit 6775949

Browse files
Merge pull request swiftlang#32982 from LucianoPAlmeida/SR-13226-constraint-requirement
[SR-13226] [Sema] Handle generic requirement failure on repairFailures
2 parents cde899d + 82d95ec commit 6775949

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,7 @@ ConstraintSystem::matchDeepEqualityTypes(Type type1, Type type2,
22292229

22302230
SmallVector<unsigned, 4> mismatches;
22312231
auto result = matchDeepTypeArguments(
2232-
*this, subflags, args1, args2, locator,
2232+
*this, subflags | TMF_ApplyingFix, args1, args2, locator,
22332233
[&mismatches](unsigned position) { mismatches.push_back(position); });
22342234

22352235
if (mismatches.empty())
@@ -4270,6 +4270,24 @@ bool ConstraintSystem::repairFailures(
42704270
break;
42714271
}
42724272

4273+
case ConstraintLocator::GenericArgument: {
4274+
// If any of the types is a hole, consider it fixed.
4275+
if (lhs->isHole() || rhs->isHole())
4276+
return true;
4277+
4278+
// Ignoring the generic argument because we may have a generic requirement
4279+
// failure e.g. `String bind T.Element`, so let's drop the generic argument
4280+
// path element and recurse in repairFailures to check and potentially
4281+
// record the requirement failure fix.
4282+
path.pop_back();
4283+
4284+
if (path.empty() || !path.back().is<LocatorPathElt::AnyRequirement>())
4285+
break;
4286+
4287+
return repairFailures(lhs, rhs, matchKind, conversionsOrFixes,
4288+
getConstraintLocator(anchor, path));
4289+
}
4290+
42734291
default:
42744292
break;
42754293
}

test/Generics/sr13226.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %target-typecheck-verify-swift
2+
struct W<T> {}
3+
4+
struct S<C1: Collection> {
5+
init(){}
6+
// expected-note@+2 {{where 'C1.Element' = 'String', 'W<C2.Element>' = 'Int'}}
7+
// expected-note@+1 {{where 'C1.Element' = 'C1', 'W<C2.Element>' = 'C2.Element'}}
8+
init<C2>(_ c2: W<C2>) where C2: Collection, C1.Element == W<C2.Element> {}
9+
// expected-note@+1 {{where 'C1.Element' = 'String', 'W<C2.Element>' = 'Int'}}
10+
static func f<C2>(_ c2: W<C2>) where C2: Collection, C1.Element == W<C2.Element> {}
11+
// expected-note@+1 {{where 'C1.Element' = 'String', 'W<C2.Element>' = 'Int'}}
12+
func instancef<C2>(_ c2: W<C2>) where C2: Collection, C1.Element == W<C2.Element> {}
13+
}
14+
let _ = S<[W<String>]>(W<[Int]>()) // expected-error{{initializer 'init(_:)' requires the types 'String' and 'Int' be equivalent}}
15+
let _ = S<[W<String>]>.f(W<[Int]>()) // expected-error{{static method 'f' requires the types 'String' and 'Int' be equivalent}}
16+
let _ = S<[W<String>]>().instancef(W<[Int]>()) // expected-error{{instance method 'instancef' requires the types 'String' and 'Int' be equivalent}}
17+
18+
// Archetypes requirement failure
19+
func genericFunc<C1: Collection, C2: Collection>(_ c2: W<C2>, c1: C1.Type) where C1.Element == W<C2.Element> {
20+
let _ = S<[W<C1>]>(W<C2>()) // expected-error{{initializer 'init(_:)' requires the types 'C1' and 'C2.Element' be equivalent}}
21+
}

0 commit comments

Comments
 (0)