From a083a37ee58dc48d92cf2b844466a295eb3e643e Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 10 Aug 2022 15:37:19 -0700 Subject: Add `none` literal that is convertible to `Optional`. (#2356) * Add `none` literal that is convertible to `Optional`. * Fix cpu code gen. * Include vk and cpu test for is-as operator test. * Inline comparison operators. Co-authored-by: Yong He --- tests/language-feature/types/optional.slang | 53 ++++++++++++++++++++++ .../types/optional.slang.expected.txt | 2 + 2 files changed, 55 insertions(+) create mode 100644 tests/language-feature/types/optional.slang create mode 100644 tests/language-feature/types/optional.slang.expected.txt (limited to 'tests/language-feature/types') diff --git a/tests/language-feature/types/optional.slang b/tests/language-feature/types/optional.slang new file mode 100644 index 000000000..ae1711cd7 --- /dev/null +++ b/tests/language-feature/types/optional.slang @@ -0,0 +1,53 @@ +// optional.slang + +// Test that `Optional` construction and conversion works. + +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute +//TEST(compute):COMPARE_COMPUTE_EX:-slang -vk -compute +//TEST(compute):COMPARE_COMPUTE_EX:-slang -cpu -compute + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +[anyValueSize(8)] +interface IFoo +{ + int method(); +} + +//TEST_INPUT: type_conformance Impl1:IFoo = 0 +struct Impl1 : IFoo +{ + int data; + int method() { return data; } +} + +Optional getVal(bool shouldHaveVal) +{ + if (shouldHaveVal) + { + Impl1 val; + val.data = 1; + return val; + } + return none; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + let v1 = getVal(true); + let v2 = getVal(false); + int result = 0; + if (v1.hasValue) + result += v1.value.data; + if (v2.hasValue) + result += v2.value.data; + outputBuffer[0] = result; + if (v1 != none) + result += v1.value.data; + if (!(v2 == none)) + result += v2.value.data; + outputBuffer[1] = result; + +} diff --git a/tests/language-feature/types/optional.slang.expected.txt b/tests/language-feature/types/optional.slang.expected.txt new file mode 100644 index 000000000..1191247b6 --- /dev/null +++ b/tests/language-feature/types/optional.slang.expected.txt @@ -0,0 +1,2 @@ +1 +2 -- cgit v1.2.3