Skip to content

C/C++: long.MinValue is written as -9223372036854775808LL, which gcc rejects, and int.MinValue becomes a long #247

Closed

Description

@matt-edmondson

What happens

CFamilyGenerator.FormatLong appends LL to the decimal digits (CFamilyGenerator.cs#L215). C and C++ have no negative integer literals. -9223372036854775808LL is unary minus applied to 9223372036854775808LL, and that does not fit in long long. gcc reports "integer constant is so large that it is unsigned". The diagnostic is an error under -std=c11 -pedantic-errors, and under -Werror for C++. Those are the flags the CGeneratedSourceCompilesTests / CppGeneratedSourceCompilesTests harnesses already use. The literal reaches FormatLong from LanguageGeneratorBase.cs#L180-L182.

int.MinValue has a related problem. It is written as a bare -2147483648 (LanguageGeneratorBase.cs#L164-L166). In C and C++, 2147483648 is a long, so the negated value is a long, not an int. An inferred C++ local (auto y = -2147483648;) then deduces long. That is the same width drift #151 fixed for other literals.

C#, Rust, Go, Python and JavaScript all handle the minimum values. LongLiteralTests covers long.MinValue only for JavaScript (#168).

Reproduction (at 440ef35)

var f = new FunctionDeclaration("lowest") { ReturnType = "long" };
f.Body.Add(new VariableDeclaration("v", "long", new LiteralExpression<long>(long.MinValue)));
f.Body.Add(new ReturnStatement(new VariableReference("v")));
new CGenerator().Generate(f);   // long long v = -9223372036854775808LL;
new CppGenerator().Generate(f); // long long v = -9223372036854775808LL;
gcc -std=c11 -Wall -Wextra -pedantic-errors -c gen.c
gen.c:3:20: error: integer constant is so large that it is unsigned
g++ -std=c++20 -Wall -Wextra -pedantic -Werror -c gen.cpp
gen.cpp:3:20: error: integer constant is so large that it is unsigned [-Werror]

Suggested fix / acceptance criteria

  • C and C++ write long.MinValue as (-9223372036854775807LL - 1), or as LLONG_MIN/INT64_MIN with the header added through RequiredImports.
  • C and C++ write int.MinValue as (-2147483647 - 1), or INT_MIN, so it stays an int.
  • LongLiteralTests gains C and C++ cases for long.MinValue and int.MinValue, and the generated source compiles under the existing harness flags.

Activity

  1. added
    bug
    on Oct 18, 2026
  2. matt-edmondson commented on Oct 18, 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

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions