From db04731900dd22b74d7bb76c8da808eb49005cc7 Mon Sep 17 00:00:00 2001 From: Mark Lacey Date: Thu, 28 Jul 2016 17:40:24 -0700 Subject: [PATCH] Do not allow Any to satisfy more specific same-type constraints. For associated types inferred to be Any, we were allowing the type to satisfy more specific same-type constraints, e.g. Element == Character (where Element is the associated type). This is clearly wrong. The fix here is very specific to empty protocol compositions, and removes some code in matchTypes() that doesn't make a lot of sense. Looking back at the history, this was added in a commit that made a handful of other changes, and it's not clear this particular change was important for the issues that commit claimed to fix (and in fact removing this regresses no tests). Fixes rdar://problem/27515965. --- lib/Sema/CSSimplify.cpp | 13 ------------- test/Constraints/generics.swift | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/Sema/CSSimplify.cpp b/lib/Sema/CSSimplify.cpp index 1474285e42566..a2af916c27b9a 100644 --- a/lib/Sema/CSSimplify.cpp +++ b/lib/Sema/CSSimplify.cpp @@ -1885,19 +1885,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind, // we hit commit_to_conversions below, but we have to add a token restriction // to ensure we wrap the metatype value in a metatype erasure. if (concrete && type2->isExistentialType()) { - - // If we're binding to an empty existential, we need to make sure that the - // conversion is valid. - if (kind == TypeMatchKind::BindType && - type2->isEmptyExistentialComposition()) { - - conversionsOrFixes.push_back(ConversionRestrictionKind::Existential); - addConstraint(ConstraintKind::SelfObjectOfProtocol, - type1, type2, getConstraintLocator(locator)); - - return SolutionKind::Solved; - } - if (kind == TypeMatchKind::ConformsTo) { conversionsOrFixes.push_back(ConversionRestrictionKind:: MetatypeToExistentialMetatype); diff --git a/test/Constraints/generics.swift b/test/Constraints/generics.swift index 94d8d172650f4..5c3ac77c42ccc 100644 --- a/test/Constraints/generics.swift +++ b/test/Constraints/generics.swift @@ -236,3 +236,20 @@ Whatever.foo(a: 23) // expected-error {{generic parameter 'A' could not be infer // Swift useless error: cannot invoke 'foo' with no arguments Whatever.bar() // expected-error {{generic parameter 'A' could not be inferred}} +// Type checker doesn't enforce same-type constraint if associated type is Any +protocol P27515965 { + associatedtype R + func f() -> R +} + +struct S27515965 : P27515965 { + func f() -> Any { return self } +} + +struct V27515965 { + init(_ tp: T) where T.R == Float {} +} + +func test(x: S27515965) -> V27515965 { + return V27515965(x) // expected-error {{generic parameter 'T' could not be inferred}} +}