|
| 1 | +"""`break`/`continue` inside a loop's `else` binds to the *enclosing* loop. |
| 2 | +
|
| 3 | +A loop's else clause is not part of its body, so Python binds loop control written |
| 4 | +there to whatever loop encloses the whole statement. The lowering used to get this |
| 5 | +wrong in two different ways: |
| 6 | +
|
| 7 | + * `ForLoopOpLowering` rewrote its orelse's trailing yield without checking the |
| 8 | + yield's kind, so the inner loop swallowed a `break` meant for the outer one — |
| 9 | + silently running every outer iteration. |
| 10 | +
|
| 11 | + * the enclosing loop's walker did claim the yield when the nested loop was a |
| 12 | + `while`, but emitted the branch while that `py.while` was still unlowered, |
| 13 | + producing a cross-region block reference the verifier rejects. |
| 14 | +
|
| 15 | +Both are now handled by deferring: a loop refuses to lower while a nested loop |
| 16 | +still holds a break/continue that binds to it, so the nested loop is flattened |
| 17 | +into the enclosing region first and the branch is same-region by construction. |
| 18 | +That handshake is also why both loop patterns share one pass. |
| 19 | +
|
| 20 | +Binding outwards is transitive: an else nested inside another else is still |
| 21 | +lexically part of whichever loop body encloses the pair, so the deferral has to |
| 22 | +follow the whole chain rather than stop one level in. |
| 23 | +""" |
| 24 | + |
| 25 | +# break in a nested for's else breaks the OUTER for. |
| 26 | +log = [] |
| 27 | +for outer in [1, 2, 3]: |
| 28 | + log.append(outer) |
| 29 | + for inner in []: |
| 30 | + pass |
| 31 | + else: |
| 32 | + break |
| 33 | +assert log == [1], log |
| 34 | + |
| 35 | +# Same with a while as the inner loop. |
| 36 | +log = [] |
| 37 | +for outer in [1, 2, 3]: |
| 38 | + log.append(outer) |
| 39 | + while False: |
| 40 | + pass |
| 41 | + else: |
| 42 | + break |
| 43 | +assert log == [1], log |
| 44 | + |
| 45 | +# continue in a nested loop's else continues the OUTER loop, skipping the rest |
| 46 | +# of the outer body. |
| 47 | +log = [] |
| 48 | +for outer in [1, 2, 3]: |
| 49 | + log.append(outer) |
| 50 | + while False: |
| 51 | + pass |
| 52 | + else: |
| 53 | + continue |
| 54 | + log.append("after-must-not-run") |
| 55 | +assert log == [1, 2, 3], log |
| 56 | + |
| 57 | +log = [] |
| 58 | +for outer in [1, 2, 3]: |
| 59 | + log.append(outer) |
| 60 | + for inner in []: |
| 61 | + pass |
| 62 | + else: |
| 63 | + continue |
| 64 | + log.append("after-must-not-run") |
| 65 | +assert log == [1, 2, 3], log |
| 66 | + |
| 67 | +# A while as the enclosing loop. |
| 68 | +log = [] |
| 69 | +n = 0 |
| 70 | +while n < 3: |
| 71 | + n += 1 |
| 72 | + log.append(n) |
| 73 | + for inner in []: |
| 74 | + pass |
| 75 | + else: |
| 76 | + continue |
| 77 | + log.append("after-must-not-run") |
| 78 | +assert log == [1, 2, 3], log |
| 79 | + |
| 80 | +# The inner loop's own body break still binds to the inner loop, and the inner |
| 81 | +# else is then skipped. |
| 82 | +log = [] |
| 83 | +for outer in [1, 2]: |
| 84 | + for inner in [10, 20]: |
| 85 | + log.append((outer, inner)) |
| 86 | + break |
| 87 | + else: |
| 88 | + log.append("inner-else-must-not-run") |
| 89 | + log.append(("after", outer)) |
| 90 | +assert log == [(1, 10), ("after", 1), (2, 10), ("after", 2)], log |
| 91 | + |
| 92 | +# Three levels: the break binds to the loop enclosing the loop whose else it is, |
| 93 | +# i.e. the middle one, so the outermost keeps iterating. |
| 94 | +log = [] |
| 95 | +for a in [1, 2]: |
| 96 | + for b in [10, 20]: |
| 97 | + log.append((a, b)) |
| 98 | + for c in []: |
| 99 | + pass |
| 100 | + else: |
| 101 | + break |
| 102 | + log.append(("outer", a)) |
| 103 | +assert log == [(1, 10), ("outer", 1), (2, 10), ("outer", 2)], log |
| 104 | + |
| 105 | +# An else that neither breaks nor continues still falls through to the exit. |
| 106 | +log = [] |
| 107 | +for outer in [1, 2]: |
| 108 | + for inner in []: |
| 109 | + pass |
| 110 | + else: |
| 111 | + log.append(("else", outer)) |
| 112 | + log.append(("after", outer)) |
| 113 | +assert log == [("else", 1), ("after", 1), ("else", 2), ("after", 2)], log |
| 114 | + |
| 115 | +# An else nested inside another else: the break binds outwards through *both*, to |
| 116 | +# the outermost loop. Checking only the first nested loop's else missed this and |
| 117 | +# silently dropped the break. |
| 118 | +log = [] |
| 119 | +for a in [1, 2, 3]: |
| 120 | + log.append(a) |
| 121 | + for b in []: |
| 122 | + pass |
| 123 | + else: |
| 124 | + for c in []: |
| 125 | + pass |
| 126 | + else: |
| 127 | + break |
| 128 | +assert log == [1], log |
| 129 | + |
| 130 | +# Same shape with `continue`, which must skip the rest of the outermost body. |
| 131 | +log = [] |
| 132 | +for a in [1, 2, 3]: |
| 133 | + log.append(a) |
| 134 | + for b in []: |
| 135 | + pass |
| 136 | + else: |
| 137 | + for c in []: |
| 138 | + pass |
| 139 | + else: |
| 140 | + continue |
| 141 | + log.append("after-must-not-run") |
| 142 | +assert log == [1, 2, 3], log |
| 143 | + |
| 144 | +# Same shape built from `while`s. Here the mis-binding was not silent: the branch |
| 145 | +# was emitted while the inner py.while was still a region of its own, which the |
| 146 | +# verifier rejects as a reference to a block in another region. |
| 147 | +log = [] |
| 148 | +for a in [1, 2, 3]: |
| 149 | + log.append(a) |
| 150 | + while False: |
| 151 | + pass |
| 152 | + else: |
| 153 | + while False: |
| 154 | + pass |
| 155 | + else: |
| 156 | + break |
| 157 | +assert log == [1], log |
| 158 | + |
| 159 | +# Three elses deep, to check the walk follows the chain rather than a fixed depth. |
| 160 | +log = [] |
| 161 | +for a in [1, 2, 3]: |
| 162 | + log.append(a) |
| 163 | + for b in []: |
| 164 | + pass |
| 165 | + else: |
| 166 | + for c in []: |
| 167 | + pass |
| 168 | + else: |
| 169 | + for d in []: |
| 170 | + pass |
| 171 | + else: |
| 172 | + break |
| 173 | +assert log == [1], log |
| 174 | + |
| 175 | +# The chain stops at the first loop *body*: this break is in the body of a loop |
| 176 | +# that happens to sit in an else, so it binds to that loop and no further. |
| 177 | +log = [] |
| 178 | +for a in [1, 2]: |
| 179 | + for b in []: |
| 180 | + pass |
| 181 | + else: |
| 182 | + for c in [10, 20]: |
| 183 | + log.append((a, c)) |
| 184 | + break |
| 185 | + log.append(("after", a)) |
| 186 | +assert log == [(1, 10), ("after", 1), (2, 10), ("after", 2)], log |
0 commit comments