Skip to content

Commit b19a7ea

Browse files
committed
[SourceKit] Mark functions on CancellableResult const
1 parent 9ccb2ef commit b19a7ea

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

include/swift/IDE/CancellableResult.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,25 @@ class CancellableResult {
118118

119119
/// Return the result kind this \c CancellableResult represents: success,
120120
/// failure or cancelled.
121-
CancellableResultKind getKind() { return Kind; }
121+
CancellableResultKind getKind() const { return Kind; }
122122

123123
/// Assuming that the result represents success, return the underlying result
124124
/// value.
125-
ResultType &getResult() {
125+
const ResultType &getResult() const {
126126
assert(getKind() == CancellableResultKind::Success);
127127
return Result;
128128
}
129129

130130
/// Assuming that the result represents success, retrieve members of the
131131
/// underlying result value.
132-
ResultType *operator->() { return &getResult(); }
132+
const ResultType *operator->() { return &getResult(); }
133133

134134
/// Assuming that the result represents success, return the underlying result
135135
/// value.
136-
ResultType &operator*() { return getResult(); }
136+
const ResultType &operator*() { return getResult(); }
137137

138138
/// Assuming that the result represents a failure, return the error message.
139-
std::string getError() {
139+
std::string getError() const {
140140
assert(getKind() == CancellableResultKind::Failure);
141141
return Error;
142142
}
@@ -152,7 +152,7 @@ class CancellableResult {
152152
template <typename NewResultType>
153153
void
154154
mapAsync(llvm::function_ref<
155-
void(ResultType &,
155+
void(const ResultType &,
156156
llvm::function_ref<void(CancellableResult<NewResultType>)>)>
157157
Transform,
158158
llvm::function_ref<void(CancellableResult<NewResultType>)> Handle) {
@@ -170,6 +170,24 @@ class CancellableResult {
170170
break;
171171
}
172172
}
173+
174+
/// If the result represents success, invoke \p Transform to create a success
175+
/// result containing new backing data.
176+
///
177+
/// If the result represents error or cancelled, propagate that kind without
178+
/// modification.
179+
template <typename NewResultType>
180+
CancellableResult<NewResultType>
181+
map(llvm::function_ref<NewResultType(const ResultType &)> Transform) {
182+
switch (getKind()) {
183+
case CancellableResultKind::Success:
184+
return CancellableResult<NewResultType>::success(Transform(getResult()));
185+
case CancellableResultKind::Failure:
186+
return CancellableResult<NewResultType>::failure(getError());
187+
case CancellableResultKind::Cancelled:
188+
return CancellableResult<NewResultType>::cancelled();
189+
}
190+
}
173191
};
174192

175193
} // namespace ide

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,8 +1147,9 @@ static bool performWithCompletionLikeOperationParams(
11471147
}
11481148

11491149
template <typename ResultType>
1150-
static int printResult(CancellableResult<ResultType> Result,
1151-
llvm::function_ref<int(ResultType &)> PrintSuccess) {
1150+
static int
1151+
printResult(CancellableResult<ResultType> Result,
1152+
llvm::function_ref<int(const ResultType &)> PrintSuccess) {
11521153
switch (Result.getKind()) {
11531154
case CancellableResultKind::Success: {
11541155
return PrintSuccess(Result.getResult());
@@ -1166,7 +1167,7 @@ static int printResult(CancellableResult<ResultType> Result,
11661167
static int printTypeContextInfo(
11671168
CancellableResult<TypeContextInfoResult> CancellableResult) {
11681169
return printResult<TypeContextInfoResult>(
1169-
CancellableResult, [](TypeContextInfoResult &Result) {
1170+
CancellableResult, [](const TypeContextInfoResult &Result) {
11701171
llvm::outs() << "-----BEGIN TYPE CONTEXT INFO-----\n";
11711172
for (auto resultItem : Result.Results) {
11721173
llvm::outs() << "- TypeName: ";
@@ -1227,7 +1228,7 @@ static int doTypeContextInfo(const CompilerInvocation &InitInvok,
12271228
static int printConformingMethodList(
12281229
CancellableResult<ConformingMethodListResults> CancellableResult) {
12291230
return printResult<ConformingMethodListResults>(
1230-
CancellableResult, [](ConformingMethodListResults &Results) {
1231+
CancellableResult, [](const ConformingMethodListResults &Results) {
12311232
auto Result = Results.Result;
12321233
if (!Result) {
12331234
return 0;
@@ -1396,7 +1397,7 @@ static int printCodeCompletionResults(
13961397
bool PrintAnnotatedDescription) {
13971398
llvm::raw_fd_ostream &OS = llvm::outs();
13981399
return printResult<CodeCompleteResult>(
1399-
CancellableResult, [&](CodeCompleteResult &Result) {
1400+
CancellableResult, [&](const CodeCompleteResult &Result) {
14001401
printCodeCompletionResultsImpl(
14011402
Result.ResultSink.Results, OS, IncludeKeywords, IncludeComments,
14021403
IncludeSourceText, PrintAnnotatedDescription,

0 commit comments

Comments
 (0)