Skip to content

Commit 129f318

Browse files
authored
Merge pull request #56 from solidjs/claude/project-thread-s86axm
Site expansion for 0.2.0; W-1 rule printed once; GUESS_WHILE_LOADING in the ref
2 parents 69ec765 + d4ea7bb commit 129f318

6 files changed

Lines changed: 231 additions & 23 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default component Counter() {
5555
Write to the equation instead, `double := 4`, and the checker refuses it:
5656

5757
```
58-
counter.rx:5: error WRITE_TO_NON_SOURCE [W-1] double is an equation: only sources are written (W-1)
58+
counter.rx:5: error WRITE_TO_NON_SOURCE [W-1] double is an equation: only sources are written
5959
```
6060

6161
Async is an `await` in an equation. The compiler tracks that the value can be

‎compiler/src/check.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,7 +2350,7 @@ impl<'a> Checker<'a> {
23502350
let info = self.syms[c].ctx.clone().unwrap();
23512351
self.uses.push(UseRec { region, name: info.key.clone(), line: self.line, display: target.name.clone() });
23522352
if !info.source {
2353-
self.err("WRITE_TO_NON_SOURCE", "O-4", format!("context {} is read beneath its provision: call an action it provides that writes it (O-4)", target.name));
2353+
self.err("WRITE_TO_NON_SOURCE", "O-4", format!("context {} is read beneath its provision: call an action it provides that writes it", target.name));
23542354
}
23552355
}
23562356
} else {
@@ -2374,7 +2374,7 @@ impl<'a> Checker<'a> {
23742374
k => format!("a {}", k.as_str()),
23752375
};
23762376
let hint = if k == SymKind::Props || k == SymKind::Value || self.syms[t].param { " — a source is written only in the region that declares it: have its owner pass an action that writes it (O-4)" } else { "" };
2377-
self.err("WRITE_TO_NON_SOURCE", "W-1", format!("{} is {}: only sources are written (W-1){}", target.name, what, hint));
2377+
self.err("WRITE_TO_NON_SOURCE", "W-1", format!("{} is {}: only sources are written{}", target.name, what, hint));
23782378
} else if let Some(imp) = self.syms[t].imported.clone() {
23792379
self.err(
23802380
"WRITE_TO_NON_SOURCE",

‎ref/src/check.ts‎

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ class Checker {
228228
private effectIns: { region: Region; e: Expr; env: Env; line: number }[] = [];
229229
private apps: { comp: Region; site: Region; line: number; tag: string; args: { field: string; e: Expr; env: Env }[]; passed: { field: string; e: Expr; env: Env }[] }[] = []; // passed: every attribute, handlers too (O-3)
230230
private actions: { name: string; body: Stmt[]; env: Env; line: number }[] = [];
231+
private handlers: { attrs: { name: string; e: Expr }[]; kids: Markup[]; h: Expr; env: Env; region: Region; line: number; what: string }[] = []; // AC-3a: element handlers, by where they are
231232
private provisions: { key: string; e: Expr; env: Env }[] = [];
232233
private importSyms = new Set<Sym>(); // MO-1d: what imports bound; a nested block may not shadow it
233234
private later: (() => void)[] = [];
@@ -255,6 +256,112 @@ class Checker {
255256
this.retained();
256257
this.resolveTree();
257258
this.loadingOnOutside();
259+
this.guessWhileLoading();
260+
}
261+
262+
// ---- AC-3a: a guess a click can make while its value loads a new question ----
263+
/** A guess composes on the value as it stands. If a click can run `guess x := …` while `x` is loading a new question that
264+
* the click itself started (its question reads what the click reads), the guess sits on the previous answer and the new
265+
* answer replaces it, so the change disappears while it is out (D-34; Solid's OPTIMISTIC_WRITE_WHILE_LOADING, at run time).
266+
* Covered when the element is inside a `loading` region whose content reads `x`, or is disabled on `x` (a form: all its
267+
* submit buttons are). Mirrors compiler/src/check.rs `guess_while_loading`. */
268+
private guessWhileLoading() {
269+
type N = Expr | Stmt;
270+
const kidsOf = (n: N): N[] => {
271+
const out: N[] = [];
272+
for (const v of Object.values(n as object)) {
273+
if (Array.isArray(v)) { for (const x of v) if (x && typeof x === "object") { if ("k" in x) out.push(x as N); else if ("v" in x) out.push((x as { v: Expr }).v); else if ("spread" in x) out.push((x as { spread: Expr }).spread); } }
274+
else if (v && typeof v === "object" && "k" in (v as object)) out.push(v as N);
275+
}
276+
return out;
277+
};
278+
const depends = (e: Expr, env: Env, t: Sym, busy: Set<Sym>): boolean => {
279+
const go = (x: Expr): boolean => {
280+
if (x.k === "fn" || x.k === "serverfn") return false;
281+
if (x.k === "id") { const u = env.lookup(x.name); if (!u) return false; if (u === t) return true; return !!(u.expr && u.env && !busy.has(u) && (busy.add(u), depends(u.expr, u.env, t, busy))); }
282+
return subExprs(x).some(go);
283+
};
284+
return go(e);
285+
};
286+
const awaits = (t: Sym, busy: Set<Sym>): boolean => {
287+
if (!t.expr || !t.env || busy.has(t)) return false;
288+
busy.add(t);
289+
const env = t.env;
290+
const go = (x: Expr): boolean => {
291+
if (x.k === "fn" || x.k === "serverfn" || x.k === "peek") return false;
292+
if (x.k === "await") return true;
293+
if (x.k === "id") { const u = env.lookup(x.name); return !!u && u !== t && (u.kind === "eq" || u.kind === "source") && awaits(u, busy); }
294+
return subExprs(x).some(go);
295+
};
296+
return go(t.expr);
297+
};
298+
const asksAbout = (t: Sym, inputs: Sym[]) => !!t.expr && !!t.env && inputs.some(i => i !== t && !(i.expr && i.env && depends(i.expr, i.env, t, new Set())) && depends(t.expr!, t.env!, i, new Set()));
299+
const inside = (r: Region | null, anc: Region) => { for (let c = r; c; c = c.parent) if (c === anc) return true; return false; };
300+
const heldBy = (r: Region, t: Sym) => {
301+
for (let c: Region | null = r; c; c = c.parent) {
302+
if (c.provides.has(HOLD) && this.readers.some(rd => inside(rd.region, c!) && depends(rd.e, rd.env, t, new Set()))) return true;
303+
if (c.isComponentRoot) break;
304+
}
305+
return false;
306+
};
307+
const disabledOn = (attrs: { name: string; e: Expr }[], env: Env, t: Sym) => attrs.some(a => a.name === "disabled" && depends(a.e, env, t, new Set()));
308+
const submitButtons = (ms: Markup[], out: { name: string; e: Expr }[][]) => {
309+
for (const m of ms) {
310+
if (m.k === "el") { if (m.tag === "button" && !m.attrs.some(a => a.name === "type" && !(a.e.k === "lit" && a.e.v === "submit"))) out.push(m.attrs); submitButtons(m.children, out); }
311+
else if (m.k === "mif") { submitButtons(m.t, out); submitButtons(m.f, out); }
312+
else if (m.k === "mfor" || m.k === "loading" || m.k === "errored" || m.k === "portal") submitButtons(m.body, out);
313+
}
314+
};
315+
const done = new Set<Expr>();
316+
for (const hr of this.handlers) {
317+
if (done.has(hr.h)) continue;
318+
done.add(hr.h);
319+
const submits: { name: string; e: Expr }[][] = [];
320+
if (hr.what === "<form> onSubmit") submitButtons(hr.kids, submits);
321+
// the bodies the click runs: the handler, and the actions it calls by name
322+
const bodies: { root: N; env: Env; via?: string }[] = [{ root: hr.h, env: hr.env }];
323+
const stack: N[] = [hr.h];
324+
while (stack.length) {
325+
const n = stack.pop()!;
326+
if (n.k === "call" && (n as Extract<Expr, { k: "call" }>).f.k === "id") {
327+
const name = ((n as Extract<Expr, { k: "call" }>).f as Extract<Expr, { k: "id" }>).name;
328+
const a = hr.env.lookup(name);
329+
if (a?.kind === "action") for (const act of this.actions) if (act.name === name && act.line === a.line) bodies.push(...act.body.map(st => ({ root: st as N, env: act.env, via: name })));
330+
}
331+
stack.push(...kidsOf(n));
332+
}
333+
// what the click is about: the names those bodies read, outside their own writes and guesses
334+
const inputs: Sym[] = [];
335+
for (const b of bodies) {
336+
const st: N[] = [b.root];
337+
while (st.length) {
338+
const n = st.pop()!;
339+
if (n.k === "optimistic" || n.k === "write") continue;
340+
if (n.k === "id") { const v = b.env.lookup((n as Extract<Expr, { k: "id" }>).name); if (v && (v.kind === "source" || v.kind === "eq" || v.kind === "value" || v.kind === "props") && !inputs.includes(v)) inputs.push(v); }
341+
st.push(...kidsOf(n));
342+
}
343+
}
344+
const seen = new Set<Sym>();
345+
for (const b of bodies) {
346+
const st: N[] = [b.root];
347+
while (st.length) {
348+
const n = st.pop()!;
349+
if (n.k === "optimistic") {
350+
const tn = (n as Extract<Stmt, { k: "optimistic" }>).target.name;
351+
const t = b.env.lookup(tn);
352+
if (t && !seen.has(t) && (seen.add(t), true)
353+
&& awaits(t, new Set()) && asksAbout(t, inputs)
354+
&& !disabledOn(hr.attrs, hr.env, t)
355+
&& !(submits.length > 0 && submits.every(at => disabledOn(at, hr.env, t)))
356+
&& !heldBy(hr.region, t)) {
357+
this.line = hr.line;
358+
this.err("GUESS_WHILE_LOADING", "AC-3a", `${hr.what} ${b.via ? `starts \`${b.via}\`, which can run` : "can run"} \`guess ${tn}\` while ${tn} is loading a new question (its question reads what the click reads): the guess composes on the previous answer, and the new answer replaces it, so the change disappears while it is out (D-34) — put the element inside the \`loading\` region whose content reads ${tn}, or give it \`disabled={pending? ${tn}}\`, or keep the pending change in a source of its own until it lands`);
359+
}
360+
}
361+
st.push(...kidsOf(n));
362+
}
363+
}
364+
}
258365
}
259366

260367
// ---- CN-3e: an outside read holds a `loading on(…)` region's change ----
@@ -835,14 +942,14 @@ class Checker {
835942
let target: Sym | undefined;
836943
if (s.target.ctx) {
837944
const c = this.ctxOf(s.target.name, env);
838-
if (c) { this.model.write(c, s.k, s.target.path.length > 0); this.uses.push({ region: env.region, name: c.ctx!.key, line: this.line, display: s.target.name }); if (!c.ctx!.source) this.err("WRITE_TO_NON_SOURCE", "O-4", `context ${s.target.name} is read beneath its provision: call an action it provides that writes it (O-4)`); }
945+
if (c) { this.model.write(c, s.k, s.target.path.length > 0); this.uses.push({ region: env.region, name: c.ctx!.key, line: this.line, display: s.target.name }); if (!c.ctx!.source) this.err("WRITE_TO_NON_SOURCE", "O-4", `context ${s.target.name} is read beneath its provision: call an action it provides that writes it`); }
839946
}
840947
else {
841948
target = this.resolve(s.target.name, env, ctx); this.model.write(target, s.k, s.target.path.length > 0);
842949
const guessable = s.k === "optimistic" && target?.kind === "eq" && !!target.eqDecl && !target.param && !target.imported; // AC-3: a guess may sit over an equation (Solid's createOptimistic(fn)); it is not a write of the truth
843950
if (guessable && target!.eqDecl!.deferred) this.err("GUESS_ON_DEFERRED", "AC-3", `${s.target.name} is deferred: its value is a lagging copy (V-16), so nothing would confirm a guess on it — guess the equation it lags`);
844951
else if (guessable) {}
845-
else if (target && target.kind !== "source") this.err("WRITE_TO_NON_SOURCE", "W-1", `${s.target.name} is ${target.kind === "eq" ? "an equation" : target.kind === "props" ? "props" : `a ${target.kind}`}: only sources are written (W-1)${target.kind === "props" || target.kind === "value" || target.param ? " — a source is written only in the region that declares it: have its owner pass an action that writes it (O-4)" : ""}`);
952+
else if (target && target.kind !== "source") this.err("WRITE_TO_NON_SOURCE", "W-1", `${s.target.name} is ${target.kind === "eq" ? "an equation" : target.kind === "props" ? "props" : `a ${target.kind}`}: only sources are written${target.kind === "props" || target.kind === "value" || target.param ? " — a source is written only in the region that declares it: have its owner pass an action that writes it (O-4)" : ""}`);
846953
else if (target?.imported) this.err("WRITE_TO_NON_SOURCE", "MO-2", `${s.target.name} is a source of ${target.imported}: a module boundary is a value boundary — write it through an action ${target.imported} exports`);
847954
}
848955
if (s.k === "optimistic") {
@@ -994,7 +1101,7 @@ class Checker {
9941101
} else this.markup(m.children, env);
9951102
break;
9961103
}
997-
for (const a of m.attrs) { this.comp(isHandler(a.name) ? "handler" : "reader", env, { at: `${m.tag} ${a.name}` }, () => this.expr(a.e, env, { scope: isHandler(a.name) ? "imperative" : "reactive" })); if (!isHandler(a.name)) this.readers.push({ region: env.region, e: a.e, env, line: this.line }); }
1104+
for (const a of m.attrs) { this.comp(isHandler(a.name) ? "handler" : "reader", env, { at: `${m.tag} ${a.name}` }, () => this.expr(a.e, env, { scope: isHandler(a.name) ? "imperative" : "reactive" })); if (!isHandler(a.name)) this.readers.push({ region: env.region, e: a.e, env, line: this.line }); else this.handlers.push({ attrs: m.attrs, kids: m.children, h: a.e, env, region: env.region, line: this.line, what: `<${m.tag}> ${a.name}` }); }
9981105
this.markup(m.children, env);
9991106
break;
10001107
}

‎scenarios/S-guess-equation.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,25 @@ export default component App() {
9999

100100
A deferred equation's value is a lagging copy (V-16); a guess has nothing
101101
to confirm it there.
102+
103+
```rx
104+
-- reject: GUESS_WHILE_LOADING
105+
export default component App() {
106+
source id := 1
107+
likes = await fetch("likes:" + id) catch (e) => 0
108+
action send() {
109+
try {
110+
guess likes := likes + 1
111+
await fetch("like:" + id)
112+
refresh likes
113+
} catch (e) {}
114+
}
115+
<button onClick={() => { id := id + 1 }}>next</button>
116+
<button onClick={() => { send() }}>{likes}</button>
117+
}
118+
```
119+
120+
A click can guess `likes` while it loads a new question about `id`, which
121+
the click reads too (AC-3a): the guess sits on the old answer and the new
122+
one replaces it. Put the button inside the `loading` region that shows
123+
`likes`, or disable it on `pending? likes`.

0 commit comments

Comments
 (0)