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-lower-generic-function.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'source/slang/slang-ir-lower-generic-function.cpp') diff --git a/source/slang/slang-ir-lower-generic-function.cpp b/source/slang/slang-ir-lower-generic-function.cpp index f2b1d1a6a..c003d6125 100644 --- a/source/slang/slang-ir-lower-generic-function.cpp +++ b/source/slang/slang-ir-lower-generic-function.cpp @@ -296,8 +296,11 @@ struct GenericFunctionLoweringContext // and emission of wrapper functions. void lowerWitnessTable(IRWitnessTable* witnessTable) { - auto interfaceType = - maybeLowerInterfaceType(cast(witnessTable->getConformanceType())); + IRInterfaceType* conformanceType = as(witnessTable->getConformanceType()); + if (!conformanceType) + return; + + auto interfaceType = maybeLowerInterfaceType(conformanceType); IRBuilder builderStorage(sharedContext->module); auto builder = &builderStorage; builder->setInsertBefore(witnessTable); @@ -353,8 +356,17 @@ struct GenericFunctionLoweringContext return; if (witnessTableType->getConformanceType()->findDecoration()) return; - auto interfaceType = - maybeLowerInterfaceType(cast(witnessTableType->getConformanceType())); + + IRInterfaceType* conformanceType = + as(witnessTableType->getConformanceType()); + + // NoneWitness generates conformance types which aren't interfaces. In + // that case, the method can just be skipped entirely, since there's no + // real witness for it and it should be in unreachable code at this + // point. + if (!conformanceType) + return; + auto interfaceType = maybeLowerInterfaceType(conformanceType); interfaceRequirementVal = sharedContext->findInterfaceRequirementVal( interfaceType, lookupInst->getRequirementKey()); -- cgit v1.2.3