Skip to content

Commit

Permalink
Merge branch 'master' into cheneym2/linktimeconstanttestspirv
Browse files Browse the repository at this point in the history
  • Loading branch information
cheneym2 authored Mar 7, 2025
2 parents eb7bf03 + e1952dc commit 76d5edb
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 30 deletions.
29 changes: 25 additions & 4 deletions source/slang/slang-check-conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,35 @@ bool SemanticsVisitor::createInvokeExprForSynthesizedCtor(
{
StructDecl* structDecl = isDeclRefTypeOf<StructDecl>(toType).getDecl();

if (!structDecl || !_getSynthesizedConstructor(
structDecl,
ConstructorDecl::ConstructorFlavor::SynthesizedDefault))
if (!structDecl)
return false;

HashSet<Type*> isVisit;
bool isCStyle = isCStyleType(toType, isVisit);
bool isCStyle = false;
if (!_getSynthesizedConstructor(
structDecl,
ConstructorDecl::ConstructorFlavor::SynthesizedDefault))
{
// When a struct has no constructor and it's not a C-style type, the initializer list is
// invalid.
isCStyle = isCStyleType(toType, isVisit);

// WAR: We currently still has to allow legacy initializer list for array type until we have
// more proper solution for array initialization, so if the right hand side is an array
// type, we will not report error and fall-back to legacy initializer list logic.
bool isArrayType = as<ArrayExpressionType>(toType) != nullptr;
if (!isCStyle && !isArrayType)
{
getSink()->diagnose(
fromInitializerListExpr->loc,
Diagnostics::cannotUseInitializerListForType,
toType);
}

return false;
}

isCStyle = isCStyleType(toType, isVisit);
// TODO: This is just a special case for a backwards-compatibility feature
// for HLSL, this flag will imply that the initializer list is synthesized
// for a type cast from a literal zero to a 'struct'. In this case, we will fall
Expand Down
2 changes: 1 addition & 1 deletion source/slang/slang-check-decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12412,7 +12412,7 @@ bool SemanticsDeclAttributesVisitor::_synthesizeCtorSignature(StructDecl* struct
// any constructors. see:
// https://github.com/shader-slang/slang/blob/master/docs/proposals/004-initialization.md#inheritance-initialization
if (_hasExplicitConstructor(structDecl, true))
return false;
return true;

// synthesize the signature first.
// The constructor's visibility level is the same as the struct itself.
Expand Down
3 changes: 3 additions & 0 deletions tests/initializer-list/explicit-ctor-diagnostic.slang
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
}
39 changes: 39 additions & 0 deletions tests/initializer-list/struct-inherit-diagnostics.slang
Original file line number Diff line number Diff line change
@@ -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};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,42 @@ struct DefaultStruct_base

__init()
{
data1 = 1;
data0 = 2;
data1 = 3;
}
};

struct DefaultStruct1 : DefaultStruct_base
{
int data2 = 1;
};
struct DefaultStruct2 : DefaultStruct_base
{
int data2 = 1;
__init()
{
if (data0 != 1)
if (data0 == 2)
{
data2 = 0;
data2 = 4;
}
}
};
struct DefaultStruct3 : DefaultStruct_base

struct DefaultStruct2 : DefaultStruct_base
{
__init()
{
}
};
struct DefaultStruct4 : DefaultStruct_base
{
};

[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
{
DefaultStruct1 s1 = {};
DefaultStruct1 s1;
DefaultStruct2 s2;
DefaultStruct3 s3;
DefaultStruct4 s4 = {};

// BUF: 1
outputBuffer[0] = true
&& s1.data0 == 1
&& s1.data1 == 1
&& s1.data2 == 1
&& s2.data0 == 1
&& s2.data1 == 1
&& s2.data2 == 1
&& s3.data0 == 1
&& s3.data1 == 1
&& s4.data0 == 1
&& s4.data1 == 1
&& s1.data0 == 2
&& s1.data1 == 3
&& s1.data2 == 4
&& s2.data0 == 2
&& s2.data1 == 3
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ struct TestAny : ITest
value = v;
}

__init()
{
value = 0;
}

uint getValue() { return value; }
}

Expand Down Expand Up @@ -183,4 +188,4 @@ void testMain(uint3 threadID: SV_DispatchThreadID)
}

expected[outputIdx++] = uint(-1);
}
}

0 comments on commit 76d5edb

Please sign in to comment.