Skip to content

Commit bf09fca

Browse files
authored
Merge pull request #25739 from theblixguy/fix/SR-10992-5.1
[5.1] [ConstraintSystem] Fix crash on function conversion reliant on conditional conformance
2 parents 0c2f0c4 + 3902cbc commit bf09fca

File tree

8 files changed

+49
-14
lines changed

8 files changed

+49
-14
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ ProtocolConformance *RequirementFailure::getConformanceForConditionalReq(
151151
return nullptr;
152152
}
153153

154-
auto *typeReqLoc = cs.getConstraintLocator(getRawAnchor(), path.drop_back(),
155-
/*summaryFlags=*/0);
154+
auto *typeReqLoc = getConstraintLocator(getRawAnchor(), path.drop_back());
156155

157156
auto result = llvm::find_if(
158157
cs.CheckedConformances,
@@ -1674,8 +1673,7 @@ bool MissingCallFailure::diagnoseAsError() {
16741673

16751674
case ConstraintLocator::AutoclosureResult: {
16761675
auto &cs = getConstraintSystem();
1677-
auto loc = cs.getConstraintLocator(getRawAnchor(), path.drop_back(),
1678-
/*summaryFlags=*/0);
1676+
auto loc = getConstraintLocator(getRawAnchor(), path.drop_back());
16791677
AutoClosureForwardingFailure failure(cs, loc);
16801678
return failure.diagnoseAsError();
16811679
}

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) const {
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: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,13 +2000,11 @@ static ConstraintFix *fixRequirementFailure(ConstraintSystem &cs, Type type1,
20002000
auto reqPath = path.drop_back();
20012001
// If underlying conformance requirement has been fixed,
20022002
// then there is no reason to fix up conditional requirements.
2003-
if (cs.hasFixFor(cs.getConstraintLocator(anchor, reqPath,
2004-
/*summaryFlags=*/0)))
2003+
if (cs.hasFixFor(cs.getConstraintLocator(anchor, reqPath)))
20052004
return nullptr;
20062005
}
20072006

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

20112009
auto reqKind = static_cast<RequirementKind>(req.getValue2());
20122010
switch (reqKind) {
@@ -2168,8 +2166,7 @@ bool ConstraintSystem::repairFailures(
21682166
getASTContext().TheEmptyTupleType);
21692167
conversionsOrFixes.push_back(AddMissingArguments::create(
21702168
*this, fnType, {FunctionType::Param(*arg)},
2171-
getConstraintLocator(anchor, path,
2172-
/*summaryFlags=*/0)));
2169+
getConstraintLocator(anchor, path)));
21732170
}
21742171
break;
21752172
}
@@ -3439,8 +3436,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
34393436
auto reqPath = ArrayRef<LocatorPathElt>(path).drop_back();
34403437
// Underlying conformance requirement is itself fixed,
34413438
// this wouldn't lead to a right solution.
3442-
if (hasFixFor(getConstraintLocator(anchor, reqPath,
3443-
/*summaryFlags=*/0)))
3439+
if (hasFixFor(getConstraintLocator(anchor, reqPath)))
34443440
return SolutionKind::Error;
34453441
}
34463442

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
@@ -1878,6 +1878,12 @@ class ConstraintSystem {
18781878
ArrayRef<ConstraintLocator::PathElement> path,
18791879
unsigned summaryFlags);
18801880

1881+
/// Retrive the constraint locator for the given anchor and
1882+
/// path, uniqued and automatically infer the summary flags
1883+
ConstraintLocator *
1884+
getConstraintLocator(Expr *anchor,
1885+
ArrayRef<ConstraintLocator::PathElement> path);
1886+
18811887
/// Retrieve the constraint locator for the given anchor and
18821888
/// an empty path, uniqued.
18831889
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)