Skip to content

Commit 5e08867

Browse files
authored
Merge pull request #25223 from xedin/rdar-50099849-5.1
[5.1][TypeChecker] Don't try to validate generic requirements when unbound…
2 parents 08d327e + 8f4bc18 commit 5e08867

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,10 @@ Type TypeChecker::applyUnboundGenericArguments(
800800
// generic arguments.
801801
auto resultType = decl->getDeclaredInterfaceType();
802802

803-
bool hasTypeVariable = false;
803+
// If types involved in requirements check have either type variables
804+
// or unbound generics, let's skip the check here, and let the solver
805+
// do it when missing types are deduced.
806+
bool skipRequirementsCheck = false;
804807

805808
// Get the substitutions for outer generic parameters from the parent
806809
// type.
@@ -817,7 +820,7 @@ Type TypeChecker::applyUnboundGenericArguments(
817820
}
818821

819822
subs = parentType->getContextSubstitutions(decl->getDeclContext());
820-
hasTypeVariable |= parentType->hasTypeVariable();
823+
skipRequirementsCheck |= parentType->hasTypeVariable();
821824
}
822825

823826
SourceLoc noteLoc = decl->getLoc();
@@ -834,13 +837,14 @@ Type TypeChecker::applyUnboundGenericArguments(
834837
subs[origTy->getCanonicalType()->castTo<GenericTypeParamType>()] =
835838
substTy;
836839

837-
hasTypeVariable |= substTy->hasTypeVariable();
840+
skipRequirementsCheck |=
841+
substTy->hasTypeVariable() || substTy->hasUnboundGenericType();
838842
}
839843

840844
// Check the generic arguments against the requirements of the declaration's
841845
// generic signature.
842846
auto dc = resolution.getDeclContext();
843-
if (!hasTypeVariable &&
847+
if (!skipRequirementsCheck &&
844848
resolution.getStage() > TypeResolutionStage::Structural) {
845849
auto result =
846850
checkGenericArguments(dc, loc, noteLoc, unboundType,

test/Constraints/generics.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,6 @@ class GenericClass<A> {}
435435
func genericFunc<T>(t: T) {
436436
_ = [T: GenericClass] // expected-error {{generic parameter 'A' could not be inferred}}
437437
// expected-note@-1 {{explicitly specify the generic arguments to fix this issue}}
438-
// expected-error@-2 3 {{type 'T' does not conform to protocol 'Hashable'}}
439438
}
440439

441440
struct SR_3525<T> {}

test/Constraints/protocols.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,27 @@ func testClonableExistential(_ v: Clonable, _ vv: Clonable.Type) {
363363
let _ = v.veryBadClonerFn() // expected-error {{member 'veryBadClonerFn' cannot be used on value of protocol type 'Clonable'; use a generic constraint instead}}
364364

365365
}
366+
367+
368+
// rdar://problem/50099849
369+
370+
protocol Trivial {
371+
associatedtype T
372+
}
373+
374+
func rdar_50099849() {
375+
struct A : Trivial {
376+
typealias T = A
377+
}
378+
379+
struct B<C : Trivial> : Trivial { // expected-note {{'C' declared as parameter to type 'B'}}
380+
typealias T = C.T
381+
}
382+
383+
struct C<W: Trivial, Z: Trivial> : Trivial where W.T == Z.T {
384+
typealias T = W.T
385+
}
386+
387+
let _ = C<A, B>() // expected-error {{generic parameter 'C' could not be inferred}}
388+
// expected-note@-1 {{explicitly specify the generic arguments to fix this issue}} {{17-17=<<#C: Trivial#>>}}
389+
}

0 commit comments

Comments
 (0)