Skip to content

Sema: Fix cycle in structural type resolution via TypeChecker::diagnoseInvalidFunctionType() #41594

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
9 changes: 7 additions & 2 deletions lib/Sema/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,12 @@ bool TypeChecker::diagnoseInvalidFunctionType(FunctionType *fnTy, SourceLoc loc,
Optional<FunctionTypeRepr *>repr,
DeclContext *dc,
Optional<TypeResolutionStage> stage) {
// Some of the below checks trigger cycles if we don't have a generic
// signature yet; we'll run the checks again in
// TypeResolutionStage::Interface.
if (stage == TypeResolutionStage::Structural)
return false;

// If the type has a placeholder, don't try to diagnose anything now since
// we'll produce a better diagnostic when (if) the expression successfully
// typechecks.
Expand Down Expand Up @@ -582,8 +588,7 @@ bool TypeChecker::diagnoseInvalidFunctionType(FunctionType *fnTy, SourceLoc loc,

// `@differentiable` function types must return a differentiable type and have
// differentiable (or `@noDerivative`) parameters.
if (extInfo.isDifferentiable() &&
stage != TypeResolutionStage::Structural) {
if (extInfo.isDifferentiable()) {
auto result = fnTy->getResult();
auto params = fnTy->getParams();
auto diffKind = extInfo.getDifferentiabilityKind();
Expand Down
9 changes: 9 additions & 0 deletions test/Generics/protocol_typealias_cycle_3.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %target-swift-frontend -typecheck %s -requirement-machine-protocol-signatures=on

protocol P {
typealias MyFunction =
@convention(c) (UnsafeMutableRawPointer?,
UnsafeMutableRawPointer?,
UnsafeMutableRawPointer?) -> CInt
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the cycle here? Just curious.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

DeclContext::getGenericSignature() => ... => TypeAliasDecl::getStructuralType() => TypeChecker::diagnoseInvalidFunctionType() => TypeBase::isRepresentable() => DeclContext::getGenericSignature()

I could fix isRepresentable() but there's no reason to perform semantic checks in structural mode at all, since we're going to revisit it in interface type mode anyway.