# Adding a "does X conform to Y" request

**URL:** <https://forums.swift.org/t/adding-a-does-x-conform-to-y-request/28349>\
**Category:** Compiler\
**Tags:** question\
**Created:** [August 29, 2019, 7:21am UTC](https://forums.swift.org/t/adding-a-does-x-conform-to-y-request/28349 "2019-08-29T07:21:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan-zheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dan-zheng/32/2534_2.png) [@dan-zheng](https://forums.swift.org/u/dan-zheng)\
**Post date:** [August 29, 2019, 7:21am UTC](https://forums.swift.org/t/adding-a-does-x-conform-to-y-request/28349/1 "2019-08-29T07:21:18Z")

</div>

Hi compiler gurus,

I wonder if there are plans to add a "does X conform to Y" request to the [request evaluator](https://github.com/apple/swift/blob/tensorflow/docs/RequestEvaluator.md) system (or other ways to unify conformance lookup logic)?

I'm curious because there multiple ways to perform conformance lookup (and find associated type witnesses), some of which have caused bugs for the [differentiable programming project](https://forums.swift.org/t/pre-pitch-swift-differentiable-programming-design-overview/25992), which needs `Differentiable` conformance lookup and associated type witness lookup during Sema, SILGen, and SIL transforms.

* * *

In my experience, the static function `TypeChecker::conformsToProtocol` has been the most reliable way of obtaining a conformance (over `ModuleDecl::lookupConformance` and `ModuleDecl::conformsToProtocol`), but it is available only during Sema, not during SIL.

It's totally possible that the current system is fine and we're just using bad APIs - [`TypeBase::getAutoDiffAssociatedTangentSpace(LookupConformanceFn lookupConformance)`](https://github.com/apple/swift/blob/2f377d9cf272317886baefe0ab3ca540b3df5392/lib/AST/Type.cpp#L4484) is one of the worst offenders, using [`DependentMemberType::substBaseType`](https://github.com/apple/swift/blob/2f377d9cf272317886baefe0ab3ca540b3df5392/lib/AST/Type.cpp#L4542) and a `LookupConformanceFn` to find associated type witnesses.

If anyone knows better ways for finding associated type witnesses (via APIs that work from SIL, not just Sema), that would be a big help too.

---

<div class="post-metadata">

**Author:** ![dan-zheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dan-zheng/32/2534_2.png) [@dan-zheng](https://forums.swift.org/u/dan-zheng)\
**Post date:** [August 29, 2019, 7:21am UTC](https://forums.swift.org/t/adding-a-does-x-conform-to-y-request/28349/2 "2019-08-29T07:21:51Z")

</div>

cc @Slava_Pestov @Douglas_Gregor

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [September 4, 2019, 1:51am UTC](https://forums.swift.org/t/adding-a-does-x-conform-to-y-request/28349/3 "2019-09-04T01:51:04Z")

</div>

TypeChecker::conformsToProtocol() is a static method so you can totally call it from SIL as well as Sema. Even non-static methods can be used now because the TypeChecker instance lives forever (but we're trying to gut and eventually remove the TypeChecker singleton altogether).

The only difference between lookupConformance() and conformsToProtocol() is the latter records a dependency for incremental builds, and checks conditional requirements. We want to recover incremental dependencies from the request graph one day. Once the remaining distinction between the two is conditional conformance checking, it would be great to merge them into a single request or function.

I'm wondering what kind of issue you encountered with the other two entry points though. lookupConformnace() is the underlying operation used by TypeChecker::conformsToProtocol(), and ModuleDecl::conformsToProtocol() just calls TypeChecker::conformsToProtocol().

---

<div class="post-metadata">

**Author:** ![dan-zheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dan-zheng/32/2534_2.png) [@dan-zheng](https://forums.swift.org/u/dan-zheng)\
**Post date:** [September 4, 2019, 4:07am UTC](https://forums.swift.org/t/adding-a-does-x-conform-to-y-request/28349/4 "2019-09-04T04:07:06Z")

</div>

> [@Slava\_Pestov](#):
>
> TypeChecker::conformsToProtocol() is a static method so you can totally call it from SIL as well as Sema. Even non-static methods can be used now because the TypeChecker instance lives forever (but we're trying to gut and eventually remove the TypeChecker singleton altogether).

The issue is `TypeChecker.h` lives in `lib/Sema` instead of `include/swift/Sema`, so it can't be included/called from SIL.

Could we please move `TypeChecker.h` to `include/swift/Sema`? That directory is currently mostly empty for some reason, maybe it's intentional for separation of concerns.

* * *

Thanks for the clarification about `lookupConformance` and `conformsToProtocol`!

> [@Slava\_Pestov](#):
>
> I'm wondering what kind of issue you encountered with the other two entry points though. lookupConformnace() is the underlying operation used by TypeChecker::conformsToProtocol(), and ModuleDecl::conformsToProtocol() just calls TypeChecker::conformsToProtocol().

I can't remember specific unresolved issues with `ModuleDecl::lookupConformance` in SIL but I'll make a note if I come across any. (You previously helped with a [`SubstitutionMap`](https://forums.swift.org/t/checking-if-dependentmembertype-conformance-requirement-is-satisfied-by-substitutionmap/25053) question - thanks again.)

Type-checking `@differentiable` attributes `where` clauses (looking up `Differentiable` protocol conformances using the right APIs and arguments) has been a longstanding source of issues though. I posted [a separate question about `@differentiable` attribute](https://forums.swift.org/t/store-differentiable-attribute-requirements-as-genericsignature-instead-of-arrayref-requirement-s/28488) and cc'd you.
