Skip to content

[Conformance checking] Better handling of escaping function types #15350

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 2 commits into from
Mar 19, 2018
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
14 changes: 12 additions & 2 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ static std::tuple<Type, Type, OptionalAdjustmentKind>
getTypesToCompare(ValueDecl *reqt, Type reqtType, bool reqtTypeIsIUO,
Type witnessType, bool witnessTypeIsIUO,
VarianceKind variance) {
// If the witness type is noescape but the requirement type is not,
// adjust the witness type to be escaping. This permits a limited form of
// covariance.
bool reqNoescapeToEscaping = false;
(void)adjustInferredAssociatedType(reqtType, reqNoescapeToEscaping);
bool witnessNoescapeToEscaping = false;
Type adjustedWitnessType =
adjustInferredAssociatedType(witnessType, witnessNoescapeToEscaping);
if (witnessNoescapeToEscaping && !reqNoescapeToEscaping)
witnessType = adjustedWitnessType;

// For @objc protocols, deal with differences in the optionality.
// FIXME: It probably makes sense to extend this to non-@objc
// protocols as well, but this requires more testing.
Expand All @@ -152,8 +163,7 @@ getTypesToCompare(ValueDecl *reqt, Type reqtType, bool reqtTypeIsIUO,
reqtType = reqtValueType;
}
bool witnessIsOptional = false;
if (Type witnessValueType =
witnessType->getOptionalObjectType()) {
if (Type witnessValueType = witnessType->getOptionalObjectType()) {
witnessIsOptional = true;
witnessType = witnessValueType;
}
Expand Down
7 changes: 7 additions & 0 deletions lib/Sema/TypeCheckProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,13 @@ RequirementMatch matchWitness(TypeChecker &tc,
AssociatedTypeDecl *getReferencedAssocTypeOfProtocol(Type type,
ProtocolDecl *proto);

/// Perform any necessary adjustments to the inferred associated type to
/// make it suitable for later use.
///
/// \param noescapeToEscaping Will be set \c true if this operation performed
/// the noescape-to-escaping adjustment.
Type adjustInferredAssociatedType(Type type, bool &noescapeToEscaping);

}

#endif // SWIFT_SEMA_PROTOCOL_H
31 changes: 30 additions & 1 deletion lib/Sema/TypeCheckProtocolInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,28 @@ AssociatedTypeInference::inferTypeWitnessesViaAssociatedType(
return result;
}

Type swift::adjustInferredAssociatedType(Type type, bool &noescapeToEscaping) {
// If we have an optional type, adjust its wrapped type.
if (auto optionalObjectType = type->getOptionalObjectType()) {
auto newOptionalObjectType =
adjustInferredAssociatedType(optionalObjectType, noescapeToEscaping);
if (newOptionalObjectType.getPointer() == optionalObjectType.getPointer())
return type;

return OptionalType::get(newOptionalObjectType);
}

// If we have a noescape function type, make it escaping.
if (auto funcType = type->getAs<FunctionType>()) {
if (funcType->isNoEscape()) {
noescapeToEscaping = true;
return FunctionType::get(funcType->getParams(), funcType->getResult(),
funcType->getExtInfo().withNoEscape(false));
}
}
return type;
}

/// Attempt to resolve a type witness via a specific value witness.
InferredAssociatedTypesByWitness
AssociatedTypeInference::inferTypeWitnessesViaValueWitness(ValueDecl *req,
Expand Down Expand Up @@ -662,10 +684,17 @@ AssociatedTypeInference::inferTypeWitnessesViaValueWitness(ValueDecl *req,
if (secondType->hasError())
return true;

// Adjust the type to a type that can be written explicitly.
bool noescapeToEscaping = false;
Type inferredType =
adjustInferredAssociatedType(secondType, noescapeToEscaping);
if (!inferredType->isMaterializable())
return true;

auto proto = Conformance->getProtocol();
if (auto assocType = getReferencedAssocTypeOfProtocol(firstDepMember,
proto)) {
Inferred.Inferred.push_back({assocType, secondType});
Inferred.Inferred.push_back({assocType, inferredType});
}

// Always allow mismatches here.
Expand Down
13 changes: 13 additions & 0 deletions test/SILGen/witnesses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,16 @@ class CrashableBase {
// CHECK-NEXT: return [[RESULT]] : $()

class GenericCrashable<T> : CrashableBase, Crashable {}

// rdar://problem/35297911: allow witness with a noescape parameter to
// match a requirement with an escaping paameter.
protocol EscapingReq {
func f(_: @escaping (Int) -> Int)
}

// CHECK-LABEL: sil private [transparent] [thunk] @$S9witnesses18EscapingCovarianceVAA0B3ReqA2aDP1fyyS2icFTW : $@convention(witness_method: EscapingReq) (@owned @callee_guaranteed (Int) -> Int, @in_guaranteed EscapingCovariance) -> ()
// CHECK-NOT: return
// CHECK: convert_escape_to_noescape %0
struct EscapingCovariance: EscapingReq {
func f(_: (Int) -> Int) { }
}
39 changes: 39 additions & 0 deletions test/decl/protocol/conforms/associated_type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,42 @@ class Foo: FooType {
func foo(action: (Bar) -> Void) {
}
}

// rdar://problem/35297911: noescape function types
protocol P1 {
associatedtype A

func f(_: A)
}

struct X1a : P1 {
func f(_: @escaping (Int) -> Int) { }
}

struct X1b : P1 {
typealias A = (Int) -> Int

func f(_: @escaping (Int) -> Int) { }
}

struct X1c : P1 {
typealias A = (Int) -> Int

func f(_: (Int) -> Int) { }
}

struct X1d : P1 {
func f(_: (Int) -> Int) { }
}

protocol P2 {
func f(_: (Int) -> Int) // expected-note{{protocol requires function 'f' with type '((Int) -> Int) -> ()'; do you want to add a stub?}}
}

struct X2a : P2 {
func f(_: (Int) -> Int) { }
}

struct X2b : P2 { // expected-error{{type 'X2b' does not conform to protocol 'P2'}}
func f(_: @escaping (Int) -> Int) { } // expected-note{{candidate has non-matching type '(@escaping (Int) -> Int) -> ()'}}
}