Skip to content

Rust: main() body has no static-type scope, so string joins and int-to-long widening fail to compile there (E0308/E0277) #338

Open

Description

@matt-edmondson

What's wrong

Every Standard generator except Rust opens a static-type scope around an EntryPoint body:

  • Go: GoGenerator.cs:2136
  • C: CGenerator.cs:1205
  • C++: CppGenerator.cs:808
  • Python: PythonGenerator.cs:855
  • JavaScript: JavaScriptGenerator.cs:734

RustGenerator.GenerateEntryPoint (RustGenerator.cs L2142-L2181) never calls OpenStaticTypeScope(entryPoint); Rust only opens one for functions (L1101). RegisterStaticType does nothing when no scope is open, so no local inside fn main(), or inside the run helper generated for an exit-code entry point, has a known type.

Two Rust fixes depend on knowing those types: the format! string join (429d953, #240) and explicit as i64 widening (PR #329, #238). Neither applies inside an entry point.

Reproduction

var ep = new EntryPoint();
ep.Body.Add(new VariableDeclaration("name", "string", new LiteralExpression<string>("World")));
ep.Body.Add(new VariableDeclaration("bang", "string", new LiteralExpression<string>("!")));
ep.Body.Add(new VariableDeclaration("greeting", "string", new BinaryExpression(new VariableReference("name"), BinaryOperator.Add, new VariableReference("bang"))));
ep.Body.Add(new VariableDeclaration("small", "int", new LiteralExpression<int>(2)));
ep.Body.Add(new VariableDeclaration("big", "long", new LiteralExpression<long>(5000000000L)));
ep.Body.Add(new VariableDeclaration("sum", "long", new BinaryExpression(new VariableReference("big"), BinaryOperator.Add, new VariableReference("small"))));

The Rust output contains:

let mut greeting: String = (name + bang);   // E0308
let mut sum: i64 = (big + small);           // E0277 cannot add `i32` to `i64`

rustc --edition 2021 rejects it. The same statements inside a FunctionDeclaration produce format!("{}{}", name, bang) and (big + (small as i64)), which compile. Go produces int64(small) for the same entry point.

Suggested fix / acceptance criteria

  • RustGenerator.GenerateEntryPoint opens OpenStaticTypeScope(entryPoint) around the body in both the plain main branch and the exit-code run branch, as GoGenerator does.
  • When AcceptsArguments is set, args is bound as Vec<String>.
  • RustGeneratedSourceCompilesTests compiles an entry point that joins two String locals and adds an int local to a long local.

Activity

  1. matt-edmondson commented on Oct 25, 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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions