aboutsummaryrefslogtreecommitdiffhomepage
path: root/errortest/mainapp/generics.slul
blob: 22b81884bb3d258be4294d9267f3e55514ea0b78 (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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

#
# Test of generic types (negative tests)
#
# Copyright © 2023-2024 Samuel Lidén Borell <samuel@kodafritt.se>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

# ERROR .18: error: Empty type parameter definition
type BadGeneric1<> = struct { }
# ERROR .18: error: Bad type parameter definition
type BadGeneric2<*> = struct { }
# ERROR +2.20: error: Identifier already in use
# ERROR .18: <-- other definition: T
type BadGeneric3<T,T> = struct { }
# ERROR .21: error: Expected an identifier here
type BadGeneric4<ref> = struct { }
# ERROR .22: error: Expected an identifier here
type BadGeneric5<ref ref> = struct { }
# ERROR .24: error: Missing , or > after type parameter name
type BadGeneric6<ref T T> = struct { }
# ERROR .20: error: Empty type parameter definition
type BadGeneric7<T,> = struct { }
# ERROR .18: error: Bad type parameter definition
type BadGeneric8<,T> = struct { }
# FIXME get rid of the first error
# ERROR +2.20: error: Expected an identifier here
# ERROR .19: error: Only reference types can be optional
type BadGeneric9<?T> = struct { }

# ERROR .19: <-- definition: T
type BadGeneric10<T> = struct {
    # FIXME get rid of the first error
    # ERROR +2.14: error: Type 'T' does not exist
    # ERROR .14: error: Type parameters can only be used within "slot" types
    Generic2<T> x
}

type BadGeneric11<T> = struct {
    # FIXME this should give an error!
    Generic2<NonGeneric3> x
}


type NonGeneric1 = struct {
    int x
}

type NonGeneric2 = struct {
    bool b
    int y
}

type NonGeneric3 = struct {
    int a
    int x
}

type Generic1<ref T> = struct {
    Generic2<slot T> same
    Generic2<ref NonGeneric3> fixed
    Generic2<slot T> also_same
    slot T elem
}

type Generic2<T> = struct {
    slot T elem
}

type Generic3<ref T, ref U> = struct {
    ref Generic2<slot T> a
    ref Generic2<slot U> b
    [2]Generic1<slot U> arr
}

type Generic4<ref T, ref U, ref V> = struct {
    Generic3<slot T, slot T> tt_ut
    Generic3<slot T, slot U> tt_uu
    Generic3<slot T, slot V> tt_uv
    Generic3<slot U, slot T> tu_ut
    Generic3<slot U, slot U> tu_uu
    Generic3<slot U, slot V> tu_uv
    Generic3<slot V, slot T> tv_ut
    Generic3<slot V, slot U> tv_uu
    Generic3<slot V, slot V> tv_uv
}

func generics_test1()
{
    NonGeneric1 ng1 = (.x=1100)
    NonGeneric2 ng2 = (.b=true, .y=2200)
    NonGeneric3 ng3 = (.a=3300, .x=3400)

    # TODO test <T> and attempting to assume specific types (should fail)
    # TODO test <ref T> and assigning a non-ref type (should fail)
    # TODO test <enum T> and assigning a non-enum type (should fail)

    # FIXME this should give an error!
    Generic2<NonGeneric1> bad_ng2

    Generic2<int> g2_int = (.elem = 111)
    Generic2<ref NonGeneric1> g2 = (.elem = refto ng1)

    Generic1<ref NonGeneric1> g1 = (
        .same = (.elem = refto ng1),
        # TODO get rid of repeated error
        # ERROR +2.27: error: Mismatch of named types
        # ERROR .33: error: Mismatch of named types
        .fixed = (.elem = refto ng2),
        .also_same = g2,
        # TODO get rid of repeated error
        # ERROR +2.17: error: Mismatch of named types
        # ERROR .23: error: Mismatch of named types
        .elem = refto ng2
    )

    var Generic2<int> g2_int_b = g2_int
    # ERROR .21: error: Incompatible types
    g2_int_b.elem = true
    # ERROR .21: error: Incompatible types
    assert g2_int_b.elem

    var Generic1<ref NonGeneric1> g1_b
    g1_b = g1
    # ERROR +2.22: error: Mismatch of named types
    # ERROR .28: error: Mismatch of named types
    g1_b.same.elem = refto ng3
    # ERROR +2.23: error: Mismatch of named types
    # ERROR .29: error: Mismatch of named types
    g1_b.fixed.elem = refto ng1
    # ERROR +2.17: error: Mismatch of named types
    # ERROR .23: error: Mismatch of named types
    g1_b.elem = refto ng3

    var Generic1<bool> g1_bool = (
        # ERROR .26: error: Incompatible types
        .same = (.elem = 123),
        # ERROR +2.27: error: Mismatch of named types
        # ERROR .33: error: Mismatch of named types
        .fixed = (.elem = refto ng2),
        # ERROR .31: error: Incompatible types
        .also_same = (.elem = g1),
        # ERROR .17: error: Incompatible types
        .elem = 123,
    )
    # ERROR .15: error: Incompatible types. Nested type does not match
    g1_bool = g1_b
    # ERROR +2.26: error: Mismatch of named types
    # ERROR .32: error: Mismatch of named types
    g1_bool.fixed.elem = refto ng1
    # ERROR .30: error: Incompatible types
    g1_bool.also_same.elem = 123

    var Generic2<bool> g2_bool = (.elem = true)

    var Generic2<ref Generic2<int>> g2_nested = (
        # ERROR +2.17: error: Incompatible types. Nested types must match exactly
        # ERROR .23: error: Incompatible types. Nested types must match exactly
        .elem = refto g2_bool
    )
    # ERROR +2.22: error: Incompatible types. Nested types must match exactly
    # ERROR .28: error: Incompatible types. Nested types must match exactly
    g2_nested.elem = refto g2_bool
    # ERROR .27: error: Incompatible types
    g2_nested.elem.elem = true
    # ERROR .17: error: Incompatible types. Nested type does not match
    g2_nested = g2_bool

    var Generic2<ref Generic2<bool>> g2_nested_bool = (
        # ERROR +2.17: error: Mismatch of named types
        # ERROR .23: error: Mismatch of named types
        .elem = refto g1
    )
    # ERROR .15: error: Incompatible types. Nested type does not match
    g2_bool = g2_nested_bool
    # ERROR .22: error: Incompatible types. Nested type does not match
    g2_nested_bool = g2_bool

    var Generic2<ref Generic2<ref Generic2<int>>> g2_doublenested = (
        # ERROR +2.17: error: Incompatible types. Nested types must match exactly
        # ERROR .23: error: Incompatible types. Nested types must match exactly
        .elem = refto g2_nested_bool
    )
    # ERROR +2.28: error: Incompatible types. Nested types must match exactly
    # ERROR .34: error: Incompatible types. Nested types must match exactly
    g2_doublenested.elem = refto g2_nested_bool
    # ERROR +2.33: error: Incompatible types. Nested types must match exactly
    # ERROR .39: error: Incompatible types. Nested types must match exactly
    g2_doublenested.elem.elem = refto g2_bool
    # ERROR .38: error: Incompatible types
    g2_doublenested.elem.elem.elem = true
    # FIXME this should give an error!
    #g2_doublenested.elem.elem[1] = true

    var Generic3<int,bool> g3_intbool = (
        .a = refto g2_int,
        .b = refto g2_bool,
        .arr = [g1_bool, g1_bool]
    )
    # ERROR +2.20: error: Incompatible types. Nested types must match exactly
    # ERROR .26: error: Incompatible types. Nested types must match exactly
    g3_intbool.a = refto g2_bool
    # ERROR +2.20: error: Incompatible types. Nested types must match exactly
    # ERROR .26: error: Incompatible types. Nested types must match exactly
    g3_intbool.b = refto g2_int
    # ERROR .32: error: Mismatch of named types
    g3_intbool.arr = [g1_bool, g2]
    # ERROR .32: error: Incompatible types. Nested type does not match
    g3_intbool.arr = [g1_bool, g1]
    # ERROR .25: error: Incompatible types
    g3_intbool.arr[1] = g1
    # ERROR .23: error: Field "oops" does not exist
    g3_intbool.arr[1].oops = g1
    # ERROR .16: error: Field "oops" does not exist
    g3_intbool.oops = g1
    # FIXME this should give an error!
    #g3_intbool[1].oops1 = g1
    # ERROR .5: error: Identifier 'oops' does not exist
    oops[0] = g1

    var Generic3<ref Generic2<ref Generic2<int>>, ref Generic2<bool>> g3_nested = (
        .a = refto g2_doublenested,
        .b = refto g2_nested_bool,
        .arr = [
            (
                .same = (.elem = refto g2_bool),
                .fixed = (.elem = refto ng3),
                .also_same = (.elem = refto g2_bool),
                .elem = refto g2_bool,
            ),
            (
                # ERROR +2.34: error: Incompatible types. Nested types must match exactly
                # ERROR .40: error: Incompatible types. Nested types must match exactly
                .same = (.elem = refto g2_int),
                # ERROR +2.35: error: Mismatch of named types
                # ERROR .41: error: Mismatch of named types
                .fixed = (.elem = refto ng2),
                # ERROR +2.39: error: Incompatible types. Nested types must match exactly
                # ERROR .45: error: Incompatible types. Nested types must match exactly
                .also_same = (.elem = refto g2_int),
                # ERROR +2.25: error: Incompatible types. Nested types must match exactly
                # ERROR .31: error: Incompatible types. Nested types must match exactly
                .elem = refto g2_int,
            ),
        ]
    )

    var int i = 123
    var ref var int intref = refto i
    var Generic4<int,bool,ref int> g4_bound = get_g4_bound()
    # ERROR .63: error: Incompatible types. Nested types must match exactly
    var Generic4<int,int,ref int> g4_bound_bad1 = get_g4_bound()
    # ERROR .65: error: Incompatible types. Nested types must match exactly
    var Generic4<int,bool,ref bool> g4_bound_bad2 = get_g4_bound()
    #var Generic4<int,bool,ref int> g4_params = get_g4_params<int,ref int,int,ref NonGeneric1,bool>(i, intref)

    # ERROR .37: error: Incompatible types. Nested types must match exactly
    g2_bool = deref g3_intbool.get_a()
    i = g3_intbool.get_a_elem()
    # ERROR +2.22: error: Incompatible types. Nested types must match exactly
    # ERROR .28: error: Incompatible types. Nested types must match exactly
    g3_intbool.set_b(refto g2_int)
    # ERROR .26: error: Incompatible types
    g3_intbool.set_inner(123)
}

func get_g4_bound() -> Generic4<int,bool,ref int> { while true { } }

# TODO
func get_g4_params<A,B,C,D,E>(slot A x, slot B y) -> var Generic4<slot C, slot E, slot B>
{
    slot B b = y
    # ERROR .20: error: Incompatible types
    var slot A a = y
    # ERROR .9: error: Incompatible types
    a = y
#{{
    # TODO these should give errors
    get_g4_params(x, y) # type params should not remain bound here
    a = 123
    a = none
    a = [123]
    var Generic4<slot C, slot E, slot B> ret = (
        .tt_ut=gen_cc,
        .tt_uu=gen_ce,
        .tt_uv=gen_cb,
        .tu_ut=gen_ec,
        .tu_uu=gen_ee,
        .tu_uv=gen_eb,
        .tv_ut=gen_bc,
        # TODO should give an error
        .tv_uu=gen_bc, # wrong!
        .tv_uv=gen_bb,
    )
    return ret
#}}
    while true {}
}


func Generic3.get_a() -> ref Generic2<slot T> { while true { } }
func Generic3.get_a_elem() -> slot T { while true { } }
func Generic3.set_b(ref Generic2<slot U> value) { }
func Generic3.set_inner(slot U inner) { }

type NonGenericType = struct {
    int x
}
func NonGenericType.some_method()
{
    # ERROR .10: error: Type parameters are not applicable on this type
    slot T a
}

func NonGenericType2.some_method()
{
    # ERROR .10: error: Type parameters are not applicable on this type
    slot T a
}
type NonGenericType2 = struct {
    int x
}

func Generic1.some_method()
{
    # ERROR .10: error: Type parameter does not exist
    slot U a
}