* Fix #5691 * Cleanup and thoughts * Use scope approach * Seperate defined/declared/declaredInScope magics * Fix declaredInScope * Update spec accordingly
This commit is contained in:
parent
d11933ad99
commit
fb58066b61
15 changed files with 180 additions and 85 deletions
|
|
@ -5019,23 +5019,16 @@ delayed until template instantiation time:
|
|||
:status: 1
|
||||
|
||||
template t(body: typed) =
|
||||
proc p = echo "hey"
|
||||
block:
|
||||
body
|
||||
|
||||
t:
|
||||
var i = 1
|
||||
echo i
|
||||
p() # fails with 'undeclared identifier: p'
|
||||
|
||||
t:
|
||||
var i = 2 # fails with 'attempt to redeclare i'
|
||||
echo i
|
||||
|
||||
The above code fails with the mysterious error message that ``i`` has already
|
||||
been declared. The reason for this is that the ``var i = ...`` bodies need to
|
||||
be type-checked before they are passed to the ``body`` parameter and type
|
||||
checking in Nim implies symbol lookups. For the symbol lookups to succeed
|
||||
``i`` needs to be added to the current (i.e. outer) scope. After type checking
|
||||
these additions to the symbol table are not rolled back (for better or worse).
|
||||
The above code fails with the error message that ``p`` is not declared.
|
||||
The reason for this is that the ``p()`` body is type-checked before getting
|
||||
passed to the ``body`` parameter and type checking in Nim implies symbol lookups.
|
||||
The same code works with ``untyped`` as the passed body is not required to be
|
||||
type-checked:
|
||||
|
||||
|
|
@ -5043,16 +5036,12 @@ type-checked:
|
|||
:test: "nim c $1"
|
||||
|
||||
template t(body: untyped) =
|
||||
proc p = echo "hey"
|
||||
block:
|
||||
body
|
||||
|
||||
t:
|
||||
var i = 1
|
||||
echo i
|
||||
|
||||
t:
|
||||
var i = 2 # compiles
|
||||
echo i
|
||||
p() # compiles
|
||||
|
||||
|
||||
Varargs of untyped
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue