KhronosGroup / KhronosGroup/GLSL

Clarify GLSL for-loop scoping rules

Open
#226 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
458
Forks
114
Avg merge
4m
Merged PRs (30d)
1

Description

The following GLSL code compiles in every GLSL implementation I have immediate access to: glslang, WebGL and Apple GLSL.

```
void main(void)
{
int y;
for (int x=0; x<10; ++x) int y;
}
```

I am trying to understand if this is intentional. The docs (6.3) have the following to say about for-loop scoping: "For both for and while loops, the sub-statement does not introduce a new scope for variable names."

There is no braced-block to introduce a new scope, and the docs explicitly state that the for-loop body does not implicitly introduce a scope.

I believe the answer is that the for-statement itself introduces a scope, since the variable `int x=0` needs to disappear at the end of the for-statement. We can get additional evidence of this, because the following program also compiles:

```
void main(void)
{
int x;
for (int x=0; x<10; ++x) {}
}
```

However, the docs don't explain this rule anywhere. The statement "For both for and while loops, the sub-statement does not introduce a new scope for variable names" should be clarified to explain that the for-statement itself introduces the scope, and the sub-statement inherits that same scope even if braces appear (which would normally introduce a new deeper scope).

Contributor guide

Open the contributing guide

Research direction

Start with specification section 6.3 and compare its wording about for- and while-loop sub-statements with the two GLSL examples in the issue. Clarify whether the for-statement introduces the scope and how its sub-statement inherits that scope, including the braced form; done means the specification explicitly explains these cases.

Written by the indexing model from the issue text.

Assessment

Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.