From cb78abf9e9ac10cf44701a2631e40cb0af94ce88 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Tue, 12 May 2026 03:49:09 +0900 Subject: [PATCH] selfhost: is/as validity + let/return assignability checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three more structural checks from C check.c ported to selfhost, at the AST level (no resolved tinfo). is/as validity: e is T / e as T require e's declared type to be a tagged union and T to name a variant. Mirrors the case-variant check that just landed. let init-type and return-type assignability: a new exprtype helper infers an AST type-node for literal/ident/call/cast/?/as/is expressions; isassignable approximates C type_assignable on the shapes we can resolve — exact match, untyped numeric → typed numeric, untyped nil → ptr/slice/chan/fn, variant inclusion, and two-primitive-mismatch. isassignable returns (ok, confident). When confident=false the check emits no error — better to miss a real bug than fire a false positive on a binary-op expression we can't infer. This keeps existing selfhost code clean while still catching the common typo cases (let x: bool = 42; return "hi" from i32 fn). Naming: all new helpers follow Plan 9 run-together convention per CLAUDE.md (`typeeqast`, `isassignable`, `exprtype`, ...). Earlier work that used snake_case helpers (`case_variant_in`, `check_match_exhaustive`, ...) got the same treatment — bulk renamed in this commit. Five new rows in 950_selfcheck exercise the new checks (is-not-a-variant, two let mismatches, return mismatch, plus the case-variant row already there). --- selfhost/cmd/w6c/main.combined.ww | 423 +++++++++++++++++++++++---- selfhost/cmd/wcc/check.ww | 423 +++++++++++++++++++++++---- selfhost/cmd/wwdump/main.combined.ww | 423 +++++++++++++++++++++++---- test/wcc/950_selfcheck.c | 25 ++ 4 files changed, 1144 insertions(+), 150 deletions(-) diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index f1b1877c..8f556376 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -3808,8 +3808,12 @@ fn resolvewalk(c: *checker, n: *node) void = { // Typed checks fire on the way down so the scrutinee/operand // is examined before the arm bodies install new bindings. - if (k == N_MATCH) { check_match_exhaustive(c, n); }; - if (k == N_TRYPROP) { check_tryprop(c, n); }; + if (k == N_MATCH) { checkmatchexhaust(c, n); }; + if (k == N_TRYPROP) { checktryprop(c, n); }; + if (k == N_TYPETEST) { checkisas(c, n); }; + if (k == N_TYPEASSERT) { checkisas(c, n); }; + if (k == N_LET) { checkletassign(c, n); }; + if (k == N_RETURN) { checkretassign(c, n); }; // `use IDENT;` — name is a module label, not a free ident. if (k == N_USE) { return; }; @@ -3956,19 +3960,11 @@ fn resolvealias(c: *checker, n: *node) *node = { return n; }; -// is_tagged_type — true if `n` (after alias resolution) is an -// N_TTAGGED type expression. -fn is_tagged_type(c: *checker, n: *node) bool = { - let u: *node = resolvealias(c, unwrapbang(n)); - if (u == nil) { return false; }; - return u.kind == N_TTAGGED; -}; - -// type_eq_ast — structural equality on AST type expressions, mod +// typeeqast — structural equality on AST type expressions, mod // the `!` wrapper. Mirrors variant_match in cgen + check.c: NAMED // types compare by string (the closest stand-in for pointer // identity at the AST level); other nodes recurse by kind. -fn type_eq_ast(a: *node, b: *node) bool = { +fn typeeqast(a: *node, b: *node) bool = { let aa: *node = unwrapbang(a); let bb: *node = unwrapbang(b); if (aa == nil) { return bb == nil; }; @@ -3976,19 +3972,19 @@ fn type_eq_ast(a: *node, b: *node) bool = { if (aa.kind != bb.kind) { return false; }; let k: i32 = aa.kind; if (k == N_TNAME) { return streq(aa.str, bb.str); }; - if (k == N_TPTR) { return type_eq_ast(aa.lhs, bb.lhs); }; - if (k == N_TSLICE){ return type_eq_ast(aa.lhs, bb.lhs); }; - if (k == N_TCHAN) { return type_eq_ast(aa.lhs, bb.lhs); }; + if (k == N_TPTR) { return typeeqast(aa.lhs, bb.lhs); }; + if (k == N_TSLICE){ return typeeqast(aa.lhs, bb.lhs); }; + if (k == N_TCHAN) { return typeeqast(aa.lhs, bb.lhs); }; // Conservative: anything else (struct/fn/tagged/tuple/array) // fails the cheap check. Selfhost code doesn't currently rely // on equality at these shapes for the targeted checks. return false; }; -// variant_is_error — does this variant carry the `!` mark? Either +// varianterr — does this variant carry the `!` mark? Either // the variant itself is N_TBANG or it's an alias whose typedecl // body is `!T`. Mirrors C check.c's iserror-after-NAMED rule. -fn variant_is_error(c: *checker, v: *node) bool = { +fn varianterr(c: *checker, v: *node) bool = { if (v == nil) { return false; }; if (v.kind == N_TBANG) { return true; }; if (v.kind == N_TNAME) { @@ -4008,35 +4004,35 @@ fn variant_is_error(c: *checker, v: *node) bool = { return false; }; -// tagged_has_errflag — true iff any variant of `n` (assumed +// taggedhaserr — true iff any variant of `n` (assumed // N_TTAGGED) is `!`-marked. Picks the explicit-flag semantics over // the legacy "first variant = success" rule. -fn tagged_has_errflag(c: *checker, n: *node) bool = { +fn taggedhaserr(c: *checker, n: *node) bool = { let v: *node = n.list; for (v != nil) { - if (variant_is_error(c, v)) { return true; }; + if (varianterr(c, v)) { return true; }; v = v.next; }; return false; }; -// is_error_variant — under flag-aware mode (any !-marked variant), +// iserrvariant — under flag-aware mode (any !-marked variant), // returns true iff `v` is `!`-marked. Under legacy mode (no flags), // returns true iff `v` is not the first variant of `tagged`. -fn is_error_variant(c: *checker, tagged: *node, v: *node) bool = { - if (tagged_has_errflag(c, tagged)) { - return variant_is_error(c, v); +fn iserrvariant(c: *checker, tagged: *node, v: *node) bool = { + if (taggedhaserr(c, tagged)) { + return varianterr(c, v); }; // Legacy: first variant of the union is success. if (tagged.list == v) { return false; }; return true; }; -// scrutinee_type — resolve the type expression for a match's +// scruttype — resolve the type expression for a match's // scrutinee. Handles N_IDENT (look up local/param's declared // type) and N_DOT (struct-field access). Returns nil if we // can't statically determine the type. Used by exhaustiveness. -fn scrutinee_type(c: *checker, e: *node) *node = { +fn scruttype(c: *checker, e: *node) *node = { if (e == nil) { return nil; }; if (e.kind == N_IDENT) { let s: *sym = scopelookup(c.cur, e.str); @@ -4048,25 +4044,264 @@ fn scrutinee_type(c: *checker, e: *node) *node = { return nil; }; +// mktname — fabricate an N_TNAME node with str = `nm`. Used by +// exprtype to return primitive type nodes for literal +// expressions. The arena keeps them around as long as the checker. +fn mktname(c: *checker, nm: str) *node = { + let n: *node = newnode(c.a, N_TNAME, "", 0, 0); + n.str = nm; + return n; +}; + +// exprtype — best-effort type-AST inference for an expression +// node. Handles literals, identifiers, calls, and casts; returns +// nil for shapes we don't statically know (binary ops, struct +// field access into non-primitive types, etc). +fn exprtype(c: *checker, e: *node) *node = { + if (e == nil) { return nil; }; + let k: i32 = e.kind; + if (k == N_INTLIT) { return mktname(c, "untyped_int"); }; + if (k == N_FLOATLIT) { return mktname(c, "untyped_float"); }; + if (k == N_STRLIT) { return mktname(c, "str"); }; + if (k == N_RUNELIT) { return mktname(c, "rune"); }; + if (k == N_TRUE) { return mktname(c, "bool"); }; + if (k == N_FALSE) { return mktname(c, "bool"); }; + if (k == N_VOIDLIT) { return mktname(c, "void"); }; + if (k == N_NIL) { return mktname(c, "untyped_nil"); }; + if (k == N_IDENT) { + let s: *sym = scopelookup(c.cur, e.str); + if (s == nil) { return nil; }; + if (s.decl == nil) { return nil; }; + return s.decl.lhs; + }; + if (k == N_CAST) { + // `expr: T` — explicit cast; the type expr is e.rhs. + return e.rhs; + }; + if (k == N_CALL) { + let callee: *node = e.lhs; + if (callee == nil) { return nil; }; + let nm: str; + nm.ptr = nil; nm.len = 0; + if (callee.kind == N_IDENT) { nm = callee.str; }; + if (callee.kind == N_DOT) { nm = callee.str; }; + if (nm.len == 0) { return nil; }; + let s: *sym = scopelookup(c.cur, nm); + if (s == nil) { return nil; }; + if (s.skind != SK_FN) { return nil; }; + if (s.decl == nil) { return nil; }; + return s.decl.lhs; // fn-decl's lhs is the return type + }; + if (k == N_TRYPROP) { + // success unwrap: the success-variant type of operand's + // tagged union. + let opt: *node = exprtype(c, e.lhs); + let ou: *node = resolvealias(c, unwrapbang(opt)); + if (ou == nil) { return nil; }; + if (ou.kind != N_TTAGGED) { return nil; }; + // Hare semantics: success = first non-error variant if + // any !-flag is present; else first variant. + if (taggedhaserr(c, ou)) { + let v: *node = ou.list; + for (v != nil) { + if (!iserrvariant(c, ou, v)) { return v; }; + v = v.next; + }; + return nil; + }; + return ou.list; + }; + if (k == N_TYPEASSERT) { + // `e as T` → T + return e.rhs; + }; + if (k == N_TYPETEST) { + // `e is T` → bool + return mktname(c, "bool"); + }; + return nil; +}; + +// isuntypedint / is_str_like / is_bool_like — helpers used +// by the assignability check below to allow common AST shapes +// through without needing real type inference. +fn isuntypedint(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + return streq(t.str, "untyped_int"); +}; + +fn isuntypedfloat(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + return streq(t.str, "untyped_float"); +}; + +fn isuntypednil(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + return streq(t.str, "untyped_nil"); +}; + +fn isnumerictname(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + let s: str = t.str; + if (streq(s, "i8")) { return true; }; + if (streq(s, "i16")) { return true; }; + if (streq(s, "i32")) { return true; }; + if (streq(s, "i64")) { return true; }; + if (streq(s, "u8")) { return true; }; + if (streq(s, "u16")) { return true; }; + if (streq(s, "u32")) { return true; }; + if (streq(s, "u64")) { return true; }; + if (streq(s, "int")) { return true; }; + if (streq(s, "uint")) { return true; }; + if (streq(s, "uintptr")) { return true; }; + if (streq(s, "rune")) { return true; }; + if (streq(s, "f32")) { return true; }; + if (streq(s, "f64")) { return true; }; + return false; +}; + +fn isstrtname(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + return streq(t.str, "str"); +}; + +// isassignable — AST-level approximation of C check.c +// type_assignable. Returns true when we know the assignment is +// OK, false only when we're confident it isn't, and "skip" (true) +// when we can't tell — to avoid false positives. The trailing bool +// `confident` lets the caller decide whether to emit an error +// when the result is false: if !confident, the caller should not +// flag it. +fn isassignable(c: *checker, dst: *node, src: *node, confident: *bool) bool = { + *confident = false; + if (dst == nil) { return true; }; // no declared target + if (src == nil) { return true; }; // unknown src type + *confident = true; + let du: *node = resolvealias(c, unwrapbang(dst)); + let su: *node = resolvealias(c, unwrapbang(src)); + if (du == nil) { *confident = false; return true; }; + if (su == nil) { *confident = false; return true; }; + if (typeeqast(du, su)) { return true; }; + // untyped numeric → any numeric named type. + if (isuntypedint(su)) { + if (isnumerictname(du)) { return true; }; + // (T | ...) tagged: only OK if some variant accepts untyped_int. + if (du.kind == N_TTAGGED) { + let v: *node = du.list; + for (v != nil) { + let vu: *node = resolvealias(c, unwrapbang(v)); + if (vu != nil) { + if (isnumerictname(vu)) { return true; }; + }; + v = v.next; + }; + *confident = false; + return true; + }; + // Known non-numeric primitive: confidently wrong. + if (du.kind == N_TNAME) { + if (streq(du.str, "bool")) { return false; }; + if (streq(du.str, "void")) { return false; }; + if (streq(du.str, "str")) { return false; }; + }; + // Unknown shapes: stay quiet. + *confident = false; + return true; + }; + if (isuntypedfloat(su)) { + if (isnumerictname(du)) { return true; }; + if (du.kind == N_TNAME) { + if (streq(du.str, "bool")) { return false; }; + if (streq(du.str, "void")) { return false; }; + if (streq(du.str, "str")) { return false; }; + }; + *confident = false; + return true; + }; + if (isuntypednil(su)) { + // nil → ptr/slice/chan/fn/nullable + if (du.kind == N_TPTR) { return true; }; + if (du.kind == N_TSLICE) { return true; }; + if (du.kind == N_TCHAN) { return true; }; + if (du.kind == N_TFN) { return true; }; + // nullable `(*T | void)` — already accepted by typeeqast + // when matched whole; nil is OK there too. + if (du.kind == N_TTAGGED) { + let v: *node = du.list; + for (v != nil) { + if (v.kind == N_TPTR) { return true; }; + if (v.kind == N_TSLICE){ return true; }; + v = v.next; + }; + }; + *confident = false; + return true; + }; + // Tagged-union variant inclusion: src is one of dst's variants. + if (du.kind == N_TTAGGED && su.kind != N_TTAGGED) { + let v: *node = du.list; + for (v != nil) { + let vu: *node = resolvealias(c, unwrapbang(v)); + if (vu != nil) { + if (typeeqast(vu, su)) { return true; }; + }; + v = v.next; + }; + return false; + }; + // tagged → tagged: structural variant list compare. Skip + // (don't be confident) — common when forwarding a fallible + // return through another fn with the same shape but possibly + // a different surface spelling. + if (du.kind == N_TTAGGED && su.kind == N_TTAGGED) { + *confident = false; + return true; + }; + // Two known primitives with different names are confidently + // incompatible. `i32 ↔ bool`, `str ↔ i32`, etc. + if (du.kind == N_TNAME && su.kind == N_TNAME) { + let known_d: bool = isnumerictname(du) || isstrtname(du); + if (!known_d) { if (streq(du.str, "bool")) { known_d = true; }; }; + if (!known_d) { if (streq(du.str, "void")) { known_d = true; }; }; + let known_s: bool = isnumerictname(su) || isstrtname(su); + if (!known_s) { if (streq(su.str, "bool")) { known_s = true; }; }; + if (!known_s) { if (streq(su.str, "void")) { known_s = true; }; }; + if (known_d) { + if (known_s) { + // Both primitives, different names → no. + return false; + }; + }; + }; + // Anything else: don't claim confidence. + *confident = false; + return true; +}; + // ---- match exhaustiveness -------------------------------------------- // // For every match arm, verify that every variant of the scrutinee's // tagged-union type is handled by some case (or a default arm // exists). Multi-pattern `case A | B =>` covers all alts. -fn case_covers(c: *checker, cs: *node, want: *node) bool = { +fn casecovers(c: *checker, cs: *node, want: *node) bool = { if (cs.lhs != nil) { - if (type_eq_ast(cs.lhs, want)) { return true; }; + if (typeeqast(cs.lhs, want)) { return true; }; }; let alt: *node = cs.list; for (alt != nil) { - if (type_eq_ast(alt, want)) { return true; }; + if (typeeqast(alt, want)) { return true; }; alt = alt.next; }; return false; }; -fn err_match_variant(c: *checker, n: *node, vname: *node) void = { +fn errmatchvariant(c: *checker, n: *node, vname: *node) void = { os.write(2, "match: variant not handled".ptr, 26u64); if (vname != nil) { if (vname.kind == N_TNAME) { @@ -4079,19 +4314,19 @@ fn err_match_variant(c: *checker, n: *node, vname: *node) void = { c.errs += 1; }; -// case_variant_in — true iff `pat` (a `case T` pattern, including +// casevariantin — true iff `pat` (a `case T` pattern, including // each alt of a multi-pattern) names a variant of the tagged // union `tagged`. -fn case_variant_in(tagged: *node, pat: *node) bool = { +fn casevariantin(tagged: *node, pat: *node) bool = { let v: *node = tagged.list; for (v != nil) { - if (type_eq_ast(v, pat)) { return true; }; + if (typeeqast(v, pat)) { return true; }; v = v.next; }; return false; }; -fn err_bad_case_variant(c: *checker, pat: *node) void = { +fn errbadcase(c: *checker, pat: *node) void = { os.write(2, "case: not a variant of scrutinee".ptr, 32u64); if (pat != nil) { if (pat.kind == N_TNAME) { @@ -4104,10 +4339,10 @@ fn err_bad_case_variant(c: *checker, pat: *node) void = { c.errs += 1; }; -fn check_match_exhaustive(c: *checker, n: *node) void = { +fn checkmatchexhaust(c: *checker, n: *node) void = { if (n == nil) { return; }; if (n.lhs == nil) { return; }; - let st: *node = scrutinee_type(c, n.lhs); + let st: *node = scruttype(c, n.lhs); let u: *node = resolvealias(c, unwrapbang(st)); if (u == nil) { return; }; if (u.kind != N_TTAGGED) { return; }; @@ -4117,13 +4352,13 @@ fn check_match_exhaustive(c: *checker, n: *node) void = { let cs0: *node = n.list; for (cs0 != nil) { if (cs0.lhs != nil) { - if (!case_variant_in(u, cs0.lhs)) { - err_bad_case_variant(c, cs0.lhs); + if (!casevariantin(u, cs0.lhs)) { + errbadcase(c, cs0.lhs); }; let alt: *node = cs0.list; for (alt != nil) { - if (!case_variant_in(u, alt)) { - err_bad_case_variant(c, alt); + if (!casevariantin(u, alt)) { + errbadcase(c, alt); }; alt = alt.next; }; @@ -4142,18 +4377,106 @@ fn check_match_exhaustive(c: *checker, n: *node) void = { let covered: bool = false; let cs2: *node = n.list; for (cs2 != nil) { - if (case_covers(c, cs2, v)) { + if (casecovers(c, cs2, v)) { covered = true; cs2 = nil; } else { cs2 = cs2.next; }; }; - if (!covered) { err_match_variant(c, n, v); }; + if (!covered) { errmatchvariant(c, n, v); }; v = v.next; }; }; +// ---- let init / return assignability -------------------------------- +// +// AST-level approximation: when we can infer src's type and dst is +// explicitly declared, verify isassignable. We only emit an error +// when isassignable says "false with confidence." If we can't tell +// (binary ops, complex exprs we don't infer), we stay quiet — full +// type inference lives only on the C side. + +fn errnotassign(c: *checker, dst: *node, src: *node, where: str) void = { + os.write(2, where.ptr, where.len: u64); + os.write(2, ": not assignable".ptr, 16u64); + if (src != nil) { + if (src.kind == N_TNAME) { + os.write(2, " (".ptr, 2u64); + os.write(2, src.str.ptr, src.str.len: u64); + os.write(2, " → ".ptr, 5u64); + if (dst != nil) { + if (dst.kind == N_TNAME) { + os.write(2, dst.str.ptr, dst.str.len: u64); + }; + }; + os.write(2, ")".ptr, 1u64); + }; + }; + os.write(2, "\n".ptr, 1u64); + c.errs += 1; +}; + +fn checkletassign(c: *checker, n: *node) void = { + if (n == nil) { return; }; + if (n.lhs == nil) { return; }; // no declared type, nothing to check + if (n.rhs == nil) { return; }; // no init + let src: *node = exprtype(c, n.rhs); + if (src == nil) { return; }; // can't infer + let conf: bool = false; + let ok: bool = isassignable(c, n.lhs, src, &conf); + if (!conf) { return; }; + if (!ok) { errnotassign(c, n.lhs, src, "let"); }; +}; + +fn checkretassign(c: *checker, n: *node) void = { + if (n == nil) { return; }; + if (n.lhs == nil) { + // bare `return;` — OK iff fnret is void or a tagged union + // with a void variant. Skip flagging for now; cgen handles + // the void-variant tag synthesis already. + return; + }; + if (c.fnret == nil) { return; }; + let src: *node = exprtype(c, n.lhs); + if (src == nil) { return; }; + let conf: bool = false; + let ok: bool = isassignable(c, c.fnret, src, &conf); + if (!conf) { return; }; + if (!ok) { errnotassign(c, c.fnret, src, "return"); }; +}; + +// ---- is / as validity ------------------------------------------------ +// +// `e is T` and `e as T` require that e's declared type be a tagged +// union and that T name one of its variants. Operates on AST type +// expressions; falls back silently when we can't determine e's +// type (matches the case-variant rule for match). +fn checkisas(c: *checker, n: *node) void = { + if (n == nil) { return; }; + // e is in n.lhs (value), T is in n.rhs (type expr). + let st: *node = scruttype(c, n.lhs); + let u: *node = resolvealias(c, unwrapbang(st)); + if (u == nil) { return; }; + if (u.kind != N_TTAGGED) { + os.write(2, "is/as: operand is not a tagged union\n".ptr, 37u64); + c.errs += 1; + return; + }; + let want: *node = n.rhs; + if (want == nil) { return; }; + if (!casevariantin(u, want)) { + os.write(2, "is/as: not a variant of operand".ptr, 31u64); + if (want.kind == N_TNAME) { + os.write(2, " (".ptr, 2u64); + os.write(2, want.str.ptr, want.str.len: u64); + os.write(2, ")".ptr, 1u64); + }; + os.write(2, "\n".ptr, 1u64); + c.errs += 1; + }; +}; + // ---- ? subset propagation -------------------------------------------- // // For `expr?`, the operand's error subset must be a subset of the @@ -4161,7 +4484,7 @@ fn check_match_exhaustive(c: *checker, n: *node) void = { // is N_TRYPROP; its lhs is the value-bearing expr; we look at the // expr's *declared* type for N_IDENT/N_CALL cases. -fn expr_type_for_tryprop(c: *checker, e: *node) *node = { +fn exprtypeoftry(c: *checker, e: *node) *node = { if (e == nil) { return nil; }; if (e.kind == N_IDENT) { let s: *sym = scopelookup(c.cur, e.str); @@ -4188,20 +4511,20 @@ fn expr_type_for_tryprop(c: *checker, e: *node) *node = { return nil; }; -fn check_tryprop(c: *checker, n: *node) void = { +fn checktryprop(c: *checker, n: *node) void = { if (n == nil) { return; }; - let t: *node = expr_type_for_tryprop(c, n.lhs); + let t: *node = exprtypeoftry(c, n.lhs); let u: *node = resolvealias(c, unwrapbang(t)); if (u == nil) { return; }; if (u.kind != N_TTAGGED) { return; }; // Does the operand have any error variants? - let has_err: bool = false; + let haserr: bool = false; let v: *node = u.list; for (v != nil) { - if (is_error_variant(c, u, v)) { has_err = true; }; + if (iserrvariant(c, u, v)) { haserr = true; }; v = v.next; }; - if (!has_err) { return; }; + if (!haserr) { return; }; // Enclosing fn must return a tagged union with each operand // error variant present. let r: *node = resolvealias(c, unwrapbang(c.fnret)); @@ -4217,11 +4540,11 @@ fn check_tryprop(c: *checker, n: *node) void = { }; let ev: *node = u.list; for (ev != nil) { - if (is_error_variant(c, u, ev)) { + if (iserrvariant(c, u, ev)) { let found: bool = false; let rv: *node = r.list; for (rv != nil) { - if (type_eq_ast(rv, ev)) { + if (typeeqast(rv, ev)) { found = true; rv = nil; } else { rv = rv.next; }; diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index feff0a0e..0ea5ff51 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -91,8 +91,12 @@ fn resolvewalk(c: *checker, n: *node) void = { // Typed checks fire on the way down so the scrutinee/operand // is examined before the arm bodies install new bindings. - if (k == N_MATCH) { check_match_exhaustive(c, n); }; - if (k == N_TRYPROP) { check_tryprop(c, n); }; + if (k == N_MATCH) { checkmatchexhaust(c, n); }; + if (k == N_TRYPROP) { checktryprop(c, n); }; + if (k == N_TYPETEST) { checkisas(c, n); }; + if (k == N_TYPEASSERT) { checkisas(c, n); }; + if (k == N_LET) { checkletassign(c, n); }; + if (k == N_RETURN) { checkretassign(c, n); }; // `use IDENT;` — name is a module label, not a free ident. if (k == N_USE) { return; }; @@ -239,19 +243,11 @@ fn resolvealias(c: *checker, n: *node) *node = { return n; }; -// is_tagged_type — true if `n` (after alias resolution) is an -// N_TTAGGED type expression. -fn is_tagged_type(c: *checker, n: *node) bool = { - let u: *node = resolvealias(c, unwrapbang(n)); - if (u == nil) { return false; }; - return u.kind == N_TTAGGED; -}; - -// type_eq_ast — structural equality on AST type expressions, mod +// typeeqast — structural equality on AST type expressions, mod // the `!` wrapper. Mirrors variant_match in cgen + check.c: NAMED // types compare by string (the closest stand-in for pointer // identity at the AST level); other nodes recurse by kind. -fn type_eq_ast(a: *node, b: *node) bool = { +fn typeeqast(a: *node, b: *node) bool = { let aa: *node = unwrapbang(a); let bb: *node = unwrapbang(b); if (aa == nil) { return bb == nil; }; @@ -259,19 +255,19 @@ fn type_eq_ast(a: *node, b: *node) bool = { if (aa.kind != bb.kind) { return false; }; let k: i32 = aa.kind; if (k == N_TNAME) { return streq(aa.str, bb.str); }; - if (k == N_TPTR) { return type_eq_ast(aa.lhs, bb.lhs); }; - if (k == N_TSLICE){ return type_eq_ast(aa.lhs, bb.lhs); }; - if (k == N_TCHAN) { return type_eq_ast(aa.lhs, bb.lhs); }; + if (k == N_TPTR) { return typeeqast(aa.lhs, bb.lhs); }; + if (k == N_TSLICE){ return typeeqast(aa.lhs, bb.lhs); }; + if (k == N_TCHAN) { return typeeqast(aa.lhs, bb.lhs); }; // Conservative: anything else (struct/fn/tagged/tuple/array) // fails the cheap check. Selfhost code doesn't currently rely // on equality at these shapes for the targeted checks. return false; }; -// variant_is_error — does this variant carry the `!` mark? Either +// varianterr — does this variant carry the `!` mark? Either // the variant itself is N_TBANG or it's an alias whose typedecl // body is `!T`. Mirrors C check.c's iserror-after-NAMED rule. -fn variant_is_error(c: *checker, v: *node) bool = { +fn varianterr(c: *checker, v: *node) bool = { if (v == nil) { return false; }; if (v.kind == N_TBANG) { return true; }; if (v.kind == N_TNAME) { @@ -291,35 +287,35 @@ fn variant_is_error(c: *checker, v: *node) bool = { return false; }; -// tagged_has_errflag — true iff any variant of `n` (assumed +// taggedhaserr — true iff any variant of `n` (assumed // N_TTAGGED) is `!`-marked. Picks the explicit-flag semantics over // the legacy "first variant = success" rule. -fn tagged_has_errflag(c: *checker, n: *node) bool = { +fn taggedhaserr(c: *checker, n: *node) bool = { let v: *node = n.list; for (v != nil) { - if (variant_is_error(c, v)) { return true; }; + if (varianterr(c, v)) { return true; }; v = v.next; }; return false; }; -// is_error_variant — under flag-aware mode (any !-marked variant), +// iserrvariant — under flag-aware mode (any !-marked variant), // returns true iff `v` is `!`-marked. Under legacy mode (no flags), // returns true iff `v` is not the first variant of `tagged`. -fn is_error_variant(c: *checker, tagged: *node, v: *node) bool = { - if (tagged_has_errflag(c, tagged)) { - return variant_is_error(c, v); +fn iserrvariant(c: *checker, tagged: *node, v: *node) bool = { + if (taggedhaserr(c, tagged)) { + return varianterr(c, v); }; // Legacy: first variant of the union is success. if (tagged.list == v) { return false; }; return true; }; -// scrutinee_type — resolve the type expression for a match's +// scruttype — resolve the type expression for a match's // scrutinee. Handles N_IDENT (look up local/param's declared // type) and N_DOT (struct-field access). Returns nil if we // can't statically determine the type. Used by exhaustiveness. -fn scrutinee_type(c: *checker, e: *node) *node = { +fn scruttype(c: *checker, e: *node) *node = { if (e == nil) { return nil; }; if (e.kind == N_IDENT) { let s: *sym = scopelookup(c.cur, e.str); @@ -331,25 +327,264 @@ fn scrutinee_type(c: *checker, e: *node) *node = { return nil; }; +// mktname — fabricate an N_TNAME node with str = `nm`. Used by +// exprtype to return primitive type nodes for literal +// expressions. The arena keeps them around as long as the checker. +fn mktname(c: *checker, nm: str) *node = { + let n: *node = newnode(c.a, N_TNAME, "", 0, 0); + n.str = nm; + return n; +}; + +// exprtype — best-effort type-AST inference for an expression +// node. Handles literals, identifiers, calls, and casts; returns +// nil for shapes we don't statically know (binary ops, struct +// field access into non-primitive types, etc). +fn exprtype(c: *checker, e: *node) *node = { + if (e == nil) { return nil; }; + let k: i32 = e.kind; + if (k == N_INTLIT) { return mktname(c, "untyped_int"); }; + if (k == N_FLOATLIT) { return mktname(c, "untyped_float"); }; + if (k == N_STRLIT) { return mktname(c, "str"); }; + if (k == N_RUNELIT) { return mktname(c, "rune"); }; + if (k == N_TRUE) { return mktname(c, "bool"); }; + if (k == N_FALSE) { return mktname(c, "bool"); }; + if (k == N_VOIDLIT) { return mktname(c, "void"); }; + if (k == N_NIL) { return mktname(c, "untyped_nil"); }; + if (k == N_IDENT) { + let s: *sym = scopelookup(c.cur, e.str); + if (s == nil) { return nil; }; + if (s.decl == nil) { return nil; }; + return s.decl.lhs; + }; + if (k == N_CAST) { + // `expr: T` — explicit cast; the type expr is e.rhs. + return e.rhs; + }; + if (k == N_CALL) { + let callee: *node = e.lhs; + if (callee == nil) { return nil; }; + let nm: str; + nm.ptr = nil; nm.len = 0; + if (callee.kind == N_IDENT) { nm = callee.str; }; + if (callee.kind == N_DOT) { nm = callee.str; }; + if (nm.len == 0) { return nil; }; + let s: *sym = scopelookup(c.cur, nm); + if (s == nil) { return nil; }; + if (s.skind != SK_FN) { return nil; }; + if (s.decl == nil) { return nil; }; + return s.decl.lhs; // fn-decl's lhs is the return type + }; + if (k == N_TRYPROP) { + // success unwrap: the success-variant type of operand's + // tagged union. + let opt: *node = exprtype(c, e.lhs); + let ou: *node = resolvealias(c, unwrapbang(opt)); + if (ou == nil) { return nil; }; + if (ou.kind != N_TTAGGED) { return nil; }; + // Hare semantics: success = first non-error variant if + // any !-flag is present; else first variant. + if (taggedhaserr(c, ou)) { + let v: *node = ou.list; + for (v != nil) { + if (!iserrvariant(c, ou, v)) { return v; }; + v = v.next; + }; + return nil; + }; + return ou.list; + }; + if (k == N_TYPEASSERT) { + // `e as T` → T + return e.rhs; + }; + if (k == N_TYPETEST) { + // `e is T` → bool + return mktname(c, "bool"); + }; + return nil; +}; + +// isuntypedint / is_str_like / is_bool_like — helpers used +// by the assignability check below to allow common AST shapes +// through without needing real type inference. +fn isuntypedint(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + return streq(t.str, "untyped_int"); +}; + +fn isuntypedfloat(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + return streq(t.str, "untyped_float"); +}; + +fn isuntypednil(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + return streq(t.str, "untyped_nil"); +}; + +fn isnumerictname(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + let s: str = t.str; + if (streq(s, "i8")) { return true; }; + if (streq(s, "i16")) { return true; }; + if (streq(s, "i32")) { return true; }; + if (streq(s, "i64")) { return true; }; + if (streq(s, "u8")) { return true; }; + if (streq(s, "u16")) { return true; }; + if (streq(s, "u32")) { return true; }; + if (streq(s, "u64")) { return true; }; + if (streq(s, "int")) { return true; }; + if (streq(s, "uint")) { return true; }; + if (streq(s, "uintptr")) { return true; }; + if (streq(s, "rune")) { return true; }; + if (streq(s, "f32")) { return true; }; + if (streq(s, "f64")) { return true; }; + return false; +}; + +fn isstrtname(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + return streq(t.str, "str"); +}; + +// isassignable — AST-level approximation of C check.c +// type_assignable. Returns true when we know the assignment is +// OK, false only when we're confident it isn't, and "skip" (true) +// when we can't tell — to avoid false positives. The trailing bool +// `confident` lets the caller decide whether to emit an error +// when the result is false: if !confident, the caller should not +// flag it. +fn isassignable(c: *checker, dst: *node, src: *node, confident: *bool) bool = { + *confident = false; + if (dst == nil) { return true; }; // no declared target + if (src == nil) { return true; }; // unknown src type + *confident = true; + let du: *node = resolvealias(c, unwrapbang(dst)); + let su: *node = resolvealias(c, unwrapbang(src)); + if (du == nil) { *confident = false; return true; }; + if (su == nil) { *confident = false; return true; }; + if (typeeqast(du, su)) { return true; }; + // untyped numeric → any numeric named type. + if (isuntypedint(su)) { + if (isnumerictname(du)) { return true; }; + // (T | ...) tagged: only OK if some variant accepts untyped_int. + if (du.kind == N_TTAGGED) { + let v: *node = du.list; + for (v != nil) { + let vu: *node = resolvealias(c, unwrapbang(v)); + if (vu != nil) { + if (isnumerictname(vu)) { return true; }; + }; + v = v.next; + }; + *confident = false; + return true; + }; + // Known non-numeric primitive: confidently wrong. + if (du.kind == N_TNAME) { + if (streq(du.str, "bool")) { return false; }; + if (streq(du.str, "void")) { return false; }; + if (streq(du.str, "str")) { return false; }; + }; + // Unknown shapes: stay quiet. + *confident = false; + return true; + }; + if (isuntypedfloat(su)) { + if (isnumerictname(du)) { return true; }; + if (du.kind == N_TNAME) { + if (streq(du.str, "bool")) { return false; }; + if (streq(du.str, "void")) { return false; }; + if (streq(du.str, "str")) { return false; }; + }; + *confident = false; + return true; + }; + if (isuntypednil(su)) { + // nil → ptr/slice/chan/fn/nullable + if (du.kind == N_TPTR) { return true; }; + if (du.kind == N_TSLICE) { return true; }; + if (du.kind == N_TCHAN) { return true; }; + if (du.kind == N_TFN) { return true; }; + // nullable `(*T | void)` — already accepted by typeeqast + // when matched whole; nil is OK there too. + if (du.kind == N_TTAGGED) { + let v: *node = du.list; + for (v != nil) { + if (v.kind == N_TPTR) { return true; }; + if (v.kind == N_TSLICE){ return true; }; + v = v.next; + }; + }; + *confident = false; + return true; + }; + // Tagged-union variant inclusion: src is one of dst's variants. + if (du.kind == N_TTAGGED && su.kind != N_TTAGGED) { + let v: *node = du.list; + for (v != nil) { + let vu: *node = resolvealias(c, unwrapbang(v)); + if (vu != nil) { + if (typeeqast(vu, su)) { return true; }; + }; + v = v.next; + }; + return false; + }; + // tagged → tagged: structural variant list compare. Skip + // (don't be confident) — common when forwarding a fallible + // return through another fn with the same shape but possibly + // a different surface spelling. + if (du.kind == N_TTAGGED && su.kind == N_TTAGGED) { + *confident = false; + return true; + }; + // Two known primitives with different names are confidently + // incompatible. `i32 ↔ bool`, `str ↔ i32`, etc. + if (du.kind == N_TNAME && su.kind == N_TNAME) { + let known_d: bool = isnumerictname(du) || isstrtname(du); + if (!known_d) { if (streq(du.str, "bool")) { known_d = true; }; }; + if (!known_d) { if (streq(du.str, "void")) { known_d = true; }; }; + let known_s: bool = isnumerictname(su) || isstrtname(su); + if (!known_s) { if (streq(su.str, "bool")) { known_s = true; }; }; + if (!known_s) { if (streq(su.str, "void")) { known_s = true; }; }; + if (known_d) { + if (known_s) { + // Both primitives, different names → no. + return false; + }; + }; + }; + // Anything else: don't claim confidence. + *confident = false; + return true; +}; + // ---- match exhaustiveness -------------------------------------------- // // For every match arm, verify that every variant of the scrutinee's // tagged-union type is handled by some case (or a default arm // exists). Multi-pattern `case A | B =>` covers all alts. -fn case_covers(c: *checker, cs: *node, want: *node) bool = { +fn casecovers(c: *checker, cs: *node, want: *node) bool = { if (cs.lhs != nil) { - if (type_eq_ast(cs.lhs, want)) { return true; }; + if (typeeqast(cs.lhs, want)) { return true; }; }; let alt: *node = cs.list; for (alt != nil) { - if (type_eq_ast(alt, want)) { return true; }; + if (typeeqast(alt, want)) { return true; }; alt = alt.next; }; return false; }; -fn err_match_variant(c: *checker, n: *node, vname: *node) void = { +fn errmatchvariant(c: *checker, n: *node, vname: *node) void = { os.write(2, "match: variant not handled".ptr, 26u64); if (vname != nil) { if (vname.kind == N_TNAME) { @@ -362,19 +597,19 @@ fn err_match_variant(c: *checker, n: *node, vname: *node) void = { c.errs += 1; }; -// case_variant_in — true iff `pat` (a `case T` pattern, including +// casevariantin — true iff `pat` (a `case T` pattern, including // each alt of a multi-pattern) names a variant of the tagged // union `tagged`. -fn case_variant_in(tagged: *node, pat: *node) bool = { +fn casevariantin(tagged: *node, pat: *node) bool = { let v: *node = tagged.list; for (v != nil) { - if (type_eq_ast(v, pat)) { return true; }; + if (typeeqast(v, pat)) { return true; }; v = v.next; }; return false; }; -fn err_bad_case_variant(c: *checker, pat: *node) void = { +fn errbadcase(c: *checker, pat: *node) void = { os.write(2, "case: not a variant of scrutinee".ptr, 32u64); if (pat != nil) { if (pat.kind == N_TNAME) { @@ -387,10 +622,10 @@ fn err_bad_case_variant(c: *checker, pat: *node) void = { c.errs += 1; }; -fn check_match_exhaustive(c: *checker, n: *node) void = { +fn checkmatchexhaust(c: *checker, n: *node) void = { if (n == nil) { return; }; if (n.lhs == nil) { return; }; - let st: *node = scrutinee_type(c, n.lhs); + let st: *node = scruttype(c, n.lhs); let u: *node = resolvealias(c, unwrapbang(st)); if (u == nil) { return; }; if (u.kind != N_TTAGGED) { return; }; @@ -400,13 +635,13 @@ fn check_match_exhaustive(c: *checker, n: *node) void = { let cs0: *node = n.list; for (cs0 != nil) { if (cs0.lhs != nil) { - if (!case_variant_in(u, cs0.lhs)) { - err_bad_case_variant(c, cs0.lhs); + if (!casevariantin(u, cs0.lhs)) { + errbadcase(c, cs0.lhs); }; let alt: *node = cs0.list; for (alt != nil) { - if (!case_variant_in(u, alt)) { - err_bad_case_variant(c, alt); + if (!casevariantin(u, alt)) { + errbadcase(c, alt); }; alt = alt.next; }; @@ -425,18 +660,106 @@ fn check_match_exhaustive(c: *checker, n: *node) void = { let covered: bool = false; let cs2: *node = n.list; for (cs2 != nil) { - if (case_covers(c, cs2, v)) { + if (casecovers(c, cs2, v)) { covered = true; cs2 = nil; } else { cs2 = cs2.next; }; }; - if (!covered) { err_match_variant(c, n, v); }; + if (!covered) { errmatchvariant(c, n, v); }; v = v.next; }; }; +// ---- let init / return assignability -------------------------------- +// +// AST-level approximation: when we can infer src's type and dst is +// explicitly declared, verify isassignable. We only emit an error +// when isassignable says "false with confidence." If we can't tell +// (binary ops, complex exprs we don't infer), we stay quiet — full +// type inference lives only on the C side. + +fn errnotassign(c: *checker, dst: *node, src: *node, where: str) void = { + os.write(2, where.ptr, where.len: u64); + os.write(2, ": not assignable".ptr, 16u64); + if (src != nil) { + if (src.kind == N_TNAME) { + os.write(2, " (".ptr, 2u64); + os.write(2, src.str.ptr, src.str.len: u64); + os.write(2, " → ".ptr, 5u64); + if (dst != nil) { + if (dst.kind == N_TNAME) { + os.write(2, dst.str.ptr, dst.str.len: u64); + }; + }; + os.write(2, ")".ptr, 1u64); + }; + }; + os.write(2, "\n".ptr, 1u64); + c.errs += 1; +}; + +fn checkletassign(c: *checker, n: *node) void = { + if (n == nil) { return; }; + if (n.lhs == nil) { return; }; // no declared type, nothing to check + if (n.rhs == nil) { return; }; // no init + let src: *node = exprtype(c, n.rhs); + if (src == nil) { return; }; // can't infer + let conf: bool = false; + let ok: bool = isassignable(c, n.lhs, src, &conf); + if (!conf) { return; }; + if (!ok) { errnotassign(c, n.lhs, src, "let"); }; +}; + +fn checkretassign(c: *checker, n: *node) void = { + if (n == nil) { return; }; + if (n.lhs == nil) { + // bare `return;` — OK iff fnret is void or a tagged union + // with a void variant. Skip flagging for now; cgen handles + // the void-variant tag synthesis already. + return; + }; + if (c.fnret == nil) { return; }; + let src: *node = exprtype(c, n.lhs); + if (src == nil) { return; }; + let conf: bool = false; + let ok: bool = isassignable(c, c.fnret, src, &conf); + if (!conf) { return; }; + if (!ok) { errnotassign(c, c.fnret, src, "return"); }; +}; + +// ---- is / as validity ------------------------------------------------ +// +// `e is T` and `e as T` require that e's declared type be a tagged +// union and that T name one of its variants. Operates on AST type +// expressions; falls back silently when we can't determine e's +// type (matches the case-variant rule for match). +fn checkisas(c: *checker, n: *node) void = { + if (n == nil) { return; }; + // e is in n.lhs (value), T is in n.rhs (type expr). + let st: *node = scruttype(c, n.lhs); + let u: *node = resolvealias(c, unwrapbang(st)); + if (u == nil) { return; }; + if (u.kind != N_TTAGGED) { + os.write(2, "is/as: operand is not a tagged union\n".ptr, 37u64); + c.errs += 1; + return; + }; + let want: *node = n.rhs; + if (want == nil) { return; }; + if (!casevariantin(u, want)) { + os.write(2, "is/as: not a variant of operand".ptr, 31u64); + if (want.kind == N_TNAME) { + os.write(2, " (".ptr, 2u64); + os.write(2, want.str.ptr, want.str.len: u64); + os.write(2, ")".ptr, 1u64); + }; + os.write(2, "\n".ptr, 1u64); + c.errs += 1; + }; +}; + // ---- ? subset propagation -------------------------------------------- // // For `expr?`, the operand's error subset must be a subset of the @@ -444,7 +767,7 @@ fn check_match_exhaustive(c: *checker, n: *node) void = { // is N_TRYPROP; its lhs is the value-bearing expr; we look at the // expr's *declared* type for N_IDENT/N_CALL cases. -fn expr_type_for_tryprop(c: *checker, e: *node) *node = { +fn exprtypeoftry(c: *checker, e: *node) *node = { if (e == nil) { return nil; }; if (e.kind == N_IDENT) { let s: *sym = scopelookup(c.cur, e.str); @@ -471,20 +794,20 @@ fn expr_type_for_tryprop(c: *checker, e: *node) *node = { return nil; }; -fn check_tryprop(c: *checker, n: *node) void = { +fn checktryprop(c: *checker, n: *node) void = { if (n == nil) { return; }; - let t: *node = expr_type_for_tryprop(c, n.lhs); + let t: *node = exprtypeoftry(c, n.lhs); let u: *node = resolvealias(c, unwrapbang(t)); if (u == nil) { return; }; if (u.kind != N_TTAGGED) { return; }; // Does the operand have any error variants? - let has_err: bool = false; + let haserr: bool = false; let v: *node = u.list; for (v != nil) { - if (is_error_variant(c, u, v)) { has_err = true; }; + if (iserrvariant(c, u, v)) { haserr = true; }; v = v.next; }; - if (!has_err) { return; }; + if (!haserr) { return; }; // Enclosing fn must return a tagged union with each operand // error variant present. let r: *node = resolvealias(c, unwrapbang(c.fnret)); @@ -500,11 +823,11 @@ fn check_tryprop(c: *checker, n: *node) void = { }; let ev: *node = u.list; for (ev != nil) { - if (is_error_variant(c, u, ev)) { + if (iserrvariant(c, u, ev)) { let found: bool = false; let rv: *node = r.list; for (rv != nil) { - if (type_eq_ast(rv, ev)) { + if (typeeqast(rv, ev)) { found = true; rv = nil; } else { rv = rv.next; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 6de333ca..7a61b3fc 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -3808,8 +3808,12 @@ fn resolvewalk(c: *checker, n: *node) void = { // Typed checks fire on the way down so the scrutinee/operand // is examined before the arm bodies install new bindings. - if (k == N_MATCH) { check_match_exhaustive(c, n); }; - if (k == N_TRYPROP) { check_tryprop(c, n); }; + if (k == N_MATCH) { checkmatchexhaust(c, n); }; + if (k == N_TRYPROP) { checktryprop(c, n); }; + if (k == N_TYPETEST) { checkisas(c, n); }; + if (k == N_TYPEASSERT) { checkisas(c, n); }; + if (k == N_LET) { checkletassign(c, n); }; + if (k == N_RETURN) { checkretassign(c, n); }; // `use IDENT;` — name is a module label, not a free ident. if (k == N_USE) { return; }; @@ -3956,19 +3960,11 @@ fn resolvealias(c: *checker, n: *node) *node = { return n; }; -// is_tagged_type — true if `n` (after alias resolution) is an -// N_TTAGGED type expression. -fn is_tagged_type(c: *checker, n: *node) bool = { - let u: *node = resolvealias(c, unwrapbang(n)); - if (u == nil) { return false; }; - return u.kind == N_TTAGGED; -}; - -// type_eq_ast — structural equality on AST type expressions, mod +// typeeqast — structural equality on AST type expressions, mod // the `!` wrapper. Mirrors variant_match in cgen + check.c: NAMED // types compare by string (the closest stand-in for pointer // identity at the AST level); other nodes recurse by kind. -fn type_eq_ast(a: *node, b: *node) bool = { +fn typeeqast(a: *node, b: *node) bool = { let aa: *node = unwrapbang(a); let bb: *node = unwrapbang(b); if (aa == nil) { return bb == nil; }; @@ -3976,19 +3972,19 @@ fn type_eq_ast(a: *node, b: *node) bool = { if (aa.kind != bb.kind) { return false; }; let k: i32 = aa.kind; if (k == N_TNAME) { return streq(aa.str, bb.str); }; - if (k == N_TPTR) { return type_eq_ast(aa.lhs, bb.lhs); }; - if (k == N_TSLICE){ return type_eq_ast(aa.lhs, bb.lhs); }; - if (k == N_TCHAN) { return type_eq_ast(aa.lhs, bb.lhs); }; + if (k == N_TPTR) { return typeeqast(aa.lhs, bb.lhs); }; + if (k == N_TSLICE){ return typeeqast(aa.lhs, bb.lhs); }; + if (k == N_TCHAN) { return typeeqast(aa.lhs, bb.lhs); }; // Conservative: anything else (struct/fn/tagged/tuple/array) // fails the cheap check. Selfhost code doesn't currently rely // on equality at these shapes for the targeted checks. return false; }; -// variant_is_error — does this variant carry the `!` mark? Either +// varianterr — does this variant carry the `!` mark? Either // the variant itself is N_TBANG or it's an alias whose typedecl // body is `!T`. Mirrors C check.c's iserror-after-NAMED rule. -fn variant_is_error(c: *checker, v: *node) bool = { +fn varianterr(c: *checker, v: *node) bool = { if (v == nil) { return false; }; if (v.kind == N_TBANG) { return true; }; if (v.kind == N_TNAME) { @@ -4008,35 +4004,35 @@ fn variant_is_error(c: *checker, v: *node) bool = { return false; }; -// tagged_has_errflag — true iff any variant of `n` (assumed +// taggedhaserr — true iff any variant of `n` (assumed // N_TTAGGED) is `!`-marked. Picks the explicit-flag semantics over // the legacy "first variant = success" rule. -fn tagged_has_errflag(c: *checker, n: *node) bool = { +fn taggedhaserr(c: *checker, n: *node) bool = { let v: *node = n.list; for (v != nil) { - if (variant_is_error(c, v)) { return true; }; + if (varianterr(c, v)) { return true; }; v = v.next; }; return false; }; -// is_error_variant — under flag-aware mode (any !-marked variant), +// iserrvariant — under flag-aware mode (any !-marked variant), // returns true iff `v` is `!`-marked. Under legacy mode (no flags), // returns true iff `v` is not the first variant of `tagged`. -fn is_error_variant(c: *checker, tagged: *node, v: *node) bool = { - if (tagged_has_errflag(c, tagged)) { - return variant_is_error(c, v); +fn iserrvariant(c: *checker, tagged: *node, v: *node) bool = { + if (taggedhaserr(c, tagged)) { + return varianterr(c, v); }; // Legacy: first variant of the union is success. if (tagged.list == v) { return false; }; return true; }; -// scrutinee_type — resolve the type expression for a match's +// scruttype — resolve the type expression for a match's // scrutinee. Handles N_IDENT (look up local/param's declared // type) and N_DOT (struct-field access). Returns nil if we // can't statically determine the type. Used by exhaustiveness. -fn scrutinee_type(c: *checker, e: *node) *node = { +fn scruttype(c: *checker, e: *node) *node = { if (e == nil) { return nil; }; if (e.kind == N_IDENT) { let s: *sym = scopelookup(c.cur, e.str); @@ -4048,25 +4044,264 @@ fn scrutinee_type(c: *checker, e: *node) *node = { return nil; }; +// mktname — fabricate an N_TNAME node with str = `nm`. Used by +// exprtype to return primitive type nodes for literal +// expressions. The arena keeps them around as long as the checker. +fn mktname(c: *checker, nm: str) *node = { + let n: *node = newnode(c.a, N_TNAME, "", 0, 0); + n.str = nm; + return n; +}; + +// exprtype — best-effort type-AST inference for an expression +// node. Handles literals, identifiers, calls, and casts; returns +// nil for shapes we don't statically know (binary ops, struct +// field access into non-primitive types, etc). +fn exprtype(c: *checker, e: *node) *node = { + if (e == nil) { return nil; }; + let k: i32 = e.kind; + if (k == N_INTLIT) { return mktname(c, "untyped_int"); }; + if (k == N_FLOATLIT) { return mktname(c, "untyped_float"); }; + if (k == N_STRLIT) { return mktname(c, "str"); }; + if (k == N_RUNELIT) { return mktname(c, "rune"); }; + if (k == N_TRUE) { return mktname(c, "bool"); }; + if (k == N_FALSE) { return mktname(c, "bool"); }; + if (k == N_VOIDLIT) { return mktname(c, "void"); }; + if (k == N_NIL) { return mktname(c, "untyped_nil"); }; + if (k == N_IDENT) { + let s: *sym = scopelookup(c.cur, e.str); + if (s == nil) { return nil; }; + if (s.decl == nil) { return nil; }; + return s.decl.lhs; + }; + if (k == N_CAST) { + // `expr: T` — explicit cast; the type expr is e.rhs. + return e.rhs; + }; + if (k == N_CALL) { + let callee: *node = e.lhs; + if (callee == nil) { return nil; }; + let nm: str; + nm.ptr = nil; nm.len = 0; + if (callee.kind == N_IDENT) { nm = callee.str; }; + if (callee.kind == N_DOT) { nm = callee.str; }; + if (nm.len == 0) { return nil; }; + let s: *sym = scopelookup(c.cur, nm); + if (s == nil) { return nil; }; + if (s.skind != SK_FN) { return nil; }; + if (s.decl == nil) { return nil; }; + return s.decl.lhs; // fn-decl's lhs is the return type + }; + if (k == N_TRYPROP) { + // success unwrap: the success-variant type of operand's + // tagged union. + let opt: *node = exprtype(c, e.lhs); + let ou: *node = resolvealias(c, unwrapbang(opt)); + if (ou == nil) { return nil; }; + if (ou.kind != N_TTAGGED) { return nil; }; + // Hare semantics: success = first non-error variant if + // any !-flag is present; else first variant. + if (taggedhaserr(c, ou)) { + let v: *node = ou.list; + for (v != nil) { + if (!iserrvariant(c, ou, v)) { return v; }; + v = v.next; + }; + return nil; + }; + return ou.list; + }; + if (k == N_TYPEASSERT) { + // `e as T` → T + return e.rhs; + }; + if (k == N_TYPETEST) { + // `e is T` → bool + return mktname(c, "bool"); + }; + return nil; +}; + +// isuntypedint / is_str_like / is_bool_like — helpers used +// by the assignability check below to allow common AST shapes +// through without needing real type inference. +fn isuntypedint(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + return streq(t.str, "untyped_int"); +}; + +fn isuntypedfloat(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + return streq(t.str, "untyped_float"); +}; + +fn isuntypednil(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + return streq(t.str, "untyped_nil"); +}; + +fn isnumerictname(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + let s: str = t.str; + if (streq(s, "i8")) { return true; }; + if (streq(s, "i16")) { return true; }; + if (streq(s, "i32")) { return true; }; + if (streq(s, "i64")) { return true; }; + if (streq(s, "u8")) { return true; }; + if (streq(s, "u16")) { return true; }; + if (streq(s, "u32")) { return true; }; + if (streq(s, "u64")) { return true; }; + if (streq(s, "int")) { return true; }; + if (streq(s, "uint")) { return true; }; + if (streq(s, "uintptr")) { return true; }; + if (streq(s, "rune")) { return true; }; + if (streq(s, "f32")) { return true; }; + if (streq(s, "f64")) { return true; }; + return false; +}; + +fn isstrtname(t: *node) bool = { + if (t == nil) { return false; }; + if (t.kind != N_TNAME) { return false; }; + return streq(t.str, "str"); +}; + +// isassignable — AST-level approximation of C check.c +// type_assignable. Returns true when we know the assignment is +// OK, false only when we're confident it isn't, and "skip" (true) +// when we can't tell — to avoid false positives. The trailing bool +// `confident` lets the caller decide whether to emit an error +// when the result is false: if !confident, the caller should not +// flag it. +fn isassignable(c: *checker, dst: *node, src: *node, confident: *bool) bool = { + *confident = false; + if (dst == nil) { return true; }; // no declared target + if (src == nil) { return true; }; // unknown src type + *confident = true; + let du: *node = resolvealias(c, unwrapbang(dst)); + let su: *node = resolvealias(c, unwrapbang(src)); + if (du == nil) { *confident = false; return true; }; + if (su == nil) { *confident = false; return true; }; + if (typeeqast(du, su)) { return true; }; + // untyped numeric → any numeric named type. + if (isuntypedint(su)) { + if (isnumerictname(du)) { return true; }; + // (T | ...) tagged: only OK if some variant accepts untyped_int. + if (du.kind == N_TTAGGED) { + let v: *node = du.list; + for (v != nil) { + let vu: *node = resolvealias(c, unwrapbang(v)); + if (vu != nil) { + if (isnumerictname(vu)) { return true; }; + }; + v = v.next; + }; + *confident = false; + return true; + }; + // Known non-numeric primitive: confidently wrong. + if (du.kind == N_TNAME) { + if (streq(du.str, "bool")) { return false; }; + if (streq(du.str, "void")) { return false; }; + if (streq(du.str, "str")) { return false; }; + }; + // Unknown shapes: stay quiet. + *confident = false; + return true; + }; + if (isuntypedfloat(su)) { + if (isnumerictname(du)) { return true; }; + if (du.kind == N_TNAME) { + if (streq(du.str, "bool")) { return false; }; + if (streq(du.str, "void")) { return false; }; + if (streq(du.str, "str")) { return false; }; + }; + *confident = false; + return true; + }; + if (isuntypednil(su)) { + // nil → ptr/slice/chan/fn/nullable + if (du.kind == N_TPTR) { return true; }; + if (du.kind == N_TSLICE) { return true; }; + if (du.kind == N_TCHAN) { return true; }; + if (du.kind == N_TFN) { return true; }; + // nullable `(*T | void)` — already accepted by typeeqast + // when matched whole; nil is OK there too. + if (du.kind == N_TTAGGED) { + let v: *node = du.list; + for (v != nil) { + if (v.kind == N_TPTR) { return true; }; + if (v.kind == N_TSLICE){ return true; }; + v = v.next; + }; + }; + *confident = false; + return true; + }; + // Tagged-union variant inclusion: src is one of dst's variants. + if (du.kind == N_TTAGGED && su.kind != N_TTAGGED) { + let v: *node = du.list; + for (v != nil) { + let vu: *node = resolvealias(c, unwrapbang(v)); + if (vu != nil) { + if (typeeqast(vu, su)) { return true; }; + }; + v = v.next; + }; + return false; + }; + // tagged → tagged: structural variant list compare. Skip + // (don't be confident) — common when forwarding a fallible + // return through another fn with the same shape but possibly + // a different surface spelling. + if (du.kind == N_TTAGGED && su.kind == N_TTAGGED) { + *confident = false; + return true; + }; + // Two known primitives with different names are confidently + // incompatible. `i32 ↔ bool`, `str ↔ i32`, etc. + if (du.kind == N_TNAME && su.kind == N_TNAME) { + let known_d: bool = isnumerictname(du) || isstrtname(du); + if (!known_d) { if (streq(du.str, "bool")) { known_d = true; }; }; + if (!known_d) { if (streq(du.str, "void")) { known_d = true; }; }; + let known_s: bool = isnumerictname(su) || isstrtname(su); + if (!known_s) { if (streq(su.str, "bool")) { known_s = true; }; }; + if (!known_s) { if (streq(su.str, "void")) { known_s = true; }; }; + if (known_d) { + if (known_s) { + // Both primitives, different names → no. + return false; + }; + }; + }; + // Anything else: don't claim confidence. + *confident = false; + return true; +}; + // ---- match exhaustiveness -------------------------------------------- // // For every match arm, verify that every variant of the scrutinee's // tagged-union type is handled by some case (or a default arm // exists). Multi-pattern `case A | B =>` covers all alts. -fn case_covers(c: *checker, cs: *node, want: *node) bool = { +fn casecovers(c: *checker, cs: *node, want: *node) bool = { if (cs.lhs != nil) { - if (type_eq_ast(cs.lhs, want)) { return true; }; + if (typeeqast(cs.lhs, want)) { return true; }; }; let alt: *node = cs.list; for (alt != nil) { - if (type_eq_ast(alt, want)) { return true; }; + if (typeeqast(alt, want)) { return true; }; alt = alt.next; }; return false; }; -fn err_match_variant(c: *checker, n: *node, vname: *node) void = { +fn errmatchvariant(c: *checker, n: *node, vname: *node) void = { os.write(2, "match: variant not handled".ptr, 26u64); if (vname != nil) { if (vname.kind == N_TNAME) { @@ -4079,19 +4314,19 @@ fn err_match_variant(c: *checker, n: *node, vname: *node) void = { c.errs += 1; }; -// case_variant_in — true iff `pat` (a `case T` pattern, including +// casevariantin — true iff `pat` (a `case T` pattern, including // each alt of a multi-pattern) names a variant of the tagged // union `tagged`. -fn case_variant_in(tagged: *node, pat: *node) bool = { +fn casevariantin(tagged: *node, pat: *node) bool = { let v: *node = tagged.list; for (v != nil) { - if (type_eq_ast(v, pat)) { return true; }; + if (typeeqast(v, pat)) { return true; }; v = v.next; }; return false; }; -fn err_bad_case_variant(c: *checker, pat: *node) void = { +fn errbadcase(c: *checker, pat: *node) void = { os.write(2, "case: not a variant of scrutinee".ptr, 32u64); if (pat != nil) { if (pat.kind == N_TNAME) { @@ -4104,10 +4339,10 @@ fn err_bad_case_variant(c: *checker, pat: *node) void = { c.errs += 1; }; -fn check_match_exhaustive(c: *checker, n: *node) void = { +fn checkmatchexhaust(c: *checker, n: *node) void = { if (n == nil) { return; }; if (n.lhs == nil) { return; }; - let st: *node = scrutinee_type(c, n.lhs); + let st: *node = scruttype(c, n.lhs); let u: *node = resolvealias(c, unwrapbang(st)); if (u == nil) { return; }; if (u.kind != N_TTAGGED) { return; }; @@ -4117,13 +4352,13 @@ fn check_match_exhaustive(c: *checker, n: *node) void = { let cs0: *node = n.list; for (cs0 != nil) { if (cs0.lhs != nil) { - if (!case_variant_in(u, cs0.lhs)) { - err_bad_case_variant(c, cs0.lhs); + if (!casevariantin(u, cs0.lhs)) { + errbadcase(c, cs0.lhs); }; let alt: *node = cs0.list; for (alt != nil) { - if (!case_variant_in(u, alt)) { - err_bad_case_variant(c, alt); + if (!casevariantin(u, alt)) { + errbadcase(c, alt); }; alt = alt.next; }; @@ -4142,18 +4377,106 @@ fn check_match_exhaustive(c: *checker, n: *node) void = { let covered: bool = false; let cs2: *node = n.list; for (cs2 != nil) { - if (case_covers(c, cs2, v)) { + if (casecovers(c, cs2, v)) { covered = true; cs2 = nil; } else { cs2 = cs2.next; }; }; - if (!covered) { err_match_variant(c, n, v); }; + if (!covered) { errmatchvariant(c, n, v); }; v = v.next; }; }; +// ---- let init / return assignability -------------------------------- +// +// AST-level approximation: when we can infer src's type and dst is +// explicitly declared, verify isassignable. We only emit an error +// when isassignable says "false with confidence." If we can't tell +// (binary ops, complex exprs we don't infer), we stay quiet — full +// type inference lives only on the C side. + +fn errnotassign(c: *checker, dst: *node, src: *node, where: str) void = { + os.write(2, where.ptr, where.len: u64); + os.write(2, ": not assignable".ptr, 16u64); + if (src != nil) { + if (src.kind == N_TNAME) { + os.write(2, " (".ptr, 2u64); + os.write(2, src.str.ptr, src.str.len: u64); + os.write(2, " → ".ptr, 5u64); + if (dst != nil) { + if (dst.kind == N_TNAME) { + os.write(2, dst.str.ptr, dst.str.len: u64); + }; + }; + os.write(2, ")".ptr, 1u64); + }; + }; + os.write(2, "\n".ptr, 1u64); + c.errs += 1; +}; + +fn checkletassign(c: *checker, n: *node) void = { + if (n == nil) { return; }; + if (n.lhs == nil) { return; }; // no declared type, nothing to check + if (n.rhs == nil) { return; }; // no init + let src: *node = exprtype(c, n.rhs); + if (src == nil) { return; }; // can't infer + let conf: bool = false; + let ok: bool = isassignable(c, n.lhs, src, &conf); + if (!conf) { return; }; + if (!ok) { errnotassign(c, n.lhs, src, "let"); }; +}; + +fn checkretassign(c: *checker, n: *node) void = { + if (n == nil) { return; }; + if (n.lhs == nil) { + // bare `return;` — OK iff fnret is void or a tagged union + // with a void variant. Skip flagging for now; cgen handles + // the void-variant tag synthesis already. + return; + }; + if (c.fnret == nil) { return; }; + let src: *node = exprtype(c, n.lhs); + if (src == nil) { return; }; + let conf: bool = false; + let ok: bool = isassignable(c, c.fnret, src, &conf); + if (!conf) { return; }; + if (!ok) { errnotassign(c, c.fnret, src, "return"); }; +}; + +// ---- is / as validity ------------------------------------------------ +// +// `e is T` and `e as T` require that e's declared type be a tagged +// union and that T name one of its variants. Operates on AST type +// expressions; falls back silently when we can't determine e's +// type (matches the case-variant rule for match). +fn checkisas(c: *checker, n: *node) void = { + if (n == nil) { return; }; + // e is in n.lhs (value), T is in n.rhs (type expr). + let st: *node = scruttype(c, n.lhs); + let u: *node = resolvealias(c, unwrapbang(st)); + if (u == nil) { return; }; + if (u.kind != N_TTAGGED) { + os.write(2, "is/as: operand is not a tagged union\n".ptr, 37u64); + c.errs += 1; + return; + }; + let want: *node = n.rhs; + if (want == nil) { return; }; + if (!casevariantin(u, want)) { + os.write(2, "is/as: not a variant of operand".ptr, 31u64); + if (want.kind == N_TNAME) { + os.write(2, " (".ptr, 2u64); + os.write(2, want.str.ptr, want.str.len: u64); + os.write(2, ")".ptr, 1u64); + }; + os.write(2, "\n".ptr, 1u64); + c.errs += 1; + }; +}; + // ---- ? subset propagation -------------------------------------------- // // For `expr?`, the operand's error subset must be a subset of the @@ -4161,7 +4484,7 @@ fn check_match_exhaustive(c: *checker, n: *node) void = { // is N_TRYPROP; its lhs is the value-bearing expr; we look at the // expr's *declared* type for N_IDENT/N_CALL cases. -fn expr_type_for_tryprop(c: *checker, e: *node) *node = { +fn exprtypeoftry(c: *checker, e: *node) *node = { if (e == nil) { return nil; }; if (e.kind == N_IDENT) { let s: *sym = scopelookup(c.cur, e.str); @@ -4188,20 +4511,20 @@ fn expr_type_for_tryprop(c: *checker, e: *node) *node = { return nil; }; -fn check_tryprop(c: *checker, n: *node) void = { +fn checktryprop(c: *checker, n: *node) void = { if (n == nil) { return; }; - let t: *node = expr_type_for_tryprop(c, n.lhs); + let t: *node = exprtypeoftry(c, n.lhs); let u: *node = resolvealias(c, unwrapbang(t)); if (u == nil) { return; }; if (u.kind != N_TTAGGED) { return; }; // Does the operand have any error variants? - let has_err: bool = false; + let haserr: bool = false; let v: *node = u.list; for (v != nil) { - if (is_error_variant(c, u, v)) { has_err = true; }; + if (iserrvariant(c, u, v)) { haserr = true; }; v = v.next; }; - if (!has_err) { return; }; + if (!haserr) { return; }; // Enclosing fn must return a tagged union with each operand // error variant present. let r: *node = resolvealias(c, unwrapbang(c.fnret)); @@ -4217,11 +4540,11 @@ fn check_tryprop(c: *checker, n: *node) void = { }; let ev: *node = u.list; for (ev != nil) { - if (is_error_variant(c, u, ev)) { + if (iserrvariant(c, u, ev)) { let found: bool = false; let rv: *node = r.list; for (rv != nil) { - if (type_eq_ast(rv, ev)) { + if (typeeqast(rv, ev)) { found = true; rv = nil; } else { rv = rv.next; }; diff --git a/test/wcc/950_selfcheck.c b/test/wcc/950_selfcheck.c index eba30de4..f01e3451 100644 --- a/test/wcc/950_selfcheck.c +++ b/test/wcc/950_selfcheck.c @@ -103,6 +103,23 @@ static const struct row rows[] = { " };\n" "};\n", "case: not a variant" }, + /* is T where T isn't a variant of the operand */ + { "fn pick() (i32 | str) = { return 1; };\n" + "fn caller() void = {\n" + " let v: (i32 | str) = pick();\n" + " if (v is f64) { };\n" + "};\n", + "is/as: not a variant" }, + /* let init-type mismatch on primitives */ + { "fn caller() void = {\n" + " let x: bool = 42;\n" + "};\n", + "let: not assignable" }, + /* return type mismatch */ + { "fn caller() i32 = {\n" + " return \"hi\";\n" + "};\n", + "return: not assignable" }, }; int @@ -136,6 +153,14 @@ main(void) "?: enclosing fn has no tagged-union return") != NULL); err_present = err_present || (err && strstr(err, "case: not a variant") != NULL); + err_present = err_present || (err && strstr(err, + "is/as: not a variant") != NULL); + err_present = err_present || (err && strstr(err, + "is/as: operand is not a tagged union") != NULL); + err_present = err_present || (err && strstr(err, + "let: not assignable") != NULL); + err_present = err_present || (err && strstr(err, + "return: not assignable") != NULL); int ok; if (expected_no_err) ok = !err_present; else ok = got_match;