From 4bd3e6e02324f913e8927fe69d32c0aafe9fc831 Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 22 Aug 2022 14:17:56 -0700 Subject: Make Optional lower to PointerType instead of a struct. (#2373) --- source/slang/slang-check-type.cpp | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) (limited to 'source/slang/slang-check-type.cpp') diff --git a/source/slang/slang-check-type.cpp b/source/slang/slang-check-type.cpp index aa2c69126..c59e1b308 100644 --- a/source/slang/slang-check-type.cpp +++ b/source/slang/slang-check-type.cpp @@ -187,15 +187,27 @@ namespace Slang return DeclRefType::create(m_astBuilder, innerDeclRef); } + bool isManagedType(Type* type) + { + if (auto declRefValueType = as(type)) + { + if (as(declRefValueType->declRef.getDecl())) + return true; + if (as(declRefValueType->declRef.getDecl())) + return true; + } + return false; + } + bool SemanticsVisitor::CoerceToProperTypeImpl( TypeExp const& typeExp, Type** outProperType, DiagnosticSink* diagSink) { + Type* result = nullptr; Type* type = typeExp.type; auto originalExpr = typeExp.exp; auto expr = originalExpr; - if(!type && expr) { expr = maybeResolveOverloadedExpr(expr, LookupMask::type, diagSink); @@ -289,18 +301,26 @@ namespace Slang // ignore non-parameter members } } - if (outProperType) - { - *outProperType = InstantiateGenericType(genericDeclRef, args); - } - return true; + result = InstantiateGenericType(genericDeclRef, args); } // default case: we expect this to already be a proper type - if (outProperType) + if (!result) + { + result = type; + } + + // Check for invalid types. + // We don't allow pointers to managed types. + if (auto ptrType = as(result)) { - *outProperType = type; + if (isManagedType(ptrType->getValueType())) + { + getSink()->diagnose(typeExp.exp, Diagnostics::cannotDefinePtrTypeToManagedResource); + } } + + *outProperType = result; return true; } -- cgit v1.2.3