diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2023-09-06 19:32:06 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-06 16:32:06 -0700 |
| commit | 891a6cf376c6b2560231502614b37c332f44ddea (patch) | |
| tree | 556f95d0125fda05fcf9dbf18b956f21fc800451 /tests/diagnostics | |
| parent | 20bd5e7440e3d28715bed449a336003ba02d7d0f (diff) | |
Make a warning if a [mutating] method is called on an in param. (#3184)
* Make a warning if a [mutating] method is passed as an in param.
* Kick CI.
---------
Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'tests/diagnostics')
| -rw-r--r-- | tests/diagnostics/param-mutation.slang | 39 | ||||
| -rw-r--r-- | tests/diagnostics/param-mutation.slang.expected | 8 |
2 files changed, 47 insertions, 0 deletions
diff --git a/tests/diagnostics/param-mutation.slang b/tests/diagnostics/param-mutation.slang new file mode 100644 index 000000000..835e645c8 --- /dev/null +++ b/tests/diagnostics/param-mutation.slang @@ -0,0 +1,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; +} + diff --git a/tests/diagnostics/param-mutation.slang.expected b/tests/diagnostics/param-mutation.slang.expected new file mode 100644 index 000000000..a1fb34fb7 --- /dev/null +++ b/tests/diagnostics/param-mutation.slang.expected @@ -0,0 +1,8 @@ +result code = 0 +standard error = { +tests/diagnostics/param-mutation.slang(17): warning 30068: mutating method 'setValue' called on `in` parameter 's'; changes will not be visible to caller. copy the parameter into a local variable if this behavior is intended + s.setValue(v + 1); + ^ +} +standard output = { +} |
