File: nulltype.d

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (198 lines) | stat: -rw-r--r-- 3,966 bytes parent folder | download | duplicates (6)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
TEST_OUTPUT:
---
pure nothrow @safe Object(bool b)
pure nothrow @safe int*(bool b)
pure nothrow @safe int[](bool b)
---

RUN_OUTPUT:
---
Success
---
*/
extern (C) int printf(const(char*) fmt, ...);

alias typeof(null) null_t;

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

void test1()
{
    null_t null1;
    typeof(null) null2;

    static assert(is(typeof(null1) == typeof(null)));
    static assert(is(typeof(null2) == typeof(null)));

    static assert(is(typeof(null1) == null_t));
    static assert(is(typeof(null2) == null_t));
}

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

interface I{}
class C{}

int f(null_t)   { return 1; }
int f(int[])    { return 2; }
int f(C)        { return 3; }

void test2()
{
    static assert(is(null_t : C));
    static assert(is(null_t : I));
    static assert(is(null_t : int[]));
    static assert(is(null_t : void*));
    static assert(is(null_t : int**));

    static assert(!is(null_t == C));
    static assert(!is(null_t == I));
    static assert(!is(null_t == int[]));
    static assert(!is(null_t == void*));
    static assert(!is(null_t == int**));

    static assert(is(null_t == null_t));

    assert(f(null) == 1);
}

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=5899

auto f5899(bool b)
{
    if (b)
        return new Object;
    else
        return null;
}
static assert(is(typeof(f5899) R == return) && is(R == Object));
pragma(msg, typeof(f5899));

auto g5899(bool b)
{
    if (b)
        return new int;
    else
        return null;
}
static assert(is(typeof(g5899) R == return) && is(R == int*));
pragma(msg, typeof(g5899));

auto h5899(bool b)
{
    if (b)
        return [1];
    else
        return null;
}
static assert(is(typeof(h5899) R == return) && is(R == int[]));
pragma(msg, typeof(h5899));

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=7278

struct Foo7278(string s)
{
    string var;
    void func()
    {
        string local = var;
    }
}

void test7278()
{
    Foo7278!null a;
    Foo7278!null b;
}

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=8221

class A8221
{
    A8221 foo() { return this; }
}
class B8221: A8221
{
    override typeof(null) foo() { return null; } // error
}
void test8221()
{
    auto a = new A8221();
    assert(a.foo() is a);
    auto b = new B8221();
    assert(b.foo() is null);
    a = b;
    assert(a.foo() is null);
}

/***************************************************/
// https://issues.dlang.org/show_bug.cgi?id=8589

void test8589()
{
    static typeof(null) retnull() { return null; }

    void test(bool result, T)()
    {
        void f(T function() dg) { assert(!dg()); }

        static assert((is(typeof(null) function() : T function())) == result);
        static assert(is(typeof( f(&retnull) )) == result);
        static assert(is(typeof( f(()=>null) )) == result);
        static if (result)
        {
            f(&retnull);
            f(()=>null);
        }
    }
    test!(true,  int*)();
    test!(true,  Object)();
    test!(false, int[int])();
    test!(false, int[])();
    test!(false, void delegate())();
}

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=9385

void test9385()
{
    assert((null ? true : false) == false);
    if (null) assert(0);
    assert(!null);
}

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=12203

void test12203()
{
    typeof(null) v;
    void foo(float) {}
    void delegate(float) dg = &foo;
    assert(dg !is null);

    dg = v; // Error: e2ir: cannot cast v of type typeof(null) to type void delegate(float)

    assert(dg  is null);
}

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

int main()
{
    test1();
    test2();
    test7278();
    test8221();
    test8589();
    test9385();
    test12203();

    printf("Success\n");
    return 0;
}