Skip to content

Commit 9ea306f

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 cb2be16 commit 9ea306f

File tree

2 files changed

+47
-52
lines changed

2 files changed

+47
-52
lines changed

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -850,12 +850,10 @@ GetOrCreateTemplateSpecialization(ASTContext &context, Sema &sema,
850850
if (specializationDecl->getInstantiatedFrom().isNull()) {
851851
// InstantiateClassTemplateSpecialization returns true if it finds an
852852
// error.
853-
bool errorFound = sema.InstantiateClassTemplateSpecialization(
854-
NoLoc, specializationDecl,
855-
TemplateSpecializationKind::TSK_ImplicitInstantiation, true);
856-
// Template specialization is suppressed if a fatal error has already been
857-
// registered so don't assert in such cases.
858-
DXVERIFY_NOMSG(sema.Diags.hasFatalErrorOccurred() || !errorFound);
853+
if (sema.InstantiateClassTemplateSpecialization(
854+
NoLoc, specializationDecl,
855+
TemplateSpecializationKind::TSK_ImplicitInstantiation, true))
856+
return QualType();
859857
}
860858
return context.getTemplateSpecializationType(
861859
TemplateName(templateDecl), templateArgs.data(), templateArgs.size(),
@@ -866,19 +864,20 @@ GetOrCreateTemplateSpecialization(ASTContext &context, Sema &sema,
866864
context, TagDecl::TagKind::TTK_Class, currentDeclContext, NoLoc, NoLoc,
867865
templateDecl, templateArgsForDecl.data(), templateArgsForDecl.size(),
868866
nullptr);
869-
// InstantiateClassTemplateSpecialization returns true if it finds an error.
870-
bool errorFound = sema.InstantiateClassTemplateSpecialization(
871-
NoLoc, specializationDecl,
872-
TemplateSpecializationKind::TSK_ImplicitInstantiation, true);
873-
// Template specialization is suppressed if a fatal error has already been
874-
// registered so don't assert in such cases.
875-
DXVERIFY_NOMSG(sema.Diags.hasFatalErrorOccurred() || !errorFound);
867+
// template specialization isn't performed if a fatal error has occurred
868+
if (!sema.Diags.hasFatalErrorOccurred()) {
869+
// InstantiateClassTemplateSpecialization returns true if it finds an error.
870+
[[maybe_unused]] bool errorFound =
871+
sema.InstantiateClassTemplateSpecialization(
872+
NoLoc, specializationDecl,
873+
TemplateSpecializationKind::TSK_ImplicitInstantiation, true);
874+
assert(!errorFound && "template specialization failed");
875+
}
876876
templateDecl->AddSpecialization(specializationDecl, InsertPos);
877877
specializationDecl->setImplicit(true);
878-
879878
QualType canonType = context.getTypeDeclType(specializationDecl);
880-
DXASSERT(isa<RecordType>(canonType),
881-
"type of non-dependent specialization is not a RecordType");
879+
assert(isa<RecordType>(canonType) &&
880+
"type of non-dependent specialization is not a RecordType");
882881
TemplateArgumentListInfo templateArgumentList(NoLoc, NoLoc);
883882
TemplateArgumentLocInfo NoTemplateArgumentLocInfo;
884883
for (unsigned i = 0; i < templateArgs.size(); i++) {
@@ -913,18 +912,17 @@ static QualType GetOrCreateMatrixSpecialization(
913912
context, *sema, matrixTemplateDecl,
914913
ArrayRef<TemplateArgument>(templateArgs));
915914

916-
#ifndef NDEBUG
917-
// Verify that we can read the field member from the template record.
918-
DXASSERT(matrixSpecializationType->getAsCXXRecordDecl(),
915+
if (!matrixSpecializationType.isNull() &&
916+
!sema->Diags.hasFatalErrorOccurred()) {
917+
assert(matrixSpecializationType->getAsCXXRecordDecl() &&
919918
"type of non-dependent specialization is not a RecordType");
920-
DeclContext::lookup_result lookupResult =
921-
matrixSpecializationType->getAsCXXRecordDecl()->lookup(
922-
DeclarationName(&context.Idents.get(StringRef("h"))));
923-
// Template specialization is suppressed if a fatal error has been registered
924-
// so only assert if lookup failed for some other reason.
925-
DXASSERT(sema->Diags.hasFatalErrorOccurred() || !lookupResult.empty(),
919+
// Verify that we can read the field member from the template record.
920+
[[maybe_unused]] DeclContext::lookup_result lookupResult =
921+
matrixSpecializationType->getAsCXXRecordDecl()->lookup(
922+
DeclarationName(&context.Idents.get(StringRef("h"))));
923+
assert(!lookupResult.empty() &&
926924
"otherwise matrix handle cannot be looked up");
927-
#endif
925+
}
928926

929927
return matrixSpecializationType;
930928
}
@@ -950,18 +948,17 @@ GetOrCreateVectorSpecialization(ASTContext &context, Sema *sema,
950948
context, *sema, vectorTemplateDecl,
951949
ArrayRef<TemplateArgument>(templateArgs));
952950

953-
#ifndef NDEBUG
954-
// Verify that we can read the field member from the template record.
955-
DXASSERT(vectorSpecializationType->getAsCXXRecordDecl(),
951+
if (!vectorSpecializationType.isNull() &&
952+
!sema->Diags.hasFatalErrorOccurred()) {
953+
assert(vectorSpecializationType->getAsCXXRecordDecl() &&
956954
"type of non-dependent specialization is not a RecordType");
957-
DeclContext::lookup_result lookupResult =
958-
vectorSpecializationType->getAsCXXRecordDecl()->lookup(
959-
DeclarationName(&context.Idents.get(StringRef("h"))));
960-
// Template specialization is suppressed if a fatal error has been registered
961-
// so only assert if lookup failed for some other reason.
962-
DXASSERT(sema->Diags.hasFatalErrorOccurred() || !lookupResult.empty(),
955+
// Verify that we can read the field member from the template record.
956+
[[maybe_unused]] DeclContext::lookup_result lookupResult =
957+
vectorSpecializationType->getAsCXXRecordDecl()->lookup(
958+
DeclarationName(&context.Idents.get(StringRef("h"))));
959+
assert(!lookupResult.empty() &&
963960
"otherwise vector handle cannot be looked up");
964-
#endif
961+
}
965962

966963
return vectorSpecializationType;
967964
}
@@ -980,18 +977,16 @@ GetOrCreateNodeOutputRecordSpecialization(ASTContext &context, Sema *sema,
980977
QualType specializationType = GetOrCreateTemplateSpecialization(
981978
context, *sema, templateDecl, ArrayRef<TemplateArgument>(templateArgs));
982979

983-
#ifdef DBG
984-
// Verify that we can read the field member from the template record.
985-
DXASSERT(specializationType->getAsCXXRecordDecl(),
980+
if (!specializationType.isNull() && !sema->Diags.hasFatalErrorOccurred()) {
981+
assert(specializationType->getAsCXXRecordDecl() &&
986982
"type of non-dependent specialization is not a RecordType");
987-
DeclContext::lookup_result lookupResult =
988-
specializationType->getAsCXXRecordDecl()->lookup(
989-
DeclarationName(&context.Idents.get(StringRef("h"))));
990-
// Template specialization is suppressed if a fatal error has been registered
991-
// so only assert if lookup failed for some other reason.
992-
DXASSERT(sema->Diags.hasFatalErrorOccurred() || !lookupResult.empty(),
983+
// Verify that we can read the field member from the template record.
984+
[[maybe_unused]] DeclContext::lookup_result lookupResult =
985+
specializationType->getAsCXXRecordDecl()->lookup(
986+
DeclarationName(&context.Idents.get(StringRef("h"))));
987+
assert(!lookupResult.empty() &&
993988
"otherwise *NodeOutputRecords handle cannot be looked up");
994-
#endif
989+
}
995990

996991
return specializationType;
997992
}

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)