From 14a1ed47a2f0d2f0728985c8f3494645bb628de9 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 22:47:43 +0000 Subject: [PATCH 1/5] A @type field's initialiser is checked, null included /** @type {() => Undefined} */ mark: null was accepted (a literal null or undefined under @type was a placeholder standing for T), so hooks.mark() passed and threw at run time. The initialiser is now checked against the annotation as a const's is, in both the synthesis and the checking path of an object literal: a field that starts as null is declared T | Null. undefined and other values, and the inline /*: T */ form, already were. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LCVsbkjtuBnPMGXCE1qJjr --- CHANGELOG.md | 8 ++ a.js | 4 + b.js | 3 + crates/inty/src/ast/mod.rs | 14 +-- crates/inty/src/infer/features/rows.rs | 51 ++------- .../inty/tests/fixtures/jsdoc_at_type_iife.js | 28 ++--- crates/inty/tests/jsdoc_at_type.rs | 107 ++++++++++++++---- docs/jsdoc-at-type.md | 55 +++++---- docs/type-system.md | 7 +- examples/playground/features/jsdoc-at-type.js | 26 +++-- 10 files changed, 179 insertions(+), 124 deletions(-) create mode 100644 a.js create mode 100644 b.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 51951a10..81d56dfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## Unreleased +### Fixes from moving Rosaclef to 276dd1f + +- An object-literal field's `/** @type {T} */` annotation checks its + initialiser, as a `const`'s does: `/** @type {() => Undefined} */ mark: + null` is an error ("expected '() => Undefined', found 'Null'"), where it + was accepted and `hooks.mark()` threw. A field that starts as `null` is + declared `T | Null`. (The `null` / `undefined` placeholder rule is gone.) + ### Multi-line annotations Long type annotations can be written across lines, nicely laid out, and mean diff --git a/a.js b/a.js new file mode 100644 index 00000000..32cee51f --- /dev/null +++ b/a.js @@ -0,0 +1,4 @@ +export const hooks = { + /** @type {() => Undefined} */ + mark: 3, +}; diff --git a/b.js b/b.js new file mode 100644 index 00000000..c2d92bf6 --- /dev/null +++ b/b.js @@ -0,0 +1,3 @@ +export const hooks = { + mark /*: () => Undefined */: 3, +}; diff --git a/crates/inty/src/ast/mod.rs b/crates/inty/src/ast/mod.rs index 9bbdcb09..0ff17fd0 100644 --- a/crates/inty/src/ast/mod.rs +++ b/crates/inty/src/ast/mod.rs @@ -458,15 +458,11 @@ pub enum ChainSegment { }, } -/// Where the annotation came from. Affects how inference treats the -/// initialiser. `Inline` annotations (TS-style `field: T` and the -/// inty-native `/*: T */`) check that the initialiser subsumes the -/// annotated type. `JsDoc` annotations (`/** @type {T} */`) follow -/// TypeScript's JSDoc semantics: a literal `null` / `undefined` -/// initialiser is treated as a placeholder and skipped — the -/// declaration is what types the field, not the seed value. This -/// matches the htmx-style `/** @type {typeof helper} */ field: null` -/// pattern that fills the field via later assignment. +/// Where the annotation came from. Either way the initialiser is +/// checked against the annotated type (a `null` start is declared +/// `T | Null`); a `JsDoc` annotation (`/** @type {T} */`) that doesn't +/// parse is ignored with a warning, as TypeScript ignores JSDoc it +/// doesn't understand, where an `Inline` one is an error. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub enum AnnotationKind { /// `/*: T */`, TS-style `field: T`, or `/** name: T */` doc form. diff --git a/crates/inty/src/infer/features/rows.rs b/crates/inty/src/infer/features/rows.rs index 61be5b5c..3494f3d2 100644 --- a/crates/inty/src/infer/features/rows.rs +++ b/crates/inty/src/infer/features/rows.rs @@ -238,30 +238,13 @@ impl InferState { // `this`: called on the object or taken off it. let annotated_type = crate::infer::lower::annotation_this(&annotated_type, &Type::ArrowThis); - // TypeScript's JSDoc convention is to allow a - // `null` / `undefined` placeholder initialiser - // for a `@type`-annotated field — the field is - // declared *as if* it were the annotated type - // and filled in by later assignment, exactly - // the htmx public-API pattern. We mirror that: - // when the source-side syntax is `@type` and - // the value is a literal placeholder, skip the - // subsume check. Inline `/*: T */` annotations - // and non-placeholder values still check - // normally so honest mismatches surface. - let is_placeholder = matches!( - value, - Expr::Lit { - value: Literal::Null | Literal::Undefined, - .. - } - ); - if !(is_jsdoc && is_placeholder) { - // Annotation first: it's what the user wrote, - // so the error message reads as "expected - // , found ". - self.subsume(ann_span, &value_type, &annotated_type)?; - } + // The initialiser is checked against the annotation whatever + // its form (`@type` or `/*: T */`), as a `const`'s is: a field + // declared `() => R` that starts as `null` throws when called + // before it is filled, so it is declared `(() => R) | Null`. + // Annotation first: it's what the user wrote, so the error + // reads "expected , found ". + self.subsume(ann_span, &value_type, &annotated_type)?; self.zonk(&annotated_type) } else { // Synthesis-mode object literal: widen primitive @@ -823,9 +806,7 @@ impl InferState { if let Some(ann) = type_annotation { // An inline annotation overrides the // contextual expected — we still check the - // value against the user-stated type, with - // the same JSDoc-placeholder relaxation as - // the synthesis arm (see `infer_object`). + // value against the user-stated type. let ann_span = Span::new(ann.span.start, ann.span.end); let typeof_table = self.build_typeof_table(env, &ann.content); let is_jsdoc = ann.kind == AnnotationKind::JsDoc; @@ -863,20 +844,8 @@ impl InferState { // `this`: called on the object or taken off it. let annotated_type = crate::infer::lower::annotation_this(&annotated_type, &Type::ArrowThis); - let is_placeholder = matches!( - value, - Expr::Lit { - value: Literal::Null | Literal::Undefined, - .. - } - ); - if is_jsdoc && is_placeholder { - // Skip the inner check — placeholder accepts. - self.subsume(ann_span, &annotated_type, &expected_prop_ty)?; - } else { - let value_type = self.check_expr(env, value, &annotated_type)?; - self.subsume(ann_span, &value_type, &expected_prop_ty)?; - } + let value_type = self.check_expr(env, value, &annotated_type)?; + self.subsume(ann_span, &value_type, &expected_prop_ty)?; props.insert(prop_name, self.zonk(&annotated_type)); } else { // A fresh object's field slot has the expected diff --git a/crates/inty/tests/fixtures/jsdoc_at_type_iife.js b/crates/inty/tests/fixtures/jsdoc_at_type_iife.js index 5f39288a..f05adb14 100644 --- a/crates/inty/tests/fixtures/jsdoc_at_type_iife.js +++ b/crates/inty/tests/fixtures/jsdoc_at_type_iife.js @@ -1,5 +1,5 @@ // Synthetic htmx-shape IIFE exercising JSDoc `@type {typeof helper}` -// declarations on null-initialised public-API fields. +// declarations on public-API fields filled in after the object is built. // // The pattern (from bigskysoftware/htmx@master/src/htmx.js:5-69): // @@ -14,28 +14,30 @@ // htmx.onLoad = onLoadHelper; // htmx.process = processNode; // -// Without `@type` parsing the public-API fields infer as `Null`, and -// the later assignments fail with "expected Null, found Function". -// With `@type {typeof X}` resolution + the JSDoc-placeholder rule for -// null initialisers, the public-API row is correctly typed at the -// helpers' function types, and downstream call sites type-check. +// A `null` initialiser is checked against the `@type` like any other +// (a field declared a function that is still `null` throws when +// called), so here the fields start as the hoisted helpers themselves +// and the late assignments re-fill them. `@type {typeof X}` types the +// public-API row at the helpers' function types, and downstream call +// sites type-check. (htmx's `null` fields would be declared +// `(typeof X) | Null`, and each call would test the field first.) var lib = (function() { 'use strict'; const api = { /** @type {typeof onLoadHelper} */ - onLoad: null, + onLoad: onLoadHelper, /** @type {typeof processNode} */ - process: null, + process: processNode, /** @type {typeof addClass} */ - addClass: null, + addClass: addClass, /** @type {typeof removeClass} */ - removeClass: null, + removeClass: removeClass, /** @type {typeof triggerEvent} */ - trigger: null, + trigger: triggerEvent, /** @type {typeof getConfig} */ - config: null, + config: getConfig, // Bare-form `@type T` style (htmx's `config` properties use this). /** @@ -91,7 +93,7 @@ var lib = (function() { })(); // Public-API call sites: each typed by the @type annotation on the -// corresponding field, not by the null initialiser. +// corresponding field. var x = lib.process(42); var s = lib.addClass(0, "active"); var detail = lib.trigger(0, "click", { x: 1, y: 2 }); diff --git a/crates/inty/tests/jsdoc_at_type.rs b/crates/inty/tests/jsdoc_at_type.rs index 4b658952..55587c77 100644 --- a/crates/inty/tests/jsdoc_at_type.rs +++ b/crates/inty/tests/jsdoc_at_type.rs @@ -1,26 +1,28 @@ //! JSDoc `@type` annotations on object-literal fields. //! //! `/** @type {T} */` placed immediately before a property declaration -//! attaches `T` as the field's declared type, even when the initialiser -//! is a placeholder `null` or `undefined`. This mirrors TypeScript's -//! JSDoc rule for the htmx-style public-API pattern: +//! attaches `T` as the field's declared type, and the initialiser is +//! checked against it as a `const`'s is. A field that starts as `null` +//! and is filled in later (the htmx-style public-API pattern) says so: //! //! ```js //! const htmx = { -//! /** @type {typeof onLoadHelper} */ +//! /** @type {(typeof onLoadHelper) | Null} */ //! onLoad: null, //! }; //! function onLoadHelper(cb) { /* … */ } -//! htmx.onLoad = onLoadHelper; // fills the placeholder -//! htmx.onLoad(myCallback); // typed by the @type annotation +//! htmx.onLoad = onLoadHelper; // fills it in //! ``` //! +//! (An earlier rule let a `null` initialiser stand for the annotated +//! type, so a field declared `() => R` and still `null` could be called: +//! accepted, and a TypeError at run time.) +//! //! Coverage: //! 1. Bare `@type T` and braced `@type {T}` are both accepted. //! 2. `typeof Helper` resolves to `Helper`'s inferred function type. -//! 3. A non-placeholder initialiser whose type doesn't subsume the -//! annotated type still produces an error — the placeholder -//! relaxation is narrow. +//! 3. An initialiser whose type doesn't subsume the annotated type is +//! an error, `null` and `undefined` included. //! 4. The forward-reference case (`@type {typeof Helper}` where //! `Helper` is declared later in the same scope) works because //! SCC inference hoists `Helper` before the annotated object @@ -46,19 +48,18 @@ fn check(src: &str) -> Result { #[test] fn jsdoc_at_type_braced_primitive() { - // The `@type {Number}` annotation declares `count` as Number even - // though the initialiser is `null`. The later assignment of `42` - // type-checks against the declared type, and the program's tail - // expression `o.count + 1` resolves to Number. + // The `@type {Number}` annotation declares `count` as Number. The + // later assignment of `42` type-checks against the declared type, + // and the program's tail expression `o.count + 1` is a Number. let src = r#" var o = { /** @type {Number} */ - count: null + count: 0 }; o.count = 42; o.count + 1 "#; - let ty = check(src).expect("placeholder + later assign should type-check"); + let ty = check(src).expect("@type field + later assign should type-check"); assert!(ty.contains("Number"), "got: {}", ty); } @@ -71,7 +72,7 @@ fn jsdoc_at_type_bare_form() { * @type Number * @default 0 */ - count: null + count: 0 }; o.count = 7; o.count @@ -88,12 +89,13 @@ fn jsdoc_at_type_typeof_helper() { // object literal is processed. let src = r#" const api = { - /** @type {typeof helper} */ + /** @type {(typeof helper) | Null} */ run: null }; function helper(n) { return n + 1; } api.run = helper; - api.run(10) + const run = api.run; + run ? run(10) : 0 "#; let ty = check(src).expect("typeof-helper pattern should type-check"); assert!(ty.contains("Number"), "got: {}", ty); @@ -197,7 +199,7 @@ fn jsdoc_at_type_multiline_braced_object() { * y: String * }} */ - point: null + point: {x: 0, y: ""} }; o.point = { x: 1, y: "a" }; o.point.y @@ -205,3 +207,70 @@ fn jsdoc_at_type_multiline_braced_object() { let ty = check(src).expect("multiline braced @type should type-check"); assert!(ty.contains("String"), "got: {}", ty); } + +#[test] +fn jsdoc_at_type_null_initialiser_is_checked() { + // A `@type` field's initialiser is checked as a `const`'s is: a field + // declared a function that starts as `null` would throw when called + // before it is filled in. Rosaclef's `hooks` and `inputSources`. + for init in ["null", "undefined"] { + let src = format!( + r#" + export const hooks = {{ + /** @type {{() => Undefined}} */ + mark: {init}, + }}; + /** function a() => Undefined */ + export function a() {{ hooks.mark(); }} + "# + ); + let err = check(&src).expect_err("a null-initialised function field should be rejected"); + assert!( + err.contains("() => Undefined") && (err.contains("Null") || err.contains("Undefined")), + "{init}: {err}" + ); + } + // Saying it may be nothing is accepted, and a call then needs a test. + let ok = r#" + export const hooks = { + /** @type {(() => Undefined) | Null} */ + mark: null, + }; + /** function a() => Undefined */ + export function a() { const m = hooks.mark; if (m) m(); } + "#; + check(ok).expect("`(() => Undefined) | Null` admits null"); + let called = r#" + export const hooks = { + /** @type {(() => Undefined) | Null} */ + mark: null, + }; + /** function a() => Undefined */ + export function a() { hooks.mark(); } + "#; + check(called).expect_err("calling a field that may be null is rejected"); +} + +#[test] +fn inline_field_annotation_null_initialiser_is_checked() { + // The inline form always checked its initialiser; pinned alongside. + for init in ["null", "undefined", "3"] { + let src = format!("export const o = {{ f /*: () => Undefined */: {init} }};"); + check(&src).expect_err(&format!("{init} is not a function")); + } +} + +#[test] +fn jsdoc_at_type_checked_against_an_expected_row() { + // The checking path (an object literal where an annotated type is + // expected) checks the initialiser too. + let src = r#" + /** type Hooks = {mark: (() => Undefined) | Null} */ + /** const h: Hooks */ + const h = { + /** @type {() => Undefined} */ + mark: null, + }; + "#; + check(src).expect_err("null against `() => Undefined`"); +} diff --git a/docs/jsdoc-at-type.md b/docs/jsdoc-at-type.md index 26cc1e70..371ed594 100644 --- a/docs/jsdoc-at-type.md +++ b/docs/jsdoc-at-type.md @@ -24,9 +24,10 @@ htmx.process = processNode; Each field is initialised with `null`, then JSDoc-typed to the forward-declared helper's type, then filled in by a later assignment. -TypeScript's JSDoc reader accepts this pattern. Without `@type` -support, inty infers each field as `Null` and every assignment fails -with `expected 'Null', found '(…) => …'`. +Without `@type` support, inty infers each field as `Null` and every +assignment fails with `expected 'Null', found '(…) => …'`. In inty the +`null` start is part of the field's type: `(typeof onLoadHelper) | Null` +(see "The initialiser is checked" below). The same scheme also covers the `htmx.config` sub-object, where fields use the bare `@type T` form (no braces): @@ -73,36 +74,35 @@ declarations before processing non-function statements, so `typeof helperName` succeeds even when `helperName` is declared after the annotated object literal. -### The placeholder rule +### The initialiser is checked -When the annotation kind is `@type` (JSDoc) **and** the property's -initialiser is a literal `null` or `undefined`, inty skips the usual -"value must subsume annotation" check and binds the field at exactly -the annotated type. This is the rule that makes the htmx pattern -work — `null` is a stand-in for the eventual helper, not a real -value the field is supposed to hold. - -Inline `/*: T */` annotations and non-placeholder values still -check normally: +The initialiser is checked against the annotation, whatever its form +(`@type {T}` or inline `/*: T */`) and whatever its value, exactly as a +`const`'s initialiser is checked against `/** const c: T */`: ```js -// Inline annotation: subsume check runs — error. -let x = { count /*: Number */: "oops" }; - -// JSDoc annotation with non-placeholder value: still error. -let y = { - /** @type {Number} */ - count: "oops" +// Error: expected '() => Undefined', found 'Null'. +export const hooks = { + /** @type {() => Undefined} */ + mark: null, }; +hooks.mark(); // would throw: mark is still null -// JSDoc annotation with placeholder: accepted, field typed as Number. -let z = { - /** @type {Number} */ - count: null +// Accepted: the field may be nothing, and a call tests it first. +export const hooks2 = { + /** @type {(() => Undefined) | Null} */ + mark: null, }; -z.count = 42; // type-checks +const m = hooks2.mark; +if (m) m(); ``` +An earlier version treated a literal `null` / `undefined` under `@type` +as a placeholder that stood for `T`, following TypeScript's JSDoc +reading without `strictNullChecks`. That let a field declared a function +be called while still `null`, a TypeError at run time, so it was +removed: the field says it may be `Null`. + ## Where it lives in the code | File | Change | @@ -111,7 +111,7 @@ z.count = 42; // type-checks | `crates/inty/src/lexer/scanner.rs` | `extract_jsdoc_type_tag` scans a `/** ... */` body for `@type` tags. Strips per-line `*` decoration for multi-line braced forms. Records the annotation with an empty `name` (= "attaches to the next named binding") and `JsDoc` kind. | | `crates/inty/src/parser/mod.rs` | `try_get_jsdoc_type_annotation` consumes unnamed JSDoc annotations whose span ends before the current position. `parse_property_definition` calls it before reading the property key. Inline `/*: T */` still wins when both are present. | | `crates/inty/src/infer/type_parser.rs` | `TypeOfTable` is a `HashMap` of pre-instantiated `typeof X` references. `TypeParser` consults it from `parse_primary_type` when it sees the `typeof` keyword. | -| `crates/inty/src/infer/features/rows.rs` | `build_typeof_table` pre-scans the annotation content for `typeof IDENT` substrings and instantiates each against the surrounding env. `infer_object` and `check_object` thread it through, skip the subsume check for `JsDoc` annotations with placeholder values, and downgrade parse failures to non-fatal warnings. | +| `crates/inty/src/infer/features/rows.rs` | `build_typeof_table` pre-scans the annotation content for `typeof IDENT` substrings and instantiates each against the surrounding env. `infer_object` and `check_object` thread it through, check the initialiser against the annotation, and downgrade a `JsDoc` annotation's parse failure to a non-fatal warning. | The pre-instantiation approach (rather than a closure stored in the parser) is deliberate: it sidesteps the `&mut InferState` borrow that @@ -123,8 +123,7 @@ in-parser instantiation would require. htmx doesn't use this shape (its `@type`s are all on object-literal fields). Adding it is a straight extension — `try_get_jsdoc_type_annotation` is already called at the right place; we'd just need an analogous - consume in `parse_var_declaration` and the subsume relaxation in - `infer_var`. + consume in `parse_var_declaration`. - **`@param` and `@returns` on function declarations.** These are the next-most-common JSDoc shapes htmx uses. Worth doing, but a diff --git a/docs/type-system.md b/docs/type-system.md index 6a31118c..8d8f6f15 100644 --- a/docs/type-system.md +++ b/docs/type-system.md @@ -552,16 +552,17 @@ Two forms of type annotations: - **Doc-comment statement** preceding a declaration: `/** var x: T */`, `/** function f(...) => T */`, `/** const c: T */`, `/** type Name = ... */`. - **Inline trailing comment** on a binder or parameter: `var x /*: T */ = 1`, `function f(x /*: Number */) { ... }`. -JSDoc `/** @type {T} */` is also accepted, attached to the next object-literal field. With a `null` or `undefined` initialiser, the field is declared at `T` and the initialiser is treated as a placeholder (matching TypeScript's JSDoc rule). `typeof Helper` inside `T` resolves to the value type of a binding in scope: +JSDoc `/** @type {T} */` is also accepted, attached to the next object-literal field. The field is declared at `T`, and its initialiser is checked against `T` as a `const`'s is, `null` and `undefined` included: a field that starts as `null` and is filled in later is declared `T | Null`. `typeof Helper` inside `T` resolves to the value type of a binding in scope: ```javascript const api = { - /** @type {typeof helper} */ + /** @type {(typeof helper) | Null} */ run: null }; function helper(n) { return n + 1; } api.run = helper; -api.run(5); // typed via `@type {typeof helper}` +const run = api.run; +run ? run(5) : 0; // typed via `@type {(typeof helper) | Null}` ``` See [jsdoc-at-type.md](jsdoc-at-type.md) for the full rule. diff --git a/examples/playground/features/jsdoc-at-type.js b/examples/playground/features/jsdoc-at-type.js index 9819b89a..5bf9a281 100644 --- a/examples/playground/features/jsdoc-at-type.js +++ b/examples/playground/features/jsdoc-at-type.js @@ -3,13 +3,15 @@ // A widespread JS pattern (popularised by htmx, jQuery, lodash): // declare a public-API object with `null`-initialised fields, then // fill them in with forward-declared helpers. `@type {typeof helper}` -// tells the type checker what type the field will end up holding — -// the `null` is just a placeholder, not the real value. +// tells the type checker what type the field holds. The initialiser +// is checked against it, as a `const`'s is: a field that starts as +// `null` says so, `(typeof helper) | Null`, and is tested before a +// call, since calling it before it is filled in would throw. const api = { - /** @type {typeof greet} */ + /** @type {(typeof greet) | Null} */ greet: null, - /** @type {typeof add} */ + /** @type {(typeof add) | Null} */ add: null, // The bare form `@type T` (no braces) is also accepted — @@ -30,14 +32,16 @@ function add(x, y) { return x + y; } api.greet = greet; api.add = add; -// `api.greet` is typed as `(String) => String`, not `Null`, because -// of the `@type` annotation. -var msg = api.greet("world"); -var n = api.add(1, 2); +// `api.greet` is `((String) => String) | Null`, from the `@type` +// annotation: copy it into a const and test that before calling. +const g = api.greet; +var msg = g ? g("world") : ""; +const a = api.add; +var n = a ? a(1, 2) : 0; // The `enabled` field is `Boolean` (not the literal `true`). var on = api.enabled && true; -// A non-placeholder value still type-checks against the annotation: -// uncomment the next line to see the error. -// var bad = { /** @type {Number} */ count: "oops" }; // error! +// The initialiser is checked against the annotation: uncomment the +// next line to see the error. +// var bad = { /** @type {() => String} */ f: null }; // error! From 7ed69969184f11dc255988d3a781e836768bcd17 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 23:21:08 +0000 Subject: [PATCH 2/5] A failed module's Error no longer leaks into its importers' types Rosaclef at ff8f065: about 20 errors in modules that are fine, e.g. "Property 'bank' not found in type Boolean" at keyboard.js's `if (pr && pr.bank === 128)`. The failed module's names are typed Error, and Error unified with anything without binding a variable it met: in `coll ? : undefined` the sum's value stayed a free `a`, and `pr && pr.bank === 128` (with a side unknown, both sides of && must agree) made it a Boolean, which every later use was checked against. A variable met by Error now becomes Error (inside a structure too; a row's tail is left alone), and so does what a constraint on an Error receiver would have found out (an element, a field), an index into an Error value, and an object literal spreading one (which said "expected '{}', found ''"). The trade-off: code that mixes a value from a failed module with others is checked less until that module is fixed, which is what Error already meant for member reads and calls. The unit test that pinned the old rule ("should not bind the var to Error") now pins the new one; a module test reproduces the Rosaclef shapes with a two-module program. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LCVsbkjtuBnPMGXCE1qJjr --- crates/inty/src/builtins/mod.rs | 12 ++++++ crates/inty/src/infer/features/arrays.rs | 3 ++ crates/inty/src/infer/features/rows.rs | 12 ++++++ crates/inty/src/infer/tests.rs | 27 ++++++------ crates/inty/src/infer/unify.rs | 54 +++++++++++++++++++++++- crates/inty/src/modules.rs | 53 +++++++++++++++++++++++ 6 files changed, 148 insertions(+), 13 deletions(-) diff --git a/crates/inty/src/builtins/mod.rs b/crates/inty/src/builtins/mod.rs index 57b350c9..7b525a0b 100644 --- a/crates/inty/src/builtins/mod.rs +++ b/crates/inty/src/builtins/mod.rs @@ -1093,6 +1093,18 @@ impl InferState { // type that flowed in already failed inference; making its // dependent uses fail their type-class checks too would // produce one noise diagnostic per use site. + // What the constraint would have found out about a value of an + // `Error` type (an element, a field) is `Error` too: left a free + // variable, its first use would pin it (`const c = bad[i]; + // c && c.f !== ""` made `c` a `Boolean`). + if let Some(first) = pred.types.first() { + if matches!(self.apply_subst(first), Type::Error) { + for t in &pred.types[1..] { + self.absorb_error(span, t); + } + return Ok(()); + } + } if pred .types .iter() diff --git a/crates/inty/src/infer/features/arrays.rs b/crates/inty/src/infer/features/arrays.rs index d4a467c4..5ec52b2a 100644 --- a/crates/inty/src/infer/features/arrays.rs +++ b/crates/inty/src/infer/features/arrays.rs @@ -146,6 +146,9 @@ impl InferState { // Try to resolve immediately for known types match &obj_type_resolved { + // An element of a value whose type failed (an import from a + // module with errors) is one too, as a member read's is. + Type::Error => Ok(Type::Error), Type::Array(elem_type) => { // Array indexing: immediately unify index with Number and return element type self.subsume(span, &index_type, &Type::Number)?; diff --git a/crates/inty/src/infer/features/rows.rs b/crates/inty/src/infer/features/rows.rs index 3494f3d2..77e7930f 100644 --- a/crates/inty/src/infer/features/rows.rs +++ b/crates/inty/src/infer/features/rows.rs @@ -128,6 +128,7 @@ impl InferState { // counts; the result is closed but `row_tail` may have been // set and then overridden back to Closed. let mut had_spread = false; + let mut spread_of_error = false; // The result of a spread waiting on its operand (`Spread`): the // fields written after it are merged over it, not added beside it, // since the operand may have them too. @@ -357,6 +358,13 @@ impl InferState { had_spread = true; let arg_ty = self.infer_expr(env, argument)?; let resolved = self.zonk(&arg_ty); + // A spread of a value whose type failed (an import + // from a module with errors): the object's fields + // aren't known, so the object's type is `Error` too. + if matches!(resolved, Type::Error) { + spread_of_error = true; + continue; + } if let Some(base) = waiting_spread.take() { (props, row_tail) = self.merge_written_fields(*spread_span, base, props)?; } @@ -397,6 +405,10 @@ impl InferState { } } + if spread_of_error { + self.unify(span, &shared_this, &Type::Error)?; + return Ok(Type::Error); + } if let Some(base) = waiting_spread.take() { (props, row_tail) = self.merge_written_fields(span, base, props)?; } diff --git a/crates/inty/src/infer/tests.rs b/crates/inty/src/infer/tests.rs index c361f72b..142fd807 100644 --- a/crates/inty/src/infer/tests.rs +++ b/crates/inty/src/infer/tests.rs @@ -3151,24 +3151,27 @@ fn error_unifies_with_compound_types() { } #[test] -fn error_unifies_with_flex_var_without_binding_it() { - // Specifically: unifying Error with a fresh var should not bind - // the var to Error (which would propagate Error to every site - // that shares the var). The current rule is to short-circuit - // before var_bind, leaving the var unconstrained. +fn error_unifies_with_flex_var_and_binds_it_to_error() { + // Unifying Error with a variable makes the variable Error too, as + // every operation on Error gives Error. Left free, the variable was + // pinned by its first later use, and every use after that was + // checked against a type nobody wrote (Rosaclef: an importer of a + // failed module reported "Property 'bank' not found in type + // Boolean"). Inside a structure, its variables become Error. let mut state = InferState::new(); let span = crate::span::Span::new(0, 0); let v = state.fresh_type_var(); state .unify(span, &Type::Error, &v) .expect("Error ~ Var must succeed"); - // The var should still be a free type variable, not bound to Error. let resolved = state.apply_subst(&v); - assert!( - matches!(resolved, Type::Var(_)), - "expected var to remain free, got {:?}", - resolved - ); + assert!(matches!(resolved, Type::Error), "got {:?}", resolved); + let w = state.fresh_type_var(); + let arr = Type::Array(Box::new(w.clone())); + state + .unify(span, &arr, &Type::Error) + .expect("Array ~ Error must succeed"); + assert!(matches!(state.apply_subst(&w), Type::Error)); } #[test] @@ -5459,7 +5462,7 @@ fn a_record_field_holds_a_callback() { // A property's own annotation says the same. let src = "const hooks = {\n\ /** @type {() => Undefined} */\n\ - mark: null,\n\ + mark: () => undefined,\n\ };\n\ hooks.mark = () => undefined;\n\ hooks.mark();\n\ diff --git a/crates/inty/src/infer/unify.rs b/crates/inty/src/infer/unify.rs index 7698de2e..1a29b048 100644 --- a/crates/inty/src/infer/unify.rs +++ b/crates/inty/src/infer/unify.rs @@ -153,7 +153,15 @@ impl InferState { // substitution emitted, no constraint added. // The original error has already been reported by the // recovery point; cascading is the noise we're avoiding. - (Type::Error, _) | (_, Type::Error) => Ok(()), + // A type variable met by `Error` becomes `Error` too: left + // free, the first later use would pin it, and every use + // after that would be checked against a type nobody wrote + // (`c ? errorValue : undefined` is `Undefined | α`, then + // `x && x.f === 1` made `α` a `Boolean`). + (Type::Error, t) | (t, Type::Error) => { + self.absorb_error(span, t); + Ok(()) + } // Same variable (Type::Var(v1), Type::Var(v2)) if v1 == v2 => Ok(()), @@ -480,6 +488,50 @@ impl InferState { self.extend_subst(span, TVarName::Flex(var), ty.clone()) } + /// Bind each free type variable of `t` to `Error` (see the `Error` + /// arm of `unify_impl`). A row's tail stays as it is: it stands for + /// fields, not for a type. + pub(crate) fn absorb_error(&mut self, span: Span, t: &Type) { + let t = self.zonk(t); + self.absorb_error_in(span, &t); + } + + fn absorb_error_in(&mut self, span: Span, t: &Type) { + match t { + Type::Var(TVarName::Flex(n)) => { + let _ = self.extend_subst(span, TVarName::Flex(*n), Type::Error); + } + Type::Func { + this_type, + params, + ret, + } => { + if let Some(this) = this_type { + self.absorb_error_in(span, this); + } + for p in params { + self.absorb_error_in(span, &p.ty); + } + self.absorb_error_in(span, ret); + } + Type::Row(row) => { + for entry in row.props.values() { + self.absorb_error_in(span, &entry.ty); + } + } + Type::Array(inner) + | Type::Maybe(_, inner) + | Type::Promise(inner) + | Type::Map(inner) => self.absorb_error_in(span, inner), + Type::Tuple(ts) | Type::Union(ts) | Type::Named(_, ts) => { + for t in ts { + self.absorb_error_in(span, t); + } + } + _ => {} + } + } + /// Unify an array type with a row type. /// Arrays have structural properties like `length: Number`. fn unify_array_with_row(&mut self, span: Span, elem: &Type, row: &RowType) -> UnifyResult<()> { diff --git a/crates/inty/src/modules.rs b/crates/inty/src/modules.rs index aba9b8ac..16dc11ea 100644 --- a/crates/inty/src/modules.rs +++ b/crates/inty/src/modules.rs @@ -1333,6 +1333,59 @@ mod tests { assert!(format!("{}", err).contains("circular")); } + // Rosaclef (moving to 276dd1f): an importer of a module that failed + // reported errors in its own, correct code ("Property 'bank' not found + // in type Boolean"). The failed module's names are typed `Error`, and a + // type variable that met one stayed free (`c ? bad : undefined` is + // `Undefined | α`, `bad[i]` and `bad.find(…)` a fresh `α`), so the + // first use pinned it (`x && x.f === 1` made it a `Boolean`) and every + // later use was checked against that. + #[test] + fn an_importer_of_a_failed_module_reports_nothing_of_its_own() { + let dir = tempdir(); + write_file( + dir.path(), + "store.js", + "export const state = { xs: [{ bank: 1, name: \"a\" }] };\nconst oops = 1 - \"s\";", + ); + write_file( + dir.path(), + "main.js", + r#"import { state } from "./store.js"; +/** function f(i: Number, name: String) => Number */ +export function f(i, name) { + const coll = state.xs; + const pr = coll ? coll.find((x) => x.name === name) : undefined; + if (pr && pr.bank === 128) return 38; + const c = state.xs[i]; + if (c && c.name !== "") return 1; + const d = state.xs.find((x) => x.bank === 1); + if (d && d.name !== "") return 2; + const copy = { ...state, more: [] }; + return copy.more.length; +} +"#, + ); + let mut state = InferState::new(); + let mut errors = Vec::new(); + for entry in ["store.js", "main.js"] { + let checked = check_entry( + &mut state, + crate::builtins::initial_env(), + &dir.path().join(entry), + ); + errors.extend(state.take_errors()); + if let Err(e) = checked { + if !errors.iter().any(|r| format!("{r:?}") == format!("{e:?}")) { + errors.push(e); + } + } + } + let shown: Vec = errors.iter().map(|e| format!("{e}")).collect(); + assert_eq!(shown.len(), 1, "only store.js's own error: {shown:#?}"); + assert!(shown[0].contains("String"), "{shown:#?}"); + } + // Rosaclef: a module that failed was checked again by each importer, // which failed with it (its own code unchecked), and its error was // reported once per importer. From 4f4c2dbee2b639ad3cc26b53ee499b3bb96cffb1 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 23:21:08 +0000 Subject: [PATCH 3/5] Narrow a binding written once after its write The get-or-create idiom every Map.get meets since #110, let x = m.get(k); if (!x) { x = []; m.set(k, x); } x.push(1); and the exec loop now that exec returns String[] | Null, while (m) { ...m[1]...; m = re.exec(s); } were rejected: only stable bindings are narrowed. Both write their binding exactly once after its initialiser, which gives an argument as short as the stable one: such a binding holds its first value until the write runs, and a value of the write's expression every time after. The resolver records, per binding, its one write when that is a plain x = e in the declaring function (not from a closure) and the binding is otherwise stable (Resolution::sole_write_at). Then: - after the statement x = e;, x is what every value of e's type is (truthy / not null / not undefined), from its own type; - after if (c) S without else, the write one of S's own statements and S not always exiting, x is c's false fact or that; - a while body up to its write sees the test's true facts, for a binding no closure mentions. A store into the binding is checked at its own type, not a narrowed one. Hoisted function declarations are checked before the statements around them and never see these narrowings. Tests cover the idioms Rosaclef worked around and the shapes that must stay rejected (a hoisted reader, break out of the branch, a second write, a write from a closure, a closure in the exec loop). Not done (3b): a property tested and read again (if (o.f) o.f()). A getter can give a function to the test and null to the call, and a record type doesn't say which fields are getters, so it can't be made sound; documented in type-system.md and flow-narrowing-safe.md. The Lean model narrows only by unwrap of a value bound once; this extension is listed in lean/GAPS.md with why. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LCVsbkjtuBnPMGXCE1qJjr --- CHANGELOG.md | 17 +++ crates/inty/src/ast/resolve.rs | 84 +++++++++++++- crates/inty/src/infer/env.rs | 21 ++++ crates/inty/src/infer/features/bindings.rs | 30 ++++- crates/inty/src/infer/features/control.rs | 128 ++++++++++++++++++++- crates/inty/src/infer/mod.rs | 4 +- crates/inty/src/infer/narrow.rs | 44 +++++++ crates/inty/src/infer/state.rs | 5 + crates/inty/tests/rosaclef_regressions.rs | 93 +++++++++++++++ docs/flow-narrowing-safe.md | 71 ++++++++++++ docs/type-system.md | 19 ++- lean/COVERAGE.md | 4 +- lean/GAPS.md | 1 + 13 files changed, 504 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81d56dfc..e4c3e8f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,23 @@ null` is an error ("expected '() => Undefined', found 'Null'"), where it was accepted and `hooks.mark()` threw. A field that starts as `null` is declared `T | Null`. (The `null` / `undefined` placeholder rule is gone.) +- An importer of a module that failed no longer reports errors in its own, + correct code ("Property 'bank' not found in type Boolean", "expected + 'Boolean | Undefined', found '{pattern: …}'"). The failed module's names + are typed `Error`; a type variable that met one stayed free and was + pinned by its first use. It is now `Error` too: unifying with `Error`, + indexing an `Error` value, a constraint on one and a spread of one all + give `Error`. +- A binding written once after its initialiser, by a plain `x = e` in its + own function, is narrowed after that write: the get-or-create idiom + (`let x = m.get(k); if (!x) { x = []; m.set(k, x); } x.push(1);`) and + the `exec` loop (`while (m) { …m[1]…; m = re.exec(s); }`) type-check + without a `const` copy. See + [docs/flow-narrowing-safe.md](docs/flow-narrowing-safe.md), "Extension: + bindings written once". +- A tested property read again (`if (o.f) o.f()`) is still not narrowed: + a getter can give a different value to each read, and a type doesn't say + which fields are getters. Copy it into a `const` and test that. ### Multi-line annotations diff --git a/crates/inty/src/ast/resolve.rs b/crates/inty/src/ast/resolve.rs index 1453bde6..08ad9d11 100644 --- a/crates/inty/src/ast/resolve.rs +++ b/crates/inty/src/ast/resolve.rs @@ -76,9 +76,33 @@ struct Binding { read_before_init: bool, /// How many functions enclose the declaration. level: usize, + /// Writes after the initialiser. + writes: u32, + /// A write from a function nested inside the declaring one. + nested_write: bool, + /// Where the identifier of a plain `x = e` writing it is. + plain_write: Option<(usize, usize)>, + /// An occurrence in a function nested inside the declaring one. + captured: bool, } impl Binding { + /// The one write after the initialiser, when it is a plain `x = e` + /// in the declaring function, and the binding is otherwise stable. + fn sole_write(&self) -> Option<(usize, usize)> { + let declared_once = match self.kind { + Kind::Let | Kind::Param => true, + Kind::Var => { + self.declarators + self.fn_decls <= 1 + && (!self.has_init || (!self.init_in_loop && !self.read_before_init)) + } + Kind::Const | Kind::Global => false, + }; + (declared_once && self.writes == 1 && !self.nested_write) + .then_some(self.plain_write) + .flatten() + } + fn stable(&self) -> bool { match self.kind { Kind::Const => true, @@ -97,6 +121,8 @@ impl Binding { #[derive(Clone, Debug, Default)] pub struct Resolution { stable: Vec, + sole_write: Vec>, + captured: Vec, constant: Vec, global: Vec, names: Vec, @@ -120,11 +146,15 @@ impl Resolution { w.stmt(s); } let stable = w.bindings.iter().map(Binding::stable).collect(); + let sole_write = w.bindings.iter().map(Binding::sole_write).collect(); + let captured = w.bindings.iter().map(|b| b.captured).collect(); let constant = w.bindings.iter().map(|b| b.kind == Kind::Const).collect(); let global = w.bindings.iter().map(|b| b.kind == Kind::Global).collect(); let names = w.bindings.into_iter().map(|b| b.name).collect(); Resolution { stable, + sole_write, + captured, constant, global, names, @@ -157,6 +187,25 @@ impl Resolution { .is_some_and(|id| self.stable[id as usize]) } + /// Where the identifier of the one write of the binding `name` at + /// `span` refers to is, when that binding is written once after its + /// initialiser, by a plain `x = e` in the function that declares it + /// (not from a nested function), and is otherwise stable. Such a + /// binding holds its initial value until that write runs and a value + /// the write assigned ever after, which is what lets narrowing follow + /// it (`infer::narrow`, "filled bindings"). + pub fn sole_write_at(&self, span: Span, name: &str) -> Option<(usize, usize)> { + let id = self.local_at(span, name)?; + self.sole_write[id as usize] + } + + /// Whether the binding `name` at `span` refers to occurs in a function + /// nested inside the one that declares it. + pub fn captured_at(&self, span: Span, name: &str) -> bool { + self.local_at(span, name) + .is_some_and(|id| self.captured[id as usize]) + } + /// Whether the identifier `name` at `span` refers to a binding that can't /// be assigned: a `const`, or a named function expression's own name. pub fn constant_at(&self, span: Span, name: &str) -> bool { @@ -240,6 +289,10 @@ impl Walker { init_in_loop: false, read_before_init: false, level: self.hoisted.len(), + writes: 0, + nested_write: false, + plain_write: None, + captured: false, }); (self.bindings.len() - 1) as BindingId } @@ -331,13 +384,31 @@ impl Walker { if b.kind == Kind::Var && early { self.bindings[id as usize].read_before_init = true; } + if self.hoisted.len() > self.bindings[id as usize].level { + self.bindings[id as usize].captured = true; + } self.register(span, id); id } + /// The binding the occurrence of `name` at `span` was registered as. + fn binding_of(&self, name: &str, span: Span) -> Option { + self.occurrences + .get(&(span.start, span.end))? + .iter() + .copied() + .find(|&id| self.bindings[id as usize].name == name) + } + fn write(&mut self, name: &str, span: Span) { let id = self.occurrence(name, span); - self.bindings[id as usize].assigned = true; + let level = self.hoisted.len(); + let b = &mut self.bindings[id as usize]; + b.assigned = true; + b.writes += 1; + if level > b.level { + b.nested_write = true; + } } /// Record the names an assignment target writes. @@ -856,9 +927,18 @@ impl Walker { self.expr(left); self.expr(right); } - Expr::Assign { left, right, .. } => { + Expr::Assign { + op, left, right, .. + } => { self.expr(right); self.write_target(left); + if let (crate::ast::AssignOp::Assign, Expr::Ident { name, span }) = + (op, left.as_ref()) + { + if let Some(id) = self.binding_of(name, *span) { + self.bindings[id as usize].plain_write = Some((span.start, span.end)); + } + } } Expr::Conditional { test, diff --git a/crates/inty/src/infer/env.rs b/crates/inty/src/infer/env.rs index 15b91e7d..40eeadfd 100644 --- a/crates/inty/src/infer/env.rs +++ b/crates/inty/src/infer/env.rs @@ -218,6 +218,27 @@ impl TypeEnv { out } + /// A copy with `key`'s binding at its own scheme, not narrowed. + pub fn unnarrowed(&self, key: &Key) -> Self { + let Some(b) = self.lookup_key(key) else { + return self.clone(); + }; + let Some(declared) = b.declared.clone() else { + return self.clone(); + }; + let mutability = b.mutability; + let mut out = self.clone(); + out.bindings.insert( + key.clone(), + Entry::new(Binding { + scheme: declared, + mutability, + declared: None, + }), + ); + out + } + /// `name`'s own scheme, narrowed or not. pub fn lookup_declared(&self, name: &str) -> Option<&TypeScheme> { let b = self.lookup_binding(name)?; diff --git a/crates/inty/src/infer/features/bindings.rs b/crates/inty/src/infer/features/bindings.rs index 68fbb75d..934590e2 100644 --- a/crates/inty/src/infer/features/bindings.rs +++ b/crates/inty/src/infer/features/bindings.rs @@ -301,6 +301,14 @@ impl InferState { } /// Infer the type of an assignment. + /// Remember what a plain `x = e` assigned (see `assigned_values`). + fn record_assigned_value(&mut self, op: AssignOp, left: &Expr, value: &Type) { + if let (AssignOp::Assign, Expr::Ident { span, .. }) = (op, left) { + self.assigned_values + .insert((span.start, span.end), value.clone()); + } + } + pub(in crate::infer) fn infer_assign( &mut self, env: &TypeEnv, @@ -309,6 +317,17 @@ impl InferState { right: &Expr, span: Span, ) -> InferResult { + // A variable is stored to at its own type, not one a test or an + // earlier write narrowed it to (a binding written once, see + // `after_sole_write`); its value is read in `env` as it is. + let unnarrowed; + let store_env = match left { + Expr::Ident { name, span } => { + unnarrowed = env.unnarrowed(&self.key_of(*span, name)); + &unnarrowed + } + _ => env, + }; // Check for assignment to immutable bindings self.check_assignment_target(env, left, span)?; @@ -318,21 +337,24 @@ impl InferState { if op == AssignOp::Assign && matches!(right, Expr::Function { .. }) && matches!(left, Expr::Member { .. } | Expr::Ident { .. }) - && lhs_polytype(self, env, left).is_none() + && lhs_polytype(self, store_env, left).is_none() { - let left_type = self.infer_store_target(env, left, span, true)?; + let left_type = self.infer_store_target(store_env, left, span, true)?; if crate::infer::features::functions::extract_callable(&self.zonk_head(&left_type)) .is_some() { self.check_expr(env, right, &left_type)?; + self.record_assigned_value(op, left, &left_type); return Ok(self.zonk_head_into(left_type)); } let right_type = self.infer_expr(env, right)?; self.subsume(span, &right_type, &left_type)?; + self.record_assigned_value(op, left, &right_type); return Ok(self.zonk_head_into(left_type)); } let right_type = self.infer_expr(env, right)?; + self.record_assigned_value(op, left, &right_type); // Subsumption check: if the LHS resolves to a polymorphic scheme // (a polymorphic var, or a polymorphic property of a polymorphic @@ -357,7 +379,7 @@ impl InferState { | AssignOp::LogicalAndAssign | AssignOp::LogicalOrAssign ) { - if let Some(expected) = lhs_polytype(self, env, left) { + if let Some(expected) = lhs_polytype(self, store_env, left) { let env_free_before = env.free_vars(); let (skolems, expected_ty) = self.skolemize(&expected); self.unify(span, &expected_ty, &right_type)?; @@ -387,7 +409,7 @@ impl InferState { op, AssignOp::Assign | AssignOp::NullishAssign | AssignOp::LogicalOrAssign ); - let left_type = self.infer_store_target(env, left, span, store_only)?; + let left_type = self.infer_store_target(store_env, left, span, store_only)?; // `x &&= v` and `x ||= v` test `x`, as `&&` and `||` do. if matches!(op, AssignOp::LogicalAndAssign | AssignOp::LogicalOrAssign) { self.require_testable(&left_type, span)?; diff --git a/crates/inty/src/infer/features/control.rs b/crates/inty/src/infer/features/control.rs index 1b3d38ad..1127d6da 100644 --- a/crates/inty/src/infer/features/control.rs +++ b/crates/inty/src/infer/features/control.rs @@ -9,8 +9,8 @@ use crate::types::{LitValue, Type, TypeScheme}; use super::super::env::{Mutability, TypeEnv}; use super::super::narrow::{ - apply_narrowing, is_singleton, narrowing_is_dead, path_from_expr, test_facts, typeof_fact, - Narrowing, Path, + apply_narrowing, is_singleton, narrow_from_declared, narrowing_is_dead, path_from_expr, + test_facts, typeof_fact, value_narrowing, Facts, Narrowing, Path, }; use super::super::state::InferState; use super::super::InferResult; @@ -272,6 +272,17 @@ impl InferState { env: &TypeEnv, test: &Expr, ) -> InferResult<(TypeEnv, TypeEnv)> { + let (cons_env, alt_env, _) = self.infer_branching_test_facts(env, test)?; + Ok((cons_env, alt_env)) + } + + /// [`Self::infer_branching_test`], and the facts the test gives (for + /// every binding, stable or not). + pub(in crate::infer) fn infer_branching_test_facts( + &mut self, + env: &TypeEnv, + test: &Expr, + ) -> InferResult<(TypeEnv, TypeEnv, Facts)> { let test_type = self.infer_expr(env, test)?; self.require_testable(&test_type, test.span())?; let facts = test_facts(test, &|e| self.fact_of(env, e)); @@ -294,7 +305,70 @@ impl InferState { span, )), ); - Ok((cons_env, alt_env)) + Ok((cons_env, alt_env, facts)) + } + + /// The binding `x` and where its identifier is, when `stmt` is + /// `x = e;` and that is the one write of `x` after its initialiser + /// (`resolve::Resolution::sole_write_at`). + fn sole_write_stmt<'s>(&self, stmt: &'s Stmt) -> Option<(&'s str, Span)> { + let Stmt::Expr { + expression: + Expr::Assign { + op: crate::ast::AssignOp::Assign, + left, + .. + }, + .. + } = stmt + else { + return None; + }; + let Expr::Ident { name, span } = left.as_ref() else { + return None; + }; + (self.language == SourceLanguage::JavaScript + && self.resolution.sole_write_at(*span, name) == Some((span.start, span.end))) + .then_some((name.as_str(), *span)) + } + + /// What is known of a binding after its one write (`x = e;`, see + /// [`Self::sole_write_stmt`]): what is known of every value of `e`'s + /// type. It holds from here on, on every path and in every closure: + /// the binding is written nowhere else, so whenever it changes again + /// it is to another value of `e`. (A test on it from before the write + /// no longer holds.) + pub(in crate::infer) fn after_sole_write(&mut self, env: &TypeEnv, stmt: &Stmt) -> TypeEnv { + let Some((name, span)) = self.sole_write_stmt(stmt) else { + return env.clone(); + }; + let key = self.key_of(span, name); + match self.sole_write_narrowing(span) { + Some(n) => narrow_from_declared(self, env, name, span, &n), + None => env.unnarrowed(&key), + } + } + + /// What every value the write at `span` assigns is known to be. + fn sole_write_narrowing(&mut self, span: Span) -> Option { + let value = self.assigned_values.get(&(span.start, span.end))?.clone(); + let value = self.apply_subst(&value); + value_narrowing(&value, self.language) + } + + /// The one write (see [`Self::sole_write_stmt`]) of the binding `name` + /// at `span` when it is one of `body`'s own statements (not nested in + /// another), and its index there. + fn sole_write_in(&self, body: &Stmt, name: &str, span: Span) -> Option<(usize, Span)> { + let write = self.resolution.sole_write_at(span, name)?; + let stmts = match body { + Stmt::Block { body, .. } => body.as_slice(), + s => std::slice::from_ref(s), + }; + stmts.iter().enumerate().find_map(|(i, s)| { + let (n, at) = self.sole_write_stmt(s)?; + (n == name && (at.start, at.end) == write).then_some((i, at)) + }) } /// The environment for code that runs only if `test` came out @@ -489,7 +563,7 @@ impl InferState { alternate: &Option>, span: Span, ) -> InferResult<(Type, TypeEnv)> { - let (cons_env, alt_env) = self.infer_branching_test(env, test)?; + let (cons_env, alt_env, facts) = self.infer_branching_test_facts(env, test)?; let (cons_type, _) = self.infer_stmt(&cons_env, consequent)?; @@ -516,12 +590,38 @@ impl InferState { // return node.value; // node is not null here let cons_exits = always_exits(consequent); let alt_exits = alternate.as_deref().is_some_and(always_exits); - let after = match (cons_exits, alt_exits) { + let mut after = match (cons_exits, alt_exits) { (true, false) => alt_env, (false, true) => cons_env, _ => env.clone(), }; + // Filling in a binding written once: `if (!x) { x = []; … }`. + // After it, either the test was false (and `x` not written since) + // or the branch ran to its end, past its write of `x` (the one + // write: nothing writes `x` again but another run of it). So `x` + // is what the test's false facts say, or what `e` always is, + // from here on (see `after_sole_write`): + // + // let x = m.get(k); // Number[] | Undefined + // if (!x) { x = []; m.set(k, x); } + // x.push(1); // x: Number[] + if alternate.is_none() && !cons_exits { + for (path, fact) in &facts.when_false { + let Path::Ident(name, at) = path else { + continue; + }; + let Some((_, write)) = self.sole_write_in(consequent, name, *at) else { + continue; + }; + let Some(written) = self.sole_write_narrowing(write) else { + continue; + }; + let either = Narrowing::AnyOf(vec![fact.clone(), written]); + after = narrow_from_declared(self, &after, name, *at, &either); + } + } + Ok((result, after)) } @@ -535,7 +635,23 @@ impl InferState { // The body runs while the test holds, and without a `break` the // loop is left only when it doesn't. (Narrowed bindings are never // written, so a test that held stays true for the iteration.) - let (body_env, exit_env) = self.infer_branching_test(env, test)?; + let (mut body_env, exit_env, facts) = self.infer_branching_test_facts(env, test)?; + // A binding written once, at the end of each iteration: `while + // (m) { …m[1]…; m = re.exec(s); }`. Up to its write the body sees + // what the test said: nothing writes it between the test and + // there. Not in a closure (one made there could run after the + // write), so only for a binding no closure mentions. + for (path, fact) in &facts.when_true { + let Path::Ident(name, at) = path else { + continue; + }; + if self.resolution.captured_at(*at, name) + || self.sole_write_in(body, name, *at).is_none() + { + continue; + } + body_env = narrow_from_declared(self, &body_env, name, *at, fact); + } self.infer_stmt(&body_env, body)?; let after = if breaks_out(body) { env.clone() diff --git a/crates/inty/src/infer/mod.rs b/crates/inty/src/infer/mod.rs index f364d0a6..1822a4f5 100644 --- a/crates/inty/src/infer/mod.rs +++ b/crates/inty/src/infer/mod.rs @@ -991,7 +991,7 @@ impl InferState { match checked { Ok((ty, new_env)) => { result = ty; - current_env = new_env; + current_env = self.after_sole_write(&new_env, stmt); // If this statement bound a name we pre-hoisted, // unify the hoisted variable with the actual // inferred type. The hoisted variable may already @@ -1793,7 +1793,7 @@ impl InferState { Stmt::Expr { expression, .. } => { let ty = self.infer_expr(env, expression)?; - Ok((ty, env.clone())) + Ok((ty, self.after_sole_write(env, stmt))) } Stmt::Var { diff --git a/crates/inty/src/infer/narrow.rs b/crates/inty/src/infer/narrow.rs index 0b281ac1..8ebde8c4 100644 --- a/crates/inty/src/infer/narrow.rs +++ b/crates/inty/src/infer/narrow.rs @@ -297,6 +297,50 @@ pub fn apply_narrowing( narrow_binding(state, env, path, narrowing) } +/// What every value of type `ty` (resolved) is known to be, as a +/// narrowing: truthy, not `null`, not `undefined`. `None` when nothing is +/// (a type not known yet may be anything). JavaScript only. +pub fn value_narrowing(ty: &Type, lang: Language) -> Option { + if lang != Language::JavaScript { + return None; + } + let arms = ty.arms(); + if arms.is_empty() || arms.iter().any(|t| matches!(t, Type::Var(_) | Type::Error)) { + return None; + } + let mut ns = Vec::new(); + if arms.iter().all(|t| !could_be_falsy(t, lang)) { + ns.push(Narrowing::Truthy); + } + for unit in [Type::Null, Type::Undefined] { + if arms.iter().all(|t| !could_be(t, &unit)) { + ns.push(Narrowing::IsNot(unit)); + } + } + match ns.len() { + 0 => None, + 1 => ns.pop(), + _ => Some(Narrowing::AllOf(ns)), + } +} + +/// Narrow the binding `name` (at `span`) from its own type, not from a +/// narrowing in force. For a binding written once (see +/// `resolve::Resolution::sole_write_at`), which isn't stable, so +/// [`apply_narrowing`] leaves it alone: the callers establish that the +/// fact holds from here on, on every path and in every closure. +pub fn narrow_from_declared( + state: &mut super::state::InferState, + env: &TypeEnv, + name: &str, + span: Span, + narrowing: &Narrowing, +) -> TypeEnv { + let key = state.key_of(span, name); + let env = env.unnarrowed(&key); + narrow_binding(state, &env, &Path::Ident(name.to_string(), span), narrowing) +} + /// Whether `narrowing` leaves the value at `path` no possible type: the /// test can't come out this way, so the branch it guards is dead. pub fn narrowing_is_dead( diff --git a/crates/inty/src/infer/state.rs b/crates/inty/src/infer/state.rs index 20f99934..1acdb31d 100644 --- a/crates/inty/src/infer/state.rs +++ b/crates/inty/src/infer/state.rs @@ -433,6 +433,10 @@ pub struct InferState { /// each of its handlers' events mentions the element) is compared /// again at each, which is exponential in how deeply such types nest. pub(in crate::infer) unify_bound_pairs: std::collections::HashSet<(TVarId, TVarId)>, + /// The type of the value each plain `x = e` assigned, by where its + /// `x` is: what narrowing knows about a binding written once + /// (`resolve::Resolution::sole_write_at`) after that write. + pub(in crate::infer) assigned_values: HashMap<(usize, usize), Type>, } /// One in-progress unfold the unifier is mid-recursion on. Two @@ -584,6 +588,7 @@ impl InferState { unfold_assumptions: Vec::new(), unify_depth: 0, unify_bound_pairs: std::collections::HashSet::new(), + assigned_values: HashMap::new(), } } diff --git a/crates/inty/tests/rosaclef_regressions.rs b/crates/inty/tests/rosaclef_regressions.rs index 74ac9220..d94b8b71 100644 --- a/crates/inty/tests/rosaclef_regressions.rs +++ b/crates/inty/tests/rosaclef_regressions.rs @@ -851,3 +851,96 @@ fn canvas_methods_are_called_on_their_context() { )); assert!(e.contains("detached"), "{e}"); } + +// Moving to 276dd1f, 3a: a `let` written once after its initialiser +// (`resolve::Resolution::sole_write_at`) follows that write. The +// get-or-create idiom every `Map.get` meets since #110, and the `exec` +// loop since `exec` returns `String[] | Null`. +#[test] +fn a_binding_written_once_is_narrowed_after_its_write() { + let map = "/** const m: Map */ const m = new Map();"; + for body in [ + "let x = m.get(k); if (!x) { x = []; m.set(k, x); } x.push(1);", + "let x = m.get(k); if (!x) { const y = []; m.set(k, y); x = y; } x.push(1);", + "let x = m.get(k); if (x === undefined) { x = []; } x.push(1);", + "let x = m.get(k); if (x === undefined) x = []; x.push(1);", + // A closure made after it sees it too: nothing but that write + // changes `x` again, and it writes an array. + "let x = m.get(k); if (!x) { x = []; m.set(k, x); } [1].forEach((v) => x.push(v));", + ] { + ok(&format!( + "{map} /** function f(k: String) => Undefined */ function f(k) {{ {body} }}" + )); + } + ok(" + /** function count(s: String) => Number */ + function count(s) { + const re = /a(b)/g; + let n = 0; + let m = re.exec(s); + while (m) { n += m[1].length; m = re.exec(s); } + return n; + } + "); +} + +// What would let a read see the binding before its write, or after a +// write the narrowing doesn't know of, stays rejected. +#[test] +fn a_binding_written_once_is_not_narrowed_where_it_may_not_hold() { + let decl = "/** let x: Number[] | Undefined */ let x = undefined;"; + for body in [ + // A hoisted function can run before the write. + "g(); x = []; function g() { return x.length; } return 0;", + "x = []; function g() { return x.length; } return g();", + // `break l` leaves the branch before its write. + "l: if (!x) { break l; x = []; } return x.length;", + // A second write: not written once. + "if (!x) { x = []; } x = undefined; return x.length;", + "if (!x) { x = []; } if (x.length > 1) x = undefined; return x.length;", + // A write in a nested branch, not the filling branch's own. + "if (!x) { if (x === undefined) { x = []; } } return x.length;", + ] { + err(&format!("export function run() {{ {decl} {body} }}")); + } + // Written from a closure: not written once in the declaring function. + err(&format!( + "{decl} export function run() {{ if (!x) {{ x = []; }} return x.length; }}" + )); + // A closure made in an `exec` loop's body can run after the write. + err(" + /** function run(s: String) => (() => String)[] */ + function run(s) { + const re = /a/g; + let m = re.exec(s); + const fs = []; + while (m) { fs.push(() => m[0]); m = re.exec(s); } + return fs; + } + "); + // The store is checked at the binding's own type. + err("let x = [1]; if (!x) { x = []; } const y /*: Number */ = 0; x = \"s\";"); +} + +// Moving to 276dd1f, 1: an object literal's `@type` field checks its +// initialiser, as a `const`'s annotation does. +#[test] +fn a_type_field_checks_its_null_initialiser() { + let e = err(" + export const hooks = { + /** @type {() => Undefined} */ + mark: null, + }; + /** function a() => Undefined */ + export function a() { hooks.mark(); } + "); + assert!(e.contains("() => Undefined") && e.contains("Null"), "{e}"); + ok(" + export const hooks = { + /** @type {(() => Undefined) | Null} */ + mark: null, + }; + /** function a() => Undefined */ + export function a() { const m = hooks.mark; if (m) m(); } + "); +} diff --git a/docs/flow-narrowing-safe.md b/docs/flow-narrowing-safe.md index 138cc4ce..658cd2b9 100644 --- a/docs/flow-narrowing-safe.md +++ b/docs/flow-narrowing-safe.md @@ -212,6 +212,77 @@ The same review's other findings are fixed too: any expression. - **`eval`:** a direct `eval` counts as writing every binding in scope. +## Extension: bindings written once + +Status: implemented (`Resolution::sole_write_at` in `ast/resolve.rs`, +`after_sole_write` and the `if` / `while` rules in +`infer/features/control.rs`). + +The stable-only rule rejects the get-or-create idiom, which every +`Map.get` meets now that it returns `V | Undefined`: + + let x = m.get(k); + if (!x) { x = []; m.set(k, x); } + x.push(1); + +and the `exec` loop (`while (m) { …m[1]…; m = re.exec(s); }`). Both write +their binding exactly once after its initialiser. That is enough for a +second, equally short soundness argument: + +> A binding whose only write after its initialiser is one plain `x = e` +> holds its initial value until that write runs, and a value of `e` +> every time after. + +The resolution pass counts writes per binding (every kind of write listed +above counts) and records the one write, when there is one, it is a plain +`x = e` to an identifier, and it isn't in a function nested inside the +declaring one. The binding must be otherwise stable (a `let`, a +parameter, a `var` declared once). Then, writing `W(x)` for what every +value of `e`'s type is (truthy, not `null`, not `undefined`): + +1. **After the write.** After the statement `x = e;`, `x` is narrowed to + `W(x)`, from its own type (a test from before the write no longer + holds). This holds on every later path and in every closure made from + there: the write ran, and anything that changes `x` again is the same + write. +2. **After a filling `if`.** For `if (c) S` with no `else`, where `S` does + not always exit and the write is one of `S`'s own statements (not + nested in another), after the `if` `x` is `P(x) ∨ W(x)`, `P` being what + `c`'s false branch says of `x`. Either `c` was false and nothing has + written `x` since (the only write is in `S`), or `S` ran to its end, + past its write. A labeled `if` that `S` can `break` out of keeps its + environment, as before. Every later change of `x` is the write again, + so this too holds in every closure made from there. +3. **A `while` body before its write.** For `while (c) B` where the write + is one of `B`'s own statements, `B` is checked with `c`'s true facts + about `x`: between the test and the write, nothing writes `x`. A + closure made there could run after the write, so this is done only for + a binding no closure mentions. The loop's exit facts about `x` are not + used. + +A store into such a binding is checked against its own type, never a +narrowed one (`infer_assign`). + +What is not covered: a second write (`x ||= []` and `x = x || []` are +writes too, but `x = x || []` alone is one: after it `x` is truthy), a +write inside a nested block of the filling branch, or one from a closure. +A function declaration is hoisted and checked before the statements +around it, so it never sees these narrowings. The rules are JavaScript +only. + +The Lean model has no narrowing of a written binding (its `unwrap` +narrows a copy); this extension is listed in `lean/GAPS.md`. + +## Not done: narrowing a property read twice + +`if (o.f) o.f()` and `o.f ? o.f(x) : d` read `o.f` twice. With nothing in +between, a data field gives the same value both times, but a getter +(`get f() { … }`) need not: it can give a function to the test and `null` +to the call. A record type doesn't say whether a field is a getter (an +object with one goes where `{f: T}` is expected), so narrowing the second +read can't be made sound without tracking getters in types. The `const` +copy (`const f = o.f; if (f) f();`) stays the way to write it. + ## Kept separate: field writes (#9) A discriminant fact (`shape.kind === "c"`) stays true only if no write, diff --git a/docs/type-system.md b/docs/type-system.md index 8d8f6f15..6b5df5a8 100644 --- a/docs/type-system.md +++ b/docs/type-system.md @@ -221,7 +221,7 @@ The other predicates that narrow: - `&&`, `||` and `?:`, including their right operands (`q !== null && q.x > 0`); - a disjunction of tests of one path: `if (s.kind === "row" || s.kind === "col")` narrows `s` to those two members (and `s.kind !== "row" && s.kind !== "col"` does in its `else`). -**A property's own type isn't narrowed.** A tag test picks a union's members, but `if (r.palette) use(r.palette)` with `palette: String | Null` leaves `r.palette` a `String | Null`: a record is mutable, and a call between the test and the use, or another reference to the same object, could store `null` there. Copy it into a `const` first: `const p = r.palette; if (p) use(p);` (the error says so). +**A property's own type isn't narrowed.** A tag test picks a union's members, but `if (r.palette) use(r.palette)` with `palette: String | Null` leaves `r.palette` a `String | Null`: a record is mutable, and a call between the test and the use, or another reference to the same object, could store `null` there. Copy it into a `const` first: `const p = r.palette; if (p) use(p);` (the error says so). This holds even with nothing between the test and the use (`if (o.f) o.f()`, `o.f ? o.f(x) : d`): each `o.f` is a read of its own, and a getter (`get f() { … }`) can give a function to the test and `null` to the call. An object with a getter has the same type as one with a field, so the checker can't tell the two reads give one value. A test also narrows: - the code after an `if` whose other branch always returns, throws, breaks or continues (`if (q === null) return 0; return q.x;`); @@ -229,6 +229,23 @@ A test also narrows: **Only bindings that never change are narrowed.** Narrowing applies to `const`s, parameters that the function never reassigns, and `let`s that are never reassigned. For any binding that is written after its initialiser, a test says nothing. This is Typed Racket's rule: a fact about a value holds as long as the value is the same, so a narrowing never has to end — not after an assignment, not in a loop, and not inside a closure that runs later. [docs/flow-narrowing-safe.md](flow-narrowing-safe.md) has the full argument. To narrow a variable you update, copy it into a `const` first. +**A binding written once follows its write.** A `let` (or a parameter, or a `var` declared once) that is written exactly once after its initialiser, by a plain `x = e` in the function that declares it (not from a closure), holds its first value until that write runs and a value of `e` ever after. So: + +- after the statement `x = e;`, `x` is what every value of `e`'s type is (truthy, not `null`, not `undefined`), on every path and in every closure from there; +- after `if (c) { …; x = e; … }` (no `else`, the write one of the branch's own statements), `x` is what `c`'s false branch says or what `e` always is: the get-or-create idiom types; +- in `while (c) { …; x = e; … }`, the body up to the write sees what `c` says of `x`, when no closure mentions `x`: the `exec` loop types. + +```javascript +let x = m.get(k); // Number[] | Undefined +if (!x) { x = []; m.set(k, x); } // x: Number[] after `x = []` +x.push(1); // x: Number[] + +let r = re.exec(s); // String[] | Null +while (r) { use(r[1]); r = re.exec(s); } +``` + +A store is checked at the binding's own type, never a narrowed one. + The `Array.prototype.find` builtin returns `T | undefined`, so the caller has to narrow before using the result: ```javascript diff --git a/lean/COVERAGE.md b/lean/COVERAGE.md index 270a928d..0c974637 100644 --- a/lean/COVERAGE.md +++ b/lean/COVERAGE.md @@ -46,10 +46,10 @@ named here that inty hasn't, or an inference function of inty's | `HasTypeS.index` | `infer_computed_member` | | `HasTypeS.setIndex` | `infer_assign`, `infer_store_target` | | `HasTypeS.mcall` | `infer_call` | -| `HasTypeS.unwrap` | `infer_nullish_coalesce`, `infer_branching_test`, `apply_narrowing`, `resolve_strip` (a test of a sum's truthiness, x ? e₃ : e₂, is unwrap x e₂ (x' ? e₃ : e₂)) | +| `HasTypeS.unwrap` | `infer_nullish_coalesce`, `infer_branching_test`, `infer_branching_test_facts`, `apply_narrowing`, `resolve_strip` (a test of a sum's truthiness, x ? e₃ : e₂, is unwrap x e₂ (x' ? e₃ : e₂)) | | `HasTypeS.litStr` | `check_expr` | | `HasTypeS.litBool` | `check_expr` | -| `HasTypeS.caseTag` | `infer_branching_test`, `apply_narrowing`, `test_facts`, `infer_stmt_switch` (a switch statement on a variant's tag is a chain of tests of one value's tag) | +| `HasTypeS.caseTag` | `infer_branching_test`, `infer_branching_test_facts`, `apply_narrowing`, `test_facts`, `infer_stmt_switch` (a switch statement on a variant's tag is a chain of tests of one value's tag) | | `HasTypeS.conv` | `unify`, `unroll_named` | | `HasTypeS.inj` | `check_expr`, `check_by_synthesis`, `subsume` | | `HasTypeS.drop` | `infer_discarded` | diff --git a/lean/GAPS.md b/lean/GAPS.md index 93afc2b1..9a505cf9 100644 --- a/lean/GAPS.md +++ b/lean/GAPS.md @@ -26,6 +26,7 @@ The other differences are not here: | The bitwise operators `&`, `\|`, `^`, `~`, `<<`, `>>`, `>>>`, of numbers | none | They convert a number to a 32-bit integer (`ToInt32`) first, which Lean's `Float` can't state without a bit-level model of 64-bit floating point. Their typing is that of `-` (numbers in, a number out), which the model already proves sound, so leaving them out loses nothing about the type system. | | Keyword arguments in calls (`infer_keyword_call`) | none | Python only. The model types the shared AST of the JavaScript fragment. | | Lowering from Python and Lua onto the shared AST | none | The model types the AST after lowering. Lowering is tested by running lowered programs against CPython and Lua instead ([ROADMAP.md](ROADMAP.md), "Frontends"). | +| Narrowing a binding written once after its initialiser (`let x = m.get(k); if (!x) { x = []; } x.push(1)`, the `exec` loop; [docs/flow-narrowing-safe.md](../docs/flow-narrowing-safe.md), "Extension: bindings written once") | narrowing is `unwrap`, of a value bound once; a variable that is assigned is never narrowed | Its soundness is an invariant of the store over time (the binding holds its first value, then only values of the one write), not a property of one expression's typing: stating it would need the model's typing to follow the order statements run in and which closures can run when. inty decides it syntactically (`Resolution::sole_write_at`), and the programs it accepts are those a `const` copy at each use makes the model accept. | | The Go backend's integer analysis (`crates/inty-go/src/kinds.rs`) | none | It is outside the type system: it can't reject a program, and only chooses how a number is represented. | ## Planned: implication constraints From d5cfa945c61faca882062cb3f38a18d4a2806f8d Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 23:39:24 +0000 Subject: [PATCH 4/5] Close two soundness holes in narrowing a binding written once A soundness audit of the previous commit found: - What a write's value is known to be didn't look through a named type: x = nxt() with nxt() => L, type L = Null | {v, next: L}, was taken to rule out null, so x.v was accepted. value_narrowing now unrolls a non-nominal named type through its arms, and says nothing for one that doesn't unfold or for a type not known yet. - A function declaration is hoisted, so it can run before the write; it only relied on being checked before the statements around it. It is now checked with what is known of written (non-stable) bindings dropped (hoisted_env), whatever statement inference reaches it after. Regression tests for both, and for a write inside try with a catch and a switch case entered directly (already rejected). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LCVsbkjtuBnPMGXCE1qJjr --- crates/inty/src/ast/resolve.rs | 5 +++ crates/inty/src/infer/env.rs | 18 ++++++++ crates/inty/src/infer/features/control.rs | 2 +- crates/inty/src/infer/features/functions.rs | 14 +++++++ crates/inty/src/infer/narrow.rs | 46 +++++++++++++++++++-- crates/inty/tests/rosaclef_regressions.rs | 26 ++++++++++++ docs/flow-narrowing-safe.md | 8 ++-- 7 files changed, 111 insertions(+), 8 deletions(-) diff --git a/crates/inty/src/ast/resolve.rs b/crates/inty/src/ast/resolve.rs index 08ad9d11..d4ca87c5 100644 --- a/crates/inty/src/ast/resolve.rs +++ b/crates/inty/src/ast/resolve.rs @@ -199,6 +199,11 @@ impl Resolution { self.sole_write[id as usize] } + /// Whether the binding `id` is never written after its initialiser. + pub fn is_stable(&self, id: BindingId) -> bool { + self.stable.get(id as usize).copied().unwrap_or(false) + } + /// Whether the binding `name` at `span` refers to occurs in a function /// nested inside the one that declares it. pub fn captured_at(&self, span: Span, name: &str) -> bool { diff --git a/crates/inty/src/infer/env.rs b/crates/inty/src/infer/env.rs index 40eeadfd..5018de21 100644 --- a/crates/inty/src/infer/env.rs +++ b/crates/inty/src/infer/env.rs @@ -275,6 +275,24 @@ impl TypeEnv { out } + /// A copy with the bindings `drop` picks at their own schemes. + pub fn without_narrowings_of(&self, drop: impl Fn(&Key) -> bool) -> Self { + let mut out = self.clone(); + for (key, e) in &self.bindings { + if let (Some(declared), true) = (&e.binding.declared, drop(key)) { + out.bindings.insert( + key.clone(), + Entry::new(Binding { + scheme: declared.clone(), + mutability: e.binding.mutability, + declared: None, + }), + ); + } + } + out + } + /// A copy with every binding's own scheme. pub fn without_narrowings(&self) -> Self { let mut out = self.clone(); diff --git a/crates/inty/src/infer/features/control.rs b/crates/inty/src/infer/features/control.rs index 1127d6da..d76d9505 100644 --- a/crates/inty/src/infer/features/control.rs +++ b/crates/inty/src/infer/features/control.rs @@ -353,7 +353,7 @@ impl InferState { fn sole_write_narrowing(&mut self, span: Span) -> Option { let value = self.assigned_values.get(&(span.start, span.end))?.clone(); let value = self.apply_subst(&value); - value_narrowing(&value, self.language) + value_narrowing(self, &value, self.language) } /// The one write (see [`Self::sole_write_stmt`]) of the binding `name` diff --git a/crates/inty/src/infer/features/functions.rs b/crates/inty/src/infer/features/functions.rs index d49a1e8b..043a7547 100644 --- a/crates/inty/src/infer/features/functions.rs +++ b/crates/inty/src/infer/features/functions.rs @@ -1249,11 +1249,24 @@ impl InferState { /// binding group. Every name in the group is visible in every /// body from the start, which is what enables forward references /// and mutual recursion. + /// The environment a function declaration is checked in: what is + /// known of a binding that is written (a binding written once, see + /// `after_sole_write`) is dropped, as the declaration is hoisted and + /// can be called before that write runs, whatever statement it is + /// checked after. + fn hoisted_env(&self, env: &TypeEnv) -> TypeEnv { + env.without_narrowings_of(|key| match key { + crate::infer::env::Key::Local(id) => !self.resolution.is_stable(*id), + crate::infer::env::Key::Name(_) => false, + }) + } + pub(in crate::infer) fn infer_function_group( &mut self, env: &TypeEnv, group: &[Stmt], ) -> InferResult { + let env = &self.hoisted_env(env); let mut hoisted = self.hoist_function_names(env, group)?; // Pass 1: infer every body with the full hoisted env in scope, then @@ -1721,6 +1734,7 @@ impl InferState { name_span: Option, span: Span, ) -> InferResult<(Type, TypeEnv)> { + let env = &self.hoisted_env(env); // Reuse the type variable hoisted by the enclosing scope if // this function was pre-bound there, otherwise start fresh. // Re-using the hoisted var is what lets mutually recursive diff --git a/crates/inty/src/infer/narrow.rs b/crates/inty/src/infer/narrow.rs index 8ebde8c4..23786e26 100644 --- a/crates/inty/src/infer/narrow.rs +++ b/crates/inty/src/infer/narrow.rs @@ -299,13 +299,20 @@ pub fn apply_narrowing( /// What every value of type `ty` (resolved) is known to be, as a /// narrowing: truthy, not `null`, not `undefined`. `None` when nothing is -/// (a type not known yet may be anything). JavaScript only. -pub fn value_narrowing(ty: &Type, lang: Language) -> Option { +/// (a type not known yet may be anything). A named type is looked through +/// (a recursive alias may be a union with `Null` in it), and one that +/// doesn't unfold says nothing. JavaScript only. +pub fn value_narrowing( + state: &super::state::InferState, + ty: &Type, + lang: Language, +) -> Option { if lang != Language::JavaScript { return None; } - let arms = ty.arms(); - if arms.is_empty() || arms.iter().any(|t| matches!(t, Type::Var(_) | Type::Error)) { + let mut arms = Vec::new(); + value_arms(state, ty, 0, &mut arms)?; + if arms.is_empty() { return None; } let mut ns = Vec::new(); @@ -324,6 +331,37 @@ pub fn value_narrowing(ty: &Type, lang: Language) -> Option { } } +/// The arms of `ty`, through sums, unions and non-nominal named types +/// (unrolled). `None` when an arm isn't known (a variable, `Error`) or a +/// named type doesn't unfold within a few steps. +fn value_arms( + state: &super::state::InferState, + ty: &Type, + depth: usize, + out: &mut Vec, +) -> Option<()> { + if depth > 8 { + return None; + } + match ty { + Type::Var(_) | Type::Error => None, + Type::Union(_) | Type::Maybe(..) => { + for arm in ty.arms() { + value_arms(state, &arm, depth + 1, out)?; + } + Some(()) + } + Type::Named(id, args) if !state.is_nominal_type(*id) => { + let body = state.unroll_named(*id, args)?; + value_arms(state, &body, depth + 1, out) + } + t => { + out.push(t.clone()); + Some(()) + } + } +} + /// Narrow the binding `name` (at `span`) from its own type, not from a /// narrowing in force. For a binding written once (see /// `resolve::Resolution::sole_write_at`), which isn't stable, so diff --git a/crates/inty/tests/rosaclef_regressions.rs b/crates/inty/tests/rosaclef_regressions.rs index d94b8b71..f74077f2 100644 --- a/crates/inty/tests/rosaclef_regressions.rs +++ b/crates/inty/tests/rosaclef_regressions.rs @@ -903,6 +903,32 @@ fn a_binding_written_once_is_not_narrowed_where_it_may_not_hold() { ] { err(&format!("export function run() {{ {decl} {body} }}")); } + // A write the `try` block may not reach before the `catch` runs. + err(&format!( + "export function run(c) {{ {decl} try {{ if (c) throw new Error(\"e\"); x = []; }} catch (e) {{}} return x.length; }}" + )); + // A `switch` case entered directly, not through the one that writes. + err(&format!( + "export function run(c) {{ {decl} switch (c) {{ case 1: x = []; case 2: return x.length; }} return 0; }}" + )); + // A hoisted function checked after the write (it reads a `const` + // declared after it) can still run before it. + err(&format!( + "export function run() {{ {decl} x = []; const j = 2; function g() {{ return x.length + j; }} return 0; }}" + )); + // What the write's value is, through a recursive alias with `null` in + // it: nothing (the value may be `null`). + err(" + /** type L = Null | {v: Number, next: L} */ + /** function nxt() => L */ + function nxt() { return null; } + export function run() { + /** let x: L */ + let x = {v: 1, next: null}; + x = nxt(); + return x.v; + } + "); // Written from a closure: not written once in the declaring function. err(&format!( "{decl} export function run() {{ if (!x) {{ x = []; }} return x.length; }}" diff --git a/docs/flow-narrowing-safe.md b/docs/flow-narrowing-safe.md index 658cd2b9..6667bcf6 100644 --- a/docs/flow-narrowing-safe.md +++ b/docs/flow-narrowing-safe.md @@ -266,9 +266,11 @@ narrowed one (`infer_assign`). What is not covered: a second write (`x ||= []` and `x = x || []` are writes too, but `x = x || []` alone is one: after it `x` is truthy), a write inside a nested block of the filling branch, or one from a closure. -A function declaration is hoisted and checked before the statements -around it, so it never sees these narrowings. The rules are JavaScript -only. +A function declaration is hoisted (it can run before the write), so it +is checked with what is known of written bindings dropped, whatever +statement inference reaches it after (`hoisted_env`). `W(x)` looks +through a recursive alias (`type L = Null | {…}` may be `null`) and is +empty for a type not known yet. The rules are JavaScript only. The Lean model has no narrowing of a written binding (its `unwrap` narrows a copy); this extension is listed in `lean/GAPS.md`. From 5df2f8000e25ab2f35e96e318e382fb9f747e894 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 07:20:59 +0000 Subject: [PATCH 5/5] Metamorphic: don't move a data decl to the end of the program prop_move_data_decl_after_user failed in a PROPTEST_CASES=10000 soak (on main too): when the function using the decl is the program's last statement, moving the decl after it makes the decl last, which changes the program's completion type the oracle compares ("14" vs "Undefined"). The oracle says such transforms should exclude themselves; this one now skips that case. The shrunk case is kept in the regression file. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LCVsbkjtuBnPMGXCE1qJjr --- crates/inty/tests/metamorphic.proptest-regressions | 1 + crates/inty/tests/metamorphic/transform.rs | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/crates/inty/tests/metamorphic.proptest-regressions b/crates/inty/tests/metamorphic.proptest-regressions index c714a164..adb85170 100644 --- a/crates/inty/tests/metamorphic.proptest-regressions +++ b/crates/inty/tests/metamorphic.proptest-regressions @@ -11,3 +11,4 @@ cc 746d133c827d2763c5ef4471de73c5e968ff34a465a58de6163f2203c7caddce # shrinks to cc 31dca0d0de9d8ad0d626f6b26e083fe37fc963148cc7faf9c7d62a0b240b6afc # shrinks to p = Program { statements: [FunctionDecl { name: "a", params: [], body: Block { body: [Return { argument: Some(Function { name: None, params: [], body: Block { body: [Return { argument: Some(Ident { name: "x", span: Span { start: 0, end: 0 } }), span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 } }, type_annotation: None, span: Span { start: 0, end: 0 } }), span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 } }, type_annotation: None, return_type_ast: None, span: Span { start: 0, end: 0 } }, Var { kind: Var, declarations: [VarDeclarator { name: "x", init: Some(Lit { value: Null, span: Span { start: 0, end: 0 } }), type_annotation: None, type_ast: None, kind: Var, span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 } }, Var { kind: Var, declarations: [VarDeclarator { name: "b", init: Some(Function { name: None, params: [], body: Block { body: [Return { argument: Some(Lit { value: Null, span: Span { start: 0, end: 0 } }), span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 } }, type_annotation: None, span: Span { start: 0, end: 0 } }), type_annotation: None, type_ast: None, kind: Var, span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 } }, FunctionDecl { name: "a", params: [Param { name: "x", span: Span { start: 0, end: 0 }, optional: false, default: None, type_ast: None }], body: Block { body: [Return { argument: Some(Object { properties: [Property { key: Ident("k"), value: Ident { name: "b", span: Span { start: 0, end: 0 } }, type_annotation: None, type_ast: None, span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 } }), span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 } }, type_annotation: None, return_type_ast: None, span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 }, type_aliases: [], class_brands: [], language: JavaScript } cc 9a938ee3d6211f8af32a2bca5385fb2fbced5948a3b2ef358f93419fe0038b69 # shrinks to p = Program { statements: [Expr { expression: Lit { value: Boolean(true), span: Span { start: 0, end: 0 } }, span: Span { start: 0, end: 0 } }, Var { kind: Const, declarations: [VarDeclarator { name: "a", init: Some(Function { name: None, params: [], body: Block { body: [Return { argument: Some(Lit { value: Null, span: Span { start: 0, end: 0 } }), span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 } }, type_annotation: None, span: Span { start: 0, end: 0 } }), type_annotation: None, type_ast: None, kind: Const, span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 }, type_aliases: [], class_brands: [], language: JavaScript } cc 94c177d51f886d5b6100bf56b19b2291506a6ca47c8d922871033e1457d9d115 # shrinks to p = Program { statements: [Expr { expression: Conditional { test: Lit { value: Null, span: Span { start: 0, end: 0 } }, consequent: Array { elements: [], span: Span { start: 0, end: 0 } }, alternate: Lit { value: Null, span: Span { start: 0, end: 0 } }, span: Span { start: 0, end: 0 } }, span: Span { start: 0, end: 0 } }, Expr { expression: Unary { op: Not, argument: Lit { value: Null, span: Span { start: 0, end: 0 } }, span: Span { start: 0, end: 0 } }, span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 }, type_aliases: [], class_brands: [], language: JavaScript } +cc e7a3adf858654c69821104c23aa222a4408535e80ef45c9d9cf52eb618b6ca23 # shrinks to p = Program { statements: [Var { kind: Var, declarations: [VarDeclarator { name: "x", init: Some(Lit { value: Null, span: Span { start: 0, end: 0 } }), type_annotation: None, type_ast: None, kind: Var, span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 } }, Expr { expression: Lit { value: Boolean(true), span: Span { start: 0, end: 0 } }, span: Span { start: 0, end: 0 } }, FunctionDecl { name: "y", params: [], body: Block { body: [Return { argument: Some(Conditional { test: Lit { value: Null, span: Span { start: 0, end: 0 } }, consequent: Lit { value: Null, span: Span { start: 0, end: 0 } }, alternate: Ident { name: "x", span: Span { start: 0, end: 0 } }, span: Span { start: 0, end: 0 } }), span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 } }, type_annotation: None, return_type_ast: None, name_span: None, span: Span { start: 0, end: 0 } }], span: Span { start: 0, end: 0 }, type_aliases: [], class_brands: [], class_decls: [], language: JavaScript } diff --git a/crates/inty/tests/metamorphic/transform.rs b/crates/inty/tests/metamorphic/transform.rs index e76eff07..6e65789f 100644 --- a/crates/inty/tests/metamorphic/transform.rs +++ b/crates/inty/tests/metamorphic/transform.rs @@ -470,6 +470,12 @@ pub fn t_move_data_decl_after_first_user(p: &Program) -> Option<(Program, Compar if !is_fn_decl { continue; } + // Moving the decl after the program's last statement would + // change which statement is last, and so the program's type + // (its completion value), which the oracle compares. + if fn_idx + 1 == n { + continue; + } let refs = referenced_names_in_stmt(fn_stmt); if !refs.contains(&name) { continue;