Skip to content

[ConstraintSystem] Implement simplification for all locator elements #41847

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 15 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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: 7 additions & 0 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,13 @@ template <typename T = Decl> T *getAsDecl(ASTNode node) {
return nullptr;
}

template <typename T = Stmt>
T *getAsStmt(ASTNode node) {
if (auto *S = node.dyn_cast<Stmt *>())
return dyn_cast_or_null<T>(S);
return nullptr;
}

SourceLoc getLoc(ASTNode node);
SourceRange getSourceRange(ASTNode node);

Expand Down
10 changes: 8 additions & 2 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3809,7 +3809,10 @@ bool MissingMemberFailure::diagnoseForDynamicCallable() const {
}

bool MissingMemberFailure::diagnoseInLiteralCollectionContext() const {
auto *expr = castToExpr(getAnchor());
auto *expr = getAsExpr(getAnchor());
if (!expr)
return false;

auto *parentExpr = findParentExpr(expr);
auto &solution = getSolution();

Expand Down Expand Up @@ -5918,8 +5921,11 @@ void MissingGenericArgumentsFailure::emitGenericSignatureNote(
return (type == params.end()) ? Type() : type->second;
};

auto baseType = anchor.dyn_cast<TypeRepr *>();
if (!baseType)
return;

SmallString<64> paramsAsString;
auto baseType = anchor.get<TypeRepr *>();
if (TypeChecker::getDefaultGenericArgumentsString(paramsAsString, GTD,
getPreferredType)) {
auto diagnostic = emitDiagnosticAt(
Expand Down
106 changes: 98 additions & 8 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ ExpressionTimer::~ExpressionTimer() {
if (!PrintWarning)
return;

const auto WarnLimit = getWarnLimit();

if (WarnLimit == 0 || elapsedMS < WarnLimit)
return;

ASTNode anchor;
if (auto *locator = Anchor.dyn_cast<ConstraintLocator *>()) {
anchor = simplifyLocatorToAnchor(locator);
Expand All @@ -87,9 +92,7 @@ ExpressionTimer::~ExpressionTimer() {
anchor = Anchor.get<Expr *>();
}

const auto WarnLimit = getWarnLimit();
if (WarnLimit != 0 && elapsedMS >= WarnLimit &&
anchor.getStartLoc().isValid()) {
if (anchor.getStartLoc().isValid()) {
Context.Diags
.diagnose(anchor.getStartLoc(), diag::debug_long_expression, elapsedMS,
WarnLimit)
Expand Down Expand Up @@ -4758,6 +4761,12 @@ void constraints::simplifyLocator(ASTNode &anchor,
path = path.slice(1);
continue;
}

if (anchor.is<Pattern *>()) {
path = path.slice(1);
continue;
}

break;

case ConstraintLocator::SubscriptMember:
Expand Down Expand Up @@ -4811,16 +4820,28 @@ void constraints::simplifyLocator(ASTNode &anchor,
}

case ConstraintLocator::Condition: {
anchor = castToExpr<IfExpr>(anchor)->getCondExpr();
if (auto *condStmt = getAsStmt<LabeledConditionalStmt>(anchor)) {
anchor = &condStmt->getCond().front();
} else {
anchor = castToExpr<IfExpr>(anchor)->getCondExpr();
}

path = path.slice(1);
continue;
}

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

anchor = branch.forThen() ? ifExpr->getThenExpr() : ifExpr->getElseExpr();
if (auto *ifStmt = getAsStmt<IfStmt>(anchor)) {
anchor =
branch.forThen() ? ifStmt->getThenStmt() : ifStmt->getElseStmt();
} else {
auto *ifExpr = castToExpr<IfExpr>(anchor);
anchor =
branch.forThen() ? ifExpr->getThenExpr() : ifExpr->getElseExpr();
}

path = path.slice(1);
continue;
}
Expand Down Expand Up @@ -4851,8 +4872,77 @@ void constraints::simplifyLocator(ASTNode &anchor,
continue;
}

default:
// FIXME: Lots of other cases to handle.
case ConstraintLocator::ClosureBodyElement: {
auto bodyElt = path[0].castTo<LocatorPathElt::ClosureBodyElement>();
anchor = bodyElt.getElement();
path = path.slice(1);
continue;
}

case ConstraintLocator::PatternMatch: {
auto patternElt = path[0].castTo<LocatorPathElt::PatternMatch>();
anchor = patternElt.getPattern();
path = path.slice(1);
continue;
}

case ConstraintLocator::PackType:
case ConstraintLocator::ParentType:
case ConstraintLocator::KeyPathType:
case ConstraintLocator::InstanceType:
case ConstraintLocator::PlaceholderType:
case ConstraintLocator::SequenceElementType:
case ConstraintLocator::ConstructorMemberType:
case ConstraintLocator::ExistentialSuperclassType:
break;

case ConstraintLocator::GenericArgument:
case ConstraintLocator::FunctionArgument:
case ConstraintLocator::SynthesizedArgument:
break;

case ConstraintLocator::DynamicLookupResult:
case ConstraintLocator::KeyPathComponentResult:
break;

case ConstraintLocator::GenericParameter:
break;

case ConstraintLocator::OpenedGeneric:
case ConstraintLocator::OpenedOpaqueArchetype:
break;

case ConstraintLocator::KeyPathRoot:
case ConstraintLocator::KeyPathValue:
break;

case ConstraintLocator::ProtocolRequirement:
case ConstraintLocator::ConditionalRequirement:
case ConstraintLocator::ConformanceRequirement:
case ConstraintLocator::TypeParameterRequirement:
break;

case ConstraintLocator::PackElement:
break;

case ConstraintLocator::PatternBindingElement: {
auto pattern = path[0].castTo<LocatorPathElt::PatternBindingElement>();
auto *patternBinding = cast<PatternBindingDecl>(anchor.get<Decl *>());
anchor = patternBinding->getInit(pattern.getIndex());
// If this pattern is uninitialized, let's use it as anchor.
if (!anchor)
anchor = patternBinding->getPattern(pattern.getIndex());
path = path.slice(1);
continue;
}

case ConstraintLocator::ImplicitConversion:
break;

case ConstraintLocator::Witness:
case ConstraintLocator::WrappedValue:
case ConstraintLocator::OptionalPayload:
case ConstraintLocator::ImplicitlyUnwrappedDisjunctionChoice:
break;
}

Expand Down