| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
R4 makes a guarded cell in a `row`/`cells` body appear as an adjacent pair over |
| 5 |
the same predicate, `when` then `unless`. R4(b) observes that such a pair |
| 6 |
expands to two INDEPENDENT `if` statements rather than an `if`/`else`, so a pair |
| 7 |
whose arms both consume the same owned value moves it twice and the generated |
| 8 |
code does not compile. R9 is what makes that inevitable: a hole is one eager |
| 9 |
evaluation whose owned result lands in a plain field. |
| 10 |
|
| 11 |
The defect is real. Reproduced 2026-09-03 as E0382 against quasi-router, in a |
| 12 |
throwaway crate holding exactly what the macro would emit: |
| 13 |
|
| 14 |
fn moved_twice(label: String, compact: bool) -> Cells { |
| 15 |
let mut cells = Vec::new(); |
| 16 |
if compact { cells.push(Cell::new(label)); } |
| 17 |
if !compact { cells.push(Cell::new(label)); } // E0382 |
| 18 |
Cells::new(cells) |
| 19 |
} |
| 20 |
|
| 21 |
What was unmeasured was its INCIDENCE, and the defect lives in code that does not |
| 22 |
exist yet, so what is measurable is the antecedent: a positional-container shape |
| 23 |
holding a two-armed conditional in emission position whose arms share a binding |
| 24 |
that is not Copy. Every one of those is a site that, re-authored under R4's |
| 25 |
pairing rule, becomes two independent `if`s. |
| 26 |
|
| 27 |
Positional containers are the shapes R4 governs: Row, Cells, Cell, Column and |
| 28 |
their plurals. A Slot and a Run accrete, so a lone guarded emission is safe there |
| 29 |
and R4(b) cannot arise. |
| 30 |
|
| 31 |
The answer is ZERO, with ten near-misses that each dissolve for a different |
| 32 |
reason. Six are `match` arms, which R7 makes safe. Four are `if`/`else`: two |
| 33 |
name the carried container or a Vec accumulator, which rebind rather than move, |
| 34 |
and two are `Act::disabled`, where R5 guards the attribute and the emission stays |
| 35 |
single. Full reading: wiki `quasi-declare-form` section 11. |
| 36 |
|
| 37 |
python3 scripts/r4b-incidence.py |
| 38 |
|
| 39 |
Reuses population.py's blanking, `#[cfg(test)]` stripping and shape discovery, so |
| 40 |
the denominator is the ratified 514. Re-run it after any change to the three |
| 41 |
shape trees: zero is a fact about today's code, not a property of the form. |
| 42 |
|
| 43 |
|
| 44 |
from __future__ import annotations |
| 45 |
|
| 46 |
import importlib.util |
| 47 |
import re |
| 48 |
|
| 49 |
from pathlib import Path |
| 50 |
|
| 51 |
ROOT = Path.home() / "Code" |
| 52 |
POP = ROOT / "quasi" / "scripts" / "population.py" |
| 53 |
|
| 54 |
spec = importlib.util.spec_from_file_location("population", POP) |
| 55 |
pop = importlib.util.module_from_spec(spec) |
| 56 |
spec.loader.exec_module(pop) |
| 57 |
|
| 58 |
|
| 59 |
POSITIONAL = {"Row", "Cells", "Cell", "Column"} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
COPY_ISH = re.compile( |
| 65 |
r"^(?:&|bool$|char$|u8$|u16$|u32$|u64$|usize$|i8$|i16$|i32$|i64$|isize$|f32$|f64$)" |
| 66 |
) |
| 67 |
|
| 68 |
|
| 69 |
def head(ret: str) -> str: |
| 70 |
|
| 71 |
t = pop.head_type(ret) |
| 72 |
return t or "" |
| 73 |
|
| 74 |
|
| 75 |
def body_of(src: str, line: int) -> str: |
| 76 |
|
| 77 |
off = 0 |
| 78 |
for _ in range(line - 1): |
| 79 |
off = src.index("\n", off) + 1 |
| 80 |
brace = src.index("{", off) |
| 81 |
end = pop.match_brace(src, brace) |
| 82 |
return src[brace:end] if end > 0 else "" |
| 83 |
|
| 84 |
|
| 85 |
def sig_at(src: str, line: int) -> str: |
| 86 |
|
| 87 |
off = 0 |
| 88 |
for _ in range(line - 1): |
| 89 |
off = src.index("\n", off) + 1 |
| 90 |
brace = src.index("{", off) |
| 91 |
return src[off:brace] |
| 92 |
|
| 93 |
|
| 94 |
def owned_params(sig: str) -> set[str]: |
| 95 |
|
| 96 |
out = set() |
| 97 |
inner = sig[sig.index("(") + 1 : sig.rindex(")")] if "(" in sig else "" |
| 98 |
for part in pop.split_top(inner): |
| 99 |
part = part.strip() |
| 100 |
if not part or ":" not in part or part.startswith("&self") or part == "self": |
| 101 |
continue |
| 102 |
name, ty = part.split(":", 1) |
| 103 |
name, ty = name.strip(), ty.strip() |
| 104 |
if not name.isidentifier(): |
| 105 |
continue |
| 106 |
if not COPY_ISH.match(ty): |
| 107 |
out.add(name) |
| 108 |
return out |
| 109 |
|
| 110 |
|
| 111 |
def conditionals(body: str) -> list[tuple[str, str, str]]: |
| 112 |
|
| 113 |
found = [] |
| 114 |
for m in re.finditer(r"\bif\b", body): |
| 115 |
i = m.end() |
| 116 |
b1 = body.find("{", i) |
| 117 |
if b1 < 0: |
| 118 |
continue |
| 119 |
cond = body[i:b1].strip() |
| 120 |
if not cond or "{" in cond: |
| 121 |
continue |
| 122 |
e1 = pop.match_brace(body, b1) |
| 123 |
if e1 < 0: |
| 124 |
continue |
| 125 |
rest = body[e1:] |
| 126 |
me = re.match(r"\s*else\s*\{", rest) |
| 127 |
if not me: |
| 128 |
continue |
| 129 |
b2 = e1 + me.end() - 1 |
| 130 |
e2 = pop.match_brace(body, b2) |
| 131 |
if e2 < 0: |
| 132 |
continue |
| 133 |
found.append((cond, body[b1:e1], body[b2:e2])) |
| 134 |
return found |
| 135 |
|
| 136 |
|
| 137 |
def match_arms(body: str) -> list[tuple[str, list[str]]]: |
| 138 |
|
| 139 |
|
| 140 |
R7 expands `given` to a real `match`, where arms are exclusive and a moved |
| 141 |
value is fine, so these are the SAFE form. Counted to show the pair rule is |
| 142 |
the only thing that produces the defect. |
| 143 |
|
| 144 |
found = [] |
| 145 |
for m in re.finditer(r"\bmatch\b", body): |
| 146 |
b = body.find("{", m.end()) |
| 147 |
if b < 0: |
| 148 |
continue |
| 149 |
scrut = body[m.end() : b].strip() |
| 150 |
if not scrut or "{" in scrut: |
| 151 |
continue |
| 152 |
e = pop.match_brace(body, b) |
| 153 |
if e < 0: |
| 154 |
continue |
| 155 |
inner = body[b + 1 : e - 1] |
| 156 |
arms, depth, start = [], 0, 0 |
| 157 |
for i, c in enumerate(inner): |
| 158 |
if c in "{([": |
| 159 |
depth += 1 |
| 160 |
elif c in "})]": |
| 161 |
depth -= 1 |
| 162 |
elif c == "," and depth == 0: |
| 163 |
arms.append(inner[start:i]) |
| 164 |
start = i + 1 |
| 165 |
arms.append(inner[start:]) |
| 166 |
found.append((scrut, [a for a in arms if a.strip()])) |
| 167 |
return found |
| 168 |
|
| 169 |
|
| 170 |
def adjacent_ifs(body: str) -> list[tuple[str, str, str]]: |
| 171 |
|
| 172 |
|
| 173 |
This is what R4's pairing rule produces, written by hand. If any exist in |
| 174 |
the tree they are the defect's antecedent without any re-authoring at all. |
| 175 |
|
| 176 |
found = [] |
| 177 |
spans = [] |
| 178 |
for m in re.finditer(r"\bif\b", body): |
| 179 |
b = body.find("{", m.end()) |
| 180 |
if b < 0: |
| 181 |
continue |
| 182 |
cond = body[m.end() : b].strip() |
| 183 |
if not cond or "{" in cond: |
| 184 |
continue |
| 185 |
e = pop.match_brace(body, b) |
| 186 |
if e < 0: |
| 187 |
continue |
| 188 |
spans.append((cond, b, e)) |
| 189 |
for (c1, _b1, e1), (c2, b2, e2) in zip(spans, spans[1:]): |
| 190 |
gap = body[e1:b2] |
| 191 |
if re.fullmatch(r"\s*(?:if\s*)?", gap.replace(c2, "", 1)) is None: |
| 192 |
continue |
| 193 |
norm = lambda c: c.strip().lstrip("!").strip() |
| 194 |
if norm(c1) == norm(c2) and (c1.strip().startswith("!") != c2.strip().startswith("!")): |
| 195 |
found.append((norm(c1), body[_b1:e1], body[b2:e2])) |
| 196 |
return found |
| 197 |
|
| 198 |
|
| 199 |
IDENT = re.compile(r"\b([a-z_][a-z0-9_]*)\b") |
| 200 |
|
| 201 |
|
| 202 |
def main() -> int: |
| 203 |
shapes = [] |
| 204 |
for rel in pop.SHAPE_DIRS: |
| 205 |
d = ROOT / rel |
| 206 |
for f in sorted(d.rglob("*.rs")): |
| 207 |
parts = f.relative_to(ROOT).parts |
| 208 |
if f.name in ("tests.rs", "parity.rs") or "tests" in parts: |
| 209 |
continue |
| 210 |
raw = f.read_text(encoding="utf-8", errors="replace") |
| 211 |
blanked = pop.strip_cfg_test(pop.blank_noncode(raw)) |
| 212 |
for h in pop.shapes_in(f): |
| 213 |
h["file"] = str(Path(h["file"]).relative_to(ROOT)) |
| 214 |
h["_blanked"] = blanked |
| 215 |
shapes.append(h) |
| 216 |
|
| 217 |
total = len(shapes) |
| 218 |
positional = [s for s in shapes if head(s["returns"]) in POSITIONAL] |
| 219 |
|
| 220 |
hits = [] |
| 221 |
for s in positional: |
| 222 |
body = body_of(s["_blanked"], s["line"]) |
| 223 |
if not body: |
| 224 |
continue |
| 225 |
owned = owned_params(sig_at(s["_blanked"], s["line"])) |
| 226 |
|
| 227 |
for m in re.finditer(r"\blet\s+(?:mut\s+)?([a-z_][a-z0-9_]*)\s*=\s*([^;]*);", body): |
| 228 |
if not m.group(2).lstrip().startswith("&"): |
| 229 |
owned.add(m.group(1)) |
| 230 |
pairs = [("if/else", c, a, b) for c, a, b in conditionals(body)] |
| 231 |
pairs += [("adjacent-if", c, a, b) for c, a, b in adjacent_ifs(body)] |
| 232 |
for scrut, arms in match_arms(body): |
| 233 |
for i in range(len(arms)): |
| 234 |
for j in range(i + 1, len(arms)): |
| 235 |
pairs.append(("match", scrut, arms[i], arms[j])) |
| 236 |
for kind, cond, a, b in pairs: |
| 237 |
ia = set(IDENT.findall(a)) |
| 238 |
ib = set(IDENT.findall(b)) |
| 239 |
shared = (ia & ib) & owned |
| 240 |
if shared: |
| 241 |
hits.append({ |
| 242 |
"kind": kind, |
| 243 |
"file": s["file"], "line": s["line"], "fn": s["fn"], |
| 244 |
"returns": s["returns"], "cond": " ".join(cond.split())[:70], |
| 245 |
"shared": sorted(shared), |
| 246 |
}) |
| 247 |
|
| 248 |
print(f"population {total}") |
| 249 |
print(f"positional-container shapes {len(positional)} (Row/Cells/Cell/Column and plurals)") |
| 250 |
print(f" with a two-armed conditional sharing an owned binding: {len(hits)}") |
| 251 |
print() |
| 252 |
for h in hits: |
| 253 |
print(f" {h['file']}:{h['line']} {h['fn']} -> {h['returns']}") |
| 254 |
print(f" [{h['kind']}] {h['cond']}") |
| 255 |
print(f" shared owned: {', '.join(h['shared'])}") |
| 256 |
if not hits: |
| 257 |
print(" none") |
| 258 |
return 0 |
| 259 |
|
| 260 |
|
| 261 |
if __name__ == "__main__": |
| 262 |
raise SystemExit(main()) |
| 263 |
|