From 786f48d32340c36a06865a333ff9066033b5b2bc Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 12 Aug 2022 07:57:41 -0700 Subject: Fix logic of `is` operator. (#2359) --- source/slang/slang-check-expr.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'source/slang/slang-check-expr.cpp') diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index cb75e3078..2ac02b978 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -1771,30 +1771,35 @@ namespace Slang { // Instead of returning a BoolLiteralExpr, we use a field to indicate this scenario, // so that the language server can still see the original syntax tree. - expr->isAlwaysTrue = true; + expr->constantVal = m_astBuilder->create(); + expr->constantVal->type = m_astBuilder->getBoolType(); + expr->constantVal->value = true; + expr->constantVal->loc = expr->loc; return expr; } // Otherwise, we need to ensure the target type is a subtype of value->type. - // For now we can only support the scenario where `expr->value` is an interface type. - if (!isInterfaceType(originalVal->type)) - { - getSink()->diagnose(expr, Diagnostics::isOperatorValueMustBeInterfaceType); - } expr->value = maybeOpenExistential(originalVal); expr->witnessArg = tryGetSubtypeWitness(expr->typeExpr.type, originalVal->type.type); if (expr->witnessArg) { + // For now we can only support the scenario where `expr->value` is an interface type. + if (!isInterfaceType(originalVal->type)) + { + getSink()->diagnose(expr, Diagnostics::isOperatorValueMustBeInterfaceType); + } return expr; } if (!as(expr->typeExpr.type) && !as(expr->value->type.type)) { - getSink()->diagnose(expr, Diagnostics::typeNotInTheSameHierarchy, expr->value->type.type, expr->typeExpr.type); + // The type is not in the same hierarchy, so we evaluate to false. + expr->constantVal = m_astBuilder->create(); + expr->constantVal->type = m_astBuilder->getBoolType(); + expr->constantVal->value = false; + expr->constantVal->loc = expr->loc; } - - expr->type = m_astBuilder->getErrorType(); return expr; } -- cgit v1.2.3