Skip to content

Commit 5424797

Browse files
[Sema] Make constraint restriction to store TypeBase * instead of Type
1 parent 4e689bb commit 5424797

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

include/swift/Sema/ConstraintSystem.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -2180,6 +2180,10 @@ class SolutionApplicationTarget {
21802180
using RewriteTargetFn = std::function<
21812181
Optional<SolutionApplicationTarget> (SolutionApplicationTarget)>;
21822182

2183+
/// Represents a conversion restriction between two types.
2184+
using ConversionRestriction =
2185+
std::tuple<TypeBase *, TypeBase *, ConversionRestrictionKind>;
2186+
21832187
enum class ConstraintSystemPhase {
21842188
ConstraintGeneration,
21852189
Solving,
@@ -2364,8 +2368,7 @@ class ConstraintSystem {
23642368
/// there are multiple ways in which one type could convert to another, e.g.,
23652369
/// given class types A and B, the solver might choose either a superclass
23662370
/// conversion or a user-defined conversion.
2367-
std::vector<std::tuple<Type, Type, ConversionRestrictionKind>>
2368-
ConstraintRestrictions;
2371+
std::vector<ConversionRestriction> ConstraintRestrictions;
23692372

23702373
/// The set of fixes applied to make the solution work.
23712374
llvm::SmallVector<ConstraintFix *, 4> Fixes;

lib/Sema/CSSimplify.cpp

+9-14
Original file line numberDiff line numberDiff line change
@@ -3902,9 +3902,8 @@ bool ConstraintSystem::repairFailures(
39023902
// If the result type of the coercion has an value to optional conversion
39033903
// we can instead suggest the conditional downcast as it is safer in
39043904
// situations like conditional binding.
3905-
auto useConditionalCast = llvm::any_of(
3906-
ConstraintRestrictions,
3907-
[&](std::tuple<Type, Type, ConversionRestrictionKind> restriction) {
3905+
auto useConditionalCast =
3906+
llvm::any_of(ConstraintRestrictions, [&](auto &restriction) {
39083907
ConversionRestrictionKind restrictionKind;
39093908
Type type1, type2;
39103909
std::tie(type1, type2, restrictionKind) = restriction;
@@ -6710,8 +6709,7 @@ static ConstraintFix *maybeWarnAboutExtraneousCast(
67106709
ConstraintSystem &cs, Type origFromType, Type origToType, Type fromType,
67116710
Type toType, SmallVector<Type, 4> fromOptionals,
67126711
SmallVector<Type, 4> toOptionals,
6713-
const std::vector<std::tuple<Type, Type, ConversionRestrictionKind>>
6714-
&constraintRestrictions,
6712+
const std::vector<ConversionRestriction> &constraintRestrictions,
67156713
ConstraintSystem::TypeMatchOptions flags,
67166714
ConstraintLocatorBuilder locator) {
67176715

@@ -6738,14 +6736,10 @@ static ConstraintFix *maybeWarnAboutExtraneousCast(
67386736
// "from" expression could be a type variable with value-to-optional
67396737
// restrictions that we have to account for optionality mismatch.
67406738
const auto subExprType = cs.getType(castExpr->getSubExpr());
6741-
if (llvm::any_of(constraintRestrictions, [&](auto &entry) {
6742-
Type type1, type2;
6743-
ConversionRestrictionKind kind;
6744-
std::tie(type1, type2, kind) = entry;
6745-
if (kind != ConversionRestrictionKind::ValueToOptional)
6746-
return false;
6747-
return fromType->isEqual(type1) && subExprType->isEqual(type2);
6748-
})) {
6739+
if (llvm::is_contained(
6740+
constraintRestrictions,
6741+
std::make_tuple(fromType.getPointer(), subExprType.getPointer(),
6742+
ConversionRestrictionKind::ValueToOptional))) {
67496743
extraOptionals++;
67506744
origFromType = OptionalType::get(origFromType);
67516745
}
@@ -11401,7 +11395,8 @@ ConstraintSystem::simplifyRestrictedConstraint(
1140111395
addFixConstraint(fix, matchKind, type1, type2, locator);
1140211396
}
1140311397

11404-
ConstraintRestrictions.push_back(std::make_tuple(type1, type2, restriction));
11398+
ConstraintRestrictions.push_back(
11399+
std::make_tuple(type1.getPointer(), type2.getPointer(), restriction));
1140511400
return SolutionKind::Solved;
1140611401
}
1140711402
case SolutionKind::Unsolved:

lib/Sema/CSSolver.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,10 @@ void ConstraintSystem::applySolution(const Solution &solution) {
231231
// Register constraint restrictions.
232232
// FIXME: Copy these directly into some kind of partial solution?
233233
for (auto restriction : solution.ConstraintRestrictions) {
234-
ConstraintRestrictions.push_back(
235-
std::make_tuple(restriction.first.first, restriction.first.second,
236-
restriction.second));
234+
auto &types = restriction.first;
235+
ConstraintRestrictions.push_back(std::make_tuple(types.first.getPointer(),
236+
types.second.getPointer(),
237+
restriction.second));
237238
}
238239

239240
// Register the solution's disjunction choices.

0 commit comments

Comments
 (0)