summaryrefslogtreecommitdiffstats
path: root/tests/diagnostics/extension-full-name.slang
blob: 2920280114f6901bdc8602f47af45b7f009adc46 (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
345
346
347
348
349
350
351
352
353
354
355
356
357
// extension-full-name.slang
//DIAGNOSTIC_TEST:SIMPLE:-target hlsl

// Define a generic type with a nested struct
struct GenericType<T>
{
    T value;

    struct InnerType
    {
        T innerValue;
    }

    InnerType inner;
}

// Define a non-generic type
struct NonGenericType
{
    float value;

    struct InnerType
    {
        float innerValue;
    }

    InnerType inner;
}

// Add an extension to the generic type
extension GenericType<half>
{
    // Type in extension
    struct State
    {
        half value;
        half factor;
    }

    // Function in extension
    State
    createState()
    {
        State state;
        state.value = value;
        state.factor = 1.0h;
        return state;
    }

    // Value/field in extension
    static State defaultState;
}

// Add an extension to a nested type inside a generic type
extension GenericType<float>.InnerType
{
    struct Options
    {
        float min;
        float max;
    }

    Options
    getOptions()
    {
        Options opts;
        opts.min = 0;
        opts.max = innerValue;
        return opts;
    }

    static Options defaultOptions;
}

// Add an extension to the non-generic type
extension NonGenericType
{
    // Type in extension
    struct Config
    {
        float threshold;
        float scale;
    }

    // Function in extension
    Config
    createConfig()
    {
        Config config;
        config.threshold = 0.5;
        config.scale = 2.0;
        return config;
    }

    // Value/field in extension
    static Config defaultConfig;
}

// Add nested types and extensions
struct Container
{
    struct Nested
    {
        float value;

        struct DeepNested
        {
            int count;
        }

        DeepNested deep;
    }

    Nested nested;
}

// Extension for deeply nested type
extension Container.Nested.DeepNested
{
    struct Record
    {
        int id;
        float value;
    }

    Record
    createRecord(int newId)
    {
        Record r;
        r.id = newId;
        r.value = 0;
        return r;
    }

    static Record defaultRecord;
}

extension Container.Nested
{
    // Type in nested extension
    struct Settings
    {
        float value;
        float threshold;
    }

    // Function in nested extension
    Settings
    createSettings()
    {
        Settings settings;
        settings.value = value;
        settings.threshold = 0.1;
        return settings;
    }

    // Value/field in nested extension
    static Settings defaultSettings;
}

[shader("compute")][numthreads(1, 1, 1)] void main(uint3 dispatchThreadID
                                                   : SV_DispatchThreadID)
{
    // Test array extensions with constraints
    testArrayExtension();

    // Test namespace extensions
    testNamespaceExtensions();

    // Type instantiation tests
    GenericType<half>.State state1;
    GenericType<float>.InnerType.Options options1;
    NonGenericType.Config config1;
    Container.Nested.Settings settings1;
    Container.Nested.DeepNested.Record record1;

    // Initialize extension types with struct literals
    state1 = {1.0h, 2.0h};  // Valid struct initialization
    options1 = {0.0, 1.0};  // Valid struct initialization
    config1 = {0.1, 0.5};   // Valid struct initialization
    settings1 = {0.2, 0.3}; // Valid struct initialization
    record1 = {1, 2.5};     // Valid struct initialization

    // Extension type mismatches - assign value type to extension type
    state1 = 0;    // Error: expected expr of type 'GenericType<half>.State', got 'int'
    options1 = 0;  // Error: expected expr of type 'GenericType<float>.InnerType.Options', got 'int'
    config1 = 0;   // Error: expected expr of type 'NonGenericType.Config', got 'int'
    settings1 = 0; // Error: expected expr of type 'Container.Nested.Settings', got 'int'
    record1 = 0;   // Error: expected expr of type 'Container.Nested.DeepNested.Record', got 'int'

    // Extension type mismatches - assign wrong extension types
    GenericType<int>.InnerType intInner;
    GenericType<float>.InnerType floatInner;

    // This should fail due to different generic parameters
    floatInner.Options floatOpts =
        intInner.getOptions(); // This won't compile as intInner doesn't have getOptions

    // Extension member access - valid cases
    state1.value = 1.0h;       // Valid
    state1.factor = 2.0h;      // Valid
    options1.min = 0.0;        // Valid
    options1.max = 1.0;        // Valid
    config1.threshold = 0.5;   // Valid
    config1.scale = 2.0;       // Valid
    settings1.value = 0.1;     // Valid
    settings1.threshold = 0.2; // Valid
    record1.id = 100;          // Valid
    record1.value = 3.14;      // Valid

    // Extension function calls
    GenericType<half> halfType;
    NonGenericType nonGenType;
    Container.Nested nested;
    Container.Nested.DeepNested deepNested;
    GenericType<float>.InnerType floatInnerType;

    state1 = halfType.createState();        // Valid
    config1 = nonGenType.createConfig();    // Valid
    settings1 = nested.createSettings();    // Valid
    record1 = deepNested.createRecord(42);  // Valid
    options1 = floatInnerType.getOptions(); // Valid

    // Type mismatches - function return values
    options1 =
        halfType
            .createState(); // Error: expected expr of type 'GenericType<float>.InnerType.Options',
                            // got 'GenericType<half>.State'
    state1 = nonGenType.createConfig(); // Error: expected expr of type 'GenericType<half>.State',
                                        // got 'NonGenericType.Config'
    config1 = nested.createSettings();  // Error: expected expr of type 'NonGenericType.Config', got
                                        // 'Container.Nested.Settings'
}

// Define an interface for constraint
interface IFoo
{
    float getValue();
}

// Struct that implements IFoo
struct Bar : IFoo
{
    float value;

    float getValue() { return value; }
}

// Struct that doesn't implement IFoo
struct Baz
{
    float value;
}

// Extension with constrained generic type parameter
extension<T : IFoo, let N : int> Array<T, N>
{
    // Add a struct type inside the extension
    struct DataStats
    {
        float average;
        float maximum;
        float minimum;
    }

    // Add a function that uses the struct type
    DataStats computeStats()
    {
        DataStats stats;
        stats.average = 0;
        stats.maximum = -1e38;
        stats.minimum = 1e38;

        for(int i = 0; i < N; i++)
        {
            float val = this[i].getValue();
            stats.average += val;
            stats.maximum = max(stats.maximum, val);
            stats.minimum = min(stats.minimum, val);
        }

        if(N > 0)
            stats.average /= float(N);

        return stats;
    }

    // Add a field
    static DataStats defaultStats;
}

void testArrayExtension()
{
    // Create an array of a type that implements IFoo
    Bar barArray[3] = { {1.0}, {2.0}, {3.0} };

    // Create an array of a type that doesn't implement IFoo
    Baz bazArray[3] = { {1.0}, {2.0}, {3.0} };

    // This should work - using the extension on a valid type
    Array<Bar, 3>.DataStats barStats;
    barStats = barArray.computeStats(); // Valid

    // Type mismatch errors
    barStats = 0; // Error: expected expr of type 'Bar[3].DataStats', got 'int'

    // This won't compile - Baz doesn't implement IFoo
    //Array<Baz, 3>.DataStats bazStats;  // This should fail because Baz doesn't implement IFoo
    //bazStats = bazArray.computeStats(); // This also won't compile

    // Type mismatch between different extension instantiations
    Array<Bar, 2>.DataStats bar2Stats;
    bar2Stats = barArray.computeStats(); // Error: expected expr of type 'Bar[2].DataStats', got 'Bar[3].DataStats'
}

// Test namespace extensions
// Define a simple base struct
struct SimpleBase
{
    float value;
}

// Define a namespace with an extension
namespace TestNamespace
{
    extension SimpleBase
    {
        struct NamedConfig
        {
            float factor;
        }

        NamedConfig createConfig()
        {
            NamedConfig config;
            config.factor = value * 2.0;
            return config;
        }
    }
}

// Test function for namespace extensions
void testNamespaceExtensions()
{
    // Need to use the namespace to access the extension methods
    using namespace TestNamespace;

    SimpleBase base;
    base.value = 5.0;

    // The type is just "SimpleBase.NamedConfig", not "TestNamespace.SimpleBase.NamedConfig"
    SimpleBase.NamedConfig config;
    config.factor = 2.0;

    // Generate a type error to check the output format
    config = 0; // Error: expected expr of type 'SimpleBase.NamedConfig', got 'int'
}