Skip to content

Commit 9fe7f49

Browse files
RexJaeschkeBillWagner
authored andcommitted
Add static local functions
1 parent 4d503b4 commit 9fe7f49

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

standard/statements.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,8 @@ local_function_modifier
505505
;
506506
507507
ref_local_function_modifier
508-
: unsafe_modifier // unsafe code support
508+
: 'static'
509+
| unsafe_modifier // unsafe code support
509510
;
510511
511512
local_function_body
@@ -561,9 +562,9 @@ Unless specified otherwise below, the semantics of all grammar elements is the s
561562
562563
The *identifier* of a *local_function_declaration* must be unique in its declared block scope, including any enclosing local variable declaration spaces. One consequence of this is that overloaded *local_function_declaration*s are not allowed.
563564
564-
A *local_function_declaration* may include one `async` ([§15.15](classes.md#1515-async-functions)) modifier and one `unsafe` ([§23.1](unsafe-code.md#231-general)) modifier. If the declaration includes the `async` modifier then the return type shall be `void` or aTaskType»` type ([§15.15.1](classes.md#15151-general)). The `unsafe` modifier uses the containing lexical scope. The `async` modifier does not use the containing lexical scope. It is a compile-time error for *type_parameter_list* or *formal_parameter_list* to contain *attributes*.
565+
A *local_function_declaration* may include one `async` ([§15.15](classes.md#1515-async-functions)) modifier and one `unsafe` ([§23.1](unsafe-code.md#231-general)) modifier. If the declaration includes the `async` modifier then the return type shall be `void` or aTaskType»` type ([§15.15.1](classes.md#15151-general)). The `unsafe` modifier uses the containing lexical scope. The `unsafe` modifier uses the containing lexical scope. The `async` modifier does not use the containing lexical scope. If the declaration includes the `static` modifier, the function is a ***static local function***; otherwise, it is a ***non-static local function***. It is a compile-time error for *type_parameter_list* or *formal_parameter_list* to contain *attributes*.
565566
566-
A local function is declared at block scope, and that function may capture variables from the enclosing scopes. It is a compile-time error if a captured variable is read by the body of the local function but is not definitely assigned before each call to the function. The compiler shall determine which variables are definitely assigned on return ([§9.4.4.33](variables.md#94433-rules-for-variables-in-local-functions)).
567+
A local function is declared at block scope, and that function may capture variables from the enclosing scopes. A non-static local function may capture variables from the enclosing scope while a static local function may not (so it has no access to enclosing locals, parameters, or `this`). It is a compile-time error if a captured variable is read by the body of the non-static local function but is not definitely assigned before each call to the function. The compiler shall determine which variables are definitely assigned on return ([§9.4.4.33](variables.md#94433-rules-for-variables-in-local-functions)).
567568
568569
When the type of `this` is a struct type, it is a compile-time error for the body of a local function to access `this`. This is true whether the access is explicit (as in `this.x`) or implicit (as in `x` where `x` is an instance member of the struct). This rule only prohibits such access and does not affect whether member lookup results in a member of the struct.
569570
@@ -604,6 +605,19 @@ Local function bodies are always reachable. The endpoint of a local function dec
604605
605606
If the type of the argument to a local function is `dynamic`, the function to be called must be resolved at compile time, not runtime.
606607
608+
A static local function
609+
610+
- May not reference instance members from an implicit or explicit `this` or `base` reference.
611+
- May reference `static` members from the enclosing scope.
612+
- May reference `constant` definitions from the enclosing scope.
613+
- May use `nameof()` to reference locals, parameters, or `this` or `base` from the enclosing scope.
614+
- May capture state from an enclosing static local function, but may not capture state outside the enclosing static local function. The same rules apply to a lambda.
615+
- May not be invoked in an expression tree.
616+
617+
Overload resolution of a call within a local function is not affected by whether the local function is static.
618+
619+
Removing the `static` modifier from a local function in a valid program does not change the meaning of the program.
620+
607621
## 13.7 Expression statements
608622
609623
An *expression_statement* evaluates a given expression. The value computed by the expression, if any, is discarded.

0 commit comments

Comments
 (0)