summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gdc.test/compilable/noreturn1.d
blob: e648a56d896ee1691ebc449f107d453f9b477225 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
REQUIRED_ARGS: -w
TEST_OUTPUT:
---
noreturn
---

Basic properties and usage mentioned in the DIP:
https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1034.md
*/

alias noreturn = typeof(*null);
pragma(msg, noreturn);

static assert(!is(noreturn == void));

// Fails
// static assert(is( typeof([]) == noreturn[] ));
// static assert(is( typeof([][0]) == noreturn ));

static assert(is( typeof(assert(0)) == noreturn ));

static assert(is( typeof(throw new Exception("")) == noreturn ));

static assert(is(noreturn == noreturn));
static assert(!is(noreturn == const noreturn));
static assert(is(noreturn : const noreturn));

static assert(!is(noreturn == int));
static assert(is(noreturn : int));

// Covariance
static assert(is(noreturn[] : int[]));
static assert(is(noreturn* : int*));
static assert(is(noreturn function() : int function()));
static assert(is(noreturn delegate() : int delegate()));

// Reject inverse conversions
static assert(!is(int[]          : noreturn[]));
static assert(!is(int*           : noreturn*));
static assert(!is(int function() : noreturn function()));
static assert(!is(int delegate() : noreturn delegate()));

static assert(noreturn.mangleof == "Nn"); // Changed from b due to conflicts with bool
static assert(noreturn.sizeof == 0);
static assert(noreturn.alignof == 0);

static assert((noreturn*).sizeof == (int*).sizeof);
static assert((noreturn[]).sizeof == (int[]).sizeof);

static assert(is(typeof(noreturn.init) == noreturn));
static assert(is(typeof((const noreturn).init) == const noreturn));
static assert(is(typeof((immutable noreturn).init) == immutable noreturn));
static assert(is(typeof((shared noreturn).init) == shared noreturn));

version (DigitalMars)
    noreturn exits(int* p)
    {
        *p = 3;
        assert(false); // *p could be valid
    }

noreturn exit();

noreturn pureexits() @nogc nothrow pure @safe { assert(0); }

noreturn callpureexits() { pureexits(); }

noreturn returnExits()
{
    return pureexits();
}

void alsoExits()
{
    return assert(0);
}

int thisAlsoExits()
{
    return assert(0);
}

void cast_()
{
    noreturn n;
    int i = n;
}

int test1(int i)
{
    if (exit())
        return i + 1;
    return i - 1;
}

noreturn tlsNoreturn;
__gshared noreturn globalNoreturn;

template CreateTLS(A)
{
    A a;
}

void* useTls()
{
    alias Tnr = CreateTLS!noreturn;
    void* a1 = &Tnr.a;
    void* a2 = &tlsNoreturn;
    void* a3 = &globalNoreturn;
    return a1 < a2 ? a2 : a3;
}

/***************************************************/

noreturn testfn(noreturn function() fn)
{
    fn();
}

noreturn testdg(noreturn delegate() dg)
{
    dg();
}

noreturn func()
{
    while(1)
    {
    }
}
alias AliasSeq(T...) = T;
alias Types = AliasSeq!(bool, byte, ubyte, short, ushort, int, uint,
                        long, ulong, char, wchar, dchar, float, double,
                        real);
void noreturnImplicit()
{
    /*
        Testing both ways because, although the underlying table
        is symmetrical the code that calls into it may be buggy.
    */
    {
        int x = 2 + func();
        int y = func() + 2;
    }
    foreach(T; Types)
    {
        T value;
        auto x = value + throw new Exception("Hello");
        auto y = (throw new Exception("wow")) + value;
    }
}