From 20bd48659d0009de5477380c335e2419f4c66f8b Mon Sep 17 00:00:00 2001 From: venkataram-nv Date: Mon, 12 Aug 2024 14:18:02 -0700 Subject: Warn when inout parameter is never written (#4777) Addresses #4698 as one approach to diagnose the potential problem. Emit warnings when a user marks a parameter as `inout` but never writes to it in the function. A new intrinsic function `unmodified(out T)` has been added to explicitly indicate that an `inout` variable will not be modified in the function. This is only one way to address the specific validation error in #4698. In general it seems that DXC does some more extensive checks on actual struct fields (as opposed to observing arbitrary struct writes), so that will be the next step. --- tests/diagnostics/inout-never-written.slang | 48 +++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/diagnostics/inout-never-written.slang (limited to 'tests/diagnostics') diff --git a/tests/diagnostics/inout-never-written.slang b/tests/diagnostics/inout-never-written.slang new file mode 100644 index 000000000..0bc59ecf3 --- /dev/null +++ b/tests/diagnostics/inout-never-written.slang @@ -0,0 +1,48 @@ +//TEST:SIMPLE(filecheck=CHK): -target spirv + +struct State +{ + float3 v; + float3 n; + int rnd; +}; + +//CHK-DAG: ([[# @LINE + 1]]): warning 41022: inout parameter 'x' is never written to +void int_never_assigned(inout int x) {} + +//CHK-DAG: ([[# @LINE + 1]]): warning 41022: inout parameter 'state' is never written to +void state_never_assigned(inout State state, inout float v) +{ + v = state.v.x; +} + +void state_assigned(inout State state) +{ + state.rnd = (int) dot(state.v, state.n); +} + +struct A +{ + int state; + + //CHK-DAG: ([[# @LINE + 1]]): warning 41023: method marked `[mutable]` but never modifies `this` + [mutating] int next() { return state; } + + [mutating] int progress() + { + unmodified(state); + return state; + } +}; + +__generic +struct B +{ + int state; + + //CHK-DAG: ([[# @LINE + 1]]): warning 41023: method marked `[mutable]` but never modifies `this` + [mutating] int next() { return state; } +}; + +//CHK-NOT: warning 41022 +//CHK-NOT: warning 41023 \ No newline at end of file -- cgit v1.2.3