diff --git a/docs/ReleaseNotes.md b/docs/ReleaseNotes.md index 375d68d2fa..f7291b1987 100644 --- a/docs/ReleaseNotes.md +++ b/docs/ReleaseNotes.md @@ -63,6 +63,9 @@ line upon naming the release. Refer to previous for appropriate section names. - The `shared` and `uniform` keywords are removed in HLSL 202x, with compatibility warnings available for earlier language versions [#8482](https://github.com/microsoft/DirectXShaderCompiler/issues/8482). +- HLSL 202x disallows putting cbuffer, tbuffer, or namespace declarations inside + a cbuffer or tbuffer + [#8484](https://github.com/microsoft/DirectXShaderCompiler/issues/8484). #### SPIR-V diff --git a/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td b/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td index cc7a4662f1..650bce5874 100644 --- a/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -7926,6 +7926,8 @@ def err_hlsl_unsupported_object_context "entry function parameters|entry function return type|" "patch constant function parameters|patch constant function return type|" "payload parameters|attributes|builtin template parameters|structured buffers|global variables|groupshared variables}1">; +def err_hlsl_unsupported_declaration_in_buffer : Error< + "unsupported declaration %0 in %select{tbuffer|cbuffer}1 declaration">; def err_hlsl_logical_binop_scalar : Error< "operands for short-circuiting logical binary operator must be scalar, for non-scalar types use '%select{and|or}0'">; def err_hlsl_ternary_scalar : Error< diff --git a/tools/clang/lib/Sema/SemaHLSL.cpp b/tools/clang/lib/Sema/SemaHLSL.cpp index 274adae6d7..4808ecd328 100644 --- a/tools/clang/lib/Sema/SemaHLSL.cpp +++ b/tools/clang/lib/Sema/SemaHLSL.cpp @@ -15363,6 +15363,19 @@ void Sema::ActOnFinishHLSLBuffer(Decl *Dcl, SourceLocation RBrace) { bool HasPackOffset = false; bool HasNonPackOffset = false; for (auto *Field : BufDecl->decls()) { + // HLSL 202x 0005 Cbuffer Contexts proposal restricts the contents of a + // cbuffer to classes (records), functions, variables, and empty + // declarations (see: https://hlsl-tc57.github.io/tc57/proposal/0005/) + if (getLangOpts().HLSLVersion >= hlsl::LangStd::v202x && + (isa(Field) || isa(Field))) { + NamedDecl *ND = cast(Field); + Diag(Field->getLocation(), + diag::err_hlsl_unsupported_declaration_in_buffer) + << ND << BufDecl->isCBuffer(); + Diag(Dcl->getLocation(), diag::note_declared_at); + Dcl->setInvalidDecl(); + } + VarDecl *Var = dyn_cast(Field); if (!Var) continue; diff --git a/tools/clang/test/SemaHLSL/hlsl/202x-cbuffer-contexts.hlsl b/tools/clang/test/SemaHLSL/hlsl/202x-cbuffer-contexts.hlsl new file mode 100644 index 0000000000..212e505152 --- /dev/null +++ b/tools/clang/test/SemaHLSL/hlsl/202x-cbuffer-contexts.hlsl @@ -0,0 +1,57 @@ +// RUN: %dxc -T lib_6_9 -verify -HV 202x %s + +cbuffer A { // expected-note{{declared here}} + int Y; + // expected-error@+1 {{unsupported declaration 'N' in cbuffer declaration}} + namespace N { + } + float4 F5; +} + +cbuffer A { // expected-note{{declared here}} + // expected-error@+1 {{unsupported declaration 'Nested' in cbuffer declaration}} + cbuffer Nested { + } +} + +tbuffer TB { // expected-note{{declared here}} + // expected-error@+1 {{unsupported declaration 'NS' in tbuffer declaration}} + namespace NS {} +} + +tbuffer TB2 { // expected-note{{declared here}} + // expected-error@+1{{unsupported declaration 'CB2' in tbuffer declaration}} + cbuffer CB2 { + int X; + } +} + +namespace Valid { + cbuffer CBValid { + int CompletelyFine; + } + + tbuffer TBValid { + float StillFine; + } + + cbuffer GoingOffTheRails { // expected-note{{declared here}} + // expected-error@+1{{unsupported declaration 'NotCool' in cbuffer declaration}} + tbuffer NotCool { + ; // even if it is empty... + } + } + + cbuffer GoingOffTheRailsAgain { // expected-note{{declared here}} + // expected-error@+1{{unsupported declaration 'StillNotCool' in cbuffer declaration}} + tbuffer StillNotCool { // expected-note{{declared here}} + // expected-error@+1{{unsupported declaration 'Turtles' in tbuffer declaration}} + cbuffer Turtles { // expected-note{{declared here}} + // expected-error@+1{{unsupported declaration 'AllTheWayDown' in cbuffer declaration}} + tbuffer AllTheWayDown { + ; + } + } + } + } +}