Skip to content

Commit b8900de

Browse files
authored
Merge pull request #37652 from ahoppen/pr/dont-crash-call-to-init
[Refactoring] Don't crash when converting a function to async that contains a call to init
2 parents 57b362c + 58be2bf commit b8900de

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
@@ -5341,13 +5341,13 @@ struct CallbackClassifier {
53415341
}
53425342
};
53435343

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

53535353
class DeclCollector : private SourceEntityWalker {
@@ -5622,7 +5622,7 @@ class AsyncConverter : private SourceEntityWalker {
56225622
llvm::DenseMap<const Decl *, Identifier> Names;
56235623
// Names of decls in each scope, where the first element is the initial scope
56245624
// and the last is the current scope.
5625-
llvm::SmallVector<llvm::DenseSet<Identifier>, 4> ScopedNames;
5625+
llvm::SmallVector<llvm::DenseSet<DeclBaseName>, 4> ScopedNames;
56265626
// Mapping of \c BraceStmt -> declarations referenced in that statement
56275627
// without first being declared. These are used to fill the \c ScopeNames
56285628
// map on entering that scope.
@@ -6697,7 +6697,7 @@ class AsyncConverter : private SourceEntityWalker {
66976697
Identifier assignUniqueName(const Decl *D, StringRef BoundName) {
66986698
Identifier Ident;
66996699
if (BoundName.empty()) {
6700-
BoundName = getDeclName(D).str();
6700+
BoundName = getDeclName(D).userFacingName();
67016701
if (BoundName.empty())
67026702
return Ident;
67036703
}

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)