Skip to content

int passed where a string is expected is silently converted to a rune: print(65) emits "A" #709

Closed

Description

@David-Noble-at-work

A Starlark caller passing an integer where a provider method declares a string gets the rune at that code
point
, silently. print(65) emits A. print(42) emits *.

Measured directly against op.Convert:

Convert(int64(65) -> string) = "A", err=<nil>
Convert(int64(42) -> string) = "*", err=<nil>

Mechanism

pkg/op/convert.go:173:

if elem.Type().ConvertibleTo(target) {
    return elem.Convert(target).Interface(), true
}

In Go, an integer type is convertible to string, and the conversion yields the character with that code
point rather than the decimal text. reflect.Value.Convert performs exactly the same conversion as string(i).

go vet's stringintconv analyzer exists to catch this, and it cannot see through reflection — so the vet
gate we run passes over it.

Why it matters

It is silent. There is no error, no warning, and the resulting string is plausible-looking garbage. A script
author writing note(count) instead of note(str(count)) gets a control character, not a message, and
nothing in the output says why.

It applies to every provider method with a string parameter, not one provider. float behaves the same
way, and a bool is correctly rejected — bool value is neither assignable nor convertible to string — which
makes the int case look even more like intended behavior by contrast.

Found via

Adding e2e coverage for #708. The fixture asserted print(42) fails with expected string, got int, which is
what toGoScalar's starlark.AsString path would produce. It does not fail: dispatch reaches op.Convert
first, and that path converts. Two conversion routes disagree about whether an int is a string, and the
permissive one wins.

Note this also means #708's design doc, commit message, and PR body assert the wrong behavior for print(42).
Correcting there.

Proposal

Exclude integer and float kinds from the string target in tryConvertible, so they fall through to the
"neither assignable nor convertible" error that bool already gets. A caller wanting text writes str(x),
which is Starlark's own rendering and correct by construction.

Worth checking whether any current caller depends on the rune behavior. Nothing should.

Acceptance

  • print(65) reports a type error rather than emitting A
  • str(65) still works and yields "65"
  • The same holds for every provider method with a string parameter, not just ui
  • A test pins it, and is proved to fail against the current behavior

Plan and design documents

Kind Document
Plan docs/plans/lossless-conversions.md

Activity

  1. David-Noble-at-work commented on Aug 27, 2026

    @David-Noble-at-work
    ContributorAuthor

    Scoped, and it is four bugs sharing one mechanism rather than one. Plan: docs/plans/lossless-conversions.md.

    Measured against op.Convert:

    Conversion Result What happened
    int64(65) → string "A" the rune at that code point
    int64(300) → int8 44 wraparound
    int64(-1) → uint8 255 sign reinterpretation
    float64(3.9) → int 3 truncation

    All four silent, all four from the same ConvertibleTo branch in convertDirect.

    Correction to this issue as filed: it says float behaves the same way as int for string targets. It does not — float64 → string is rejected, because Go only permits integer types to convert to string. bool → string is likewise rejected. That contrast is exactly what makes the integer case read as deliberate.

    The fix cannot be a blanket rule by kind. file.mkdir(mode=0o644) arrives as a Starlark int and must reach os.FileMode, a uint32. Rejecting int→uint32 outright breaks it. The question is not which types are involved — it is whether this value survives the trip.

    Proposed rule: a numeric conversion is allowed exactly when it round-trips. Convert, convert back, compare. That covers three of the four rows and keeps 0o644 → uint32 working, since it round-trips exactly.

    Integer → string is rejected outright rather than round-trip tested — 65 → "A" → 65 would pass a naive round-trip and is still wrong. Nobody passing 65 to a string parameter means "A"; str() exists and renders correctly by construction.

    Two open questions in the plan: whether int64 → float64 should get the same treatment (it loses precision for very large integers), and confirming the guard belongs in convertDirect, where it would also govern slice elements, map values, and struct fields — which is the intent, but worth stating rather than discovering.

  2. David-Noble-at-work commented on Aug 27, 2026

    @David-Noble-at-work
    ContributorAuthor

    Ruling: conversions follow Python's rule, not Go's. Plan rewritten — docs/plans/lossless-conversions.md.

    A cross-category conversion is an error, whatever the value. Within the integer category, the value must be in range.

    Category is decided by kind. Range is the only question a value gets asked, and only integer→integer asks it.

    From → To Verdict Why
    integer → string error Python never coerces; str(x) says it properly
    float → integer error TypeError: 'float' object cannot be interpreted as an integer
    integer → integer in range, or error struct.error: byte format requires -128 <= number <= 127
    integer → float allow Python widens implicitly (1 + 2.0)

    This replaces the round-trip rule I proposed earlier, and the difference matters. A round-trip test would accept float(3.0) → int — it is integral and survives the trip — while rejecting 3.9. Python rejects both: [1,2,3][1.0] is a TypeError regardless of the value being integral. A rule that accepts 3.0 and rejects 3.9 is one no author can predict, so category beats value.

    It also settles the open question about int64 → float64: Python widens implicitly there, so it stays allowed with no check. I had been leaning toward applying a uniform round-trip rule, which would have caught large-integer precision loss. Python-like says leave it.

    Integer → string still needs rejecting by kind rather than by range — 65 → "A" round-trips back to 65, so a value-based check lets it through.

    file.mkdir(mode=0o644) keeps working: integer→integer, in range. That is precisely why the rule cannot be "reject int→uint32".

    Out of scope, stated so it is not mistaken for an oversight:

    • string ↔ []byte stays implicit. Python requires explicit .encode()/.decode(), and Starlark has a distinct bytes type, so a strictly Python-shaped surface would change this — but it touches every provider method taking bytes and is much wider than int passed where a string is expected is silently converted to a rune: print(65) emits "A" #709.
    • bool where an integer is wanted stays rejected. Python allows it (True == 1); this would be a loosening, and a bool arriving where a number is expected is far likelier to be a mistake than an intent.

    Placement: the guard goes in convertDirect, where the ConvertibleTo branch lives. That call is the last resort for the entire conversion ladder, so the rule will also govern slice elements, map values, and struct fields. Intended, and a wider blast radius than this issue's title suggests.

  3. David-Noble-at-work commented on Aug 27, 2026

    @David-Noble-at-work
    ContributorAuthor

    Blocked on #711. The serialization half has been backed out of the lossless-conversions branch, leaving it purely about the conversion rule.

    Applying the category rule alone turns five existing tests red, all on save/load paths:

    file.copy: param mode: float64 value is neither assignable nor convertible to fs.FileMode
    

    Not a defect in the rule — LoadGraph uses plain json.Unmarshal, so every number in a reloaded graph is a float64, and Convert has been truncating it back. The rule removes the truncation and exposes what was underneath. That is #711, and it has to land first.

    A prototype of the fix also produced a checksum mismatch on reload, so it reaches graph identity rather than only deserialization — which is why it is its own issue rather than a step here.

    The category-rule work stands and is proved: six tests written before the fix, each failing for its documented reason, now passing. One more still to update — convert_struct_test.go:41 hydrates a struct from a map with a literal float64 reaching an int field, no JSON involved. That test encodes the old lenient contract and belongs to this issue.

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

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions