Skip to content

Reduce over-eager slice constant folding to match CPython bytecode #26

@youknowone

Description

@youknowone

Summary

RustPython still folds slice objects and tuple-of-slice literals more aggressively than CPython. This creates
large structural bytecode drift in slice-heavy tests.

Evidence

Representative mismatches include:

  • CPython LOAD_CONST None vs RustPython LOAD_CONST slice(None, None, -1)
  • CPython scalar constants vs RustPython prebuilt tuple-of-slices constants
  • CPython list/loop shape vs RustPython early slice-object materialization

Examples seen in:

  • Lib/test/test_buffer.py
  • Lib/dataclasses.py
  • Lib/test/test_math.py

Specific cases from reports include:

  • test_buffer.py::transpose
  • test_buffer.py::test_memoryview_compare_multidim_mixed
  • test_buffer.py::test_memoryview_compare_multidim_suboffsets
  • dataclasses.py::_process_class

Expected direction

Slice lowering should follow CPython’s compilation structure. RustPython should avoid turning slice syntax
into constants earlier than CPython does, especially when that changes control-flow shape or constant-table
layout.

Likely implementation areas

  • crates/codegen/src/compile.rs
  • literal/constant-folding paths used during AST or IR lowering

Done when

The slice-heavy functions above stop producing slice(...) and tuple-of-slices constants where CPython still
emits scalar constants or later slice-building bytecode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions