Skip to content

fix(hy-check): reject fmt.eprint* when os is shadowed (fixes #3598) - #3614

Merged
limitlessprojects merged 3 commits into
limitlessprojects:mainfrom
dextermos-dev:fix/issue-3598-fmt-eprint-os-shadowing
Nov 4, 2026
Merged

limitlessprojects merged 3 commits into
limitlessprojects:mainfrom
dextermos-dev:fix/issue-3598-fmt-eprint-os-shadowing

Conversation

@dextermos-dev

Copy link
Copy Markdown
Contributor

Overview

Fixes #3598.

Under GUARANTEES.md Row 13 (BUILDABLE), programs accepted by hy-check must emit buildable Go code.
In hy-emit (crates/hy-emit/src/expr.rs), fmt.eprintln, fmt.eprint, and fmt.eprintf lower to fmt.Fprint*(os.Stderr, ...).

When os is shadowed by an in-scope parameter, local binding, loop variable, closure argument, generic type parameter, or package-level declaration (const, fn, struct, enum, type alias), the emitted Go code resolves os.Stderr to the shadowing entity, causing downstream go build to fail with os.Stderr undefined.

Changes

  1. crates/hy-check/src/buildable.rs:

    • In check_call_operands, when checking calls to fmt.eprint, fmt.eprintln, and fmt.eprintf:
      • Inspect whether os is shadowed using is_os_shadowed().
      • Reject with HYB-E1002:
        cannot call fmt.<name> because os is shadowed by an in-scope declaration (HYB-E1002)
    • Implement is_os_shadowed(&self) -> bool covering:
      • Lexical scopes: function parameters, receiver bindings, local bindings (:=, let, var), loop variables, closure arguments.
      • Generic function type parameters (self.fn_type_params.contains("os")).
      • File-level / package-level declarations (self.ck.pkg_names.contains_key("os"), self.file_consts, self.file_untyped_consts).
      • Kind scopes / constant lookups (self.lookup_kind("os")).
  2. crates/hy-check/tests/issue_3598_eprint_os_shadowing.rs:
    Comprehensive test suite with 15 test cases covering:

    • Parameter os string -> rejects HYB-E1002
    • Local os := "darwin" -> rejects HYB-E1002
    • Parameter os int on eprintf -> rejects HYB-E1002
    • Package const os = "linux" -> rejects HYB-E1002
    • Package function fn os() {} -> rejects HYB-E1002
    • Package struct struct os { version int } -> rejects HYB-E1002
    • Package enum enum os { linux, darwin } -> rejects HYB-E1002
    • Package type alias type os = string -> rejects HYB-E1002
    • Generic type parameter fn test[os any]() -> rejects HYB-E1002
    • Method receiver fn (os *machine) report_error() -> rejects HYB-E1002
    • Closure parameter cb := fn(os string) -> rejects HYB-E1002
    • Loop variable for os in items -> rejects HYB-E1002
    • Unshadowed fmt.eprint* -> cleanly accepted
    • Valid import "os" with fmt.eprintln -> cleanly accepted
    • Popped inner block { os := ... } with outer fmt.eprintln -> cleanly accepted without false positives

Verification

cargo test -p hy-check --test issue_3598_eprint_os_shadowing
# 15 passed; 0 failed

cargo test -p hy-check
# 180 passed; all integration test suites passed; 0 regressions

Settlement Wallet (Base L2 USDC):
0x8366bCe3a2D379De7656D7A67015789FaF999f20

@limitlessprojects
limitlessprojects merged commit 70c4f08 into limitlessprojects:main Nov 4, 2026
3 checks passed
@limitlessprojects

Copy link
Copy Markdown
Owner

Landed in 70c4f08 via merge --no-ff. Verification (FULL battery, ALL-GREEN): merge gate clean vs bfe8a14; cargo test --workspace 411/411 suites ok — new issue_3598_eprint_os_shadowing covers param/local/package-const/package-fn os shadow rejects across eprint/eprintln/eprintf plus accept controls; corpus harness 135 files, tripwire not fired; differential loop FULL AGREEMENT 45/45 accept-side + 90/90 reject-side.

Before landing I audited is_os_shadowed() against false-positive risk: all six probes (scopes, fn_type_params, lookup_kind → kind_scopes+file_consts, pkg_names package-level declarations, file_consts, file_untyped_consts) are genuine shadow sources and none consult the import table, so import "os" + fmt.eprintln stays clean. The guard is also correctly gated on lookup_kind("fmt").is_none() so a locally shadowed fmt doesn't reach this path as a package call. Fixes #3598.

One honest note: the merge commit message lost two short fragments (an os mention and an import phrase) to shell quoting — the tree, verification evidence, and CREDIT are unaffected; full evidence lives here.

Bounty: tracked on BOUNTY.md / discussion #295 rails. CREDIT: @dextermos-dev

limitlessprojects pushed a commit that referenced this pull request Nov 6, 2026
…et/post, pin E11 ? propagation (fixes #3789) (bradwue313)

buildable.rs: httptest.get/post calls now reject when `strings` or `http` is shadowed by an in-scope
declaration (HYB-E1002 at the call span, is_pkg_symbol_shadowed) — both lower to strings.TrimSuffix /
http.NewRequest (Row 13 BUILDABLE, following the #3598/#3614/#3727 shadow-package pattern). New suite
shadowed_httptest_and_e11_handler_3789 (7 tests) also pins the existing E11 ? propagation behavior:
shadowed `http` or response-writer param at a ? site inside http.handler rejects with HYB-E2011;
unshadowed positive control accepts.

In-merge fixture fixup (credited): the PR's patch replaced 10 UTF-8 arrows in unrelated doc comments
(8 x U+2192, 2 x U+2194 -> literal ?) and appended a stray EOF blank line; all restored byte-identical
to prior main inside this merge. Net change: +25 check lines + 85-line test file only.

Verification (battery on trial-merge worktree vs main @ 973453c): merge-gate CLEAN; cargo test
--workspace 457 suites ok; corpus 135/135 tripwire not fired; differential 45/45 accept + 90/90
reject FULL AGREEMENT.

CREDIT: @bradwue313
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:land-candidate Shepherd: fresh, gate-clean, verifiable (set from classify()) bot:stale-base Shepherd: behind main beyond threshold

Projects

None yet

2 participants