Skip to content

Commit d470fc2

Browse files
rudkxtkremenek
authored andcommitted
Do not allow Any to satisfy more specific same-type constraints. (#3856)
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.
1 parent 8cb3581 commit d470fc2

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,19 +1885,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
18851885
// we hit commit_to_conversions below, but we have to add a token restriction
18861886
// to ensure we wrap the metatype value in a metatype erasure.
18871887
if (concrete && type2->isExistentialType()) {
1888-
1889-
// If we're binding to an empty existential, we need to make sure that the
1890-
// conversion is valid.
1891-
if (kind == TypeMatchKind::BindType &&
1892-
type2->isEmptyExistentialComposition()) {
1893-
1894-
conversionsOrFixes.push_back(ConversionRestrictionKind::Existential);
1895-
addConstraint(ConstraintKind::SelfObjectOfProtocol,
1896-
type1, type2, getConstraintLocator(locator));
1897-
1898-
return SolutionKind::Solved;
1899-
}
1900-
19011888
if (kind == TypeMatchKind::ConformsTo) {
19021889
conversionsOrFixes.push_back(ConversionRestrictionKind::
19031890
MetatypeToExistentialMetatype);

test/Constraints/generics.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,20 @@ Whatever.foo(a: 23) // expected-error {{generic parameter 'A' could not be infer
236236
// <rdar://problem/21718955> Swift useless error: cannot invoke 'foo' with no arguments
237237
Whatever.bar() // expected-error {{generic parameter 'A' could not be inferred}}
238238

239+
// <rdar://problem/27515965> Type checker doesn't enforce same-type constraint if associated type is Any
240+
protocol P27515965 {
241+
associatedtype R
242+
func f() -> R
243+
}
244+
245+
struct S27515965 : P27515965 {
246+
func f() -> Any { return self }
247+
}
248+
249+
struct V27515965 {
250+
init<T : P27515965>(_ tp: T) where T.R == Float {}
251+
}
252+
253+
func test(x: S27515965) -> V27515965 {
254+
return V27515965(x) // expected-error {{generic parameter 'T' could not be inferred}}
255+
}

0 commit comments

Comments
 (0)