From 3ca27faa23a92124f26875a6f00bcfc3a1c6431e Mon Sep 17 00:00:00 2001 From: ArielG-NV <159081215+ArielG-NV@users.noreply.github.com> Date: Fri, 30 May 2025 14:54:31 -0700 Subject: Ensure we do not have an `initExpr` on a `VarDecl` inside an `InterfaceDecl` (#7283) * Ensure we do not have an initExpr on a var inside an InterfaceDecl Ensure we do not have an initExpr on a var inside an InterfaceDecl. If we do, send an error. Ensure the language server does not segfault with this error as per the issue. * format code * split tests --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- source/slang/slang-check-decl.cpp | 11 +++++++++++ tests/bugs/static-const-init-expr-with-interface-1.slang | 15 +++++++++++++++ tests/bugs/static-const-init-expr-with-interface-2.slang | 15 +++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 tests/bugs/static-const-init-expr-with-interface-1.slang create mode 100644 tests/bugs/static-const-init-expr-with-interface-2.slang diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 71aa71e69..cc65edd2f 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -2464,6 +2464,17 @@ bool DiagnoseIsAllowedInitExpr(VarDeclBase* varDecl, DiagnosticSink* sink) return false; } + if (as(varDecl->parentDecl)) + { + if (sink && varDecl->initExpr) + sink->diagnose( + varDecl, + Diagnostics::cannotHaveInitializer, + varDecl, + "an interface requirement"); + return false; + } + return true; } diff --git a/tests/bugs/static-const-init-expr-with-interface-1.slang b/tests/bugs/static-const-init-expr-with-interface-1.slang new file mode 100644 index 000000000..bab63c167 --- /dev/null +++ b/tests/bugs/static-const-init-expr-with-interface-1.slang @@ -0,0 +1,15 @@ +//TEST:LANG_SERVER(filecheck=CHECK): + +//HOVER:7,25 +//CHECK:Bug.badVar = 0 +interface Bug +{ + static const int badVar = 0; +} + +RWStructuredBuffer b; +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + b[0] = Bug::badVar; +} diff --git a/tests/bugs/static-const-init-expr-with-interface-2.slang b/tests/bugs/static-const-init-expr-with-interface-2.slang new file mode 100644 index 000000000..6976f1303 --- /dev/null +++ b/tests/bugs/static-const-init-expr-with-interface-2.slang @@ -0,0 +1,15 @@ +//TEST:SIMPLE(filecheck=CHECK): -stage compute -entry computeMain -target spirv + +//CHECK: error 30623 + +interface Bug +{ + static const int badVar = 0; +} + +RWStructuredBuffer b; +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + b[0] = Bug::badVar; +} -- cgit v1.2.3