Skip to content

Commit 44abeb4

Browse files
authored
Merge pull request #25409 from xedin/rdar-51641323-5.1
[5.1][ConstraintSystem] Replace special locator for return of single expr …
2 parents aab538e + a844bcf commit 44abeb4

File tree

7 files changed

+48
-24
lines changed

7 files changed

+48
-24
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,7 +2300,7 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
23002300
CS.TC.isConvertibleTo(srcFT->getResult(), contextualType, CS.DC)) {
23012301

23022302
auto locator =
2303-
CS.getConstraintLocator(expr, ConstraintLocator::ContextualType);
2303+
CS.getConstraintLocator(expr, LocatorPathElt::getContextualType());
23042304
ContextualFailure failure =
23052305
ContextualFailure(nullptr, CS, srcFT, contextualType, locator);
23062306
auto diagnosed = failure.diagnoseAsError();
@@ -2381,7 +2381,7 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
23812381
if (contextualType->isExistentialType()) {
23822382
MissingContextualConformanceFailure failure(
23832383
expr, CS, CTP, exprType, contextualType,
2384-
CS.getConstraintLocator(expr, ConstraintLocator::ContextualType));
2384+
CS.getConstraintLocator(expr, LocatorPathElt::getContextualType()));
23852385
return failure.diagnoseAsError();
23862386
}
23872387

@@ -5724,7 +5724,7 @@ bool FailureDiagnosis::diagnoseClosureExpr(
57245724

57255725
MissingArgumentsFailure failure(
57265726
expr, CS, fnType, inferredArgCount - actualArgCount,
5727-
CS.getConstraintLocator(CE, ConstraintLocator::ContextualType));
5727+
CS.getConstraintLocator(CE, LocatorPathElt::getContextualType()));
57285728
return failure.diagnoseAsError();
57295729
}
57305730

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,8 +2934,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
29342934
// literals and expressions representing an implicit return type of the single
29352935
// expression functions.
29362936
if (auto elt = locator.last()) {
2937-
if (elt->getKind() == ConstraintLocator::ClosureResult ||
2938-
elt->getKind() == ConstraintLocator::SingleExprFuncResultType) {
2937+
if (elt->isClosureResult() || elt->isResultOfSingleExprFunction()) {
29392938
if (kind >= ConstraintKind::Subtype &&
29402939
(type1->isUninhabited() || type2->isVoid())) {
29412940
increaseScore(SK_FunctionConversion);
@@ -5397,8 +5396,9 @@ ConstraintSystem::simplifyKeyPathConstraint(Type keyPathTy,
53975396
{rootTy, valueTy});
53985397
// Let's check whether deduced key path type would match
53995398
// expected contextual one.
5400-
return matchTypes(resolvedKPTy, keyPathTy, ConstraintKind::Bind, subflags,
5401-
locator.withPathElement(ConstraintLocator::ContextualType));
5399+
return matchTypes(
5400+
resolvedKPTy, keyPathTy, ConstraintKind::Bind, subflags,
5401+
locator.withPathElement(LocatorPathElt::getContextualType()));
54025402
}
54035403

54045404
ConstraintSystem::SolutionKind

lib/Sema/CSSolver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,10 +1172,10 @@ ConstraintSystem::solveImpl(Expr *&expr,
11721172
if (getContextualTypePurpose() == CTP_YieldByReference)
11731173
constraintKind = ConstraintKind::Bind;
11741174

1175+
bool isForSingleExprFunction =
1176+
getContextualTypePurpose() == CTP_ReturnSingleExpr;
11751177
auto *convertTypeLocator = getConstraintLocator(
1176-
expr, getContextualTypePurpose() == CTP_ReturnSingleExpr
1177-
? ConstraintLocator::SingleExprFuncResultType
1178-
: ConstraintLocator::ContextualType);
1178+
expr, LocatorPathElt::getContextualType(isForSingleExprFunction));
11791179

11801180
if (allowFreeTypeVariables == FreeTypeVariableBinding::UnresolvedType) {
11811181
convertType = convertType.transform([&](Type type) -> Type {

lib/Sema/ConstraintLocator.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, Expr *anchor,
8181
case KeyPathType:
8282
case KeyPathRoot:
8383
case KeyPathValue:
84-
case SingleExprFuncResultType:
8584
if (unsigned numValues = numNumericValuesInPathElement(elt.getKind())) {
8685
id.AddInteger(elt.getValue());
8786
if (numValues > 1)
@@ -351,7 +350,10 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) {
351350
break;
352351

353352
case ContextualType:
354-
out << "contextual type";
353+
if (elt.isResultOfSingleExprFunction())
354+
out << "expected result type of the function with a single expression";
355+
else
356+
out << "contextual type";
355357
break;
356358

357359
case SynthesizedArgument:
@@ -373,10 +375,6 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) {
373375
case KeyPathValue:
374376
out << " keypath value";
375377
break;
376-
377-
case SingleExprFuncResultType:
378-
out << " expected result type of the function with a single expression";
379-
break;
380378
}
381379
}
382380
out << ']';

lib/Sema/ConstraintLocator.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
133133
KeyPathRoot,
134134
/// The value of a key path
135135
KeyPathValue,
136-
/// The expected type of the function with a single expression body.
137-
SingleExprFuncResultType,
138136
};
139137

140138
/// Determine the number of numeric values used for the given path
@@ -163,13 +161,12 @@ class ConstraintLocator : public llvm::FoldingSetNode {
163161
case Witness:
164162
case ImplicitlyUnwrappedDisjunctionChoice:
165163
case DynamicLookupResult:
166-
case ContextualType:
167164
case KeyPathType:
168165
case KeyPathRoot:
169166
case KeyPathValue:
170-
case SingleExprFuncResultType:
171167
return 0;
172168

169+
case ContextualType:
173170
case OpenedGeneric:
174171
case GenericArgument:
175172
case NamedTupleElement:
@@ -236,7 +233,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
236233
case KeyPathType:
237234
case KeyPathRoot:
238235
case KeyPathValue:
239-
case SingleExprFuncResultType:
240236
return 0;
241237

242238
case FunctionArgument:
@@ -394,6 +390,10 @@ class ConstraintLocator : public llvm::FoldingSetNode {
394390
return PathElement(base);
395391
}
396392

393+
static PathElement getContextualType(bool isForSingleExprFunction = false) {
394+
return PathElement(ContextualType, isForSingleExprFunction);
395+
}
396+
397397
/// Retrieve the kind of path element.
398398
PathElementKind getKind() const {
399399
switch (static_cast<StoredKind>(storedKind)) {
@@ -502,6 +502,17 @@ class ConstraintLocator : public llvm::FoldingSetNode {
502502
bool isKeyPathComponent() const {
503503
return getKind() == PathElementKind::KeyPathComponent;
504504
}
505+
506+
bool isClosureResult() const {
507+
return getKind() == PathElementKind::ClosureResult;
508+
}
509+
510+
/// Determine whether this element points to the contextual type
511+
/// associated with result of a single expression function.
512+
bool isResultOfSingleExprFunction() const {
513+
return getKind() == PathElementKind::ContextualType ? bool(getValue())
514+
: false;
515+
}
505516
};
506517

507518
/// Return the summary flags for an entire path.

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,8 +2225,8 @@ Type TypeChecker::typeCheckExpressionImpl(Expr *&expr, DeclContext *dc,
22252225
Type convertTo = convertType.getType();
22262226
if (options.contains(TypeCheckExprFlags::ExpressionTypeMustBeOptional)) {
22272227
assert(!convertTo && "convertType and type check options conflict");
2228-
auto *convertTypeLocator = cs.getConstraintLocator(
2229-
cs.getConstraintLocator(expr), ConstraintLocator::ContextualType);
2228+
auto *convertTypeLocator =
2229+
cs.getConstraintLocator(expr, LocatorPathElt::getContextualType());
22302230
Type var = cs.createTypeVariable(convertTypeLocator);
22312231
convertTo = getOptionalType(expr->getLoc(), var);
22322232
}
@@ -2610,7 +2610,7 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
26102610

26112611
// Save the locator we're using for the expression.
26122612
Locator =
2613-
cs.getConstraintLocator(expr, ConstraintLocator::ContextualType);
2613+
cs.getConstraintLocator(expr, LocatorPathElt::getContextualType());
26142614

26152615
// Collect constraints from the pattern.
26162616
Type patternType = cs.generateConstraints(pattern, Locator);

test/type/opaque.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,18 @@ struct OtherGeneric<X, Y, Z> {
379379
var y: GenericWithOpaqueAssoc<Y>.Assoc
380380
var z: GenericWithOpaqueAssoc<Z>.Assoc
381381
}
382+
383+
384+
protocol P_51641323 {
385+
associatedtype T
386+
387+
var foo: Self.T { get }
388+
}
389+
390+
func rdar_51641323() {
391+
struct Foo: P_51641323 {
392+
var foo: some P_51641323 { {} }
393+
// expected-error@-1 {{return type of property 'foo' requires that '() -> ()' conform to 'P_51641323'}}
394+
// expected-note@-2 {{opaque return type declared here}}
395+
}
396+
}

0 commit comments

Comments
 (0)