@@ -169,11 +169,6 @@ See [[#keyword-summary]] for a list of keywords.
169169 <tr><td> `IDENT`<td> `[a-zA-Z][0-9a-zA-Z_] *`
170170</table>
171171
172- Note: literals are parsed greedy. This means that for statements like `a -5`
173- this will *not* parse as `a` `minus` `5` but instead as `a` `-5` which
174- may be unexpected. A space must be inserted after the `-` if the first
175- expression is desired.
176-
177172An identifier must not have the same spelling as a keyword or as a reserved keyword.
178173
179174## Attributes ## {#attributes}
@@ -344,10 +339,64 @@ Examples of predeclared objects are:
344339* built-in types.
345340
346341A declaration must not introduce a name when that identifier is
347- already in scope at the start of the declaration.
348- That is, shadow names are not allowed in [SHORTNAME] .
342+ already in scope with the same end scope as another instance of that name.
343+ When an identifier is referenced and multiple declarations are in scope, the
344+ reference is resolved to the closest preceding in scope declaration.
345+
346+ <div class='example' heading='Valid and invalid declarations'>
347+ <xmp>
348+ // Invalid, cannot reuse built-in function names. See [[#builtin-functions]] .
349+ var<private> modf : f32 = 0.0;
350+
351+ // Valid, foo_1 is in scope until the end of the program.
352+ var<private> foo : f32 = 0.0; // foo_1
353+
354+ // Valid, bar_1 is in scope until the end of the program.
355+ var<private> bar : u32 = 0u; // bar_1
356+
357+ // Valid, my_func_1 is in scope until the end of the program.
358+ // Valid, foo_2 is in scope until the end of the program.
359+ fn my_func(foo : f32) { // my_func_1, foo_2
360+ // Any reference to 'foo' resolves to the function parameter.
361+
362+ // Invalid, the scope of foo_3 ends at the of the function.
363+ var foo : f32; // foo_3
364+
365+ // Valid, bar_2 is in scope until the end of the program.
366+ var bar : u32; // bar_2
367+ // References to 'bar' resolve to bar_2
368+ {
369+ // Valid, bar_3 is in scope until the end of the compound statement.
370+ var bar : u32; // bar_3
371+ // References to 'bar' resolve to bar_3
372+
373+ // Invalid, bar_4 has the same end scope as bar_3.
374+ var bar : i32; // bar_4
375+
376+ // Valid, i_1 is in scope until the end of the for loop
377+ for (var i : i32 = 0; i < 10; i = i + 1) { // i_1
378+ // Invalid, i_2 has the same end scope as i_1.
379+ var i : i32 = 1; // i_2.
380+ }
381+ }
382+
383+ // Invalid, bar_5 has the same end scope as bar_2.
384+ var bar : u32; // bar_5
385+ }
386+
387+ // Invalid, bar_6 has the same end scope as bar_1.
388+ var<private> bar : u32 = 1u; // bar_6
389+
390+ // Invalid, my_func_2 has the same end scope as my_func_1.
391+ fn my_func() { } // my_func_2
349392
350- Issue: PR [1472] (https://github.com/gpuweb/gpuweb/pull/1472) is intended to relax this constraint.
393+ // Valid, my_foo_1 is in scope until the end of the program.
394+ fn my_foo(
395+ // Valid, my_foo_2 is in scope until the end of the function.
396+ my_foo : i32 // my_foo_2
397+ ) { }
398+ </xmp>
399+ </div>
351400
352401There are multiple levels of scoping depending on how and where things are
353402declared.
@@ -3995,26 +4044,18 @@ A function declaration must only occur at [=module scope=].
39954044The function name is [=in scope=] from the start of the formal parameter list
39964045until the end of the program.
39974046
3998- The function name must be different from the name of every other declared object, including
3999- those declared inside other functions.
4000-
4001- Note: This restriction is in addition to the rules in [[#declaration-and-scope]] .
4002-
4003- Issue: PR [#1472] (https://github.com/gpuweb/gpuweb/pull/1472) is intended to relax this constraint.
4004-
40054047A <dfn noexport>formal parameter</dfn> declaration specifies an identifier name and a type for a value that must be
40064048provided when invoking the function.
40074049A formal parameter may have attributes.
40084050See [[#function-calls]] .
4009- The scope of the identifier is the whole function body .
4051+ The identifier is [=in scope=] until the end of the function.
40104052Two formal parameters for a given function must not have the same name.
40114053
40124054If the function declaration does not specify a return type, then the function return type is [=void=] .
40134055
40144056If the return type is not [=void=] ,
40154057then the last statement in the function body must be a [=return=] statement.
40164058
4017-
40184059<pre class='def'>
40194060function_decl
40204061 : attribute_list* function_header compound_statement
0 commit comments