From 81afffee72fd91741fd6420bfb8ef563de157526 Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Tue, 21 Jul 2020 00:23:21 -0300 Subject: [PATCH 1/2] [CSSimplify] Handle generic argument for unsatisfied generic requirement bind constraint in repairFailures --- lib/Sema/CSSimplify.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/Sema/CSSimplify.cpp b/lib/Sema/CSSimplify.cpp index c2004954f6ec2..a4fb30f5c3fc0 100644 --- a/lib/Sema/CSSimplify.cpp +++ b/lib/Sema/CSSimplify.cpp @@ -2229,7 +2229,7 @@ ConstraintSystem::matchDeepEqualityTypes(Type type1, Type type2, SmallVector mismatches; auto result = matchDeepTypeArguments( - *this, subflags, args1, args2, locator, + *this, subflags | TMF_ApplyingFix, args1, args2, locator, [&mismatches](unsigned position) { mismatches.push_back(position); }); if (mismatches.empty()) @@ -4270,6 +4270,24 @@ bool ConstraintSystem::repairFailures( break; } + case ConstraintLocator::GenericArgument: { + // If any of the types is a hole, consider it fixed. + if (lhs->isHole() || rhs->isHole()) + return true; + + // Ignoring the generic argument because we may have a generic requirement + // failure e.g. `String bind T.Element`, so let's drop the generic argument + // path element and recurse in repairFailures to check and potentially + // record the requirement failure fix. + path.pop_back(); + + if (path.empty() || !path.back().is()) + break; + + return repairFailures(lhs, rhs, matchKind, conversionsOrFixes, + getConstraintLocator(anchor, path)); + } + default: break; } From 82d95ecebc361e2e5d7555070ef9c0d8adf63f47 Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Tue, 21 Jul 2020 00:23:31 -0300 Subject: [PATCH 2/2] [tests] Adding regression tests for SR-13226 --- test/Generics/sr13226.swift | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 test/Generics/sr13226.swift diff --git a/test/Generics/sr13226.swift b/test/Generics/sr13226.swift new file mode 100644 index 0000000000000..aa03ef58df038 --- /dev/null +++ b/test/Generics/sr13226.swift @@ -0,0 +1,21 @@ +// RUN: %target-typecheck-verify-swift +struct W {} + +struct S { + init(){} + // expected-note@+2 {{where 'C1.Element' = 'String', 'W' = 'Int'}} + // expected-note@+1 {{where 'C1.Element' = 'C1', 'W' = 'C2.Element'}} + init(_ c2: W) where C2: Collection, C1.Element == W {} + // expected-note@+1 {{where 'C1.Element' = 'String', 'W' = 'Int'}} + static func f(_ c2: W) where C2: Collection, C1.Element == W {} + // expected-note@+1 {{where 'C1.Element' = 'String', 'W' = 'Int'}} + func instancef(_ c2: W) where C2: Collection, C1.Element == W {} +} +let _ = S<[W]>(W<[Int]>()) // expected-error{{initializer 'init(_:)' requires the types 'String' and 'Int' be equivalent}} +let _ = S<[W]>.f(W<[Int]>()) // expected-error{{static method 'f' requires the types 'String' and 'Int' be equivalent}} +let _ = S<[W]>().instancef(W<[Int]>()) // expected-error{{instance method 'instancef' requires the types 'String' and 'Int' be equivalent}} + +// Archetypes requirement failure +func genericFunc(_ c2: W, c1: C1.Type) where C1.Element == W { + let _ = S<[W]>(W()) // expected-error{{initializer 'init(_:)' requires the types 'C1' and 'C2.Element' be equivalent}} +}