Skip to content

Commit d7fc22a

Browse files
committed
[IDE] Simplify isImplicitSingleExpressionReturn
Use the generalized implied result logic, and rename to `isImpliedResult` since that's really what we're querying here, and it needs to handle implicit-last-exprs if enabled.
1 parent 61a4148 commit d7fc22a

16 files changed

+67
-95
lines changed

include/swift/IDE/AfterPoundExprCompletion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace ide {
2626
class AfterPoundExprCompletion : public TypeCheckCompletionCallback {
2727
struct Result {
2828
Type ExpectedTy;
29-
bool IsImplicitSingleExpressionReturn;
29+
bool IsImpliedResult;
3030

3131
/// Whether the surrounding context is async and thus calling async
3232
/// functions is supported.

include/swift/IDE/CodeCompletionContext.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class CodeCompletionContext {
3939
/// There is no known contextual type. All types are equally good.
4040
None,
4141

42-
/// There is a contextual type from a single-expression closure/function
43-
/// body. The context is a hint, and enables unresolved member completion,
44-
/// but should not hide any results.
45-
SingleExpressionBody,
42+
/// There is a contextual type from e.g a single-expression closure/function
43+
/// body, where the return is implied. The context is a hint, and enables
44+
/// unresolved member completion, but should not hide any results.
45+
Implied,
4646

4747
/// There are known contextual types, or there aren't but a nonvoid type is
4848
/// expected.

include/swift/IDE/CodeCompletionResultType.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ class ExpectedTypeContext {
4444
/// Pre typechecked type of the expression at the completion position.
4545
Type IdealType;
4646

47-
/// Whether the `ExpectedTypes` comes from a single-expression body, e.g.
47+
/// Whether the `ExpectedTypes` comes from an implied result, e.g.
4848
/// `foo({ here })`.
4949
///
5050
/// Since the input may be incomplete, we take into account that the types are
5151
/// only a hint.
52-
bool IsImplicitSingleExpressionReturn = false;
52+
bool IsImpliedResult = false;
5353
bool PreferNonVoid = false;
5454

5555
/// If not empty, \c PossibleTypes are ignored and types that have an
@@ -86,7 +86,7 @@ class ExpectedTypeContext {
8686
if (!IdealType || !Other.IdealType || !IdealType->isEqual(Other.IdealType)) {
8787
IdealType = Type();
8888
}
89-
IsImplicitSingleExpressionReturn |= Other.IsImplicitSingleExpressionReturn;
89+
IsImpliedResult |= Other.IsImpliedResult;
9090
PreferNonVoid &= Other.PreferNonVoid;
9191
ExpectedCustomAttributeKinds |= Other.ExpectedCustomAttributeKinds;
9292
}
@@ -96,7 +96,7 @@ class ExpectedTypeContext {
9696
void setIdealType(Type IdealType) { this->IdealType = IdealType; }
9797

9898
bool requiresNonVoid() const {
99-
if (IsImplicitSingleExpressionReturn)
99+
if (IsImpliedResult)
100100
return false;
101101
if (PreferNonVoid)
102102
return true;
@@ -105,13 +105,12 @@ class ExpectedTypeContext {
105105
return llvm::all_of(PossibleTypes, [](Type Ty) { return !Ty->isVoid(); });
106106
}
107107

108-
bool isImplicitSingleExpressionReturn() const {
109-
return IsImplicitSingleExpressionReturn;
108+
bool isImpliedResult() const {
109+
return IsImpliedResult;
110110
}
111111

112-
void
113-
setIsImplicitSingleExpressionReturn(bool IsImplicitSingleExpressionReturn) {
114-
this->IsImplicitSingleExpressionReturn = IsImplicitSingleExpressionReturn;
112+
void setIsImpliedResult(bool IsImpliedResult) {
113+
this->IsImpliedResult = IsImpliedResult;
115114
}
116115

117116
bool getPreferNonVoid() const { return PreferNonVoid; }

include/swift/IDE/CompletionLookup.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
234234
void setIsStaticMetatype(bool value) { IsStaticMetatype = value; }
235235

236236
void setExpectedTypes(
237-
ArrayRef<Type> Types, bool isImplicitSingleExpressionReturn,
238-
bool preferNonVoid = false,
237+
ArrayRef<Type> Types, bool isImpliedResult, bool preferNonVoid = false,
239238
OptionSet<CustomAttributeKind> expectedCustomAttributeKinds = {}) {
240-
expectedTypeContext.setIsImplicitSingleExpressionReturn(
241-
isImplicitSingleExpressionReturn);
239+
expectedTypeContext.setIsImpliedResult(isImpliedResult);
242240
expectedTypeContext.setPreferNonVoid(preferNonVoid);
243241
expectedTypeContext.setPossibleTypes(Types);
244242
expectedTypeContext.setExpectedCustomAttributeKinds(
@@ -269,8 +267,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
269267
if (expectedTypeContext.empty() &&
270268
!expectedTypeContext.getPreferNonVoid()) {
271269
return CodeCompletionContext::TypeContextKind::None;
272-
} else if (expectedTypeContext.isImplicitSingleExpressionReturn()) {
273-
return CodeCompletionContext::TypeContextKind::SingleExpressionBody;
270+
} else if (expectedTypeContext.isImpliedResult()) {
271+
return CodeCompletionContext::TypeContextKind::Implied;
274272
} else {
275273
return CodeCompletionContext::TypeContextKind::Required;
276274
}

include/swift/IDE/ExprCompletion.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ namespace ide {
2323
class ExprTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
2424
public:
2525
struct Result {
26-
/// If the code completion expression is an implicit return in a
26+
/// If the code completion expression is an implied result, e.g in a
2727
/// single-expression closure.
28-
bool IsImplicitSingleExpressionReturn;
28+
bool IsImpliedResult;
2929

3030
/// Whether the surrounding context is async and thus calling async
3131
/// functions is supported.
@@ -75,7 +75,7 @@ class ExprTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
7575
/// If \c AddUnresolvedMemberCompletions is false, the
7676
/// \p UnresolvedMemberBaseType is ignored.
7777
void addResult(
78-
bool IsImplicitSingleExpressionReturn, bool IsInAsyncContext,
78+
bool IsImpliedResult, bool IsInAsyncContext,
7979
Type UnresolvedMemberBaseType,
8080
llvm::SmallDenseMap<const VarDecl *, Type> SolutionSpecificVarTypes);
8181

include/swift/IDE/PostfixCompletion.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ class PostfixCompletionCallback : public TypeCheckCompletionCallback {
5050
/// we know that we can't retrieve a value from it anymore.
5151
bool ExpectsNonVoid;
5252

53-
/// If the code completion expression occurs as a single statement in a
54-
/// single-expression closure. In such cases we don't want to disfavor
55-
/// results that produce 'Void' because the user might intend to make the
56-
/// closure a multi-statment closure, in which case this expression is no
57-
/// longer implicitly returned.
58-
bool IsImplicitSingleExpressionReturn;
53+
/// If the code completion expression occurs as e.g a single statement in a
54+
/// single-expression closure, where the return is implied. In such cases
55+
/// we don't want to disfavor results that produce 'Void' because the user
56+
/// might intend to make the closure a multi-statment closure, in which case
57+
/// this expression is no longer implicitly returned.
58+
bool IsImpliedResult;
5959

6060
/// Whether the surrounding context is async and thus calling async
6161
/// functions is supported.

include/swift/IDE/TypeCheckCompletionCallback.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,17 @@ struct WithSolutionSpecificVarTypesRAII {
119119
static void setInterfaceType(VarDecl *VD, Type Ty);
120120
};
121121

122-
/// Whether the given completion expression is the only expression in its
123-
/// containing closure or function body and its value is implicitly returned.
122+
/// Whether the given completion expression is an implied result of a closure
123+
/// or function (e.g in a single-expression closure where the return is
124+
/// implicit).
124125
///
125126
/// If these conditions are met, code completion needs to avoid penalizing
126127
/// completion results that don't match the expected return type when
127128
/// computing type relations, as since no return statement was explicitly
128129
/// written by the user, it's possible they intend the single expression not
129130
/// as the return value but merely the first entry in a multi-statement body
130131
/// they just haven't finished writing yet.
131-
bool isImplicitSingleExpressionReturn(constraints::ConstraintSystem &CS,
132-
Expr *CompletionExpr);
132+
bool isImpliedResult(const constraints::Solution &S, Expr *CompletionExpr);
133133

134134
/// Returns \c true iff the decl context \p DC allows calling async functions.
135135
bool isContextAsync(const constraints::Solution &S, DeclContext *DC);

include/swift/IDE/UnresolvedMemberCompletion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class UnresolvedMemberTypeCheckCompletionCallback
2727
: public TypeCheckCompletionCallback {
2828
struct Result {
2929
Type ExpectedTy;
30-
bool IsImplicitSingleExpressionReturn;
30+
bool IsImpliedResult;
3131

3232
/// Whether the surrounding context is async and thus calling async
3333
/// functions is supported.

lib/IDE/AfterPoundExprCompletion.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ using namespace swift::constraints;
2222
using namespace swift::ide;
2323

2424
void AfterPoundExprCompletion::sawSolutionImpl(const constraints::Solution &S) {
25-
auto &CS = S.getConstraintSystem();
2625
Type ExpectedTy = getTypeForCompletion(S, CompletionExpr);
2726

2827
bool IsAsync = isContextAsync(S, DC);
@@ -32,8 +31,8 @@ void AfterPoundExprCompletion::sawSolutionImpl(const constraints::Solution &S) {
3231
return R.ExpectedTy->isEqual(ExpectedTy);
3332
};
3433
if (!llvm::any_of(Results, IsEqual)) {
35-
bool SingleExprBody = isImplicitSingleExpressionReturn(CS, CompletionExpr);
36-
Results.push_back({ExpectedTy, SingleExprBody, IsAsync});
34+
bool IsImpliedResult = isImpliedResult(S, CompletionExpr);
35+
Results.push_back({ExpectedTy, IsImpliedResult, IsAsync});
3736
}
3837
}
3938

@@ -50,8 +49,7 @@ void AfterPoundExprCompletion::collectResults(
5049
UnifiedTypeContext.setPreferNonVoid(true);
5150

5251
for (auto &Result : Results) {
53-
Lookup.setExpectedTypes({Result.ExpectedTy},
54-
Result.IsImplicitSingleExpressionReturn,
52+
Lookup.setExpectedTypes({Result.ExpectedTy}, Result.IsImpliedResult,
5553
/*expectsNonVoid=*/true);
5654
Lookup.addPoundAvailable(ParentStmtKind);
5755
Lookup.addObjCPoundKeywordCompletions(/*needPound=*/false);

lib/IDE/ArgumentCompletion.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,7 @@ void ArgumentTypeCheckCompletionCallback::collectResults(
346346
for (auto &Result : Results) {
347347
if (Result.IncludeSignature) {
348348
Lookup.setHaveLParen(true);
349-
Lookup.setExpectedTypes(ExpectedCallTypes,
350-
/*isImplicitSingleExpressionReturn=*/false);
349+
Lookup.setExpectedTypes(ExpectedCallTypes, /*isImpliedResult=*/false);
351350

352351
auto SemanticContext = SemanticContextKind::None;
353352
NominalTypeDecl *BaseNominal = nullptr;

0 commit comments

Comments
 (0)