Skip to content

Commit 76a8f5f

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 0e7591a commit 76a8f5f

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
@@ -830,11 +830,12 @@ GetOrCreateTemplateSpecialization(ASTContext &context, Sema &sema,
830830
if (specializationDecl->getInstantiatedFrom().isNull()) {
831831
// InstantiateClassTemplateSpecialization returns true if it finds an
832832
// error.
833-
DXVERIFY_NOMSG(false ==
834-
sema.InstantiateClassTemplateSpecialization(
835-
NoLoc, specializationDecl,
836-
TemplateSpecializationKind::TSK_ImplicitInstantiation,
837-
true));
833+
bool errorFound = sema.InstantiateClassTemplateSpecialization(
834+
NoLoc, specializationDecl,
835+
TemplateSpecializationKind::TSK_ImplicitInstantiation, true);
836+
// Template specialization is suppressed if a fatal error has already been
837+
// registered so don't assert in such cases.
838+
DXVERIFY_NOMSG(sema.Diags.hasFatalErrorOccurred() || !errorFound);
838839
}
839840
return context.getTemplateSpecializationType(
840841
TemplateName(templateDecl), templateArgs.data(), templateArgs.size(),
@@ -846,11 +847,12 @@ GetOrCreateTemplateSpecialization(ASTContext &context, Sema &sema,
846847
templateDecl, templateArgsForDecl.data(), templateArgsForDecl.size(),
847848
nullptr);
848849
// InstantiateClassTemplateSpecialization returns true if it finds an error.
849-
DXVERIFY_NOMSG(false ==
850-
sema.InstantiateClassTemplateSpecialization(
851-
NoLoc, specializationDecl,
852-
TemplateSpecializationKind::TSK_ImplicitInstantiation,
853-
true));
850+
bool errorFound = sema.InstantiateClassTemplateSpecialization(
851+
NoLoc, specializationDecl,
852+
TemplateSpecializationKind::TSK_ImplicitInstantiation, true);
853+
// Template specialization is suppressed if a fatal error has already been
854+
// registered so don't assert in such cases.
855+
DXVERIFY_NOMSG(sema.Diags.hasFatalErrorOccurred() || !errorFound);
854856
templateDecl->AddSpecialization(specializationDecl, InsertPos);
855857
specializationDecl->setImplicit(true);
856858

@@ -898,7 +900,9 @@ static QualType GetOrCreateMatrixSpecialization(
898900
DeclContext::lookup_result lookupResult =
899901
matrixSpecializationType->getAsCXXRecordDecl()->lookup(
900902
DeclarationName(&context.Idents.get(StringRef("h"))));
901-
DXASSERT(!lookupResult.empty(),
903+
// Template specialization is suppressed if a fatal error has been registered
904+
// so only assert if lookup failed for some other reason.
905+
DXASSERT(sema->Diags.hasFatalErrorOccurred() || !lookupResult.empty(),
902906
"otherwise matrix handle cannot be looked up");
903907
#endif
904908

@@ -933,7 +937,9 @@ GetOrCreateVectorSpecialization(ASTContext &context, Sema *sema,
933937
DeclContext::lookup_result lookupResult =
934938
vectorSpecializationType->getAsCXXRecordDecl()->lookup(
935939
DeclarationName(&context.Idents.get(StringRef("h"))));
936-
DXASSERT(!lookupResult.empty(),
940+
// Template specialization is suppressed if a fatal error has been registered
941+
// so only assert if lookup failed for some other reason.
942+
DXASSERT(sema->Diags.hasFatalErrorOccurred() || !lookupResult.empty(),
937943
"otherwise vector handle cannot be looked up");
938944
#endif
939945

@@ -961,7 +967,9 @@ GetOrCreateNodeOutputRecordSpecialization(ASTContext &context, Sema *sema,
961967
DeclContext::lookup_result lookupResult =
962968
specializationType->getAsCXXRecordDecl()->lookup(
963969
DeclarationName(&context.Idents.get(StringRef("h"))));
964-
DXASSERT(!lookupResult.empty(),
970+
// Template specialization is suppressed if a fatal error has been registered
971+
// so only assert if lookup failed for some other reason.
972+
DXASSERT(sema->Diags.hasFatalErrorOccurred() || !lookupResult.empty(),
965973
"otherwise *NodeOutputRecords handle cannot be looked up");
966974
#endif
967975

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)