Skip to content

fix(hy-check): reject typed int variables in return statements and channel sends for float targets (fixes #2575) - #2587

Open
Dimank92 wants to merge 1 commit into
limitlessprojects:mainfrom
Dimank92:fix/issue-2575-return-and-chan-send-float-int-soundness
Open

Dimank92 wants to merge 1 commit into
limitlessprojects:mainfrom
Dimank92:fix/issue-2575-return-and-chan-send-float-int-soundness

Conversation

@Dimank92

Copy link
Copy Markdown
Contributor

Summary

Fixes #2575.

This PR addresses soundness issues in hy-check where typed int variables bypassed type checking when returned from functions declaring float64 return types and when sent to channels of type chan float64.

Soundness Analysis & Defect Resolution

  1. Return Statements:

    • In report_return_conflict, kinds_conflict(want, got) alone permits int values to be returned for float64 result types because kinds_conflict(Float, Int) evaluates to false.
    • We introduce val: &Expr to report_return_conflict and check int_float_conflict = *want == OpKind::Float && *got == OpKind::Int && !self.is_untyped_int_const(val).
    • If a typed int variable (e.g. var i int = 1; return i) is returned in single or multi-return positions, it is strictly rejected with HYB-E1002.
  2. Channel Send Statements:

    • In Stmt::Send and SelectArmKind::Send, sending to chan float64 previously omitted primitive element assignability checks, allowing typed int variables to bypass HYB-E3011.
    • We enforce check_chan_send_operand_kinds, rejecting typed integer variables sent to chan float64 with HYB-E1002.
  3. Untyped vs Typed Constants:

    • Untyped integer constants (1, const k = 1; return k, ch <- k, k + 1) remain legal and accepted per Go semantics.
    • Crucially, unlike naive checks that accept any const identifier, we track is_untyped on Define and file_untyped_consts on Collector. This ensures typed constants (e.g. const k int = 1; return k / ch <- k) are properly rejected in compliance with the Go language specification.

Verification

  • Added test suite in compiler/crates/hy-check/tests/return_and_send_float_int_soundness.rs:
    • single_return_typed_int_to_float_is_rejected
    • multi_return_typed_int_to_float_is_rejected
    • channel_send_typed_int_to_float_chan_is_rejected
    • select_send_typed_int_to_float_chan_is_rejected
    • untyped_int_constants_in_return_and_send_are_accepted
    • typed_int_constants_in_return_and_send_are_rejected
  • Full test suite passing (178/178 unit tests green).

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

Labels

bot:conflict Shepherd: merge conflicts vs main bot:stale-base Shepherd: behind main beyond threshold

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Soundness: typed int variables bypass type checking in return statements and channel sends for float targets

1 participant