Skip to content

Go maps the AST's int to Go's 64-bit int, so shifts and overflow give different values than every other target #422

Open

Description

@matt-edmondson

What's wrong

Coder/Languages/GoGenerator.cs:139 maps { "int", "int" }. Go's int is 64 bits on every platform Go targets in practice, so a Go int is not the AST's int. Every other generator treats int as 32 bits:

  • C# int
  • C/C++ int
  • Rust i32 (RustGenerator.cs:71)

The Go table is also inconsistent with itself: uint → uint32, short → int16 and long → int64 all name an explicit width, and only int does not.

Failure scenario

The AST int wrap(int x) { return (x << 31) >> 31; } is emitted as:

func wrap(x int) int { return ((x << 31) >> 31) }

wrap(1) prints 1 under go run. The same AST gives -1 in C#, C (gcc), C++ (g++), Rust and JavaScript. Overflow differs the same way: int.MaxValue + 1 wraps to int.MinValue everywhere except Go, where it becomes 2147483648. Both programs compile, so the Go output computes a different value with no error.

Suggested fix / acceptance criteria

  • Map int to int32 in the Go type table.
  • Check that the generator's existing mixed-width conversion logic (the code that already handles int16/uint32/int64 operands) inserts the conversions now needed where an int32 meets an untyped constant, a len(...) result or an index expression. Examples: slice indexing and len return Go int, so int32(len(xs)) / xs[int(i)] may be required.
  • Add a test that (x << 31) >> 31 with x = 1 evaluates to -1 in the Go output, and a test for int.MaxValue + 1 wrapping.

Related but distinct: #362 (shift typed as the wider operand).

Activity

  1. added
    bug
    on Oct 31, 2026
  2. matt-edmondson commented on Oct 31, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions