Sergio0694 / ComputeSharp

A .NET library to run C# code in parallel on the GPU through DX12, D2D1, and dynamically generated HLSL compute and pixel shaders, with the goal of making GPU computing easy to use for all .NET developers! 🚀
MIT License
2.75k stars 123 forks source link

Add diagnostics for invalid 'this' expressions #705

Closed Sergio0694 closed 11 months ago

Sergio0694 commented 11 months ago

Closes #480

Description

This PR adds a new diagnostic error when using invalid this expressions in a shader (either DX12 or D2D). Using this is allowed for member accesses (eg. this.field), because the HLSL rewriter is rewriting those and omitting the this identifier in the final HLSL source, but the keyword itself is not actually valid in HLSL. So we now have a nice diagnostic rather than an obscure error.

rickbrew commented 11 months ago

Does this mean you can't have a local variable and a private field with the same name, and be able to access both?

Sergio0694 commented 11 months ago

Correct, that's also another problem I'm not entirely sure how to solve. Currently that will compile just fine, but the rewriter will just drop this., so you'll just end up assigning the variable to itself, which compiles fine but just does nothing. I suppose I should special case that check and emit another diagnostic, probably 🤔

rickbrew commented 11 months ago

Correct, that's also another problem I'm not entirely sure how to solve.

Rename instance fields? e.g. private float value; --> private float __this_value;

Sergio0694 commented 11 months ago

Yeah that might be a simpler solution actually 😄