diff options
| -rw-r--r-- | source/slang/slang-parser.cpp | 44 | ||||
| -rw-r--r-- | tests/language-feature/generics/generic-subscript.slang | 24 |
2 files changed, 51 insertions, 17 deletions
diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp index 6ae41a2eb..98cfd121c 100644 --- a/source/slang/slang-parser.cpp +++ b/source/slang/slang-parser.cpp @@ -3932,28 +3932,38 @@ static void parseStorageDeclBody(Parser* parser, ContainerDecl* decl) static NodeBase* parseSubscriptDecl(Parser* parser, void* /*userData*/) { - SubscriptDecl* decl = parser->astBuilder->create<SubscriptDecl>(); - parser->FillPosition(decl); - parser->PushScope(decl); + return parseOptGenericDecl( + parser, + [&](GenericDecl* genericParent) + { + SubscriptDecl* decl = parser->astBuilder->create<SubscriptDecl>(); + parser->FillPosition(decl); + parser->PushScope(decl); - // TODO: the use of this name here is a bit magical... - decl->nameAndLoc.name = getName(parser, "operator[]"); + // TODO: the use of this name here is a bit magical... + decl->nameAndLoc.name = getName(parser, "operator[]"); - parseParameterList(parser, decl); + parseParameterList(parser, decl); - if (AdvanceIf(parser, TokenType::RightArrow)) - { - decl->returnType = parser->ParseTypeExp(); - } - else - { - decl->returnType.exp = parser->astBuilder->create<IncompleteExpr>(); - } + if (AdvanceIf(parser, TokenType::RightArrow)) + { + decl->returnType = parser->ParseTypeExp(); + } + else + { + decl->returnType.exp = parser->astBuilder->create<IncompleteExpr>(); + } - parseStorageDeclBody(parser, decl); + auto funcScope = parser->currentScope; + parser->PopScope(); + maybeParseGenericConstraints(parser, genericParent); + parser->PushScope(funcScope); - parser->PopScope(); - return decl; + parseStorageDeclBody(parser, decl); + + parser->PopScope(); + return decl; + }); } /// Peek in the token stream and return `true` if it looks like a modern-style variable declaration diff --git a/tests/language-feature/generics/generic-subscript.slang b/tests/language-feature/generics/generic-subscript.slang new file mode 100644 index 000000000..87eec3045 --- /dev/null +++ b/tests/language-feature/generics/generic-subscript.slang @@ -0,0 +1,24 @@ +//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK): -output-using-type + +//TEST_INPUT: set output = out ubuffer(data=[0 0 0 0], stride=4) +RWStructuredBuffer<float> output; + +struct Tx +{ + float x; + __subscript<I>(I index) -> float + where I:IInteger + { + get { return x + index.toInt(); } + set { x = newValue;} + } +} + +[numthreads(1,1,1)] +void computeMain() +{ + Tx obj; + obj[0] = 3.0; + // CHECK: 5.0 + output[0] = obj[2]; +}
\ No newline at end of file |
