OP explicitly excluded the with ambiguity from his question, because he is well aware of it.. It is about the range....
Exactly!
I made this test as an example for someone in a different forum to show that it is very easy to introduce ambiguity and hence (not so) subtle sources for hard-to-track-down bugs in non-trivial programs. And this has been this way for the 48 years that I am programming in Pascal now...
What indeed got me worried is that there is no range checking when the assignment to BufferB [Counter] is done. I would have expected that there would be a runtime error (out of bounds), regardless which "Counter" is being set to 1 at the beginning on the nested WITH block.
If it is B.Counter, as the documentation at 13.2.8 states
This also is a clear example of the fact that the variables are tried last to first, i. e., when the compiler encounters a variable reference, it will first check if it is a field or method of the last variable. If not, then it will check the last-but-one, and so on
. So setting Counter to 1, it should generate a runtime error on the BufferB assignment, as it is defined as an array of 101..200, trying to access element [1].
There also should be a range check error if, contrary to the documentation, A.Counter would be assigned the 1.
But what gets really troubling is the output of the program. BufferA [1] has clearly been changed from the initialized 0 to 1 (Counter), BufferB [101] shows the initialized value of "?" instead of the character "1". So is the assignment to BufferB [Counter] in this case ignored, as nothing is written to the first element of the array, "just in case" the compiler is "assuming" to write to the first element, regardless what the logical array boundaries are
or does it write that byte just "somewhere"?
As I would expect that without specifically changing the range checking to be off, it is always on, this does not work as expected. Btw, I compiled and ran this from within Lazarus 3.2, and I am definitely NOT intentionally turning off any of the range or IO checking, as that goes against the basic principles of Pascal.