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

Abierto
#7,667 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
45/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Área
compilers

Línea de trabajo

Start by reading the operators documentation linked in the issue, then locate the compiler handling for chained comparisons and compare it with the split form using the provided generic reproducer. Confirm whether the chain requires an implicit copy while both split comparisons borrow the middle operand; done when the chained form compiles with the stated trait bounds and a regression test covers it.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug_feature_triaged Team: Mojo Compiler
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)
Lenguaje dominante
Mojo
Estrellas
30.2k
Forks
3.2k
Merge medio
6 d 20 h
PR fusionados (30 d)
25

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de modular/modular

Todos los issues de modular/modular

Issues similares

Más issues de Compilers

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.