[BUG] Chained comparison `a <= b < c` requires the middle operand to be `ImplicitlyCopyable`; equivalent split form does not
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
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:
- Split into two comparisons joined by
and. - Write
low <= item.copy() < high(forces a copy that the split form doesn't need). - Add
ImplicitlyCopyableto 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 <= itemcompiles 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
ImplicitlyCopyabletypes likeIntwork 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 genericRange[T].__contains__doingself.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
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de modular/modular
-
bug_feature_triaged Team: Mojo Libraries
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Los mantenedores suelen responder en 2 días
-
[BUG]Incomplete Apache LicenseAbiertobug bug_feature_triaged documentation mojo
Dificultad 1/5 Menos de una hora Aptitud para principiantes 62/100
Los mantenedores suelen responder en 2 días
-
`Counter.lt()` and `Counter.gt()` disagree with `__eq__`, and `a.lt(a)` can be `True`Posiblemente ocupada @YidaWeng la tomó hace 2 días. Abiertobug_feature_triaged Team: Mojo Libraries
Dificultad 2/5 1-3 horas Aptitud para principiantes 67/100
modular/modular#7696 · 2 comentarios ·
Los mantenedores suelen responder en 2 días
-
[BUG] `Counter` `-=`, `+=` and `&=` leave keys behind when they remove many keysPosiblemente ocupada @Tharun-Kumar-McW la tomó hace 5 días. Abiertobug bug_feature_triaged mojo-stdlib Team: Mojo Libraries
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
modular/modular#7658 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
[BUG] exp(NaN) returns infAbiertobug bug_feature_triaged mojo Team: Mojo Libraries
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
modular/modular#7646 · 3 comentarios ·
Los mantenedores suelen responder en 2 días
Todos los issues de modular/modular
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
sagemathinc/sagebrush#1683 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
baseballyama/rsvelte#5235 ·
Los mantenedores suelen responder en 1 día
-
lang-core: member access on a component instance reads its internal fields instead of giving nullAbiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
midspiral/LemmaScript#355 · 1 comentario ·
Los mantenedores suelen responder en 1 día