From a8bc5983eae60ca37e853041e989a654c1247876 Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 21 Aug 2020 01:10:45 -0700 Subject: Allow calling a generic function with an existential value (dynamic dispatch) (#1508) * Allow calling a generic function with an existential value (dynamic dispatch). * Fixes per review comments. * Clean up implementation by having `openExistential` return `ExtractExistentialType` instead of a DeclRef to the interface with a `ThisTypeSubstitution`. * More cleanups Co-authored-by: Tim Foley Co-authored-by: Yong He --- source/slang/slang-check-conformance.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'source/slang/slang-check-conformance.cpp') diff --git a/source/slang/slang-check-conformance.cpp b/source/slang/slang-check-conformance.cpp index 4dcfb3065..e1904dc71 100644 --- a/source/slang/slang-check-conformance.cpp +++ b/source/slang/slang-check-conformance.cpp @@ -268,6 +268,30 @@ namespace Slang } } } + else if (auto extractExistentialType = as(subType)) + { + // An ExtractExistentialType from an existential value of type I + // is a subtype of I. + // We need to check and make sure the interface type of the `ExtractExistentialType` + // is equal to `superType`. + auto interfaceDeclRef = extractExistentialType->interfaceDeclRef; + auto thisTypeSubst = findThisTypeSubstitution(interfaceDeclRef.substitutions.substitutions, interfaceDeclRef.getDecl()); + SLANG_ASSERT(thisTypeSubst && thisTypeSubst == interfaceDeclRef.substitutions.substitutions); + // The interfaceDeclRef in `extractExistentialType` contains a `ThisTypeSubstitution` + // to allow member lookup to return correct substituted types. Here we just need + // to know if that interface is the same as the superType, so we need to exclude + // the `ThisTypeSubstitution` from comparison. + interfaceDeclRef.substitutions.substitutions = thisTypeSubst->outer; + if (interfaceDeclRef.equals(superTypeDeclRef)) + { + if (outWitness) + { + *outWitness = thisTypeSubst->witness; + } + return true; + } + return false; + } else if(auto taggedUnionType = as(subType)) { // A tagged union type conforms to an interface if all of -- cgit v1.2.3