Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shadowing variables inside of flow control don't produce shadowing warning #4029

Closed
Jasper-Bekkers opened this issue Oct 21, 2021 · 1 comment

Comments

@Jasper-Bekkers
Copy link

This doesn't produce a shadowing warning:

http://shader-playground.timjones.io/f463aa8e1b9249b2dabc84168c522ff7

void stuff(out float3 a) {
     if(true) { 
          float3 a = 1; 
     } 
}

Whereas this does:

http://shader-playground.timjones.io/86bc74ba95742f8f45f7f398faa3e9d5

void stuff(out float3 a) { float3 a = 1; }

I think the language should either allow variable shadowing all together, or dis-allow it all together, not have exceptions for when variables are shadowed inside if-statements like this.

@Jasper-Bekkers Jasper-Bekkers changed the title Shadowin variables inside of flow control don't produce shadowing warning Shadowing variables inside of flow control don't produce shadowing warning Oct 21, 2021
@pow2clk
Copy link
Member

pow2clk commented Oct 25, 2021

Just for clarity, I'll be pedantic and point out that the diagnostic message produced by the second case is an error and the exception isn't for if-statements but for different scopes. Placing the shadowing variable in an anonymous nested scope has the same effect of silencing the error.

Allowing variable shadowing in nested scopes is consistent with many languages that are both familiar to authors and the origin point of porting efforts. Parameters are considered in the same scope as the declared variables, so they are disallowed as being in the same scope.

In particular, this is how C++ behaves and how HLSL has always behaved. Our intent is to narrow the gap between HLSL and C++. I'm afraid this is inconsistent with that goal.

If you'd like warnings for whenever shadowing occurs even in this scenario, you can enable the -Wshadow flag.

@pow2clk pow2clk closed this as completed Oct 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants