Skip to content

Commit d3ede19

Browse files
committed
Generalize inference of Error type requirements from typed throws
1 parent ff45fec commit d3ede19

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

lib/AST/RequirementMachine/RequirementMachineRequests.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,8 +849,11 @@ InferredGenericSignatureRequest::evaluate(
849849
// Finish by adding any remaining requirements. This is used to introduce
850850
// inferred same-type requirements when building the generic signature of
851851
// an extension whose extended type is a generic typealias.
852+
SmallVector<Requirement, 4> rawAddedRequirements;
852853
for (const auto &req : addedRequirements)
853-
requirements.push_back({req, SourceLoc(), /*wasInferred=*/true});
854+
desugarRequirement(req, SourceLoc(), rawAddedRequirements, errors);
855+
for (const auto &req : rawAddedRequirements)
856+
requirements.push_back({req, SourceLoc(), /*inferred=*/true});
854857

855858
// Re-order requirements so that inferred requirements appear last. This
856859
// ensures that if an inferred requirement is redundant with some other

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -768,12 +768,10 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
768768
inferenceSources.emplace_back(thrownTypeRepr, thrownType);
769769

770770
// Add conformance of this type to the Error protocol.
771-
if (thrownType->isTypeParameter()) {
772-
if (auto errorProtocol = ctx.getErrorDecl()) {
773-
extraReqs.push_back(
774-
Requirement(RequirementKind::Conformance, thrownType,
775-
errorProtocol->getDeclaredInterfaceType()));
776-
}
771+
if (auto errorProtocol = ctx.getErrorDecl()) {
772+
extraReqs.push_back(
773+
Requirement(RequirementKind::Conformance, thrownType,
774+
errorProtocol->getDeclaredInterfaceType()));
777775
}
778776
}
779777
}

test/decl/func/typed_throws.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,14 @@ struct HasASubscript {
135135

136136
// expected-error@+1{{thrown type 'any Codable & Error' (aka 'any Decodable & Encodable & Error') does not conform to the 'Error' protocol}}
137137
func throwCodableErrors() throws(any Codable & Error) { }
138+
139+
enum Either<First, Second> {
140+
case first(First)
141+
case second(Second)
142+
}
143+
144+
extension Either: Error where First: Error, Second: Error { }
145+
146+
func f<E1, E2>(_ error: Either<E1, E2>) throws(Either<E1, E2>) {
147+
throw error
148+
}

0 commit comments

Comments
 (0)