Skip to content

Commit e94abe4

Browse files
authored
Merge pull request #41847 from xedin/add-missing-locator-simplifications
[ConstraintSystem] Implement simplification for all locator elements
2 parents a64b75e + 2c35b65 commit e94abe4

File tree

3 files changed

+113
-10
lines changed

3 files changed

+113
-10
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,13 @@ template <typename T = Decl> T *getAsDecl(ASTNode node) {
566566
return nullptr;
567567
}
568568

569+
template <typename T = Stmt>
570+
T *getAsStmt(ASTNode node) {
571+
if (auto *S = node.dyn_cast<Stmt *>())
572+
return dyn_cast_or_null<T>(S);
573+
return nullptr;
574+
}
575+
569576
SourceLoc getLoc(ASTNode node);
570577
SourceRange getSourceRange(ASTNode node);
571578

lib/Sema/CSDiagnostics.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3811,7 +3811,10 @@ bool MissingMemberFailure::diagnoseForDynamicCallable() const {
38113811
}
38123812

38133813
bool MissingMemberFailure::diagnoseInLiteralCollectionContext() const {
3814-
auto *expr = castToExpr(getAnchor());
3814+
auto *expr = getAsExpr(getAnchor());
3815+
if (!expr)
3816+
return false;
3817+
38153818
auto *parentExpr = findParentExpr(expr);
38163819
auto &solution = getSolution();
38173820

@@ -5920,8 +5923,11 @@ void MissingGenericArgumentsFailure::emitGenericSignatureNote(
59205923
return (type == params.end()) ? Type() : type->second;
59215924
};
59225925

5926+
auto baseType = anchor.dyn_cast<TypeRepr *>();
5927+
if (!baseType)
5928+
return;
5929+
59235930
SmallString<64> paramsAsString;
5924-
auto baseType = anchor.get<TypeRepr *>();
59255931
if (TypeChecker::getDefaultGenericArgumentsString(paramsAsString, GTD,
59265932
getPreferredType)) {
59275933
auto diagnostic = emitDiagnosticAt(

lib/Sema/ConstraintSystem.cpp

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ ExpressionTimer::~ExpressionTimer() {
7676
if (!PrintWarning)
7777
return;
7878

79+
const auto WarnLimit = getWarnLimit();
80+
81+
if (WarnLimit == 0 || elapsedMS < WarnLimit)
82+
return;
83+
7984
ASTNode anchor;
8085
if (auto *locator = Anchor.dyn_cast<ConstraintLocator *>()) {
8186
anchor = simplifyLocatorToAnchor(locator);
@@ -87,9 +92,7 @@ ExpressionTimer::~ExpressionTimer() {
8792
anchor = Anchor.get<Expr *>();
8893
}
8994

90-
const auto WarnLimit = getWarnLimit();
91-
if (WarnLimit != 0 && elapsedMS >= WarnLimit &&
92-
anchor.getStartLoc().isValid()) {
95+
if (anchor.getStartLoc().isValid()) {
9396
Context.Diags
9497
.diagnose(anchor.getStartLoc(), diag::debug_long_expression, elapsedMS,
9598
WarnLimit)
@@ -4758,6 +4761,12 @@ void constraints::simplifyLocator(ASTNode &anchor,
47584761
path = path.slice(1);
47594762
continue;
47604763
}
4764+
4765+
if (anchor.is<Pattern *>()) {
4766+
path = path.slice(1);
4767+
continue;
4768+
}
4769+
47614770
break;
47624771

47634772
case ConstraintLocator::SubscriptMember:
@@ -4811,16 +4820,28 @@ void constraints::simplifyLocator(ASTNode &anchor,
48114820
}
48124821

48134822
case ConstraintLocator::Condition: {
4814-
anchor = castToExpr<IfExpr>(anchor)->getCondExpr();
4823+
if (auto *condStmt = getAsStmt<LabeledConditionalStmt>(anchor)) {
4824+
anchor = &condStmt->getCond().front();
4825+
} else {
4826+
anchor = castToExpr<IfExpr>(anchor)->getCondExpr();
4827+
}
4828+
48154829
path = path.slice(1);
48164830
continue;
48174831
}
48184832

48194833
case ConstraintLocator::TernaryBranch: {
48204834
auto branch = path[0].castTo<LocatorPathElt::TernaryBranch>();
4821-
auto *ifExpr = castToExpr<IfExpr>(anchor);
48224835

4823-
anchor = branch.forThen() ? ifExpr->getThenExpr() : ifExpr->getElseExpr();
4836+
if (auto *ifStmt = getAsStmt<IfStmt>(anchor)) {
4837+
anchor =
4838+
branch.forThen() ? ifStmt->getThenStmt() : ifStmt->getElseStmt();
4839+
} else {
4840+
auto *ifExpr = castToExpr<IfExpr>(anchor);
4841+
anchor =
4842+
branch.forThen() ? ifExpr->getThenExpr() : ifExpr->getElseExpr();
4843+
}
4844+
48244845
path = path.slice(1);
48254846
continue;
48264847
}
@@ -4851,8 +4872,77 @@ void constraints::simplifyLocator(ASTNode &anchor,
48514872
continue;
48524873
}
48534874

4854-
default:
4855-
// FIXME: Lots of other cases to handle.
4875+
case ConstraintLocator::ClosureBodyElement: {
4876+
auto bodyElt = path[0].castTo<LocatorPathElt::ClosureBodyElement>();
4877+
anchor = bodyElt.getElement();
4878+
path = path.slice(1);
4879+
continue;
4880+
}
4881+
4882+
case ConstraintLocator::PatternMatch: {
4883+
auto patternElt = path[0].castTo<LocatorPathElt::PatternMatch>();
4884+
anchor = patternElt.getPattern();
4885+
path = path.slice(1);
4886+
continue;
4887+
}
4888+
4889+
case ConstraintLocator::PackType:
4890+
case ConstraintLocator::ParentType:
4891+
case ConstraintLocator::KeyPathType:
4892+
case ConstraintLocator::InstanceType:
4893+
case ConstraintLocator::PlaceholderType:
4894+
case ConstraintLocator::SequenceElementType:
4895+
case ConstraintLocator::ConstructorMemberType:
4896+
case ConstraintLocator::ExistentialSuperclassType:
4897+
break;
4898+
4899+
case ConstraintLocator::GenericArgument:
4900+
case ConstraintLocator::FunctionArgument:
4901+
case ConstraintLocator::SynthesizedArgument:
4902+
break;
4903+
4904+
case ConstraintLocator::DynamicLookupResult:
4905+
case ConstraintLocator::KeyPathComponentResult:
4906+
break;
4907+
4908+
case ConstraintLocator::GenericParameter:
4909+
break;
4910+
4911+
case ConstraintLocator::OpenedGeneric:
4912+
case ConstraintLocator::OpenedOpaqueArchetype:
4913+
break;
4914+
4915+
case ConstraintLocator::KeyPathRoot:
4916+
case ConstraintLocator::KeyPathValue:
4917+
break;
4918+
4919+
case ConstraintLocator::ProtocolRequirement:
4920+
case ConstraintLocator::ConditionalRequirement:
4921+
case ConstraintLocator::ConformanceRequirement:
4922+
case ConstraintLocator::TypeParameterRequirement:
4923+
break;
4924+
4925+
case ConstraintLocator::PackElement:
4926+
break;
4927+
4928+
case ConstraintLocator::PatternBindingElement: {
4929+
auto pattern = path[0].castTo<LocatorPathElt::PatternBindingElement>();
4930+
auto *patternBinding = cast<PatternBindingDecl>(anchor.get<Decl *>());
4931+
anchor = patternBinding->getInit(pattern.getIndex());
4932+
// If this pattern is uninitialized, let's use it as anchor.
4933+
if (!anchor)
4934+
anchor = patternBinding->getPattern(pattern.getIndex());
4935+
path = path.slice(1);
4936+
continue;
4937+
}
4938+
4939+
case ConstraintLocator::ImplicitConversion:
4940+
break;
4941+
4942+
case ConstraintLocator::Witness:
4943+
case ConstraintLocator::WrappedValue:
4944+
case ConstraintLocator::OptionalPayload:
4945+
case ConstraintLocator::ImplicitlyUnwrappedDisjunctionChoice:
48564946
break;
48574947
}
48584948

0 commit comments

Comments
 (0)