Skip to content

Commit b072ddc

Browse files
authored
Merge pull request #37717 from ahoppen/pr-5.5/dont-crash-call-to-init
[5.5][Refactoring] Don't crash when converting a function to async that contains a call to init
2 parents 353cfce + 5ce687c commit b072ddc

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5349,13 +5349,13 @@ struct CallbackClassifier {
53495349
}
53505350
};
53515351

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

53615361
class DeclCollector : private SourceEntityWalker {
@@ -5630,7 +5630,7 @@ class AsyncConverter : private SourceEntityWalker {
56305630
llvm::DenseMap<const Decl *, Identifier> Names;
56315631
// Names of decls in each scope, where the first element is the initial scope
56325632
// and the last is the current scope.
5633-
llvm::SmallVector<llvm::DenseSet<Identifier>, 4> ScopedNames;
5633+
llvm::SmallVector<llvm::DenseSet<DeclBaseName>, 4> ScopedNames;
56345634
// Mapping of \c BraceStmt -> declarations referenced in that statement
56355635
// without first being declared. These are used to fill the \c ScopeNames
56365636
// map on entering that scope.
@@ -6705,7 +6705,7 @@ class AsyncConverter : private SourceEntityWalker {
67056705
Identifier assignUniqueName(const Decl *D, StringRef BoundName) {
67066706
Identifier Ident;
67076707
if (BoundName.empty()) {
6708-
BoundName = getDeclName(D).str();
6708+
BoundName = getDeclName(D).userFacingName();
67096709
if (BoundName.empty())
67106710
return Ident;
67116711
}

test/refactoring/ConvertAsync/basic.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,3 +844,15 @@ func testPreserveComments3() {
844844
// PRESERVE-TRAILING-COMMENT-CALL: let s = await simple()
845845
// PRESERVE-TRAILING-COMMENT-CALL-NEXT: print(s)
846846
// PRESERVE-TRAILING-COMMENT-CALL-NOT: // make sure we pickup this trailing comment if we're converting the function, but not the call
847+
848+
class TestConvertFunctionWithCallToFunctionsWithSpecialName {
849+
required init() {}
850+
subscript(index: Int) -> Int { return index }
851+
852+
// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+1):3
853+
static func example() -> Self {
854+
let x = self.init()
855+
_ = x[1]
856+
return x
857+
}
858+
}

0 commit comments

Comments
 (0)