Skip to content

Infer error conformance for type parameters used in typed throws #69181

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
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
19 changes: 14 additions & 5 deletions lib/AST/RequirementMachine/RequirementLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,15 +562,17 @@ struct InferRequirementsWalker : public TypeWalker {
// - `@differentiable(_linear)`: add
// `T: Differentiable`, `T == T.TangentVector` requirements.
if (auto *fnTy = ty->getAs<AnyFunctionType>()) {
// Add a new conformance constraint for a fixed protocol.
auto addConformanceConstraint = [&](Type type, ProtocolDecl *protocol) {
Requirement req(RequirementKind::Conformance, type,
protocol->getDeclaredInterfaceType());
desugarRequirement(req, SourceLoc(), reqs, errors);
};

auto &ctx = module->getASTContext();
auto *differentiableProtocol =
ctx.getProtocol(KnownProtocolKind::Differentiable);
if (differentiableProtocol && fnTy->isDifferentiable()) {
auto addConformanceConstraint = [&](Type type, ProtocolDecl *protocol) {
Requirement req(RequirementKind::Conformance, type,
protocol->getDeclaredInterfaceType());
desugarRequirement(req, SourceLoc(), reqs, errors);
};
auto addSameTypeConstraint = [&](Type firstType,
AssociatedTypeDecl *assocType) {
auto secondType = assocType->getDeclaredInterfaceType()
Expand All @@ -596,6 +598,13 @@ struct InferRequirementsWalker : public TypeWalker {
constrainParametersAndResult(fnTy->getDifferentiabilityKind() ==
DifferentiabilityKind::Linear);
}

// Infer that the thrown error type conforms to Error.
if (auto thrownError = fnTy->getThrownError()) {
if (auto errorProtocol = ctx.getErrorDecl()) {
addConformanceConstraint(thrownError, errorProtocol);
}
}
}

if (!ty->isSpecialized())
Expand Down
22 changes: 22 additions & 0 deletions lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,28 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
inferenceSources.emplace_back(typeRepr, type);
}

// Handle the thrown error type.
auto effectiveFunc = func ? func
: subscr ? subscr->getEffectfulGetAccessor()
: nullptr;
if (effectiveFunc) {
if (auto thrownTypeRepr = effectiveFunc->getThrownTypeRepr()) {
auto thrownOptions = baseOptions | TypeResolutionFlags::Direct;
const auto thrownType = resolution.withOptions(thrownOptions)
.resolveType(thrownTypeRepr);

// Add this type as an inference source.
inferenceSources.emplace_back(thrownTypeRepr, thrownType);

// Add conformance of this type to the Error protocol.
if (auto errorProtocol = ctx.getErrorDecl()) {
extraReqs.push_back(
Requirement(RequirementKind::Conformance, thrownType,
errorProtocol->getDeclaredInterfaceType()));
}
}
}

// Gather requirements from the result type.
auto *resultTypeRepr = [&subscr, &func, &macro]() -> TypeRepr * {
if (subscr) {
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 @@ -33,7 +33,6 @@ func testThrownMyErrorType() {
func throwsGeneric<T: Error>(errorType: T.Type) throws(T) { }

func throwsBadGeneric<T>(errorType: T.Type) throws(T) { }
// expected-error@-1{{thrown type 'T' does not conform to the 'Error' protocol}}

func throwsUnusedInSignature<T: Error>() throws(T) { }
// expected-error@-1{{generic parameter 'T' is not used in function signature}}
Expand Down Expand Up @@ -103,3 +102,24 @@ func testMapArray(numbers: [Int]) {
let _: Int = error // expected-error{{cannot convert value of type 'MyError' to specified type 'Int'}}
}
}

// Inference of Error conformance from the use of a generic parameter in typed
// throws.
func requiresError<E: Error>(_: E.Type) { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A -debug-generic-signatures | FileCheck test might be more direct than the requiresError() thing, but it probably doesn't matter


func infersThrowing<E>(_ error: E.Type) throws(E) {
requiresError(error)
}

func infersThrowingNested<E>(_ body: () throws(E) -> Void) {
requiresError(E.self)
}

struct HasASubscript {
subscript<E>(_: E.Type) -> Int {
get throws(E) {
requiresError(E.self)
return 0
}
}
}