summaryrefslogtreecommitdiff
path: root/tests/diagnostics
diff options
context:
space:
mode:
Diffstat (limited to 'tests/diagnostics')
-rw-r--r--tests/diagnostics/ctor-noncopyable-retval-legal.slang103
-rw-r--r--tests/diagnostics/ctor-ordinary-retval-legal.slang104
2 files changed, 207 insertions, 0 deletions
diff --git a/tests/diagnostics/ctor-noncopyable-retval-legal.slang b/tests/diagnostics/ctor-noncopyable-retval-legal.slang
new file mode 100644
index 000000000..d4cfc5030
--- /dev/null
+++ b/tests/diagnostics/ctor-noncopyable-retval-legal.slang
@@ -0,0 +1,103 @@
+//TEST:SIMPLE(filecheck=CHECK):
+
+// Test to catch compilation time errors with return values in constructors within a NonCopyable struct.
+// A constructor from the callsite's point of view is a function that returns the struct type.
+// A constructor from the inside the body is treated as a function that modifies `this` and not return.
+
+// In the following test-cases where the constructor either returns an expr or no value,
+// within a condition for early exit or not, it automatically gets resolved as
+// return *this; or return;
+
+[__NonCopyableType] struct S
+{
+ float v;
+}
+
+[__NonCopyableType] struct S1 : S
+{
+ __init()
+ {
+ this.v = 5;
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return this;
+ }
+}
+
+[__NonCopyableType] struct S4 : S
+{
+ __init(float u)
+ {
+ if (u != 0)
+ {
+ this.v = u;
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return this;
+ }
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return;
+ }
+}
+
+[__NonCopyableType] struct S2 : S
+{
+ __init()
+ {
+ S2 t;
+ t.v = 5;
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return;
+ }
+}
+
+[__NonCopyableType] struct S5 : S
+{
+ __init(float u)
+ {
+ if (u != 0)
+ {
+ S5 t;
+ t.v = u;
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return;
+ }
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return;
+ }
+}
+
+[__NonCopyableType] struct S3 : S
+{
+ __init()
+ {
+ S3 t;
+ t.v = 5;
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return t;
+ }
+}
+
+[__NonCopyableType] struct S6 : S
+{
+ __init(float u)
+ {
+ if (u != 0)
+ {
+ S6 t;
+ t.v = u;
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return t;
+ }
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return;
+ }
+}
+
+void main()
+{
+ S1 s1;
+ S2 s2;
+ S3 s3;
+ S4 s4 = S4(0);
+ S5 s5 = S5(0);
+ S6 s6 = S6(0);
+}
diff --git a/tests/diagnostics/ctor-ordinary-retval-legal.slang b/tests/diagnostics/ctor-ordinary-retval-legal.slang
new file mode 100644
index 000000000..f2d1765ff
--- /dev/null
+++ b/tests/diagnostics/ctor-ordinary-retval-legal.slang
@@ -0,0 +1,104 @@
+//TEST:SIMPLE(filecheck=CHECK):
+
+// Test to catch compilation time errors with return values in constructors within an ordinary struct.
+// A constructor from the callsite's point of view is a function that returns the struct type.
+// A constructor from the inside the body is treated as a function that modifies `this` and not return.
+
+// In the following test-cases where the constructor either returns an expr or no value,
+// within a condition for early exit or not, it automatically gets resolved as
+// this = t;
+// return *this; or return;
+
+struct S
+{
+ float v;
+}
+
+struct S1a : S
+{
+ __init()
+ {
+ this.v = 5;
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return this;
+ }
+}
+
+struct S1b : S
+{
+ __init(float u)
+ {
+ if (u != 0)
+ {
+ this.v = u;
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return this;
+ }
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return;
+ }
+}
+
+struct S2a : S
+{
+ __init()
+ {
+ S2a t;
+ t.v = 5;
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return;
+ }
+}
+
+struct S2b : S
+{
+ __init(float u)
+ {
+ if (u != 0)
+ {
+ S2b t;
+ t.v = u;
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return;
+ }
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return;
+ }
+}
+
+struct S3a : S
+{
+ __init()
+ {
+ S3a t;
+ t.v = 5;
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return t;
+ }
+}
+
+struct S3b : S
+{
+ __init(float u)
+ {
+ if (u != 0)
+ {
+ S3b t;
+ t.v = u;
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return t;
+ }
+ // CHECK-NOT: ([[# @LINE+1]]): error
+ return;
+ }
+}
+
+void main()
+{
+ S1a S1a;
+ S2a S2a;
+ S3a S3a;
+ S1b S1b = S1b(0);
+ S2b S2b = S2b(0);
+ S3b S3b = S3b(0);
+}