Skip to content

[6.1] [IDE] Handle UnresolvedMemberExpr for solver-based cursor info #78012

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
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,10 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
/// \c nullptr.
ArgumentList *getArgs() const;

/// If the expression has a DeclNameLoc, returns it. Otherwise, returns
/// an nullp DeclNameLoc.
DeclNameLoc getNameLoc() const;

/// Produce a mapping from each subexpression to its parent
/// expression, with the provided expression serving as the root of
/// the parent map.
Expand Down
19 changes: 19 additions & 0 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,25 @@ ArgumentList *Expr::getArgs() const {
return nullptr;
}

DeclNameLoc Expr::getNameLoc() const {
if (auto *DRE = dyn_cast<DeclRefExpr>(this))
return DRE->getNameLoc();
if (auto *UDRE = dyn_cast<UnresolvedDeclRefExpr>(this))
return UDRE->getNameLoc();
if (auto *ODRE = dyn_cast<OverloadedDeclRefExpr>(this))
return ODRE->getNameLoc();
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(this))
return UDE->getNameLoc();
if (auto *UME = dyn_cast<UnresolvedMemberExpr>(this))
return UME->getNameLoc();
if (auto *MRE = dyn_cast<MemberRefExpr>(this))
return MRE->getNameLoc();
if (auto *DRME = dyn_cast<DynamicMemberRefExpr>(this))
return DRME->getNameLoc();

return DeclNameLoc();
}

llvm::DenseMap<Expr *, Expr *> Expr::getParentMap() {
class RecordingTraversal : public ASTWalker {
public:
Expand Down
19 changes: 1 addition & 18 deletions lib/IDE/CursorInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,23 +214,6 @@ class NodeFinder : ASTWalker {
return Action::Continue();
}

/// Retrieve the name location for an expression that supports cursor info.
DeclNameLoc getExprNameLoc(Expr *E) {
if (auto *DRE = dyn_cast<DeclRefExpr>(E))
return DRE->getNameLoc();

if (auto *UDRE = dyn_cast<UnresolvedDeclRefExpr>(E))
return UDRE->getNameLoc();

if (auto *ODRE = dyn_cast<OverloadedDeclRefExpr>(E))
return ODRE->getNameLoc();

if (auto *UDE = dyn_cast<UnresolvedDotExpr>(E))
return UDE->getNameLoc();

return DeclNameLoc();
}

PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
if (auto closure = dyn_cast<ClosureExpr>(E)) {
DeclContextStack.push_back(closure);
Expand All @@ -247,7 +230,7 @@ class NodeFinder : ASTWalker {
}
}

if (getExprNameLoc(E).getBaseNameLoc() != LocToResolve)
if (E->getNameLoc().getBaseNameLoc() != LocToResolve)
return Action::Continue(E);

assert(Result == nullptr);
Expand Down
14 changes: 14 additions & 0 deletions test/SourceKit/CursorInfo/issue-77981.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
struct S {
static func foo(_ x: Int) -> S {}
static func foo(_ x: String) -> S {}
}

// https://github.com/swiftlang/swift/issues/77981 - Make sure we can resolve
// solver-based cursor info for UnresolvedMemberExprs.
func bar() {
// RUN: %sourcekitd-test -req=cursor -pos=%(line + 1):15 %s -- %s | %FileCheck %s
let _: S = .foo()
}

// CHECK-DAG: source.lang.swift.ref.function.method.static (2:15-2:28)
// CHECK-DAG: source.lang.swift.ref.function.method.static (3:15-3:31)