Skip to content

Commit b7ed1c7

Browse files
author
Tim Corringham
committed
Amend template specialization DXASSERT conditions
Clang suppresses template specialization if a fatal error has been reported in order to reduce the risk of a cascade of secondary error diagnostics. However, DXC DXASSERTs if template specialization fails - even if that is due to an unrelated fatal error - which has the unintended result of hiding the fatal error and hence providing no indication of what the problem is. The DXASSERT conditions have been amended so they are no longer raised if a fatal error has been registered.
1 parent cb6f0ba commit b7ed1c7

2 files changed

Lines changed: 43 additions & 13 deletions

File tree

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -868,11 +868,12 @@ GetOrCreateTemplateSpecialization(ASTContext &context, Sema &sema,
868868
if (specializationDecl->getInstantiatedFrom().isNull()) {
869869
// InstantiateClassTemplateSpecialization returns true if it finds an
870870
// error.
871-
DXVERIFY_NOMSG(false ==
872-
sema.InstantiateClassTemplateSpecialization(
873-
NoLoc, specializationDecl,
874-
TemplateSpecializationKind::TSK_ImplicitInstantiation,
875-
true));
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);
876877
}
877878
return context.getTemplateSpecializationType(
878879
TemplateName(templateDecl), templateArgs.data(), templateArgs.size(),
@@ -884,11 +885,12 @@ GetOrCreateTemplateSpecialization(ASTContext &context, Sema &sema,
884885
templateDecl, templateArgsForDecl.data(), templateArgsForDecl.size(),
885886
nullptr);
886887
// InstantiateClassTemplateSpecialization returns true if it finds an error.
887-
DXVERIFY_NOMSG(false ==
888-
sema.InstantiateClassTemplateSpecialization(
889-
NoLoc, specializationDecl,
890-
TemplateSpecializationKind::TSK_ImplicitInstantiation,
891-
true));
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);
892894
templateDecl->AddSpecialization(specializationDecl, InsertPos);
893895
specializationDecl->setImplicit(true);
894896

@@ -936,7 +938,9 @@ static QualType GetOrCreateMatrixSpecialization(
936938
DeclContext::lookup_result lookupResult =
937939
matrixSpecializationType->getAsCXXRecordDecl()->lookup(
938940
DeclarationName(&context.Idents.get(StringRef("h"))));
939-
DXASSERT(!lookupResult.empty(),
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(),
940944
"otherwise matrix handle cannot be looked up");
941945
#endif
942946

@@ -971,7 +975,9 @@ GetOrCreateVectorSpecialization(ASTContext &context, Sema *sema,
971975
DeclContext::lookup_result lookupResult =
972976
vectorSpecializationType->getAsCXXRecordDecl()->lookup(
973977
DeclarationName(&context.Idents.get(StringRef("h"))));
974-
DXASSERT(!lookupResult.empty(),
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(),
975981
"otherwise vector handle cannot be looked up");
976982
#endif
977983

@@ -999,7 +1005,9 @@ GetOrCreateNodeOutputRecordSpecialization(ASTContext &context, Sema *sema,
9991005
DeclContext::lookup_result lookupResult =
10001006
specializationType->getAsCXXRecordDecl()->lookup(
10011007
DeclarationName(&context.Idents.get(StringRef("h"))));
1002-
DXASSERT(!lookupResult.empty(),
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(),
10031011
"otherwise *NodeOutputRecords handle cannot be looked up");
10041012
#endif
10051013

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %dxc -T lib_6_8 -verify %s
2+
3+
// Clang suppresses template specialization if a fatal error has been
4+
// 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
8+
// has been registered, and that the error diagnostic is generated.
9+
10+
float a;
11+
12+
// the include file doesn't exist - this should produce a fatal error diagnostic
13+
// expected-error@+1 {{'a.h' file not found}}
14+
#include "a.h"
15+
16+
void b() {};
17+
18+
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;
22+
}

0 commit comments

Comments
 (0)