From e1952dc8cf8f5b62d00ce114e353c5390cc6c37a Mon Sep 17 00:00:00 2001 From: kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> Date: Thu, 6 Mar 2025 22:38:28 -0600 Subject: Fix a bug in default ctor synthesizing (#6527) * Fix a bug in default ctor synthesizing - This is fix for the implementation bug, when a struct has explicit ctor we should not synthesize the default ctor anymore. - When invoke the synthesized ctor converted from initializer list, we should check if the struct is a c-style type if it struct has no synthesized ctor. In this case we should report error because it's invalid to use initializer list here. - The only exception is the unsized array, we still have to fall back to use the legacy initializer list logic to initialize the unsized array until we formalize a proper solution. - update test. --- .../explicit-ctor-diagnostic.slang | 3 ++ .../struct-inherit-diagnostics.slang | 39 +++++++++++++++ tests/initializer-list/struct-inherit.slang | 57 ++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 tests/initializer-list/struct-inherit-diagnostics.slang create mode 100644 tests/initializer-list/struct-inherit.slang (limited to 'tests/initializer-list') diff --git a/tests/initializer-list/explicit-ctor-diagnostic.slang b/tests/initializer-list/explicit-ctor-diagnostic.slang index a9d0e9858..6ce65c016 100644 --- a/tests/initializer-list/explicit-ctor-diagnostic.slang +++ b/tests/initializer-list/explicit-ctor-diagnostic.slang @@ -16,4 +16,7 @@ void test() { // CHECK: error 39999: too many arguments to call (got 2, expected 1) ExplicitCtor e = {1, 2}; // error, no ctor matches initializer list. + + // CHECK: error 39999: not enough arguments to call (got 0, expected 1) + ExplicitCtor e1 = {}; } diff --git a/tests/initializer-list/struct-inherit-diagnostics.slang b/tests/initializer-list/struct-inherit-diagnostics.slang new file mode 100644 index 000000000..247431570 --- /dev/null +++ b/tests/initializer-list/struct-inherit-diagnostics.slang @@ -0,0 +1,39 @@ +//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): + +struct DefaultStruct_base +{ + int data0; + __init() + { + data0 = 2; + } +}; + +struct DefaultStruct1 : DefaultStruct_base +{ + int data1 = 1; +}; + +struct DefaultStruct2 : DefaultStruct_base +{ + +}; + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + //CHECK: error 30504: cannot use initializer list for type 'DefaultStruct1' + DefaultStruct1 s1 = {}; + + //CHECK: error 30504: cannot use initializer list for type 'DefaultStruct1' + DefaultStruct1 s2 = {1}; + + //CHECK: error 30504: cannot use initializer list for type 'DefaultStruct1' + DefaultStruct1 s3 = {1, 2}; + + //CHECK: error 30504: cannot use initializer list for type 'DefaultStruct2' + DefaultStruct2 s4 = {}; + + //CHECK: error 30504: cannot use initializer list for type 'DefaultStruct2' + DefaultStruct2 s5 = {1}; +} diff --git a/tests/initializer-list/struct-inherit.slang b/tests/initializer-list/struct-inherit.slang new file mode 100644 index 000000000..71487a74f --- /dev/null +++ b/tests/initializer-list/struct-inherit.slang @@ -0,0 +1,57 @@ +//TEST(compute, vulkan):COMPARE_COMPUTE(filecheck-buffer=BUF):-vk -compute -entry computeMain +//TEST(compute, vulkan):COMPARE_COMPUTE(filecheck-buffer=BUF):-vk -compute -entry computeMain -emit-spirv-directly +//TEST(compute, vulkan):COMPARE_COMPUTE(filecheck-buffer=BUF):-cpu -compute -entry computeMain +//TEST(smoke,compute):COMPARE_COMPUTE(filecheck-buffer=BUF):-dx12 -use-dxil -compute -entry computeMain + +//TEST_INPUT:ubuffer(data=[0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +static int myTwo = 2; +static int myThree = 1+2; + +struct DefaultStruct_base +{ + int data0 = 1; + int data1; + + __init() + { + data0 = 2; + data1 = 3; + } +}; + +struct DefaultStruct1 : DefaultStruct_base +{ + int data2 = 1; + __init() + { + if (data0 == 2) + { + data2 = 4; + } + } +}; + +struct DefaultStruct2 : DefaultStruct_base +{ + __init() + { + } +}; + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + DefaultStruct1 s1; + DefaultStruct2 s2; + + // BUF: 1 + outputBuffer[0] = true + && s1.data0 == 2 + && s1.data1 == 3 + && s1.data2 == 4 + && s2.data0 == 2 + && s2.data1 == 3 + ; +} -- cgit v1.2.3