From 19867ffca6dca7995c799354081219c9e76f13d1 Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 20 Feb 2025 14:48:51 -0800 Subject: Simplify implicit cast ctors for vector & matrix. (#6408) * Simplify implicit cast ctors for vector & matrix. * Fix formatting. * Fix tests. * Fix Falcor test. * Mark __builtin_cast as internal. --- source/slang/slang-check-constraint.cpp | 59 +++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 7 deletions(-) (limited to 'source/slang/slang-check-constraint.cpp') diff --git a/source/slang/slang-check-constraint.cpp b/source/slang/slang-check-constraint.cpp index 872d2616c..642a4bf6a 100644 --- a/source/slang/slang-check-constraint.cpp +++ b/source/slang/slang-check-constraint.cpp @@ -715,13 +715,6 @@ DeclRef SemanticsVisitor::trySolveConstraintSystem( // system as being solved now, as a result of the witness we found. } - // Add a flat cost to all unconstrained generic params. - for (auto typeParamDecl : genericDeclRef.getDecl()->getMembersOfType()) - { - if (!constrainedGenericParams.contains(typeParamDecl)) - outBaseCost += kConversionCost_UnconstraintGenericParam; - } - // Make sure we haven't constructed any spurious constraints // that we aren't able to satisfy: for (auto c : system->constraints) @@ -732,6 +725,58 @@ DeclRef SemanticsVisitor::trySolveConstraintSystem( } } + // Verify that all type coercion constraints can be satisfied. + for (auto constraintDecl : + genericDeclRef.getDecl()->getMembersOfType()) + { + DeclRef constraintDeclRef = + m_astBuilder + ->getGenericAppDeclRef( + genericDeclRef, + args.getArrayView().arrayView, + constraintDecl) + .as(); + auto fromType = constraintDeclRef.substitute(m_astBuilder, constraintDecl->fromType.Ptr()); + auto toType = constraintDeclRef.substitute(m_astBuilder, constraintDecl->toType.Ptr()); + auto conversionCost = getConversionCost(toType, fromType); + if (constraintDecl->findModifier()) + { + if (conversionCost > kConversionCost_GeneralConversion) + { + // The type arguments are not implicitly convertible, return failure. + return DeclRef(); + } + } + else + { + if (conversionCost == kConversionCost_Impossible) + { + // The type arguments are not convertible, return failure. + return DeclRef(); + } + } + if (auto fromDecl = isDeclRefTypeOf(constraintDecl->fromType)) + { + constrainedGenericParams.add(fromDecl.getDecl()); + } + if (auto toDecl = isDeclRefTypeOf(constraintDecl->toType)) + { + constrainedGenericParams.add(toDecl.getDecl()); + } + // If we are to expand the support of type coercion constraint beyond simple builtin core + // module functions, then the witness should be a reference to the conversion function. For + // now, this isn't required, and it is not easy to get it from the coercion logic, so we + // leave it empty. + args.add(m_astBuilder->getTypeCoercionWitness(fromType, toType, DeclRef())); + } + + // Add a flat cost to all unconstrained generic params. + for (auto typeParamDecl : genericDeclRef.getDecl()->getMembersOfType()) + { + if (!constrainedGenericParams.contains(typeParamDecl)) + outBaseCost += kConversionCost_UnconstraintGenericParam; + } + return m_astBuilder->getGenericAppDeclRef(genericDeclRef, args.getArrayView().arrayView); } -- cgit v1.2.3