diff options
| -rw-r--r-- | source/slang/slang-check-decl.cpp | 11 | ||||
| -rw-r--r-- | source/slang/slang-diagnostic-defs.h | 5 | ||||
| -rw-r--r-- | tests/diagnostics/subscript-missing-return-type.slang | 32 |
3 files changed, 47 insertions, 1 deletions
diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 7a0dcb06f..f8a80c09b 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -10405,7 +10405,16 @@ void SemanticsDeclHeaderVisitor::visitAbstractStorageDeclCommon(ContainerDecl* d void SemanticsDeclHeaderVisitor::visitSubscriptDecl(SubscriptDecl* decl) { - decl->returnType = CheckUsableType(decl->returnType, decl); + // __subscript needs to have a return type specified. Check if return type + // is missing (represented as IncompleteExpr) and return an error. + if (decl->returnType.exp && as<IncompleteExpr>(decl->returnType.exp)) + { + getSink()->diagnose(decl, Diagnostics::subscriptMustHaveReturnType); + } + else if (decl->returnType.exp) + { + decl->returnType = CheckUsableType(decl->returnType, decl); + } visitAbstractStorageDeclCommon(decl); diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h index 3babc9a56..0814bb2a0 100644 --- a/source/slang/slang-diagnostic-defs.h +++ b/source/slang/slang-diagnostic-defs.h @@ -1710,6 +1710,11 @@ DIAGNOSTIC( Error, multiDimensionalArrayNotSupported, "multi-dimensional array is not supported.") +DIAGNOSTIC( + 30901, + Error, + subscriptMustHaveReturnType, + "__subscript declaration must have a return type specified after '->'") // 310xx: properties // 311xx: accessors diff --git a/tests/diagnostics/subscript-missing-return-type.slang b/tests/diagnostics/subscript-missing-return-type.slang new file mode 100644 index 000000000..23c2cda5e --- /dev/null +++ b/tests/diagnostics/subscript-missing-return-type.slang @@ -0,0 +1,32 @@ +// Test that __subscript without return type produces proper error message + +//TEST:SIMPLE(filecheck=CHECK): -target spirv + +struct MyType +{ + int val[12]; + + // CHECK: ([[# @LINE+1]]): error 30901: __subscript declaration must have a return type specified after '->' + __subscript(int x, int y) + { + get { return val[x*3 + y]; } + } +} + +// This should compile fine - subscript with return type +struct MyType2 +{ + int val[12]; + __subscript(int x, int y) -> int + { + get { return val[x*3 + y]; } + } +} + +[shader("compute")] +[numthreads(1,1,1)] +void computeMain() +{ + MyType2 obj; + int v = obj[1, 2]; +}
\ No newline at end of file |
