Skip to content

[Refactoring] Don't crash when converting a function to async that contains a call to init #37652

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 1 commit into from
Jun 1, 2021
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
12 changes: 6 additions & 6 deletions lib/IDE/Refactoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5341,13 +5341,13 @@ struct CallbackClassifier {
}
};

/// Name of a decl if it has one, an empty \c Identifier otherwise.
static Identifier getDeclName(const Decl *D) {
/// Base name of a decl if it has one, an empty \c DeclBaseName otherwise.
static DeclBaseName getDeclName(const Decl *D) {
if (auto *VD = dyn_cast<ValueDecl>(D)) {
if (VD->hasName())
return VD->getBaseIdentifier();
return VD->getBaseName();
}
return Identifier();
return DeclBaseName();
}

class DeclCollector : private SourceEntityWalker {
Expand Down Expand Up @@ -5622,7 +5622,7 @@ class AsyncConverter : private SourceEntityWalker {
llvm::DenseMap<const Decl *, Identifier> Names;
// Names of decls in each scope, where the first element is the initial scope
// and the last is the current scope.
llvm::SmallVector<llvm::DenseSet<Identifier>, 4> ScopedNames;
llvm::SmallVector<llvm::DenseSet<DeclBaseName>, 4> ScopedNames;
// Mapping of \c BraceStmt -> declarations referenced in that statement
// without first being declared. These are used to fill the \c ScopeNames
// map on entering that scope.
Expand Down Expand Up @@ -6697,7 +6697,7 @@ class AsyncConverter : private SourceEntityWalker {
Identifier assignUniqueName(const Decl *D, StringRef BoundName) {
Identifier Ident;
if (BoundName.empty()) {
BoundName = getDeclName(D).str();
BoundName = getDeclName(D).userFacingName();
if (BoundName.empty())
return Ident;
}
Expand Down
12 changes: 12 additions & 0 deletions test/refactoring/ConvertAsync/basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -844,3 +844,15 @@ func testPreserveComments3() {
// PRESERVE-TRAILING-COMMENT-CALL: let s = await simple()
// PRESERVE-TRAILING-COMMENT-CALL-NEXT: print(s)
// PRESERVE-TRAILING-COMMENT-CALL-NOT: // make sure we pickup this trailing comment if we're converting the function, but not the call

class TestConvertFunctionWithCallToFunctionsWithSpecialName {
required init() {}
subscript(index: Int) -> Int { return index }

// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+1):3
static func example() -> Self {
let x = self.init()
_ = x[1]
return x
}
}