Skip to content

fix(hy-check): reject shadowed lowering packages os, filepath, and fmt in fmt.print and main wrapper (Row 13 BUILDABLE) - #3727

Merged
limitlessprojects merged 1 commit into
limitlessprojects:mainfrom
seenorht:fix/shadowed-lowering-os-filepath-soundness
Nov 6, 2026
Merged

limitlessprojects merged 1 commit into
limitlessprojects:mainfrom
seenorht:fix/shadowed-lowering-os-filepath-soundness

Conversation

@seenorht

@seenorht seenorht commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Under GUARANTEES.md Row 13 (BUILDABLE):

Any program accepted by hyb check must emit Go code that compiles under go build.

Following the maintainer's observation on #3598 (where shadowing os caused fmt.eprint* lowering to fmt.Fprint*(os.Stderr, ...) to fail downstream compilation), there are two other lowering paths in hy-emit that inject unqualified package calls without import guarding in hy-check:

  1. fmt.print on byte slices ([]byte):
    In hy-emit/src/expr.rs:1555:
    fmt.print on a byte buffer lowers to os.Stdout.Write(b).
    When os is shadowed by an in-scope parameter, local binding, loop variable, closure argument, or package-level symbol, emitted Go produces os.Stdout undefined or type mismatch under go build.

  2. fn main() with error return or ? operator:
    In hy-emit/src/lib.rs:1545:
    When main returns an error or contains ?, hy-emit wraps the main function and injects:

    • fmt.Fprintln(os.Stderr, filepath.Base(os.Args[0])+":", hyErr)
    • os.Exit(1) or os.Exit(code)
      When os, filepath, or fmt is shadowed in-scope by a package-level declaration, parameter, or constant, emitted Go fails downstream compilation because os.Exit, os.Stderr, filepath.Base, or fmt.Fprintln resolves to the shadowing user symbol.

Both patterns were accepted by hy-check with Verdict::Accept and failed downstream go build.

Reproducer Matrix

Source Pattern hy-check before Emitted Go Lowering go build hy-check after
var os int = 0; fn main() { b := "hi".bytes(); fmt.print(b) } Accept os.Stdout.Write(b) ❌ os.Stdout undefined 🛑 Reject (HYB-E1002)
var os int = 0; fn main() error { return errors.new("err") } Accept fmt.Fprintln(os.Stderr...); os.Exit(1) ❌ os.Stderr undefined 🛑 Reject (HYB-E1002)
var filepath string = ""; fn main() error { return errors.new("err") } Accept filepath.Base(os.Args[0]) ❌ cannot call non-function filepath.Base 🛑 Reject (HYB-E1002)
var fmt int = 1; fn main() error { return errors.new("err") } Accept fmt.Fprintln(...) ❌ fmt.Fprintln undefined 🛑 Reject (HYB-E1002)
var os int = 0; fn main() { _ = os.open("x")? } Accept os.Exit(1) ❌ os.Exit undefined 🛑 Reject (HYB-E1002)

Fix

In compiler/crates/hy-check/src/buildable.rs:

  1. fmt.print byte slice check:
    Extended the built-in call check on pkg == "fmt" to inspect name == "print" with single slice-of-byte argument when is_os_shadowed(), emitting HYB-E1002.
  2. fn main() lowering check:
    In fn_decl, when name == "main" and the function returns an error/exit code or its body contains ? (main_needs_wrapper), check is_os_shadowed(), is_filepath_shadowed(), and is_fmt_shadowed(). If any lowering package is shadowed, emit HYB-E1002 diagnostic explaining that the error wrapper requires access to os, filepath, and fmt.
  3. Added helper methods is_pkg_symbol_shadowed, is_filepath_shadowed, and is_fmt_shadowed.
  4. Added AST traversal helpers (block_has_try, stmt_has_try, place_has_try, expr_has_try) to detect ? inside fn main().

Verification

  • New hermetic test suite added: compiler/crates/hy-check/tests/shadowed_lowering_os_filepath_soundness.rs covering:
    • 10 negative reject controls for shadowed os, filepath, and fmt in fmt.print(bytes) and error-returning / ?-using fn main().
    • 5 positive accept controls ensuring normal unshadowed fmt.print(bytes), normal error-returning fn main(), and non-error fn main() with local os variable pass cleanly.
  • Preflight hygiene:
    • tools/check_conflict_markers.py: OK (none)
    • tools/check_test_registration.py: OK (38 test files, 35 discovery patterns)
    • tools/check_guarantees.py: OK (table, registry, corpus and doc agree)

Bounty payout coordinate (BOUNTY.md / discussion #295):
USDC (Base): 0x2fbD90378ACb304b4c474B433bf1Ea041EcB4290

@seenorht
seenorht force-pushed the fix/shadowed-lowering-os-filepath-soundness branch 13 times, most recently from 429ab60 to 37ceeab Compare November 5, 2026 12:22
@github-actions github-actions Bot added the bot:land-candidate Shepherd: fresh, gate-clean, verifiable (set from classify()) label Nov 5, 2026
@seenorht
seenorht force-pushed the fix/shadowed-lowering-os-filepath-soundness branch 4 times, most recently from e5e47f8 to 8413b8a Compare November 5, 2026 20:16
…t in fmt.print and main wrapper (Row 13 BUILDABLE)
@seenorht
seenorht force-pushed the fix/shadowed-lowering-os-filepath-soundness branch from 8413b8a to 821020f Compare November 5, 2026 20:17
@limitlessprojects
limitlessprojects merged commit 1c5e1ca into limitlessprojects:main Nov 6, 2026
3 checks passed
@limitlessprojects

Copy link
Copy Markdown
Owner

Landed: merged to main as the merge of 821020f1 ✅

Thank you @seenorht — the main-wrapper shadowing class is closed. Battery on your head (already contained main; tree asserted pre-commit):

Gate Result
Merge gate clean, head tree 88e2da18
cargo test --workspace 440 suites ok, 0 failed
Corpus harness verdict agreement 135/135, tripwire not fired
Differential loop FULL accept-side 45/45 build+run, reject-side 90/90

What landed

  • fn main needing the error/exit wrapper (non-empty results or try in body) now rejects when os / filepath / fmt are shadowed — the wrapper lowers to os.Exit / os.Stderr / filepath.Base / fmt.Fprintln, and none of those exist under a shadowed name.
  • fmt.print byte-slice/[]u8 argument soundness via slice_element_type_name, io.read_all byte return via Field callee, Try operand element typing.
  • 271-line pinning suite shadowed_lowering_os_filepath_soundness.rs.

The shadow-rename fixits make each rejection actionable; the wrapper-need gate (results or block_has_try) keeps the check from over-firing on plain mains.

Bounty: recorded per BOUNTY.md / discussion #295.

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())

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants