Skip to content

Commit 9cdfe56

Browse files
committed
[Typed throws] Address comments about inferring Error conformances
1 parent fe524c8 commit 9cdfe56

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ struct InferRequirementsWalker : public TypeWalker {
599599
DifferentiabilityKind::Linear);
600600
}
601601

602-
// Infer that the thrown error type conforms to Error.
602+
// Infer that the thrown error type of a function type conforms to Error.
603603
if (auto thrownError = fnTy->getThrownError()) {
604604
if (auto errorProtocol = ctx.getErrorDecl()) {
605605
addConformanceConstraint(thrownError, errorProtocol);

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
758758
: subscr ? subscr->getEffectfulGetAccessor()
759759
: nullptr;
760760
if (effectiveFunc) {
761+
// Infer constraints from the thrown type of a declaration.
761762
if (auto thrownTypeRepr = effectiveFunc->getThrownTypeRepr()) {
762763
auto thrownOptions = baseOptions | TypeResolutionFlags::Direct;
763764
const auto thrownType = resolution.withOptions(thrownOptions)
@@ -767,10 +768,12 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
767768
inferenceSources.emplace_back(thrownTypeRepr, thrownType);
768769

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

0 commit comments

Comments
 (0)