modular icon indicating copy to clipboard operation
modular copied to clipboard

[BUG] Chained comparison `a <= b < c` requires the middle operand to be `ImplicitlyCopyable`; equivalent split form does not

Open spencerlbeard opened this issue 2 weeks ago • 3 comments

Actual behavior

In generic code, a chained comparison whose middle operand has a generic type that is Comparable & Copyable & Deinitable (but not ImplicitlyCopyable) fails to compile:

def gcontains[T: Comparable & Copyable & Deinitable](low: T, item: T, high: T) -> Bool:
    return low <= item < high   # <-- error here
error: value of type 'T' cannot be implicitly copied, it does not conform to 'ImplicitlyCopyable'
    return low <= item < high
                  ^~~~
note: consider transferring the value with '^'
    return low <= item < high
                  ^
                      ^
note: you can copy it explicitly with '.copy()'
    return low <= item < high
                  ^
                      .copy()
error: failed to parse the provided Mojo source module

Expected behavior

The operators documentation states that a < b < c is equivalent to (a < b) and (b < c) ("The middle value is evaluated once, not twice"). The semantically identical split form compiles with the same trait bounds and requires no copy at all:

def gcontains[T: Comparable & Copyable & Deinitable](low: T, item: T, high: T) -> Bool:
    return low <= item and item < high   # compiles and runs fine

Since function parameters are immutable read-only references by default, both comparisons in the chain should be able to borrow item exactly as they do in the split form. Requiring ImplicitlyCopyable on the middle operand is stricter than what the documented-equivalent split form requires, and the discrepancy is not mentioned anywhere in the docs.

Workarounds all exist but are awkward for a plain range check:

  1. Split into two comparisons joined by and.
  2. Write low <= item.copy() < high (forces a copy that the split form doesn't need).
  3. Add ImplicitlyCopyable to the trait bounds, which excludes legitimate non-cheap-to-copy types from using the range at all.

Steps to reproduce

# chained.mojo
def gcontains[T: Comparable & Copyable & Deinitable](low: T, item: T, high: T) -> Bool:
    return low <= item < high

def main():
    print(gcontains[Int](0, 5, 10))
mojo run chained.mojo

Observed details (all verified on Mojo 1.2.0):

  • A single comparison low <= item compiles fine with these bounds — no copy required. So operator calls themselves do not consume their operands; the requirement is specific to the chained form.
  • The split form (low <= item and item < high) compiles and runs correctly with identical bounds, proving two borrows of the middle operand suffice.
  • Adding .copy() on the middle operand makes the chain compile (with a real copy).
  • Concrete ImplicitlyCopyable types like Int work in both forms at call sites, so this only surfaces when writing generic code — which is exactly where range-check helpers live. I hit it implementing a generic Range[T].__contains__ doing self.low <= item < self.high.

I searched the tracker for existing reports ("chained comparison", "cannot be implicitly copied", comparison ImplicitlyCopyable) and found none covering this case. Related issues in the same "compiler requires/creates an implicit copy where a borrow or move would suffice" family: #7614, #7659, #7504.

System information

Mojo 1.2.0 (2016d0cd)

Installed via pixi from https://conda.modular.com/max-nightly/ (resolved packages: max 26.7.0, mojo 1.2.0 release, mojo-compiler 1.2.0 release).

Pixi version: 0.84.0
Platform: linux-64
Virtual packages: __linux=6.6.87.2=0, __glibc=2.43=0, __archspec=1=x86_64_v4
OS: Linux 6.6.87.2-microsoft-standard-WSL2 x86_64 (Ubuntu on WSL2)

spencerlbeard avatar Nov 18 '26 05:11 spencerlbeard

Thanks for reporting, @spencerlbeard, and for the reduced reproducer with the split-form and .copy() controls.

This is a compiler bug, and we'd welcome a fix from a volunteer, so I've added help wanted. If you'd like to take it, please say so here first.

The compiler copies the first middle operand to reuse it in the second comparison. It already borrows the operands after it, so a < b.copy() < c < d compiles. Any middle operand whose type is not ImplicitlyCopyable hits this, so a concrete struct fails too. It reproduces on Mojo 1.2.0 and on the 1.3.0.dev2026112405 nightly. The documented equivalence with the split form stands, and low <= item and item < high is the workaround until a fix lands.

The copy is the emitRValue call on exprs[1] in ChainedCmpOpNode::emitIR, in Mojo/lib/MojoParser/ExprNodes.cpp. emitNextCmp already keeps the value category of the later operands, and the first middle operand likely needs the same. A test fits beside the chained-comparison cases in Mojo/test/mojo-parser/exprs/exprs_misc.mojo.

weiweichen avatar Nov 26 '26 02:11 weiweichen

Hi @weiweichen, I'd love to take this on if it's okay!

kathleenbanawa avatar Nov 26 '26 22:11 kathleenbanawa

go for it!

weiweichen avatar Nov 27 '26 00:11 weiweichen