Skip to content

Preserve source bounds in widening integer casts with safe interval-bound handling #25407

Description

@haohuaijin

Is your feature request related to a problem or challenge?

Integer widening casts currently leave unbounded interval endpoints unbounded in the destination type. This loses useful information: an unbounded UInt32 interval cast to Int64 still represents values in [0, 4294967295]. Preserving those limits lets interval analysis prove that subsequent arithmetic is safe and retain valid optimizations.

PR #25234 explored preserving these bounds, but the change exposed existing interval-bound failures in negation and narrowing casts during merge-queue testing:

  • Negation does not correctly handle a concrete signed minimum such as i32::MIN.
  • Narrowing casts can fail on inferred interval endpoints even when the actual input values fit in the destination type.

Previously, those endpoints remained unbounded, so these paths did not encounter the concrete limits. The review agreed to defer widening to a separate change so that the overflow and division correctness fixes could remain focused.

Describe the solution you'd like

Preserve source-type limits when widening integer intervals, together with the boundary handling needed to use those limits safely downstream.

  • Materialize source-type minima and maxima for unbounded endpoints when the destination integer type can represent the entire source domain. Preserve existing finite bounds.
  • Handle negation at signed minima conservatively and consistently with runtime arithmetic semantics.
  • Handle narrowing of inferred bounds without rejecting valid queries solely because an estimated endpoint is out of range. Preserve runtime cast semantics and do not exclude values that can occur at execution time.
  • Retain the overflow and integer-division safeguards from fix: prevent unsafe integer interval propagation #25234.

Add targeted coverage for fully and partially unbounded widening casts, signed/unsigned type transitions, negation at signed extrema, and subsequent narrowing at both endpoints. Include composed expressions that exercise these transitions, the affected SQLite regressions, and the FIFO and sort cases from #25234 that motivated preserving source bounds.

Describe alternatives you've considered

Keep widening casts conservative by leaving unbounded endpoints unbounded. This avoids exposing the boundary-handling gaps, but loses opportunities to prove arithmetic safe.

Additional context

No response

Activity

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

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions