Skip to content
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
5 changes: 4 additions & 1 deletion lib/AST/RequirementMachine/RequirementMachineRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,8 +849,11 @@ InferredGenericSignatureRequest::evaluate(
// Finish by adding any remaining requirements. This is used to introduce
// inferred same-type requirements when building the generic signature of
// an extension whose extended type is a generic typealias.
SmallVector<Requirement, 4> rawAddedRequirements;
for (const auto &req : addedRequirements)
requirements.push_back({req, SourceLoc(), /*wasInferred=*/true});
desugarRequirement(req, SourceLoc(), rawAddedRequirements, errors);
for (const auto &req : rawAddedRequirements)
requirements.push_back({req, SourceLoc(), /*inferred=*/true});

// Re-order requirements so that inferred requirements appear last. This
// ensures that if an inferred requirement is redundant with some other
Expand Down
10 changes: 10 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12248,6 +12248,16 @@ ConstraintSystem::simplifyKeyPathConstraint(
// { root in root[keyPath: kp] }.
boundRoot = fnTy->getParams()[0].getParameterType();
boundValue = fnTy->getResult();

// Key paths never throw, so if the function has a thrown error type
// that is a type variable, infer it to be Never.
if (auto thrownError = fnTy->getThrownError()) {
if (thrownError->isTypeVariableOrMember()) {
(void)matchTypes(
thrownError, getASTContext().getNeverType(),
ConstraintKind::Equal, TMF_GenerateConstraints, locator);
}
}
}

if (boundRoot &&
Expand Down
10 changes: 4 additions & 6 deletions lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,12 +768,10 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
inferenceSources.emplace_back(thrownTypeRepr, thrownType);

// Add conformance of this type to the Error protocol.
if (thrownType->isTypeParameter()) {
if (auto errorProtocol = ctx.getErrorDecl()) {
extraReqs.push_back(
Requirement(RequirementKind::Conformance, thrownType,
errorProtocol->getDeclaredInterfaceType()));
}
if (auto errorProtocol = ctx.getErrorDecl()) {
extraReqs.push_back(
Requirement(RequirementKind::Conformance, thrownType,
errorProtocol->getDeclaredInterfaceType()));
}
}
}
Expand Down
22 changes: 21 additions & 1 deletion test/decl/func/typed_throws.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ func mapArray<T, U, E: Error>(_ array: [T], body: (T) throws(E) -> U) throws(E)
return resultArray
}

struct Person {
var name: String
}

func addOrThrowUntyped(_ i: Int, _ j: Int) throws -> Int { i + j }
func addOrThrowMyError(_ i: Int, _ j: Int) throws(MyError) -> Int { i + j }

func testMapArray(numbers: [Int]) {
func testMapArray(numbers: [Int], friends: [Person]) {
// Note: try is not required, because this throws Never
_ = mapArray(numbers) { $0 + 1 }

Expand All @@ -101,6 +105,11 @@ func testMapArray(numbers: [Int]) {
} catch {
let _: Int = error // expected-error{{cannot convert value of type 'MyError' to specified type 'Int'}}
}

do {
// Keypath-as-function
_ = mapArray(friends, body: \Person.name)
}
}

// Inference of Error conformance from the use of a generic parameter in typed
Expand All @@ -126,3 +135,14 @@ struct HasASubscript {

// expected-error@+1{{thrown type 'any Codable & Error' (aka 'any Decodable & Encodable & Error') does not conform to the 'Error' protocol}}
func throwCodableErrors() throws(any Codable & Error) { }

enum Either<First, Second> {
case first(First)
case second(Second)
}

extension Either: Error where First: Error, Second: Error { }

func f<E1, E2>(_ error: Either<E1, E2>) throws(Either<E1, E2>) {
throw error
}