Skip to content

[5.7][Refactoring] Fix a crash that occurred when converting if/else of enum with raw value to switch statement #58635

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
26 changes: 18 additions & 8 deletions lib/IDE/Refactoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2514,14 +2514,24 @@ bool RefactoringActionConvertToSwitchStmt::performChange() {
SourceManager &SM;

bool isFunctionNameAllowed(BinaryExpr *E) {
auto FunctionBody = dyn_cast<DotSyntaxCallExpr>(E->getFn())->getFn();
auto FunctionDeclaration = dyn_cast<DeclRefExpr>(FunctionBody)->getDecl();
const auto FunctionName = dyn_cast<FuncDecl>(FunctionDeclaration)
->getBaseIdentifier().str();
return FunctionName == "~="
|| FunctionName == "=="
|| FunctionName == "__derived_enum_equals"
|| FunctionName == "__derived_struct_equals";
Expr *Fn = E->getFn();
if (auto DotSyntaxCall = dyn_cast_or_null<DotSyntaxCallExpr>(Fn)) {
Fn = DotSyntaxCall->getFn();
}
DeclRefExpr *DeclRef = dyn_cast_or_null<DeclRefExpr>(Fn);
if (!DeclRef) {
return false;
}
auto FunctionDeclaration = dyn_cast_or_null<FuncDecl>(DeclRef->getDecl());
if (!FunctionDeclaration) {
return false;
}
auto &ASTCtx = FunctionDeclaration->getASTContext();
const auto FunctionName = FunctionDeclaration->getBaseIdentifier();
return FunctionName == ASTCtx.Id_MatchOperator ||
FunctionName == ASTCtx.Id_EqualsOperator ||
FunctionName == ASTCtx.Id_derived_enum_equals ||
FunctionName == ASTCtx.Id_derived_struct_equals;
}

void appendPattern(Expr *LHS, Expr *RHS) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
enum EnumWithUnderlyingValue: String {
case north, south, east, west
}



func foo(test: EnumWithUnderlyingValue) {
switch test {
case .north:
print("north")
case .south:
print("south")
case .east:
print("east")
default:
break
}
}



2 changes: 1 addition & 1 deletion test/refactoring/ConvertToSwitchStmt/basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func checkEmptyBody(e: E) {
}
}

// RUN: rm -rf %t.result && mkdir -p %t.result
// RUN: %empty-directory(%t.result)

// RUN: %refactor -convert-to-switch-stmt -source-filename %s -pos=9:3 -end-pos=16:4 > %t.result/L9-3.swift
// RUN: %target-swift-frontend-typecheck %t.result/L9-3.swift
Expand Down
22 changes: 22 additions & 0 deletions test/refactoring/ConvertToSwitchStmt/enum_with_raw_value.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
enum EnumWithUnderlyingValue: String {
case north, south, east, west
}



func foo(test: EnumWithUnderlyingValue) {
if test == .north {
print("north")
} else if test == .south {
print("south")
} else if test == .east {
print("east")
}
}


// RUN: %empty-directory(%t.result)

// RUN: %refactor -convert-to-switch-stmt -source-filename %s -pos=8:3 -end-pos=14:4 > %t.result/L8-3.swift
// RUN: %target-swift-frontend-typecheck %t.result/L8-3.swift
// RUN: diff -u %S/Outputs/enum_with_raw_value/L8-3.swift.expected %t.result/L8-3.swift