Skip to content

Commit b72b3cb

Browse files
authored
Merge pull request #25721 from theblixguy/fix/SR-10992
[ConstraintSystem] Fix crash on function conversion reliant on conditional conformance
2 parents 1b5e579 + 2e31ca6 commit b72b3cb

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
@@ -304,8 +304,7 @@ ProtocolConformance *RequirementFailure::getConformanceForConditionalReq(
304304
return nullptr;
305305
}
306306

307-
auto *typeReqLoc = cs.getConstraintLocator(getRawAnchor(), path.drop_back(),
308-
/*summaryFlags=*/0);
307+
auto *typeReqLoc = getConstraintLocator(getRawAnchor(), path.drop_back());
309308

310309
auto result = llvm::find_if(
311310
cs.CheckedConformances,
@@ -1926,8 +1925,7 @@ bool MissingCallFailure::diagnoseAsError() {
19261925

19271926
case ConstraintLocator::AutoclosureResult: {
19281927
auto &cs = getConstraintSystem();
1929-
auto loc = cs.getConstraintLocator(getRawAnchor(), path.drop_back(),
1930-
/*summaryFlags=*/0);
1928+
auto loc = getConstraintLocator(getRawAnchor(), path.drop_back());
19311929
AutoClosureForwardingFailure failure(cs, loc);
19321930
return failure.diagnoseAsError();
19331931
}

lib/Sema/CSDiagnostics.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ class FailureDiagnostic {
153153
return nullptr;
154154
}
155155

156+
/// Retrive the constraint locator for the given anchor and
157+
/// path, uniqued and automatically calculate the summary flags
158+
ConstraintLocator *
159+
getConstraintLocator(Expr *anchor,
160+
ArrayRef<ConstraintLocator::PathElement> path) {
161+
return CS.getConstraintLocator(anchor, path);
162+
}
163+
156164
/// \returns true is locator hasn't been simplified down to expression.
157165
bool hasComplexLocator() const { return HasComplexLocator; }
158166

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,13 +2054,11 @@ static ConstraintFix *fixRequirementFailure(ConstraintSystem &cs, Type type1,
20542054
auto reqPath = path.drop_back();
20552055
// If underlying conformance requirement has been fixed,
20562056
// then there is no reason to fix up conditional requirements.
2057-
if (cs.hasFixFor(cs.getConstraintLocator(anchor, reqPath,
2058-
/*summaryFlags=*/0)))
2057+
if (cs.hasFixFor(cs.getConstraintLocator(anchor, reqPath)))
20592058
return nullptr;
20602059
}
20612060

2062-
auto *reqLoc = cs.getConstraintLocator(anchor, path,
2063-
/*summaryFlags=*/0);
2061+
auto *reqLoc = cs.getConstraintLocator(anchor, path);
20642062

20652063
auto reqKind = static_cast<RequirementKind>(req.getValue2());
20662064
switch (reqKind) {
@@ -2232,8 +2230,7 @@ bool ConstraintSystem::repairFailures(
22322230
getASTContext().TheEmptyTupleType);
22332231
conversionsOrFixes.push_back(AddMissingArguments::create(
22342232
*this, fnType, {FunctionType::Param(*arg)},
2235-
getConstraintLocator(anchor, path,
2236-
/*summaryFlags=*/0)));
2233+
getConstraintLocator(anchor, path)));
22372234
}
22382235
break;
22392236
}

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
@@ -1932,6 +1932,12 @@ class ConstraintSystem {
19321932
ArrayRef<ConstraintLocator::PathElement> path,
19331933
unsigned summaryFlags);
19341934

1935+
/// Retrive the constraint locator for the given anchor and
1936+
/// path, uniqued and automatically infer the summary flags
1937+
ConstraintLocator *
1938+
getConstraintLocator(Expr *anchor,
1939+
ArrayRef<ConstraintLocator::PathElement> path);
1940+
19351941
/// Retrieve the constraint locator for the given anchor and
19361942
/// an empty path, uniqued.
19371943
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)