From 7349dc5cff49cf22c82eb912813e47f30cd7a757 Mon Sep 17 00:00:00 2001 From: Julius Ikkala Date: Sat, 28 Jun 2025 05:39:24 +0300 Subject: Minimal optional constraints (#7422) * Parse optional witness syntax * Allow failing optional constraint * Make `is` work with optional constraint * Allow using optional constraint in checked if statements * Fix tests * Make it work with structs * Fix MSVC build error * Disallow using `as` with optional constraints * Update test to match split is/as errors * Add tests * Fix uninitialized variables in tests * Add tests of incorrect uses & fix related bugs * Mention optional constraints in docs * format code * Fix type unification with NoneWitness * Fix formatting --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: Nathan V. Morrical --- source/slang/slang-ir-specialize-dispatch.cpp | 34 +++++++++++++++++---------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'source/slang/slang-ir-specialize-dispatch.cpp') diff --git a/source/slang/slang-ir-specialize-dispatch.cpp b/source/slang/slang-ir-specialize-dispatch.cpp index e4d1af93a..0c8b248b0 100644 --- a/source/slang/slang-ir-specialize-dispatch.cpp +++ b/source/slang/slang-ir-specialize-dispatch.cpp @@ -232,23 +232,31 @@ void ensureWitnessTableSequentialIDs(SharedGenericsLoweringContext* sharedContex { auto interfaceType = cast(inst->getDataType())->getConformanceType(); - auto interfaceLinkage = interfaceType->findDecoration(); - SLANG_ASSERT( - interfaceLinkage && "An interface type does not have a linkage," - "but a witness table associated with it has one."); - auto interfaceName = interfaceLinkage->getMangledName(); - auto idAllocator = - linkage->mapInterfaceMangledNameToSequentialIDCounters.tryGetValue( - interfaceName); - if (!idAllocator) + if (as(interfaceType)) { - linkage->mapInterfaceMangledNameToSequentialIDCounters[interfaceName] = 0; - idAllocator = + auto interfaceLinkage = interfaceType->findDecoration(); + SLANG_ASSERT( + interfaceLinkage && "An interface type does not have a linkage," + "but a witness table associated with it has one."); + auto interfaceName = interfaceLinkage->getMangledName(); + auto idAllocator = linkage->mapInterfaceMangledNameToSequentialIDCounters.tryGetValue( interfaceName); + if (!idAllocator) + { + linkage->mapInterfaceMangledNameToSequentialIDCounters[interfaceName] = 0; + idAllocator = + linkage->mapInterfaceMangledNameToSequentialIDCounters.tryGetValue( + interfaceName); + } + seqID = *idAllocator; + ++(*idAllocator); + } + else + { + // NoneWitness, has special ID of -1. + seqID = uint32_t(-1); } - seqID = *idAllocator; - ++(*idAllocator); linkage->mapMangledNameToRTTIObjectIndex[witnessTableMangledName] = seqID; } -- cgit v1.2.3