Skip to content

[ConstraintSystem] Replace special locator for return of single expr … #25383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2239,7 +2239,7 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
CS.TC.isConvertibleTo(srcFT->getResult(), contextualType, CS.DC)) {

auto locator =
CS.getConstraintLocator(expr, ConstraintLocator::ContextualType);
CS.getConstraintLocator(expr, LocatorPathElt::getContextualType());
ContextualFailure failure =
ContextualFailure(nullptr, CS, srcFT, contextualType, locator);
auto diagnosed = failure.diagnoseAsError();
Expand Down Expand Up @@ -2320,7 +2320,7 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
if (contextualType->isExistentialType()) {
MissingContextualConformanceFailure failure(
expr, CS, CTP, exprType, contextualType,
CS.getConstraintLocator(expr, ConstraintLocator::ContextualType));
CS.getConstraintLocator(expr, LocatorPathElt::getContextualType()));
return failure.diagnoseAsError();
}

Expand Down Expand Up @@ -5687,7 +5687,7 @@ bool FailureDiagnosis::diagnoseClosureExpr(

MissingArgumentsFailure failure(
expr, CS, fnType, inferredArgCount - actualArgCount,
CS.getConstraintLocator(CE, ConstraintLocator::ContextualType));
CS.getConstraintLocator(CE, LocatorPathElt::getContextualType()));
return failure.diagnoseAsError();
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2935,8 +2935,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
// literals and expressions representing an implicit return type of the single
// expression functions.
if (auto elt = locator.last()) {
if (elt->getKind() == ConstraintLocator::ClosureResult ||
elt->getKind() == ConstraintLocator::SingleExprFuncResultType) {
if (elt->isClosureResult() || elt->isResultOfSingleExprFunction()) {
if (kind >= ConstraintKind::Subtype &&
(type1->isUninhabited() || type2->isVoid())) {
increaseScore(SK_FunctionConversion);
Expand Down Expand Up @@ -5405,8 +5404,9 @@ ConstraintSystem::simplifyKeyPathConstraint(Type keyPathTy,
{rootTy, valueTy});
// Let's check whether deduced key path type would match
// expected contextual one.
return matchTypes(resolvedKPTy, keyPathTy, ConstraintKind::Bind, subflags,
locator.withPathElement(ConstraintLocator::ContextualType));
return matchTypes(
resolvedKPTy, keyPathTy, ConstraintKind::Bind, subflags,
locator.withPathElement(LocatorPathElt::getContextualType()));
}

ConstraintSystem::SolutionKind
Expand Down
6 changes: 3 additions & 3 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,10 +1176,10 @@ ConstraintSystem::solveImpl(Expr *&expr,
if (getContextualTypePurpose() == CTP_YieldByReference)
constraintKind = ConstraintKind::Bind;

bool isForSingleExprFunction =
getContextualTypePurpose() == CTP_ReturnSingleExpr;
auto *convertTypeLocator = getConstraintLocator(
expr, getContextualTypePurpose() == CTP_ReturnSingleExpr
? ConstraintLocator::SingleExprFuncResultType
: ConstraintLocator::ContextualType);
expr, LocatorPathElt::getContextualType(isForSingleExprFunction));

if (allowFreeTypeVariables == FreeTypeVariableBinding::UnresolvedType) {
convertType = convertType.transform([&](Type type) -> Type {
Expand Down
9 changes: 4 additions & 5 deletions lib/Sema/ConstraintLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, Expr *anchor,
case KeyPathRoot:
case KeyPathValue:
case KeyPathComponentResult:
case SingleExprFuncResultType:
if (unsigned numValues = numNumericValuesInPathElement(elt.getKind())) {
id.AddInteger(elt.getValue());
if (numValues > 1)
Expand Down Expand Up @@ -364,7 +363,10 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) {
break;

case ContextualType:
out << "contextual type";
if (elt.isResultOfSingleExprFunction())
out << "expected result type of the function with a single expression";
else
out << "contextual type";
break;

case SynthesizedArgument:
Expand All @@ -390,9 +392,6 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) {
case KeyPathComponentResult:
out << "key path component result";
break;
case SingleExprFuncResultType:
out << " expected result type of the function with a single expression";
break;
}
}
out << ']';
Expand Down
21 changes: 16 additions & 5 deletions lib/Sema/ConstraintLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
KeyPathValue,
/// The result type of a key path component. Not used for subscripts.
KeyPathComponentResult,
/// The expected type of the function with a single expression body.
SingleExprFuncResultType,
};

/// Determine the number of numeric values used for the given path
Expand Down Expand Up @@ -165,14 +163,13 @@ class ConstraintLocator : public llvm::FoldingSetNode {
case Witness:
case ImplicitlyUnwrappedDisjunctionChoice:
case DynamicLookupResult:
case ContextualType:
case KeyPathType:
case KeyPathRoot:
case KeyPathValue:
case KeyPathComponentResult:
case SingleExprFuncResultType:
return 0;

case ContextualType:
case OpenedGeneric:
case GenericArgument:
case NamedTupleElement:
Expand Down Expand Up @@ -240,7 +237,6 @@ class ConstraintLocator : public llvm::FoldingSetNode {
case KeyPathRoot:
case KeyPathValue:
case KeyPathComponentResult:
case SingleExprFuncResultType:
return 0;

case FunctionArgument:
Expand Down Expand Up @@ -398,6 +394,10 @@ class ConstraintLocator : public llvm::FoldingSetNode {
return PathElement(base);
}

static PathElement getContextualType(bool isForSingleExprFunction = false) {
return PathElement(ContextualType, isForSingleExprFunction);
}

/// Retrieve the kind of path element.
PathElementKind getKind() const {
switch (static_cast<StoredKind>(storedKind)) {
Expand Down Expand Up @@ -506,6 +506,17 @@ class ConstraintLocator : public llvm::FoldingSetNode {
bool isKeyPathComponent() const {
return getKind() == PathElementKind::KeyPathComponent;
}

bool isClosureResult() const {
return getKind() == PathElementKind::ClosureResult;
}

/// Determine whether this element points to the contextual type
/// associated with result of a single expression function.
bool isResultOfSingleExprFunction() const {
return getKind() == PathElementKind::ContextualType ? bool(getValue())
: false;
}
};

/// Return the summary flags for an entire path.
Expand Down
6 changes: 3 additions & 3 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2219,8 +2219,8 @@ Type TypeChecker::typeCheckExpressionImpl(Expr *&expr, DeclContext *dc,
Type convertTo = convertType.getType();
if (options.contains(TypeCheckExprFlags::ExpressionTypeMustBeOptional)) {
assert(!convertTo && "convertType and type check options conflict");
auto *convertTypeLocator = cs.getConstraintLocator(
cs.getConstraintLocator(expr), ConstraintLocator::ContextualType);
auto *convertTypeLocator =
cs.getConstraintLocator(expr, LocatorPathElt::getContextualType());
Type var = cs.createTypeVariable(convertTypeLocator, TVO_CanBindToNoEscape);
convertTo = getOptionalType(expr->getLoc(), var);
}
Expand Down Expand Up @@ -2604,7 +2604,7 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,

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

// Collect constraints from the pattern.
Type patternType = cs.generateConstraints(pattern, Locator);
Expand Down
15 changes: 15 additions & 0 deletions test/type/opaque.swift
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,18 @@ struct OtherGeneric<X, Y, Z> {
var y: GenericWithOpaqueAssoc<Y>.Assoc
var z: GenericWithOpaqueAssoc<Z>.Assoc
}


protocol P_51641323 {
associatedtype T

var foo: Self.T { get }
}

func rdar_51641323() {
struct Foo: P_51641323 {
var foo: some P_51641323 { {} }
// expected-error@-1 {{return type of property 'foo' requires that '() -> ()' conform to 'P_51641323'}}
// expected-note@-2 {{opaque return type declared here}}
}
}