Skip to content

Commit

Permalink
Fix issue in wrongly using base contructor
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizhangNV committed Jan 16, 2025
1 parent e8db8ac commit 07caeb2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
12 changes: 4 additions & 8 deletions cmake/SlangTarget.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -505,14 +505,10 @@ function(slang_add_target dir type)
endif()
install(
TARGETS ${target} ${export_args}
ARCHIVE DESTINATION ${archive_subdir}
${ARGN}
LIBRARY DESTINATION ${library_subdir}
${ARGN}
RUNTIME DESTINATION ${runtime_subdir}
${ARGN}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
${ARGN}
ARCHIVE DESTINATION ${archive_subdir} ${ARGN}
LIBRARY DESTINATION ${library_subdir} ${ARGN}
RUNTIME DESTINATION ${runtime_subdir} ${ARGN}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ${ARGN}
)
endmacro()

Expand Down
45 changes: 29 additions & 16 deletions source/slang/slang-check-conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,19 @@ bool SemanticsVisitor::_invokeExprForExplicitCtor(
fromInitializerListExpr->args);

ctorInvokeExpr = CheckTerm(ctorInvokeExpr);

if (!canCoerce(toType, ctorInvokeExpr->type, ctorInvokeExpr, nullptr))
{
// We can report error here because struct having explicit constructor is
// not a C-Style struct, we should not fall back to the legacy initializer list.
getSink()->diagnose(
fromInitializerListExpr->loc,
Diagnostics::typeMismatch,
toType,
ctorInvokeExpr->type);
return false;
}

if (outExpr && ctorInvokeExpr)
{
*outExpr = ctorInvokeExpr;
Expand Down Expand Up @@ -423,6 +436,19 @@ bool SemanticsVisitor::_invokeExprForSynthesizedCtor(

ctorInvokeExpr = subVisitor.CheckExpr(ctorInvokeExpr);

auto errorHandled = [&]()
{
if (!isCStyle)
{
Slang::ComPtr<ISlangBlob> blob;
tempSink.getBlobIfNeeded(blob.writeRef());
getSink()->diagnoseRaw(
Severity::Error,
static_cast<char const*>(blob->getBufferPointer()));
}
return false;
};

if (ctorInvokeExpr)
{
// The reason we need to check the coercion again is that the ResolveInvoke() could still
Expand All @@ -436,41 +462,28 @@ bool SemanticsVisitor::_invokeExprForSynthesizedCtor(
// will be called after 'ResolveInvoke()'. However, in this initialize list to synthesized
// constructor translation path, 'coerce()' will not be called. But that is the only case,
// therefore, we will do a quick check here.
//
// TODO: should we improve the ResolveInvoke() to handle this case? Because A.__init(int a)
// should not be found at first place, Base class cannot construct a Derived class.
if (!tempSink.getErrorCount())
{
if (!canCoerce(toType, ctorInvokeExpr->type, ctorInvokeExpr, nullptr))
{
// TODO:
// 1. Create a more explainable error message.
// 2. We should not diagnose here, because we will need a fall back mechanism.
tempSink.diagnose(
fromInitializerListExpr->loc,
Diagnostics::typeMismatch,
toType,
ctorInvokeExpr->type);
return false;
return errorHandled();
}
else
{
if (outExpr)
*outExpr = ctorInvokeExpr;

return true;
}
}
else
{
if (isCStyle)
{
return false;
}
Slang::ComPtr<ISlangBlob> blob;
tempSink.getBlobIfNeeded(blob.writeRef());
getSink()->diagnoseRaw(
Severity::Error,
static_cast<char const*>(blob->getBufferPointer()));
return errorHandled();
}
}
return false;
Expand Down

0 comments on commit 07caeb2

Please sign in to comment.