summaryrefslogtreecommitdiffstats
path: root/tests/diagnostics/ctor-ordinary-retval-legal.slang
blob: 8117b4c802c0047b1e2a02837adda3baa599573e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//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;
    __init() { v = 0; }
}

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);
}