Skip to content

[5.1] [ConstraintSystem] Fix crash on function conversion reliant on conditional conformance #25739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ ProtocolConformance *RequirementFailure::getConformanceForConditionalReq(
return nullptr;
}

auto *typeReqLoc = cs.getConstraintLocator(getRawAnchor(), path.drop_back(),
/*summaryFlags=*/0);
auto *typeReqLoc = getConstraintLocator(getRawAnchor(), path.drop_back());

auto result = llvm::find_if(
cs.CheckedConformances,
Expand Down Expand Up @@ -1674,8 +1673,7 @@ bool MissingCallFailure::diagnoseAsError() {

case ConstraintLocator::AutoclosureResult: {
auto &cs = getConstraintSystem();
auto loc = cs.getConstraintLocator(getRawAnchor(), path.drop_back(),
/*summaryFlags=*/0);
auto loc = getConstraintLocator(getRawAnchor(), path.drop_back());
AutoClosureForwardingFailure failure(cs, loc);
return failure.diagnoseAsError();
}
Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ class FailureDiagnostic {
return nullptr;
}

/// Retrive the constraint locator for the given anchor and
/// path, uniqued and automatically calculate the summary flags
ConstraintLocator *
getConstraintLocator(Expr *anchor,
ArrayRef<ConstraintLocator::PathElement> path) const {
return CS.getConstraintLocator(anchor, path);
}

/// \returns true is locator hasn't been simplified down to expression.
bool hasComplexLocator() const { return HasComplexLocator; }

Expand Down
12 changes: 4 additions & 8 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2000,13 +2000,11 @@ static ConstraintFix *fixRequirementFailure(ConstraintSystem &cs, Type type1,
auto reqPath = path.drop_back();
// If underlying conformance requirement has been fixed,
// then there is no reason to fix up conditional requirements.
if (cs.hasFixFor(cs.getConstraintLocator(anchor, reqPath,
/*summaryFlags=*/0)))
if (cs.hasFixFor(cs.getConstraintLocator(anchor, reqPath)))
return nullptr;
}

auto *reqLoc = cs.getConstraintLocator(anchor, path,
/*summaryFlags=*/0);
auto *reqLoc = cs.getConstraintLocator(anchor, path);

auto reqKind = static_cast<RequirementKind>(req.getValue2());
switch (reqKind) {
Expand Down Expand Up @@ -2168,8 +2166,7 @@ bool ConstraintSystem::repairFailures(
getASTContext().TheEmptyTupleType);
conversionsOrFixes.push_back(AddMissingArguments::create(
*this, fnType, {FunctionType::Param(*arg)},
getConstraintLocator(anchor, path,
/*summaryFlags=*/0)));
getConstraintLocator(anchor, path)));
}
break;
}
Expand Down Expand Up @@ -3439,8 +3436,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
auto reqPath = ArrayRef<LocatorPathElt>(path).drop_back();
// Underlying conformance requirement is itself fixed,
// this wouldn't lead to a right solution.
if (hasFixFor(getConstraintLocator(anchor, reqPath,
/*summaryFlags=*/0)))
if (hasFixFor(getConstraintLocator(anchor, reqPath)))
return SolutionKind::Error;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/CSStep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ StepResult ComponentStep::take(bool prevFailed) {
// Let's drop `generic parameter '...'` part of the locator to
// group all of the missing generic parameters related to the
// same path together.
defaultableGenericParams[CS.getConstraintLocator(anchor, path.drop_back(),
/*summaryFlags=*/0)]
defaultableGenericParams[CS.getConstraintLocator(anchor,
path.drop_back())]
.push_back(locator->getGenericParameter());
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,12 @@ getAlternativeLiteralTypes(KnownProtocolKind kind) {
return *AlternativeLiteralTypes[index];
}

ConstraintLocator *ConstraintSystem::getConstraintLocator(
Expr *anchor, ArrayRef<ConstraintLocator::PathElement> path) {
auto summaryFlags = ConstraintLocator::getSummaryFlagsForPath(path);
return getConstraintLocator(anchor, path, summaryFlags);
}

ConstraintLocator *ConstraintSystem::getConstraintLocator(
Expr *anchor,
ArrayRef<ConstraintLocator::PathElement> path,
Expand Down
6 changes: 6 additions & 0 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,12 @@ class ConstraintSystem {
ArrayRef<ConstraintLocator::PathElement> path,
unsigned summaryFlags);

/// Retrive the constraint locator for the given anchor and
/// path, uniqued and automatically infer the summary flags
ConstraintLocator *
getConstraintLocator(Expr *anchor,
ArrayRef<ConstraintLocator::PathElement> path);

/// Retrieve the constraint locator for the given anchor and
/// an empty path, uniqued.
ConstraintLocator *getConstraintLocator(Expr *anchor) {
Expand Down
11 changes: 11 additions & 0 deletions test/Generics/conditional_conformances.swift
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,14 @@ extension BinaryInteger {
// expected-error@-1 {{referencing instance method 'reduce' on 'ClosedRange' requires that 'Self.Stride' conform to 'SignedInteger'}}
}
}

// SR-10992

protocol SR_10992_P {}
struct SR_10992_S<T> {}
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'}}

func sr_10992_foo(_ fn: (SR_10992_S<String>) -> Void) {}
func sr_10992_bar(_ fn: (SR_10992_P) -> Void) {
sr_10992_foo(fn) // expected-error {{global function 'sr_10992_foo' requires that 'String' conform to 'SR_10992_P'}}
}
10 changes: 10 additions & 0 deletions validation-test/compiler_crashers_2_fixed/sr10992.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: not %target-swift-frontend -typecheck %s

protocol P {}
struct S<T> {}
extension S : P where T : P {}

func foo(_ fn: (S<String>) -> Void) {}
func bar(_ fn: (P) -> Void) {
foo(fn)
}