summaryrefslogtreecommitdiff
path: root/tests/diagnostics/param-mutation.slang
blob: 835e645c886e6e28f5305b29a12f0ece0746092f (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
// param-mutation.slang

// When a parameter is passed to `in`, it is mutable but generates a warning as it probably (?)
// isn't what the programmer intended.

//DIAGNOSTIC_TEST:SIMPLE:

struct MutatingStruct
{
    [mutating] void setValue(int value) { m_value = value; }
    int m_value;
};

int doThing(MutatingStruct s, int v)
{
    // Should generate a warning. 
    s.setValue(v + 1);
    return s.m_value;
}

// For non-copyable types (such as HitObject or NonCopyableStruct declared below), if passed as as `in`
// should produce an error.
// NOTE! This *doesn't* produce an error (or warning) because NonCopyable types are *implicitly* 
// made *ref* when parsed as arguments. 

[__NonCopyableType]
struct NonCopyableStruct
{
    [mutating] void setValue(int value) { m_value = value; }
    int m_value;
};

int doThing2(NonCopyableStruct s, int v)
{
    // Currently doesn't produce an error/warning because NonCopyableStruct is passed as *ref* implicitly.
    s.setValue(v + 1);
    return s.m_value;
}