Skip to content

[Constraint solver] Do not allow unavailable decls to be favored. #10147

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 1 commit into from
Jun 6, 2017
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
7 changes: 4 additions & 3 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2980,8 +2980,8 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
}
}
}
// If the invocation's argument expression has a favored constraint,

// If the invocation's argument expression has a favored type,
// use that information to determine whether a specific overload for
// the initializer should be favored.
if (favoredType && result.FavoredChoice == ~0U) {
Expand All @@ -2996,7 +2996,8 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
argType = ctor.Decl->getInnermostDeclContext()
->mapTypeIntoContext(argType);
if (argType->isEqual(favoredType))
result.FavoredChoice = result.ViableCandidates.size();
if (!ctor->getAttrs().isUnavailable(getASTContext()))
result.FavoredChoice = result.ViableCandidates.size();
}
}
}
Expand Down
17 changes: 14 additions & 3 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2338,11 +2338,21 @@ bool ConstraintSystem::solveRec(SmallVectorImpl<Solution> &solutions,
/// Whether we should short-circuit a disjunction that already has a
/// solution when we encounter the given constraint.
static bool shortCircuitDisjunctionAt(Constraint *constraint,
Constraint *successfulConstraint) {

Constraint *successfulConstraint,
ASTContext &ctx) {

// If the successfully applied constraint is favored, we'll consider that to
// be the "best".
if (successfulConstraint->isFavored() && !constraint->isFavored()) {
#if !defined(NDEBUG)
if (successfulConstraint->getKind() == ConstraintKind::BindOverload) {
auto overloadChoice = successfulConstraint->getOverloadChoice();
assert((!overloadChoice.isDecl() ||
!overloadChoice.getDecl()->getAttrs().isUnavailable(ctx)) &&
"Unavailable decl should not be favored!");
}
#endif

return true;
}

Expand Down Expand Up @@ -2533,7 +2543,8 @@ bool ConstraintSystem::solveSimplified(
// short-circuit the disjunction.
if (lastSolvedChoice) {
auto *lastChoice = lastSolvedChoice->getConstraint();
if (shortCircuitDisjunctionAt(&currentChoice, lastChoice))
if (shortCircuitDisjunctionAt(&currentChoice, lastChoice,
getASTContext()))
break;
}

Expand Down
6 changes: 5 additions & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,11 @@ void ConstraintSystem::addOverloadSet(Type boundType,
*favoredChoice,
useDC,
locator);


assert((!favoredChoice->isDecl() ||
!favoredChoice->getDecl()->getAttrs().isUnavailable(
getASTContext())) &&
"Cannot make unavailable decl favored!");
bindOverloadConstraint->setFavored();

overloads.push_back(bindOverloadConstraint);
Expand Down
17 changes: 17 additions & 0 deletions test/Constraints/availability.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-typecheck-verify-swift -swift-version 4

// Ensure that we do not select the unavailable failable init as the
// only solution and then fail to typecheck.
protocol P {}

class C : P {
@available(swift, obsoleted: 4)
public init?(_ c: C) {
}

public init<T : P>(_ c: T) {}
}

func f(c: C) {
let _: C? = C(c)
}