<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/124359>124359</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Kaleidoscope variables are resotred in a First In First Out order rather then First In Last Out
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          jmriesen
      </td>
    </tr>
</table>

<pre>
    # Issue summery
The Kaleidoscope tutorial restores shadowed variables in a First-In-First-Out fashion rather then a First-In-Last-Out fashion. 
All the programming languages I can think of off the top of my head restore variables in an FILO fashion.
Additional there is the associated code comment 
```c++
// Pop all our variables from scope.
```
Which seems to indicate that to me that FILO ordering was intended.
# Example of a program where this difference matters
```
# Note this sequence operator is given as an example of a user defined operator in chapter 6.
def binary : 1 (x y) y;
var a = 9 in (var a = 10, a = 11 in 0):a;
```
I would expect this expression to evaluate to 9, however it currently evaluates to 10
# Purposed solution
Iterate over the `OldBindings` in reverse order.

Existing code
```c++
// Pop all our variables from scope.
for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
    NamedValues[VarNames[i].first] = OldBindings[i];
```

Purposed change
```c++
// Pop all our variables from scope.
for (unsigned i = VarNames.size(); i != 0; --i)
    NamedValues[VarNames[i-1].first] = OldBindings[i-1];
```
# Affected files
A naive ripgrep search shows the following files as affected. 
``` bash
llvm-project % rg "// Pop all our variables from scope." -l
docs-build/docs/html/_sources/tutorial/MyFirstLanguageFrontend/LangImpl07.rst.txt
docs-build/docs/html/tutorial/MyFirstLanguageFrontend/LangImpl08.html
docs-build/docs/html/tutorial/MyFirstLanguageFrontend/LangImpl07.html
llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp
llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp
llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp
llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
llvm/examples/Kaleidoscope/Chapter9/toy.cpp
llvm/examples/Kaleidoscope/Chapter7/toy.cpp
llvm/examples/Kaleidoscope/Chapter8/toy.cpp
llvm/examples/Kaleidoscope/MCJIT/lazy/toy.cpp
llvm/examples/Kaleidoscope/MCJIT/lazy/toy-jit.cpp
llvm/examples/Kaleidoscope/MCJIT/cached/toy.cpp
llvm/examples/Kaleidoscope/MCJIT/cached/toy-jit.cpp
llvm/examples/Kaleidoscope/MCJIT/complete/toy.cpp
llvm/examples/Kaleidoscope/MCJIT/initial/toy.cpp
llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl07.rst
```

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0Vk2T2ygQ_TX40mWXhKyxffBBzsRV3s3XIZU9biFoSWQRaAHZ4_z6rcb2xJNsbTLObJUOCPq9pl9D0yIE3VrENSs3rLyfiDF2zq8_915jQDupnTquGS9gF8KIEMa-R39kWfWxQ_hdGNTKBekGhDhG57Uw4DFE5zFA6IRyB1SwF16L2mAAbUHAVvsQpzs7PQ3ejxEaETrtLHgRO_QQO3xi-EY8tZsBy6rKGDKEwbvWi77XtgUjbDuKFgPsQAoLsdP2L3ANuKZJxtEN9NsfoUOhLnv9ZocWtrs37x-dkS-ldNTOiuTSI-iQ6EQITmoRUYF0CkG6vkcbaXvsLjt9kvENfVnF-JbxLXxwAwhjwI3-ynHjXQ9Jytk1mmXVH52WHQTEPkB0oK3SUkSE2IlIE_15mDbtvEJPUhwEBRPRKlSJkRfw-kH0g0FSQFx0g0MKKHY6gNJNgx6tROhFjOjDN1shkncuns0D_j0mYzegF9F5kqXVe0peIBnx2t8Y0IPCRltUVwgLshNDRA93tE2FDdTaCn8EVlSQA-PLBzgyvoIjK0jFvfAggBX3sCI048uvM3nG-KvLOKfljPEVKypxwl7HsoODG40CfBhQxlNE-DB4DIGOYnSAe2HGpLSDFRF37oB79KAjyNF7tNEcH61ScvKL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