Skip to content

Commit 7f66c33

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 01007bc commit 7f66c33

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -869,11 +869,12 @@ GetOrCreateTemplateSpecialization(ASTContext &context, Sema &sema,
869869
if (specializationDecl->getInstantiatedFrom().isNull()) {
870870
// InstantiateClassTemplateSpecialization returns true if it finds an
871871
// error.
872-
DXVERIFY_NOMSG(false ==
873-
sema.InstantiateClassTemplateSpecialization(
874-
NoLoc, specializationDecl,
875-
TemplateSpecializationKind::TSK_ImplicitInstantiation,
876-
true));
872+
bool errorFound = sema.InstantiateClassTemplateSpecialization(
873+
NoLoc, specializationDecl,
874+
TemplateSpecializationKind::TSK_ImplicitInstantiation, true);
875+
// Template specialization is suppressed if a fatal error has already been
876+
// registered so don't assert in such cases.
877+
DXVERIFY_NOMSG(sema.Diags.hasFatalErrorOccurred() || !errorFound);
877878
}
878879
return context.getTemplateSpecializationType(
879880
TemplateName(templateDecl), templateArgs.data(), templateArgs.size(),
@@ -885,11 +886,12 @@ GetOrCreateTemplateSpecialization(ASTContext &context, Sema &sema,
885886
templateDecl, templateArgsForDecl.data(), templateArgsForDecl.size(),
886887
nullptr);
887888
// InstantiateClassTemplateSpecialization returns true if it finds an error.
888-
DXVERIFY_NOMSG(false ==
889-
sema.InstantiateClassTemplateSpecialization(
890-
NoLoc, specializationDecl,
891-
TemplateSpecializationKind::TSK_ImplicitInstantiation,
892-
true));
889+
bool errorFound = sema.InstantiateClassTemplateSpecialization(
890+
NoLoc, specializationDecl,
891+
TemplateSpecializationKind::TSK_ImplicitInstantiation, true);
892+
// Template specialization is suppressed if a fatal error has already been
893+
// registered so don't assert in such cases.
894+
DXVERIFY_NOMSG(sema.Diags.hasFatalErrorOccurred() || !errorFound);
893895
templateDecl->AddSpecialization(specializationDecl, InsertPos);
894896
specializationDecl->setImplicit(true);
895897

@@ -937,7 +939,9 @@ static QualType GetOrCreateMatrixSpecialization(
937939
DeclContext::lookup_result lookupResult =
938940
matrixSpecializationType->getAsCXXRecordDecl()->lookup(
939941
DeclarationName(&context.Idents.get(StringRef("h"))));
940-
DXASSERT(!lookupResult.empty(),
942+
// Template specialization is suppressed if a fatal error has been registered
943+
// so only assert if lookup failed for some other reason.
944+
DXASSERT(sema->Diags.hasFatalErrorOccurred() || !lookupResult.empty(),
941945
"otherwise matrix handle cannot be looked up");
942946
#endif
943947

@@ -972,7 +976,9 @@ GetOrCreateVectorSpecialization(ASTContext &context, Sema *sema,
972976
DeclContext::lookup_result lookupResult =
973977
vectorSpecializationType->getAsCXXRecordDecl()->lookup(
974978
DeclarationName(&context.Idents.get(StringRef("h"))));
975-
DXASSERT(!lookupResult.empty(),
979+
// Template specialization is suppressed if a fatal error has been registered
980+
// so only assert if lookup failed for some other reason.
981+
DXASSERT(sema->Diags.hasFatalErrorOccurred() || !lookupResult.empty(),
976982
"otherwise vector handle cannot be looked up");
977983
#endif
978984

@@ -1028,7 +1034,9 @@ GetOrCreateNodeOutputRecordSpecialization(ASTContext &context, Sema *sema,
10281034
DeclContext::lookup_result lookupResult =
10291035
specializationType->getAsCXXRecordDecl()->lookup(
10301036
DeclarationName(&context.Idents.get(StringRef("h"))));
1031-
DXASSERT(!lookupResult.empty(),
1037+
// Template specialization is suppressed if a fatal error has been registered
1038+
// so only assert if lookup failed for some other reason.
1039+
DXASSERT(sema->Diags.hasFatalErrorOccurred() || !lookupResult.empty(),
10321040
"otherwise *NodeOutputRecords handle cannot be looked up");
10331041
#endif
10341042

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)