Skip to content

Commit 90589ae

Browse files
committed
Add a new section that explains the variable scope problem with for-loop
1 parent f6f07c3 commit 90589ae

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/coming-from-hlsl.md

+31
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,37 @@ int MyFunc()
6464
```
6565

6666

67+
### Variable scope in `for`-loop follows the old FXC rule
68+
The older version of HLSL compiler, FXC, had a little different variable scope for the `for`-loop compared to other languages.
69+
70+
FXC produces an error or a warning for the following code:
71+
```
72+
for (int i = 0; i < 4; i++) {...}
73+
for (int i = 0; i < 4; i++) {...}
74+
```
75+
76+
The warning looks like the following:
77+
```
78+
my_shader.hlsl(8,14): warning X3078: 'i': loop control variable conflicts with a previous declaration in the outer scope; most recent declaration will be used
79+
```
80+
81+
This is no longer the case with the recent HLSL compiler, DXC. But Slang respects the old FXC rule and you may encounter an error like:
82+
```
83+
error 30200: declaration of 'i' conflicts with existing declaration
84+
```
85+
86+
To resolve the problem, you can modify the shader to reuse the previously declared variable:
87+
```
88+
for (int i = 0; i < 4; i++) {...}
89+
for (i = 0; i < 4; i++) {...} // Reuse the variable declared from the previous for-loop
90+
```
91+
92+
Or, you can explicitly set the language to be `slang`:
93+
```
94+
slangc.exe -lang slang -stage vertex -entry vertexMain my_shader.hlsl
95+
```
96+
97+
6798
### Member functions are immutable by default
6899
By default, Slang member functions do not allow mutations to `this`. It is as if the member function has the `const` keyword in C/C++.
69100

0 commit comments

Comments
 (0)