Skip to content

[ConstraintSystem] Extend solver support for designated types for ope… #19909

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
Oct 16, 2018
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
79 changes: 52 additions & 27 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1629,13 +1629,12 @@ static bool isOperatorBindOverload(Constraint *bindOverload) {
// Given a bind overload constraint for an operator, return the
// protocol designated as the first place to look for overloads of the
// operator.
static NominalTypeDecl *
getOperatorDesignatedNominalType(Constraint *bindOverload) {
static ArrayRef<NominalTypeDecl *>
getOperatorDesignatedNominalTypes(Constraint *bindOverload) {
auto choice = bindOverload->getOverloadChoice();
auto *funcDecl = cast<FuncDecl>(choice.getDecl());
auto *operatorDecl = funcDecl->getOperatorDecl();
auto designatedTypes = operatorDecl->getDesignatedNominalTypes();
return !designatedTypes.empty() ? designatedTypes[0] : nullptr;
return operatorDecl->getDesignatedNominalTypes();
}

void ConstraintSystem::partitionDisjunction(
Expand All @@ -1660,8 +1659,8 @@ void ConstraintSystem::partitionDisjunction(
SmallVector<unsigned, 4> disabled;
SmallVector<unsigned, 4> unavailable;
SmallVector<unsigned, 4> globalScope;
SmallVector<unsigned, 4> definedInDesignatedType;
SmallVector<unsigned, 4> definedInExtensionOfDesignatedType;
SmallVector<SmallVector<unsigned, 4>, 4> definedInDesignatedType;
SmallVector<SmallVector<unsigned, 4>, 4> definedInExtensionOfDesignatedType;
SmallVector<unsigned, 4> everythingElse;
SmallSet<Constraint *, 16> taken;

Expand Down Expand Up @@ -1722,28 +1721,43 @@ void ConstraintSystem::partitionDisjunction(

// Now collect the overload choices that are defined within the type
// that was designated in the operator declaration.
auto *designatedNominal = getOperatorDesignatedNominalType(Choices[0]);
if (designatedNominal) {
forEachChoice(Choices, [&](unsigned index, Constraint *constraint) -> bool {
auto *decl = constraint->getOverloadChoice().getDecl();
auto *funcDecl = cast<FuncDecl>(decl);

auto *parentDecl = funcDecl->getParent()->getAsDecl();
if (parentDecl == designatedNominal) {
definedInDesignatedType.push_back(index);
return true;
}
auto designatedNominalTypes = getOperatorDesignatedNominalTypes(Choices[0]);
if (!designatedNominalTypes.empty()) {
forEachChoice(
Choices, [&](unsigned constraintIndex, Constraint *constraint) -> bool {
auto *decl = constraint->getOverloadChoice().getDecl();
auto *funcDecl = cast<FuncDecl>(decl);

auto *parentDecl = funcDecl->getParent()->getAsDecl();
for (auto designatedTypeIndex : indices(designatedNominalTypes)) {
auto *designatedNominal =
designatedNominalTypes[designatedTypeIndex];
if (parentDecl == designatedNominal) {
if (designatedTypeIndex >= definedInDesignatedType.size())
definedInDesignatedType.resize(designatedTypeIndex + 1);
auto &constraints = definedInDesignatedType[designatedTypeIndex];
constraints.push_back(constraintIndex);
return true;
}

if (auto *extensionDecl = dyn_cast<ExtensionDecl>(parentDecl)) {
parentDecl = extensionDecl->getExtendedNominal();
if (parentDecl == designatedNominal) {
definedInExtensionOfDesignatedType.push_back(index);
return true;
if (auto *extensionDecl = dyn_cast<ExtensionDecl>(parentDecl)) {
parentDecl = extensionDecl->getExtendedNominal();
if (parentDecl == designatedNominal) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use a extract resize + add into a separate function to deduplicate logic here for types and extensions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'll do that in a follow-up.

if (designatedTypeIndex >=
definedInExtensionOfDesignatedType.size())
definedInExtensionOfDesignatedType.resize(
designatedTypeIndex + 1);

auto &constraints =
definedInExtensionOfDesignatedType[designatedTypeIndex];
constraints.push_back(constraintIndex);
return true;
}
}
}
}

return false;
});
return false;
});
}

// Gather the remaining options.
Expand All @@ -1762,8 +1776,19 @@ void ConstraintSystem::partitionDisjunction(
};

// Now create the partitioning based on what was collected.
appendPartition(definedInDesignatedType);
appendPartition(definedInExtensionOfDesignatedType);

// First we'll add partitions for each of the overloads we found in
// types that were designated as part of the operator declaration.
for (auto designatedTypeIndex : indices(designatedNominalTypes)) {
if (designatedTypeIndex < definedInDesignatedType.size()) {
auto &primary = definedInDesignatedType[designatedTypeIndex];
appendPartition(primary);
}
if (designatedTypeIndex < definedInExtensionOfDesignatedType.size()) {
auto &secondary = definedInExtensionOfDesignatedType[designatedTypeIndex];
appendPartition(secondary);
}
}
appendPartition(everythingElse);
appendPartition(globalScope);
appendPartition(unavailable);
Expand Down
10 changes: 5 additions & 5 deletions stdlib/public/core/Policy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,15 @@ infix operator &>> : BitwiseShiftPrecedence, FixedWidthInteger

infix operator * : MultiplicationPrecedence, Numeric
infix operator &* : MultiplicationPrecedence, FixedWidthInteger
infix operator / : MultiplicationPrecedence, BinaryInteger
infix operator / : MultiplicationPrecedence, BinaryInteger, FloatingPoint
infix operator % : MultiplicationPrecedence, BinaryInteger
infix operator & : MultiplicationPrecedence, BinaryInteger

// "Additive"

infix operator + : AdditionPrecedence, Numeric
infix operator + : AdditionPrecedence, Numeric, String, Strideable
infix operator &+ : AdditionPrecedence, FixedWidthInteger
infix operator - : AdditionPrecedence, Numeric
infix operator - : AdditionPrecedence, Numeric, Strideable
infix operator &- : AdditionPrecedence, FixedWidthInteger
infix operator | : AdditionPrecedence, BinaryInteger
infix operator ^ : AdditionPrecedence, BinaryInteger
Expand Down Expand Up @@ -482,9 +482,9 @@ infix operator *= : AssignmentPrecedence, Numeric
infix operator &*= : AssignmentPrecedence, FixedWidthInteger
infix operator /= : AssignmentPrecedence, BinaryInteger
infix operator %= : AssignmentPrecedence, BinaryInteger
infix operator += : AssignmentPrecedence, Numeric
infix operator += : AssignmentPrecedence, Numeric, String, Strideable
infix operator &+= : AssignmentPrecedence, FixedWidthInteger
infix operator -= : AssignmentPrecedence, Numeric
infix operator -= : AssignmentPrecedence, Numeric, Strideable
infix operator &-= : AssignmentPrecedence, FixedWidthInteger
infix operator <<= : AssignmentPrecedence, BinaryInteger
infix operator &<<= : AssignmentPrecedence, FixedWidthInteger
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// RUN: %target-typecheck-verify-swift -solver-expression-time-threshold=1
// RUN: %target-typecheck-verify-swift -solver-expression-time-threshold=1 -swift-version 5 -solver-disable-shrink -disable-constraint-solver-performance-hacks -solver-enable-operator-designated-types
// REQUIRES: tools-release,no_asserts

let i: Int? = 1
let j: Int?
let k: Int? = 2

let _ = [i, j, k].reduce(0 as Int?) {
// expected-error@-1 {{reasonable time}}
$0 != nil && $1 != nil ? $0! + $1! : ($0 != nil ? $0! : ($1 != nil ? $1! : nil))
}