It is nothing wrong with the code just that the debug info has wrongly defined scope for the variable y in the example.
In the example in function test1 when it is about to return (line 29) the value y has wrong value (52). It should be 11.
In the function test2 when it is about to return (line 15) the variable z should be "not in current context"
sdcc.exe -mstm8 test2.c --out-fmt-elf --debug
SDCC : hc08/stm8 3.6.9 # (CYGWIN)
Bugs: #2908
Wiki: SDCC 4.3.0 Release
Wiki: SDCC 4.4.0 Release
Wiki: SDCC 4.5.0 Release
Wiki: SDCC 4.6.0 Release
Seems the symbols does not have any block numbers attached.
This is what is in symbol given to dwWriteSymbol. Block is always 0...