Skip to content

constant that overflows its declared fixed-width type is accepted, emitted Go fails: cannot use 300 as int8 value (overflows) #148

Closed

Description

@anYoneo

Summary

A narrowing constant assignment passes hyb check (0 diagnostics) and the emitted Go cannot be built: overflows.

fn main() {
    var n int8 = 300
    fmt.println("{n}")
}
$ hyb check f.hy
f.hy: OK (0 diagnostics)

$ hyb build f.hy --emit-go && go vet ./...
./a.out.go:8:15: cannot use 300 (untyped int constant) as int8 value in variable declaration (overflows)

The conversion form is unaffected when its operand is a variable — that is the runtime truncation path and it works correctly:

fn main() {
    x := 300
    y := byte(x)      // ok: runtime truncation
    fmt.println("{y}") // 44
}

So the checker treats the two shapes differently in Go's eyes but identically in its own: a constant initializer that cannot fit the declared type is a compile error in Go, and nothing in hyb check notices.

Searched for duplicates

repo:limitlessprojects/Go-Hybrid overflow → 0 hits. No issue or PR mentions constant-overflow at a declared type.

Suggested fix

Two options, both consistent with existing behaviour:

  1. Checker diagnostic (preferred): when a local's declared type is a fixed-width numeric and its initializer is a constant literal, range-check the literal against the type and report a new code (HYB-E3xxx). This matches how the checker already reasons about the declared contract rather than the emitted Go.
  2. Emitter refusal in the same shape as unbound_crate_call_refused_honestly (an honest "this cannot be represented" message rather than an emission that dies later).

Option 1 gives the author the diagnostic they need at the point of the mistake.

Environment

Commit f99d6d0 (main at the time of writing), Go 1.26.9 (the pinned version).

Activity

  1. added 2 commits that reference this issue on Oct 17, 2026
    0671868
    d897142
  2. added a commit that references this issue on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions