Skip to content

Commit cc8f9d8

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

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

docs/coming-from-hlsl.md

+30
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,36 @@ int MyFunc()
7575
}
7676
```
7777

78+
#### Variable scope in `for`-loop follows the old FXC rule
79+
The older version of HLSL compiler, FXC, had a little different variable scope for the `for`-loop compared to other languages.
80+
81+
FXC produces an error or a warning for the following code:
82+
```
83+
for (int i = 0; i < 4; i++) {...}
84+
for (int i = 0; i < 4; i++) {...}
85+
```
86+
87+
The warning looks like the following:
88+
```
89+
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
90+
```
91+
92+
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:
93+
```
94+
error 30200: declaration of 'i' conflicts with existing declaration
95+
```
96+
97+
To resolve the problem, you can modify the shader to reuse the previously declared variable:
98+
```
99+
for (int i = 0; i < 4; i++) {...}
100+
for (i = 0; i < 4; i++) {...} // Reuse the variable declared from the previous for-loop
101+
```
102+
103+
Or, you can explicitly set the language to be `slang`:
104+
```
105+
slangc.exe -lang slang -stage vertex -entry vertexMain my_shader.hlsl
106+
```
107+
78108

79109
#### Member functions are immutable by default
80110
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++.

0 commit comments

Comments
 (0)