wcc/ww: reject mismatched integer binop operands (#26)
cstage rejects a binop whose two integer operands have different types (e.g. int vs i32 from len()); wwstage accepted it, miscompiling under no-implicit-promotion. Align wwstage UP: unifyarith now chases aliases and loud-rejects an integer-type mismatch, routing the ordered-comparison ops through the same path with the error message threaded on `e`. Per the user's no-implicit-promotion decision. Scope carve-outs: EQ/NEQ stay out of the reject (#34, the comparison operators keep their own widening rule) and a rune literal is exempt (#35, N_RUNELIT is still untyped at this point). Adds the 29-case test/wcc/949_intbinop_mismatch.c and its Makefile wiring.
This commit is contained in:
@@ -2404,18 +2404,18 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = {
|
||||
};
|
||||
|
||||
// unifyarith — usual-arithmetic-conversion analogue at the AST-tnode
|
||||
// layer. Mirrors cstage cmd/wcc/check.c:580-596 `unify_arith` and harec
|
||||
// ref/harec/src/types.c type_promote. Trailing `return ltn` covers
|
||||
// mismatched typed pairs; cstage flags the same shape — wwstage's
|
||||
// checker stays silent here per existing discipline.
|
||||
// layer. Mirrors cstage cmd/wcc/check.c:1034-1069 `unify_arith` and harec
|
||||
// ref/harec/src/types.c type_promote. A typed/typed operand mismatch is
|
||||
// loud (#26), aligning wwstage down to cstage; the only promotion left is
|
||||
// the one-sided alias-vs-base chase (cstage check.c:1060-1067).
|
||||
//
|
||||
// Nil-on-valid classification (5-lite-b #34, A.6.2.1c #24): both ltn
|
||||
// and rtn can be nil when an operand was an inherent-IDENT bail
|
||||
// (exprtype N_IDENT arm L1596-1599 — SK_USE module ref or pseudo-
|
||||
// builtin callee with sym.decl == nil; #19 retires these as
|
||||
// dedicated AST kinds). Propagation, not silent gap — asserttyped
|
||||
// gates those idents at the consumer layer.
|
||||
fn unifyarith(c: *checker, ltn: *node, rtn: *node) *node = {
|
||||
// Nil-on-valid classification (5-lite-b #34, A.6.2.1c #24): ltn or rtn
|
||||
// can be nil when an operand was an inherent-IDENT bail (exprtype
|
||||
// N_IDENT arm L1596-1599 — SK_USE module ref or pseudo-builtin callee
|
||||
// with sym.decl == nil; #19 retires these as dedicated AST kinds).
|
||||
// Propagation, not silent gap — asserttyped gates those idents at the
|
||||
// consumer layer, so a nil operand never reaches the loud reject below.
|
||||
fn unifyarith(c: *checker, e: *node, ltn: *node, rtn: *node) *node = {
|
||||
let lu: bool = isuntypedint(ltn) || isuntypedfloat(ltn);
|
||||
let ru: bool = isuntypedint(rtn) || isuntypedfloat(rtn);
|
||||
if (lu && ru) {
|
||||
@@ -2431,10 +2431,54 @@ fn unifyarith(c: *checker, ltn: *node, rtn: *node) *node = {
|
||||
if (ru) {
|
||||
if (isassignable(c, ltn, rtn, &conf)) { return ltn; };
|
||||
};
|
||||
// A rune LITERAL is untyped_rune in cstage (cmd/wcc/check.c:1296),
|
||||
// assignable to any integer or rune (cmd/wcc/type.c:379), so cstage's
|
||||
// unify_arith accepts `ch == 'x'` / `c - '0'` and returns the typed
|
||||
// side. wwstage stamps N_RUNELIT concrete `rune` (the #29 divergence,
|
||||
// exprtype :2927), so the literal does not reach the isuntyped arms
|
||||
// above. Mirror cstage here off the operand node so the rune-literal /
|
||||
// integer mix stays valid (35 selfhost+lib sites) under the #26 reject.
|
||||
if (e != nil) {
|
||||
let lr: bool = e.lhs != nil && e.lhs.kind == nkind.N_RUNELIT;
|
||||
let rr: bool = e.rhs != nil && e.rhs.kind == nkind.N_RUNELIT;
|
||||
if (lr && isinttypeast(rtn)) { return rtn; };
|
||||
if (rr && isinttypeast(ltn)) { return ltn; };
|
||||
};
|
||||
if (typeeqast(c, ltn, rtn)) { return ltn; };
|
||||
// Mismatched typed pair — return ltn so the binop stamps something;
|
||||
// 5-lite-b: the trailing nil-on-mismatch shape was eliminated when
|
||||
// the helper was split out of binoptype.
|
||||
// One-sided alias vs its (transitive) base promotes to the alias
|
||||
// side; alias-vs-different-alias stays rejected even when the bases
|
||||
// agree. Mirrors cstage unify_arith (cmd/wcc/check.c:1060-1067) and
|
||||
// harec type_promote (ref/harec/src/check.c:1083-1105). The error
|
||||
// axis (varianterr) must agree on both sides so a `!i32` alias does
|
||||
// NOT promote against a plain i32 (#246, 949_errtype_compare).
|
||||
// A user alias is an SK_TYPE sym WITH a decl body; the primitives are
|
||||
// SK_TYPE too but decl == nil (check.ww:95-112), so aliassym alone
|
||||
// would mis-flag i32 as "named". This decl != nil gate is the wwstage
|
||||
// analog of cstage's `kind == TY_NAMED` (primitives are TY_I32 etc).
|
||||
let ls: *sym = aliassym(c, unwrapbang(ltn));
|
||||
let rs: *sym = aliassym(c, unwrapbang(rtn));
|
||||
let la: bool = ls != nil && ls.decl != nil;
|
||||
let ra: bool = rs != nil && rs.decl != nil;
|
||||
if (!(la && ra)) {
|
||||
let da: *node = resolvealias(c, ltn);
|
||||
let db: *node = resolvealias(c, rtn);
|
||||
if (da != nil && db != nil
|
||||
&& varianterr(c, ltn) == varianterr(c, rtn)
|
||||
&& typeeqast(c, da, db)) {
|
||||
if (la) { return ltn; };
|
||||
return rtn;
|
||||
};
|
||||
};
|
||||
// ww requires explicit integer conversion in binary ops (Go-faithful,
|
||||
// user-blessed #26): a typed/typed operand mismatch is loud, NOT
|
||||
// promoted. Diverges from Hare type_promote's implicit signed-widen
|
||||
// (ref/harec/src/check.c:1079/1337, STORAGE_INT 1129-1134). cstage
|
||||
// already rejects the same shape (cmd/wcc/check.c:1068); this aligns
|
||||
// wwstage down. A nil operand is an inherent-IDENT bail (see header) —
|
||||
// propagate it, never reject.
|
||||
if (ltn != nil && rtn != nil) {
|
||||
deffolderr(c, e, "operands have differing types");
|
||||
};
|
||||
return ltn;
|
||||
};
|
||||
|
||||
@@ -2549,7 +2593,7 @@ fn binoptype(c: *checker, e: *node) *node = {
|
||||
(rtn != nil && !numkindast(c, rtn))) {
|
||||
deffolderr(c, e, "arithmetic on non-numeric type");
|
||||
};
|
||||
return unifyarith(c, ltn, rtn);
|
||||
return unifyarith(c, e, ltn, rtn);
|
||||
};
|
||||
if (op == tkind.TK_AMP || op == tkind.TK_PIPE ||
|
||||
op == tkind.TK_CARET || op == tkind.TK_LSHIFT ||
|
||||
@@ -2558,19 +2602,19 @@ fn binoptype(c: *checker, e: *node) *node = {
|
||||
(rtn != nil && !intkindast(c, rtn))) {
|
||||
deffolderr(c, e, "bitwise on non-integer type");
|
||||
};
|
||||
return unifyarith(c, ltn, rtn);
|
||||
return unifyarith(c, e, ltn, rtn);
|
||||
};
|
||||
if (op == tkind.TK_EQ || op == tkind.TK_NEQ ||
|
||||
op == tkind.TK_LT || op == tkind.TK_LE ||
|
||||
op == tkind.TK_GT || op == tkind.TK_GE) {
|
||||
// cstage routes every comparison through unify_arith (check.c:952/
|
||||
// 955 cbinop), which loud-rejects an error-typed operand paired
|
||||
// with a differing type, e.g. strconv.invalid != i32. wwstage's
|
||||
// unifyarith stays silent on generic typed mismatches (5-lite-b),
|
||||
// but the error-type case is a real cs!=ww edge and must reject to
|
||||
// match cstage (#246, rule 10). Scoped to error operands so the
|
||||
// broad differing-types flip (typeeqast vs cstage type_eq
|
||||
// asymmetry risk) stays out.
|
||||
// cstage routes every comparison through unify_arith (check.c:1195/
|
||||
// 1200 cbinop), which loud-rejects a differing-types pair, e.g.
|
||||
// strconv.invalid != i32. wwstage routes the ORDERED arm through
|
||||
// unifyarith below (#26), but EQ/NEQ stays here on the error-type-
|
||||
// only reject: a full unifyarith route for EQ/NEQ would expose the
|
||||
// typeeqast-vs-cstage-type_eq asymmetry on struct / tuple / fn-ptr
|
||||
// equality. The error-type case (#246, rule 10) is the one real
|
||||
// cs!=ww edge EQ/NEQ must still reject.
|
||||
if (varianterr(c, ltn) || varianterr(c, rtn)) {
|
||||
if (!typeeqast(c, ltn, rtn)) {
|
||||
deffolderr(c, e, "operands have differing types");
|
||||
@@ -2587,6 +2631,17 @@ fn binoptype(c: *checker, e: *node) *node = {
|
||||
(rtn != nil && !numkindast(c, rtn))) {
|
||||
deffolderr(c, e, "ordered comparison on non-numeric");
|
||||
};
|
||||
// #26: an ordered comparison routes through unifyarith for
|
||||
// the differing-types reject — cstage's cbinop sends every
|
||||
// comparison through unify_arith (cmd/wcc/check.c:1195/1200),
|
||||
// so `int < len(s):i32` is loud there. Guarded off error
|
||||
// operands (the #246 block above already rejects those, so
|
||||
// this avoids a double diagnostic) and scoped to ordered,
|
||||
// keeping the typeeqast-vs-type_eq asymmetry risk off the
|
||||
// untouched EQ/NEQ arm.
|
||||
if (!varianterr(c, ltn) && !varianterr(c, rtn)) {
|
||||
unifyarith(c, e, ltn, rtn);
|
||||
};
|
||||
};
|
||||
return mktname(c, "bool");
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user