Skip to content

Commit c89b46c

Browse files
authored
Merge pull request #11726 from ethereum/yulScoping
Clarify Yul scoping rules.
2 parents e9cab0f + 363278d commit c89b46c

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

docs/yul.rst

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,18 @@ as explained below) and all declarations
546546
introduce new identifiers into these scopes.
547547

548548
Identifiers are visible in
549-
the block they are defined in (including all sub-nodes and sub-blocks).
549+
the block they are defined in (including all sub-nodes and sub-blocks):
550+
Functions are visible in the whole block (even before their definitions) while
551+
variables are only visible starting from the statement after the ``VariableDeclaration``.
550552

551-
As an exception, the scope of the "init" part of the or-loop
553+
In particular,
554+
variables cannot be referenced in the right hand side of their own variable
555+
declaration.
556+
Functions can be referenced already before their declaration (if they are visible).
557+
558+
As an exception to the general scoping rule, the scope of the "init" part of the for-loop
552559
(the first block) extends across all other parts of the for loop.
553-
This means that variables declared in the init part (but not inside a
560+
This means that variables (and functions) declared in the init part (but not inside a
554561
block inside the init part) are visible in all other parts of the for-loop.
555562

556563
Identifiers declared in the other parts of the for loop respect the regular
@@ -559,21 +566,15 @@ syntactical scoping rules.
559566
This means a for-loop of the form ``for { I... } C { P... } { B... }`` is equivalent
560567
to ``{ I... for {} C { P... } { B... } }``.
561568

562-
563569
The parameters and return parameters of functions are visible in the
564570
function body and their names have to be distinct.
565571

566-
Variables can only be referenced after their declaration. In particular,
567-
variables cannot be referenced in the right hand side of their own variable
568-
declaration.
569-
Functions can be referenced already before their declaration (if they are visible).
572+
Inside functions, it is not possible to reference a variable that was declared
573+
outside of that function.
570574

571575
Shadowing is disallowed, i.e. you cannot declare an identifier at a point
572576
where another identifier with the same name is also visible, even if it is
573-
not accessible.
574-
575-
Inside functions, it is not possible to access a variable that was declared
576-
outside of that function.
577+
not possible to reference it because it was declared outside the current function.
577578

578579
Formal Specification
579580
--------------------

0 commit comments

Comments
 (0)