Skip to content

Commit f76db4b

Browse files
committed
[ConstraintSystem] Fix a crash related to function conversion reliant on conditional conformance
1 parent 7002b14 commit f76db4b

File tree

8 files changed

+48
-12
lines changed

8 files changed

+48
-12
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ ProtocolConformance *RequirementFailure::getConformanceForConditionalReq(
175175
return nullptr;
176176
}
177177

178-
auto *typeReqLoc = cs.getConstraintLocator(getRawAnchor(), path.drop_back(),
179-
/*summaryFlags=*/0);
178+
auto *typeReqLoc = getConstraintLocator(getRawAnchor(), path.drop_back());
180179

181180
auto result = llvm::find_if(
182181
cs.CheckedConformances,
@@ -1758,8 +1757,7 @@ bool MissingCallFailure::diagnoseAsError() {
17581757

17591758
case ConstraintLocator::AutoclosureResult: {
17601759
auto &cs = getConstraintSystem();
1761-
auto loc = cs.getConstraintLocator(getRawAnchor(), path.drop_back(),
1762-
/*summaryFlags=*/0);
1760+
auto loc = getConstraintLocator(getRawAnchor(), path.drop_back());
17631761
AutoClosureForwardingFailure failure(cs, loc);
17641762
return failure.diagnoseAsError();
17651763
}

lib/Sema/CSDiagnostics.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ class FailureDiagnostic {
145145
return nullptr;
146146
}
147147

148+
/// Retrive the constraint locator for the given anchor and
149+
/// path, uniqued and automatically calculate the summary flags
150+
ConstraintLocator *
151+
getConstraintLocator(Expr *anchor,
152+
ArrayRef<ConstraintLocator::PathElement> path) {
153+
return CS.getConstraintLocator(anchor, path);
154+
}
155+
148156
/// \returns true is locator hasn't been simplified down to expression.
149157
bool hasComplexLocator() const { return HasComplexLocator; }
150158

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,13 +2001,11 @@ static ConstraintFix *fixRequirementFailure(ConstraintSystem &cs, Type type1,
20012001
auto reqPath = path.drop_back();
20022002
// If underlying conformance requirement has been fixed,
20032003
// then there is no reason to fix up conditional requirements.
2004-
if (cs.hasFixFor(cs.getConstraintLocator(anchor, reqPath,
2005-
/*summaryFlags=*/0)))
2004+
if (cs.hasFixFor(cs.getConstraintLocator(anchor, reqPath)))
20062005
return nullptr;
20072006
}
20082007

2009-
auto *reqLoc = cs.getConstraintLocator(anchor, path,
2010-
/*summaryFlags=*/0);
2008+
auto *reqLoc = cs.getConstraintLocator(anchor, path);
20112009

20122010
auto reqKind = static_cast<RequirementKind>(req.getValue2());
20132011
switch (reqKind) {
@@ -2169,8 +2167,7 @@ bool ConstraintSystem::repairFailures(
21692167
getASTContext().TheEmptyTupleType);
21702168
conversionsOrFixes.push_back(AddMissingArguments::create(
21712169
*this, fnType, {FunctionType::Param(*arg)},
2172-
getConstraintLocator(anchor, path,
2173-
/*summaryFlags=*/0)));
2170+
getConstraintLocator(anchor, path)));
21742171
}
21752172
break;
21762173
}

lib/Sema/CSStep.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ StepResult ComponentStep::take(bool prevFailed) {
313313
// Let's drop `generic parameter '...'` part of the locator to
314314
// group all of the missing generic parameters related to the
315315
// same path together.
316-
defaultableGenericParams[CS.getConstraintLocator(anchor, path.drop_back(),
317-
/*summaryFlags=*/0)]
316+
defaultableGenericParams[CS.getConstraintLocator(anchor,
317+
path.drop_back())]
318318
.push_back(locator->getGenericParameter());
319319
}
320320

lib/Sema/ConstraintSystem.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,12 @@ getAlternativeLiteralTypes(KnownProtocolKind kind) {
374374
return *AlternativeLiteralTypes[index];
375375
}
376376

377+
ConstraintLocator *ConstraintSystem::getConstraintLocator(
378+
Expr *anchor, ArrayRef<ConstraintLocator::PathElement> path) {
379+
auto summaryFlags = ConstraintLocator::getSummaryFlagsForPath(path);
380+
return getConstraintLocator(anchor, path, summaryFlags);
381+
}
382+
377383
ConstraintLocator *ConstraintSystem::getConstraintLocator(
378384
Expr *anchor,
379385
ArrayRef<ConstraintLocator::PathElement> path,

lib/Sema/ConstraintSystem.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,12 @@ class ConstraintSystem {
18881888
ArrayRef<ConstraintLocator::PathElement> path,
18891889
unsigned summaryFlags);
18901890

1891+
/// Retrive the constraint locator for the given anchor and
1892+
/// path, uniqued and automatically infer the summary flags
1893+
ConstraintLocator *
1894+
getConstraintLocator(Expr *anchor,
1895+
ArrayRef<ConstraintLocator::PathElement> path);
1896+
18911897
/// Retrieve the constraint locator for the given anchor and
18921898
/// an empty path, uniqued.
18931899
ConstraintLocator *getConstraintLocator(Expr *anchor) {

test/Generics/conditional_conformances.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,14 @@ extension BinaryInteger {
402402
// expected-error@-1 {{referencing instance method 'reduce' on 'ClosedRange' requires that 'Self.Stride' conform to 'SignedInteger'}}
403403
}
404404
}
405+
406+
// SR-10992
407+
408+
protocol SR_10992_P {}
409+
struct SR_10992_S<T> {}
410+
extension SR_10992_S: SR_10992_P where T: SR_10992_P {} // expected-note {{requirement from conditional conformance of 'SR_10992_S<String>' to 'SR_10992_P'}}
411+
412+
func sr_10992_foo(_ fn: (SR_10992_S<String>) -> Void) {}
413+
func sr_10992_bar(_ fn: (SR_10992_P) -> Void) {
414+
sr_10992_foo(fn) // expected-error {{global function 'sr_10992_foo' requires that 'String' conform to 'SR_10992_P'}}
415+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: not %target-swift-frontend -typecheck %s
2+
3+
protocol P {}
4+
struct S<T> {}
5+
extension S : P where T : P {}
6+
7+
func foo(_ fn: (S<String>) -> Void) {}
8+
func bar(_ fn: (P) -> Void) {
9+
foo(fn)
10+
}

0 commit comments

Comments
 (0)