Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit 8d950ff

Browse files
committed
Merge branch 'master' of github.com:apple/swift into tensorflow-stage
* 'master' of github.com:apple/swift: Sema: Move preCheckExpression() into its own file [ConstraintSystem] Infer whether locator is related to return of a single-expression function
2 parents 83e22e3 + 2765df6 commit 8d950ff

File tree

8 files changed

+1898
-1895
lines changed

8 files changed

+1898
-1895
lines changed

lib/Sema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ add_swift_host_library(swiftSema STATIC
4444
MiscDiagnostics.cpp
4545
PCMacro.cpp
4646
PlaygroundTransform.cpp
47+
PreCheckExpr.cpp
4748
ResilienceDiagnostics.cpp
4849
SourceLoader.cpp
4950
TypeCheckAccess.cpp

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3784,9 +3784,8 @@ bool ConstraintSystem::generateConstraints(
37843784

37853785
// Substitute type variables in for unresolved types.
37863786
if (allowFreeTypeVariables == FreeTypeVariableBinding::UnresolvedType) {
3787-
bool isForSingleExprFunction = (ctp == CTP_ReturnSingleExpr);
3788-
auto *convertTypeLocator = getConstraintLocator(
3789-
expr, LocatorPathElt::ContextualType(isForSingleExprFunction));
3787+
auto *convertTypeLocator =
3788+
getConstraintLocator(expr, LocatorPathElt::ContextualType());
37903789

37913790
convertType = convertType.transform([&](Type type) -> Type {
37923791
if (type->is<UnresolvedType>()) {

lib/Sema/CSSimplify.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5256,9 +5256,15 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
52565256
}
52575257

52585258
// Single expression function with implicit `return`.
5259-
if (elt->isResultOfSingleExprFunction()) {
5260-
increaseScore(SK_FunctionConversion);
5261-
return getTypeMatchSuccess();
5259+
if (elt->is<LocatorPathElt::ContextualType>()) {
5260+
auto anchor = locator.getAnchor();
5261+
auto contextualInfo = getContextualTypeInfo(anchor);
5262+
assert(contextualInfo &&
5263+
"Found contextual type locator without additional information");
5264+
if (contextualInfo->purpose == CTP_ReturnSingleExpr) {
5265+
increaseScore(SK_FunctionConversion);
5266+
return getTypeMatchSuccess();
5267+
}
52625268
}
52635269
}
52645270
}
@@ -10541,9 +10547,8 @@ void ConstraintSystem::addContextualConversionConstraint(
1054110547
}
1054210548

1054310549
// Add the constraint.
10544-
bool isForSingleExprFunction = (purpose == CTP_ReturnSingleExpr);
10545-
auto *convertTypeLocator = getConstraintLocator(
10546-
expr, LocatorPathElt::ContextualType(isForSingleExprFunction));
10550+
auto *convertTypeLocator =
10551+
getConstraintLocator(expr, LocatorPathElt::ContextualType());
1054710552
addConstraint(constraintKind, getType(expr), conversionType,
1054810553
convertTypeLocator, /*isFavored*/ true);
1054910554
}

lib/Sema/ConstraintLocator.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,6 @@ unsigned LocatorPathElt::getNewSummaryFlags() const {
9797
llvm_unreachable("Unhandled PathElementKind in switch.");
9898
}
9999

100-
bool LocatorPathElt::isResultOfSingleExprFunction() const {
101-
if (auto elt = getAs<ContextualType>())
102-
return elt->isForSingleExprFunction();
103-
return false;
104-
}
105-
106100
/// Determine whether given locator points to the subscript reference
107101
/// e.g. `foo[0]` or `\Foo.[0]`
108102
bool ConstraintLocator::isSubscriptMemberRef() const {
@@ -412,10 +406,7 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) const {
412406
break;
413407

414408
case ContextualType:
415-
if (elt.isResultOfSingleExprFunction())
416-
out << "expected result type of the function with a single expression";
417-
else
418-
out << "contextual type";
409+
out << "contextual type";
419410
break;
420411

421412
case SynthesizedArgument: {

lib/Sema/ConstraintLocator.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
139139
bool isClosureResult() const {
140140
return getKind() == PathElementKind::ClosureResult;
141141
}
142-
143-
/// Determine whether this element points to the contextual type
144-
/// associated with result of a single expression function.
145-
bool isResultOfSingleExprFunction() const;
146142
};
147143

148144
/// Return the summary flags for an entire path.
@@ -647,20 +643,6 @@ class LocatorPathElt::ClosureBody final : public StoredIntegerElement<1> {
647643
}
648644
};
649645

650-
class LocatorPathElt::ContextualType final : public StoredIntegerElement<1> {
651-
public:
652-
ContextualType(bool isForSingleExprFunction = false)
653-
: StoredIntegerElement(ConstraintLocator::ContextualType, isForSingleExprFunction) {}
654-
655-
/// Whether this element points to the contextual type associated with the
656-
/// result of a single expression function.
657-
bool isForSingleExprFunction() const { return bool(getValue()); }
658-
659-
static bool classof(const LocatorPathElt *elt) {
660-
return elt->getKind() == ConstraintLocator::ContextualType;
661-
}
662-
};
663-
664646
class LocatorPathElt::Witness final : public StoredPointerElement<ValueDecl> {
665647
public:
666648
Witness(ValueDecl *witness)

lib/Sema/ConstraintLocatorPathElts.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ CUSTOM_LOCATOR_PATH_ELT(ClosureBody)
6161
SIMPLE_LOCATOR_PATH_ELT(ConstructorMember)
6262

6363
/// The desired contextual type passed in to the constraint system.
64-
CUSTOM_LOCATOR_PATH_ELT(ContextualType)
64+
SIMPLE_LOCATOR_PATH_ELT(ContextualType)
6565

6666
/// A result of an expression involving dynamic lookup.
6767
SIMPLE_LOCATOR_PATH_ELT(DynamicLookupResult)

0 commit comments

Comments
 (0)