Skip to content

Commit a5db248

Browse files
author
Tim Corringham
committed
Update template specialization assert handling
Amend the fix for DXASSERTS in template specialization by incorporating changes from review comments.
1 parent b7ed1c7 commit a5db248

2 files changed

Lines changed: 47 additions & 52 deletions

File tree

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -868,12 +868,10 @@ GetOrCreateTemplateSpecialization(ASTContext &context, Sema &sema,
868868
if (specializationDecl->getInstantiatedFrom().isNull()) {
869869
// InstantiateClassTemplateSpecialization returns true if it finds an
870870
// error.
871-
bool errorFound = sema.InstantiateClassTemplateSpecialization(
872-
NoLoc, specializationDecl,
873-
TemplateSpecializationKind::TSK_ImplicitInstantiation, true);
874-
// Template specialization is suppressed if a fatal error has already been
875-
// registered so don't assert in such cases.
876-
DXVERIFY_NOMSG(sema.Diags.hasFatalErrorOccurred() || !errorFound);
871+
if (sema.InstantiateClassTemplateSpecialization(
872+
NoLoc, specializationDecl,
873+
TemplateSpecializationKind::TSK_ImplicitInstantiation, true))
874+
return QualType();
877875
}
878876
return context.getTemplateSpecializationType(
879877
TemplateName(templateDecl), templateArgs.data(), templateArgs.size(),
@@ -884,19 +882,20 @@ GetOrCreateTemplateSpecialization(ASTContext &context, Sema &sema,
884882
context, TagDecl::TagKind::TTK_Class, currentDeclContext, NoLoc, NoLoc,
885883
templateDecl, templateArgsForDecl.data(), templateArgsForDecl.size(),
886884
nullptr);
887-
// InstantiateClassTemplateSpecialization returns true if it finds an error.
888-
bool errorFound = sema.InstantiateClassTemplateSpecialization(
889-
NoLoc, specializationDecl,
890-
TemplateSpecializationKind::TSK_ImplicitInstantiation, true);
891-
// Template specialization is suppressed if a fatal error has already been
892-
// registered so don't assert in such cases.
893-
DXVERIFY_NOMSG(sema.Diags.hasFatalErrorOccurred() || !errorFound);
885+
// template specialization isn't performed if a fatal error has occurred
886+
if (!sema.Diags.hasFatalErrorOccurred()) {
887+
// InstantiateClassTemplateSpecialization returns true if it finds an error.
888+
[[maybe_unused]] bool errorFound =
889+
sema.InstantiateClassTemplateSpecialization(
890+
NoLoc, specializationDecl,
891+
TemplateSpecializationKind::TSK_ImplicitInstantiation, true);
892+
assert(!errorFound && "template specialization failed");
893+
}
894894
templateDecl->AddSpecialization(specializationDecl, InsertPos);
895895
specializationDecl->setImplicit(true);
896-
897896
QualType canonType = context.getTypeDeclType(specializationDecl);
898-
DXASSERT(isa<RecordType>(canonType),
899-
"type of non-dependent specialization is not a RecordType");
897+
assert(isa<RecordType>(canonType) &&
898+
"type of non-dependent specialization is not a RecordType");
900899
TemplateArgumentListInfo templateArgumentList(NoLoc, NoLoc);
901900
TemplateArgumentLocInfo NoTemplateArgumentLocInfo;
902901
for (unsigned i = 0; i < templateArgs.size(); i++) {
@@ -931,18 +930,17 @@ static QualType GetOrCreateMatrixSpecialization(
931930
context, *sema, matrixTemplateDecl,
932931
ArrayRef<TemplateArgument>(templateArgs));
933932

934-
#ifndef NDEBUG
935-
// Verify that we can read the field member from the template record.
936-
DXASSERT(matrixSpecializationType->getAsCXXRecordDecl(),
933+
if (!matrixSpecializationType.isNull() &&
934+
!sema->Diags.hasFatalErrorOccurred()) {
935+
assert(matrixSpecializationType->getAsCXXRecordDecl() &&
937936
"type of non-dependent specialization is not a RecordType");
938-
DeclContext::lookup_result lookupResult =
939-
matrixSpecializationType->getAsCXXRecordDecl()->lookup(
940-
DeclarationName(&context.Idents.get(StringRef("h"))));
941-
// Template specialization is suppressed if a fatal error has been registered
942-
// so only assert if lookup failed for some other reason.
943-
DXASSERT(sema->Diags.hasFatalErrorOccurred() || !lookupResult.empty(),
937+
// Verify that we can read the field member from the template record.
938+
[[maybe_unused]] DeclContext::lookup_result lookupResult =
939+
matrixSpecializationType->getAsCXXRecordDecl()->lookup(
940+
DeclarationName(&context.Idents.get(StringRef("h"))));
941+
assert(!lookupResult.empty() &&
944942
"otherwise matrix handle cannot be looked up");
945-
#endif
943+
}
946944

947945
return matrixSpecializationType;
948946
}
@@ -968,18 +966,17 @@ GetOrCreateVectorSpecialization(ASTContext &context, Sema *sema,
968966
context, *sema, vectorTemplateDecl,
969967
ArrayRef<TemplateArgument>(templateArgs));
970968

971-
#ifndef NDEBUG
972-
// Verify that we can read the field member from the template record.
973-
DXASSERT(vectorSpecializationType->getAsCXXRecordDecl(),
969+
if (!vectorSpecializationType.isNull() &&
970+
!sema->Diags.hasFatalErrorOccurred()) {
971+
assert(vectorSpecializationType->getAsCXXRecordDecl() &&
974972
"type of non-dependent specialization is not a RecordType");
975-
DeclContext::lookup_result lookupResult =
976-
vectorSpecializationType->getAsCXXRecordDecl()->lookup(
977-
DeclarationName(&context.Idents.get(StringRef("h"))));
978-
// Template specialization is suppressed if a fatal error has been registered
979-
// so only assert if lookup failed for some other reason.
980-
DXASSERT(sema->Diags.hasFatalErrorOccurred() || !lookupResult.empty(),
973+
// Verify that we can read the field member from the template record.
974+
[[maybe_unused]] DeclContext::lookup_result lookupResult =
975+
vectorSpecializationType->getAsCXXRecordDecl()->lookup(
976+
DeclarationName(&context.Idents.get(StringRef("h"))));
977+
assert(!lookupResult.empty() &&
981978
"otherwise vector handle cannot be looked up");
982-
#endif
979+
}
983980

984981
return vectorSpecializationType;
985982
}
@@ -998,18 +995,16 @@ GetOrCreateNodeOutputRecordSpecialization(ASTContext &context, Sema *sema,
998995
QualType specializationType = GetOrCreateTemplateSpecialization(
999996
context, *sema, templateDecl, ArrayRef<TemplateArgument>(templateArgs));
1000997

1001-
#ifdef DBG
1002-
// Verify that we can read the field member from the template record.
1003-
DXASSERT(specializationType->getAsCXXRecordDecl(),
998+
if (!specializationType.isNull() && !sema->Diags.hasFatalErrorOccurred()) {
999+
assert(specializationType->getAsCXXRecordDecl() &&
10041000
"type of non-dependent specialization is not a RecordType");
1005-
DeclContext::lookup_result lookupResult =
1006-
specializationType->getAsCXXRecordDecl()->lookup(
1007-
DeclarationName(&context.Idents.get(StringRef("h"))));
1008-
// Template specialization is suppressed if a fatal error has been registered
1009-
// so only assert if lookup failed for some other reason.
1010-
DXASSERT(sema->Diags.hasFatalErrorOccurred() || !lookupResult.empty(),
1001+
// Verify that we can read the field member from the template record.
1002+
[[maybe_unused]] DeclContext::lookup_result lookupResult =
1003+
specializationType->getAsCXXRecordDecl()->lookup(
1004+
DeclarationName(&context.Idents.get(StringRef("h"))));
1005+
assert(!lookupResult.empty() &&
10111006
"otherwise *NodeOutputRecords handle cannot be looked up");
1012-
#endif
1007+
}
10131008

10141009
return specializationType;
10151010
}

tools/clang/test/DXC/specialization_with_fatal_error.hlsl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
// Clang suppresses template specialization if a fatal error has been
44
// registered (this reduces the risk of a cascade of secondary errors).
5-
// However, DXC DXASSERTs if a template specialization fails - which
6-
// prevents the error diagnostic being generated.
7-
// We check here that a DXASSERT is no longer raised if a fatal error
5+
// However, DXC asserted if a template specialization failed - which
6+
// prevented the error diagnostic being generated.
7+
// We check here that an assert is no longer raised if a fatal error
88
// has been registered, and that the error diagnostic is generated.
99

1010
float a;
@@ -16,7 +16,7 @@ float a;
1616
void b() {};
1717

1818
int3 c(int X) {
19-
// DXASSERT was triggered if include file a.h doesn't exist, and the error
20-
// diagnostic was not produced.
21-
return X.xxx;
19+
// an assert was triggered for the expression below when include file a.h
20+
// doesn't exist, and the error diagnostic expected above was not produced.
21+
return X.xxx;
2222
}

0 commit comments

Comments
 (0)