Skip to content

Commit c0af43a

Browse files
authored
Merge pull request #13197 from rudkx/rework-iuo
Some small constraint system cleanups.
2 parents f324011 + 6cebc92 commit c0af43a

File tree

6 files changed

+63
-72
lines changed

6 files changed

+63
-72
lines changed

lib/Sema/CSApply.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7514,9 +7514,6 @@ bool ConstraintSystem::applySolutionFix(Expr *expr,
75147514
}
75157515

75167516
switch (fix.first.getKind()) {
7517-
case FixKind::None:
7518-
llvm_unreachable("no-fix marker should never make it into solution");
7519-
75207517
case FixKind::ForceOptional: {
75217518
const Expr *unwrapped = affected->getValueProvidingExpr();
75227519
auto type = solution.simplifyType(getType(affected))

lib/Sema/CSGen.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -916,18 +916,6 @@ namespace {
916916
}
917917
}
918918

919-
// If the paren expr has a favored type, and the subExpr doesn't,
920-
// propagate downwards. Otherwise, propagate upwards.
921-
if (auto parenExpr = dyn_cast<ParenExpr>(expr)) {
922-
if (!CS.getFavoredType(parenExpr->getSubExpr())) {
923-
CS.setFavoredType(parenExpr->getSubExpr(),
924-
CS.getFavoredType(parenExpr));
925-
} else if (!CS.getFavoredType(parenExpr)) {
926-
CS.setFavoredType(parenExpr,
927-
CS.getFavoredType(parenExpr->getSubExpr()));
928-
}
929-
}
930-
931919
return { true, expr };
932920
}
933921

@@ -1619,10 +1607,6 @@ namespace {
16191607
}
16201608

16211609
virtual Type visitParenExpr(ParenExpr *expr) {
1622-
if (auto favoredTy = CS.getFavoredType(expr->getSubExpr())) {
1623-
CS.setFavoredType(expr, favoredTy);
1624-
}
1625-
16261610
auto &ctx = CS.getASTContext();
16271611
auto parenType = CS.getType(expr->getSubExpr())->getInOutObjectType();
16281612
auto parenFlags = ParameterTypeFlags().withInOut(expr->isSemanticallyInOutExpr());

lib/Sema/CSSimplify.cpp

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,13 +2395,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
23952395
continue;
23962396
}
23972397

2398-
// If the first thing we found is a fix, add a "don't fix" marker.
2399-
if (conversionsOrFixes.empty()) {
2400-
constraints.push_back(
2401-
Constraint::createFixed(*this, constraintKind, FixKind::None,
2402-
type1, type2, fixedLocator));
2403-
}
2404-
24052398
auto fix = *potential.getFix();
24062399
constraints.push_back(
24072400
Constraint::createFixed(*this, constraintKind, fix, type1, type2,
@@ -4193,7 +4186,21 @@ ConstraintSystem::simplifyApplicableFnConstraint(
41934186
ConstraintLocatorBuilder outerLocator =
41944187
getConstraintLocator(anchor, parts, locator.getSummaryFlags());
41954188

4196-
retry:
4189+
unsigned unwrapCount = 0;
4190+
if (shouldAttemptFixes()) {
4191+
// If we have an optional type, try forcing it to see if that
4192+
// helps. Note that we only deal with function and metatype types
4193+
// below, so there is no reason not to attempt to strip these off
4194+
// immediately.
4195+
while (auto objectType2 = desugar2->getOptionalObjectType()) {
4196+
type2 = objectType2;
4197+
desugar2 = type2->getDesugaredType();
4198+
4199+
// Track how many times we do this so that we can record a fix for each.
4200+
++unwrapCount;
4201+
}
4202+
}
4203+
41974204
// For a function, bind the output and convert the argument to the input.
41984205
auto func1 = type1->castTo<FunctionType>();
41994206
if (auto func2 = dyn_cast<FunctionType>(desugar2)) {
@@ -4221,6 +4228,11 @@ ConstraintSystem::simplifyApplicableFnConstraint(
42214228
== SolutionKind::Error)
42224229
return SolutionKind::Error;
42234230

4231+
// Record any fixes we attempted to get to the correct solution.
4232+
while (unwrapCount-- > 0)
4233+
if (recordFix(FixKind::ForceOptional, getConstraintLocator(locator)))
4234+
return SolutionKind::Error;
4235+
42244236
return SolutionKind::Solved;
42254237
}
42264238

@@ -4231,23 +4243,18 @@ ConstraintSystem::simplifyApplicableFnConstraint(
42314243
return formUnsolved();
42324244

42334245
// Construct the instance from the input arguments.
4234-
return simplifyConstructionConstraint(instance2, func1, subflags,
4246+
auto simplified = simplifyConstructionConstraint(instance2, func1, subflags,
42354247
/*FIXME?*/ DC,
42364248
FunctionRefKind::SingleApply,
42374249
getConstraintLocator(outerLocator));
4238-
}
4239-
4240-
if (!shouldAttemptFixes())
4241-
return SolutionKind::Error;
42424250

4243-
// If we're coming from an optional type, unwrap the optional and try again.
4244-
if (auto objectType2 = desugar2->getOptionalObjectType()) {
4245-
if (recordFix(FixKind::ForceOptional, getConstraintLocator(locator)))
4246-
return SolutionKind::Error;
4251+
// Record any fixes we attempted to get to the correct solution.
4252+
if (simplified == SolutionKind::Solved)
4253+
while (unwrapCount-- > 0)
4254+
if (recordFix(FixKind::ForceOptional, getConstraintLocator(locator)))
4255+
return SolutionKind::Error;
42474256

4248-
type2 = objectType2;
4249-
desugar2 = type2->getDesugaredType();
4250-
goto retry;
4257+
return simplified;
42514258
}
42524259

42534260
return SolutionKind::Error;
@@ -4729,15 +4736,15 @@ bool ConstraintSystem::recordFix(Fix fix, ConstraintLocatorBuilder locator) {
47294736
}
47304737

47314738
// Record the fix.
4732-
if (fix.getKind() != FixKind::None) {
4733-
// Increase the score. If this would make the current solution worse than
4734-
// the best solution we've seen already, stop now.
4735-
increaseScore(SK_Fix);
4736-
if (worseThanBestSolution())
4737-
return true;
47384739

4739-
Fixes.push_back({fix, getConstraintLocator(locator)});
4740-
}
4740+
// Increase the score. If this would make the current solution worse than
4741+
// the best solution we've seen already, stop now.
4742+
increaseScore(SK_Fix);
4743+
if (worseThanBestSolution())
4744+
return true;
4745+
4746+
Fixes.push_back({fix, getConstraintLocator(locator)});
4747+
47414748
return false;
47424749
}
47434750

@@ -4746,31 +4753,40 @@ ConstraintSystem::simplifyFixConstraint(Fix fix, Type type1, Type type2,
47464753
ConstraintKind matchKind,
47474754
TypeMatchOptions flags,
47484755
ConstraintLocatorBuilder locator) {
4749-
if (recordFix(fix, locator))
4750-
return SolutionKind::Error;
4751-
47524756
// Try with the fix.
47534757
TypeMatchOptions subflags =
47544758
getDefaultDecompositionOptions(flags) | TMF_ApplyingFix;
47554759
switch (fix.getKind()) {
4756-
case FixKind::None:
4757-
return matchTypes(type1, type2, matchKind, subflags, locator);
4758-
47594760
case FixKind::ForceOptional:
4760-
case FixKind::OptionalChaining:
4761+
case FixKind::OptionalChaining: {
47614762
// Assume that '!' was applied to the first type.
4762-
return matchTypes(type1->getRValueObjectType()->getOptionalObjectType(),
4763-
type2, matchKind, subflags, locator);
4763+
auto result =
4764+
matchTypes(type1->getRValueObjectType()->getOptionalObjectType(), type2,
4765+
matchKind, subflags, locator);
4766+
if (result == SolutionKind::Solved)
4767+
if (recordFix(fix, locator))
4768+
return SolutionKind::Error;
47644769

4770+
return result;
4771+
}
47654772
case FixKind::ForceDowncast:
47664773
// These work whenever they are suggested.
4774+
if (recordFix(fix, locator))
4775+
return SolutionKind::Error;
4776+
47674777
return SolutionKind::Solved;
47684778

4769-
case FixKind::AddressOf:
4779+
case FixKind::AddressOf: {
47704780
// Assume that '&' was applied to the first type, turning an lvalue into
47714781
// an inout.
4772-
return matchTypes(InOutType::get(type1->getRValueType()), type2,
4773-
matchKind, subflags, locator);
4782+
auto result = matchTypes(InOutType::get(type1->getRValueType()), type2,
4783+
matchKind, subflags, locator);
4784+
if (result == SolutionKind::Solved)
4785+
if (recordFix(fix, locator))
4786+
return SolutionKind::Error;
4787+
4788+
return result;
4789+
}
47744790

47754791
case FixKind::CoerceToCheckedCast:
47764792
llvm_unreachable("handled elsewhere");

lib/Sema/Constraint.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,6 @@ Type Fix::getTypeArgument(ConstraintSystem &cs) const {
493493

494494
StringRef Fix::getName(FixKind kind) {
495495
switch (kind) {
496-
case FixKind::None:
497-
return "prevent fixes";
498496
case FixKind::ForceOptional:
499497
return "fix: force optional";
500498
case FixKind::OptionalChaining:

lib/Sema/Constraint.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,6 @@ enum RememberChoice_t : bool {
232232
/// Describes the kind of fix to apply to the given constraint before
233233
/// visiting it.
234234
enum class FixKind : uint8_t {
235-
/// No fix, which is used as a placeholder indicating that future processing
236-
/// of this constraint should not attempt fixes.
237-
None,
238-
239235
/// Introduce a '!' to force an optional unwrap.
240236
ForceOptional,
241237

@@ -264,9 +260,7 @@ class Fix {
264260
friend class Constraint;
265261

266262
public:
267-
Fix() : Kind(FixKind::None), Data(0) { }
268-
269-
Fix(FixKind kind) : Kind(kind), Data(0) {
263+
Fix(FixKind kind) : Kind(kind), Data(0) {
270264
assert(kind != FixKind::ForceDowncast && "Use getForceDowncast()");
271265
}
272266

lib/Sema/ConstraintSystem.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ class ConstraintLocator;
9090

9191
/// Describes a conversion restriction or a fix.
9292
struct RestrictionOrFix {
93-
ConversionRestrictionKind Restriction;
94-
Fix TheFix;
93+
union {
94+
ConversionRestrictionKind Restriction;
95+
Fix TheFix;
96+
};
9597
bool IsRestriction;
9698

9799
public:
@@ -1559,11 +1561,11 @@ class ConstraintSystem {
15591561

15601562
TypeBase* getFavoredType(Expr *E) {
15611563
assert(E != nullptr);
1562-
return this->FavoredTypes[E];
1564+
return this->FavoredTypes[E->getSemanticsProvidingExpr()];
15631565
}
15641566
void setFavoredType(Expr *E, TypeBase *T) {
15651567
assert(E != nullptr);
1566-
this->FavoredTypes[E] = T;
1568+
this->FavoredTypes[E->getSemanticsProvidingExpr()] = T;
15671569
}
15681570

15691571
/// Set the type in our type map for a given expression. The side

0 commit comments

Comments
 (0)