Skip to content

Commit 6a13fc5

Browse files
authored
Merge pull request #15944 from huonw/fatalError-bodies
[IDE] Tweak replace.bodies.with.fatalError.
2 parents 3c94a31 + 75313cf commit 6a13fc5

File tree

11 files changed

+95
-23
lines changed

11 files changed

+95
-23
lines changed

include/swift/IDE/RefactoringKinds.def

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#define RANGE_REFACTORING(KIND, NAME, ID) SEMANTIC_REFACTORING(KIND, NAME, ID)
1111
#endif
1212

13+
#ifndef INTERNAL_RANGE_REFACTORING
14+
#define INTERNAL_RANGE_REFACTORING(KIND, NAME, ID) RANGE_REFACTORING(KIND, NAME, ID)
15+
#endif
16+
1317
#ifndef CURSOR_REFACTORING
1418
#define CURSOR_REFACTORING(KIND, NAME, ID) SEMANTIC_REFACTORING(KIND, NAME, ID)
1519
#endif
@@ -60,9 +64,14 @@ RANGE_REFACTORING(ExpandTernaryExpr, "Expand Ternary Expression", expand.ternary
6064

6165
RANGE_REFACTORING(ConvertToTernaryExpr, "Convert To Ternary Expression", convert.ternary.expr)
6266

63-
RANGE_REFACTORING(ReplaceBodiesWithFatalError, "Replace function bodies with fatalError", replace.bodies.with.fatalError)
67+
// These internal refactorings are designed to be helpful for working on
68+
// the compiler/standard library, etc., but are likely to be just confusing and
69+
// noise for general development.
70+
71+
INTERNAL_RANGE_REFACTORING(ReplaceBodiesWithFatalError, "Replace Function Bodies With 'fatalError()'", replace.bodies.with.fatalError)
6472

6573
#undef CURSOR_REFACTORING
74+
#undef INTERNAL_RANGE_REFACTORING
6675
#undef RANGE_REFACTORING
6776
#undef SEMANTIC_REFACTORING
6877
#undef REFACTORING

lib/IDE/Refactoring.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,7 @@ bool RefactoringActionReplaceBodiesWithFatalError::isApplicable(
16391639
}
16401640

16411641
bool RefactoringActionReplaceBodiesWithFatalError::performChange() {
1642-
const StringRef replacement = "{ fatalError() }";
1642+
const StringRef replacement = "{\nfatalError()\n}";
16431643
llvm::SmallPtrSet<Decl *, 16> Found;
16441644
for (auto decl : RangeInfo.DeclaredDecls) {
16451645
FindAllSubDecls(Found).walk(decl.VD);
@@ -3099,9 +3099,14 @@ collectAvailableRefactorings(SourceFile *SF, RangeConfig Range,
30993099
Range.getEnd(SF->getASTContext().SourceMgr));
31003100
ResolvedRangeInfo Result = Resolver.resolve();
31013101

3102+
bool enableInternalRefactoring = getenv("SWIFT_ENABLE_INTERNAL_REFACTORING_ACTIONS");
3103+
31023104
#define RANGE_REFACTORING(KIND, NAME, ID) \
31033105
if (RefactoringAction##KIND::isApplicable(Result, DiagEngine)) \
31043106
Scratch.push_back(RefactoringKind::KIND);
3107+
#define INTERNAL_RANGE_REFACTORING(KIND, NAME, ID) \
3108+
if (enableInternalRefactoring) \
3109+
RANGE_REFACTORING(KIND, NAME, ID)
31053110
#include "swift/IDE/RefactoringKinds.def"
31063111

31073112
RangeStartMayNeedRename = rangeStartMayNeedRename(Result);

test/refactoring/ReplaceBodiesWithFatalError/Outputs/basic/L12-14.swift.expected

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ var qux: Int = 0 {
99
didSet { print("bye") }
1010
}
1111

12-
func generic<T>(_: T) -> Int { fatalError() }
12+
func generic<T>(_: T) -> Int {
13+
fatalError()
14+
}
1315

1416
class Foo {
1517
var foo: Int

test/refactoring/ReplaceBodiesWithFatalError/Outputs/basic/L16-49.swift.expected

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,43 @@ func generic<T>(_: T) -> Int {
1616
class Foo {
1717
var foo: Int
1818

19-
var bar: Int { fatalError() }
19+
var bar: Int {
20+
fatalError()
21+
}
2022

2123
var baz: Int {
22-
get { fatalError() }
23-
set { fatalError() }
24+
get {
25+
fatalError()
26+
}
27+
set {
28+
fatalError()
29+
}
2430
}
2531

2632
var qux: Int {
27-
willSet { fatalError() }
28-
didSet { fatalError() }
33+
willSet {
34+
fatalError()
35+
}
36+
didSet {
37+
fatalError()
38+
}
2939
}
3040

31-
init() { fatalError() }
41+
init() {
42+
fatalError()
43+
}
3244

33-
func nonGeneric() -> Int { fatalError() }
45+
func nonGeneric() -> Int {
46+
fatalError()
47+
}
3448

35-
func generic<T>(_: T) -> Int { fatalError() }
49+
func generic<T>(_: T) -> Int {
50+
fatalError()
51+
}
3652

37-
deinit { fatalError() }
53+
deinit {
54+
fatalError()
55+
}
3856
}
3957

4058
extension Foo {

test/refactoring/ReplaceBodiesWithFatalError/Outputs/basic/L19-22.swift.expected

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ func generic<T>(_: T) -> Int {
1616
class Foo {
1717
var foo: Int
1818

19-
var bar: Int { fatalError() }
19+
var bar: Int {
20+
fatalError()
21+
}
2022

2123
var baz: Int {
2224
get { return 0 }

test/refactoring/ReplaceBodiesWithFatalError/Outputs/basic/L3-5.swift.expected

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var foo: Int = 0
22

3-
var bar: Int { fatalError() }
3+
var bar: Int {
4+
fatalError()
5+
}
46

57
var qux: Int = 0 {
68
willSet { print("hi") }

test/refactoring/ReplaceBodiesWithFatalError/Outputs/basic/L30-44.swift.expected

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,22 @@ class Foo {
2727

2828
var qux: Int {
2929
willSet { print("hi") }
30-
didSet { fatalError() }
30+
didSet {
31+
fatalError()
32+
}
3133
}
3234

33-
init() { fatalError() }
35+
init() {
36+
fatalError()
37+
}
3438

35-
func nonGeneric() -> Int { fatalError() }
39+
func nonGeneric() -> Int {
40+
fatalError()
41+
}
3642

37-
func generic<T>(_: T) -> Int { fatalError() }
43+
func generic<T>(_: T) -> Int {
44+
fatalError()
45+
}
3846

3947
deinit {
4048
print("bye")

test/refactoring/ReplaceBodiesWithFatalError/Outputs/basic/L51-59.swift.expected

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ class Foo {
4949
}
5050

5151
extension Foo {
52-
func nonGeneric2() -> Int { fatalError() }
52+
func nonGeneric2() -> Int {
53+
fatalError()
54+
}
5355

54-
func generic2<T>(_: T) -> Int { fatalError() }
56+
func generic2<T>(_: T) -> Int {
57+
fatalError()
58+
}
5559
}
5660

5761

test/refactoring/ReplaceBodiesWithFatalError/Outputs/basic/L7-10.swift.expected

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ var bar: Int {
55
}
66

77
var qux: Int = 0 {
8-
willSet { fatalError() }
9-
didSet { fatalError() }
8+
willSet {
9+
fatalError()
10+
}
11+
didSet {
12+
fatalError()
13+
}
1014
}
1115

1216
func generic<T>(_: T) -> Int {

test/refactoring/ReplaceBodiesWithFatalError/Outputs/basic/L8.swift.expected

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ var bar: Int {
55
}
66

77
var qux: Int = 0 {
8-
willSet { fatalError() }
8+
willSet {
9+
fatalError()
10+
}
911
didSet { print("bye") }
1012
}
1113

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var bar: Int {
2+
return 0
3+
}
4+
// CHECK-NO-ENV-LABEL: Action begins
5+
// CHECK-NO-ENV-NEXT: Action ends
6+
7+
// CHECK-ENV-LABEL: Action begins
8+
// CHECK-ENV-NEXT: Replace Function Bodies With 'fatalError()'
9+
// CHECK-ENV-NEXT: Action ends
10+
11+
// replace.bodies.with.fatalError/-replace-bodies-with-fatalError should only be
12+
// found if the appropriate environment variable is set.
13+
14+
// RUN: %refactor -actions -source-filename %s -pos=1:1 -end-pos=3:2 | %FileCheck %s -check-prefix CHECK-NO-ENV
15+
16+
// RUN: env SWIFT_ENABLE_INTERNAL_REFACTORING_ACTIONS=1 %refactor -actions -source-filename %s -pos=1:1 -end-pos=3:2 | %FileCheck %s -check-prefix CHECK-ENV

0 commit comments

Comments
 (0)