Skip to content

Commit b6e9a1d

Browse files
committed
[IDE] Allow resolving of unresolved decl refs when computing shorthand shadows
1 parent 44262a3 commit b6e9a1d

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

include/swift/Sema/IDETypeChecking.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ namespace swift {
164164
constraints::SolutionApplicationTarget &target, bool needsPrecheck,
165165
llvm::function_ref<void(const constraints::Solution &)> callback);
166166

167+
/// Thunk around \c TypeChecker::resolveDeclRefExpr to make it available to
168+
/// \c swift::ide
169+
Expr *resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *Context,
170+
bool replaceInvalidRefsWithErrors);
171+
167172
LookupResult
168173
lookupSemanticMember(DeclContext *DC, Type ty, DeclName name);
169174

@@ -339,12 +344,17 @@ namespace swift {
339344
/// these shorthand shadows.
340345
/// The first element in the pair is the implicitly declared variable and the
341346
/// second variable is the shadowed one.
347+
/// If a \c DeclContext is passed, it is used to resolve any
348+
/// \c UnresolvedDeclRef that a shorthand shadow may refer to.
342349
SmallVector<std::pair<ValueDecl *, ValueDecl *>, 1>
343-
getShorthandShadows(CaptureListExpr *CaptureList);
350+
getShorthandShadows(CaptureListExpr *CaptureList, DeclContext *DC = nullptr);
344351

345352
/// Same as above but for shorthand `if let foo {` syntax.
353+
/// If a \c DeclContext is passed, it is used to resolve any
354+
/// \c UnresolvedDeclRef that a shorthand shadow may refer to.
346355
SmallVector<std::pair<ValueDecl *, ValueDecl *>, 1>
347-
getShorthandShadows(LabeledConditionalStmt *CondStmt);
356+
getShorthandShadows(LabeledConditionalStmt *CondStmt,
357+
DeclContext *DC = nullptr);
348358
}
349359

350360
#endif

lib/IDE/IDETypeChecking.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -915,13 +915,22 @@ Type swift::getResultTypeOfKeypathDynamicMember(SubscriptDecl *SD) {
915915
}
916916

917917
SmallVector<std::pair<ValueDecl *, ValueDecl *>, 1>
918-
swift::getShorthandShadows(CaptureListExpr *CaptureList) {
918+
swift::getShorthandShadows(CaptureListExpr *CaptureList, DeclContext *DC) {
919919
SmallVector<std::pair<ValueDecl *, ValueDecl *>, 1> Result;
920920
for (auto Capture : CaptureList->getCaptureList()) {
921921
if (Capture.PBD->getPatternList().size() != 1) {
922922
continue;
923923
}
924-
auto *DRE = dyn_cast_or_null<DeclRefExpr>(Capture.PBD->getInit(0));
924+
Expr *Init = Capture.PBD->getInit(0);
925+
if (!Init) {
926+
continue;
927+
}
928+
if (auto UDRE = dyn_cast<UnresolvedDeclRefExpr>(Init)) {
929+
if (DC) {
930+
Init = resolveDeclRefExpr(UDRE, DC, /*replaceInvalidRefsWithErrors=*/false);
931+
}
932+
}
933+
auto *DRE = dyn_cast_or_null<DeclRefExpr>(Init);
925934
if (!DRE) {
926935
continue;
927936
}
@@ -946,17 +955,23 @@ swift::getShorthandShadows(CaptureListExpr *CaptureList) {
946955
}
947956

948957
SmallVector<std::pair<ValueDecl *, ValueDecl *>, 1>
949-
swift::getShorthandShadows(LabeledConditionalStmt *CondStmt) {
958+
swift::getShorthandShadows(LabeledConditionalStmt *CondStmt, DeclContext *DC) {
950959
SmallVector<std::pair<ValueDecl *, ValueDecl *>, 1> Result;
951960
for (const StmtConditionElement &Cond : CondStmt->getCond()) {
952961
if (Cond.getKind() != StmtConditionElement::CK_PatternBinding) {
953962
continue;
954963
}
955-
auto Init = dyn_cast<DeclRefExpr>(Cond.getInitializer());
956-
if (!Init) {
964+
Expr *Init = Cond.getInitializer();
965+
if (auto UDRE = dyn_cast<UnresolvedDeclRefExpr>(Init)) {
966+
if (DC) {
967+
Init = resolveDeclRefExpr(UDRE, DC, /*replaceInvalidRefsWithErrors=*/false);
968+
}
969+
}
970+
auto InitDeclRef = dyn_cast<DeclRefExpr>(Init);
971+
if (!InitDeclRef) {
957972
continue;
958973
}
959-
auto ReferencedVar = dyn_cast_or_null<VarDecl>(Init->getDecl());
974+
auto ReferencedVar = dyn_cast_or_null<VarDecl>(InitDeclRef->getDecl());
960975
if (!ReferencedVar) {
961976
continue;
962977
}

lib/Sema/TypeChecker.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,11 @@ bool swift::typeCheckForCodeCompletion(
551551
callback);
552552
}
553553

554+
Expr *swift::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *Context,
555+
bool replaceInvalidRefsWithErrors) {
556+
return TypeChecker::resolveDeclRefExpr(UDRE, Context, replaceInvalidRefsWithErrors);
557+
}
558+
554559
void TypeChecker::checkForForbiddenPrefix(ASTContext &C, DeclBaseName Name) {
555560
if (C.TypeCheckerOpts.DebugForbidTypecheckPrefix.empty())
556561
return;

0 commit comments

Comments
 (0)