Skip to content

Commit 0467ef9

Browse files
authored
Add support for static local functions (#869)
1 parent 63a30b3 commit 0467ef9

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

standard/statements.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,8 @@ local_function_modifier
507507
;
508508
509509
ref_local_function_modifier
510-
: unsafe_modifier // unsafe code support
510+
: 'static'
511+
| unsafe_modifier // unsafe code support
511512
;
512513
513514
local_function_body
@@ -563,9 +564,9 @@ Unless specified otherwise below, the semantics of all grammar elements is the s
563564
564565
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.
565566
566-
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*.
567+
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)). 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*. If the local function is declared in an unsafe context23.2), the local function may include unsafe code, even if the local function declaration doesn't include the `unsafe` modifier.
567568
568-
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)).
569+
A local function is declared at block scope. A non-static local function may capture variables from the enclosing scope while a static local function shall not (so it has no access to enclosing locals, parameters, non-static local functions, or `this`). It is a compile-time error if a captured variable is read by the body of a 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)).
569570
570571
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.
571572
@@ -606,6 +607,13 @@ Local function bodies are always reachable. The endpoint of a local function dec
606607
607608
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.
608609
610+
A local function shall not be used in an expression tree.
611+
612+
A static local function
613+
614+
- May reference static members, type parameters, constant definitions and static local functions from the enclosing scope.
615+
- Shall not reference `this` or `base` nor instance members from an implicit `this` reference, nor local variables, parameters, or non-static local functions from the enclosing scope. However, all these are permitted in a `nameof()` expression.
616+
609617
## 13.7 Expression statements
610618
611619
An *expression_statement* evaluates a given expression. The value computed by the expression, if any, is discarded.

0 commit comments

Comments
 (0)