[BUG] In `x if C else y` with a compile-time constant `C`, `Int(b)` for a `Bool` `b` in the dead branch is typed `Scalar[DType.bool]`, so valid code is rejected
Les mainteneurs répondent en général sous 2 jours
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 48/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- Active
- Domaine
- compilers
Piste de recherche
Start with IfElseOpNode::emitIR in lib/MojoParser/ExprNodes.cpp, especially the hlcf.comptime.if path and emitBranchUnderAssumption; then compare the SIMD initializers at std/simd.mojo:862 and std/simd.mojo:691. Reproduce with the supplied v1 if True else Int(flag) example and check the other listed constant and runtime condition cases. Done when constant-condition branches type-check consistently with runtime-condition branches.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Bug description
Actual behavior
When the condition of a conditional expression is a compile-time constant (True, False, a literal comparison such as 1 != 2, or a comptime value), a scalar constructor applied to a Bool in the dead branch gets the type Scalar[DType.bool] instead of the requested type, and the expression is rejected:
comptime COUNT_EMPTY = False
def weight(s: String) -> Int:
return Int(s.byte_length() == 0) if COUNT_EMPTY else s.byte_length()
def main():
print(weight("abc"))
t.mojo:5:38: warning: left hand side expression of 'if False' is dead
return Int(s.byte_length() == 0) if COUNT_EMPTY else s.byte_length()
^
t.mojo:5:38: error: value of type 'Scalar[DType.bool]' is not compatible with value of type 'Int'
return Int(s.byte_length() == 0) if COUNT_EMPTY else s.byte_length()
~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~
With COUNT_EMPTY = True the same function compiles. Toggling a configuration flag shouldn't make the other branch ill-typed.
The variations narrow it down (flag: Bool, b: Int, v1: Int):
| Expression | Result |
|---|---|
v1 if True else Int(flag) |
error: Int is not compatible with Scalar[DType.bool] |
Int(flag) if False else v1 |
error |
v1 if (1 != 2) else Int(b < 2) |
error |
UInt8(1) if True else UInt8(flag) |
error: UInt8 is not compatible with Scalar[DType.bool] |
var r: Int = v1 if True else Int(flag) (contextual type given) |
error |
Int(flag) if True else v1 (the live branch) |
OK |
v1 if (b != 2) else Int(b < 2) (runtime condition) |
OK |
v1 if True else Int(b), Int(UInt8(3)), Int(Float64(3.5)) |
OK |
Int(flag) if use_flag else n with a parameter use_flag: Bool |
OK |
Outside a conditional expression, Int(flag) is an Int (var z = Int(flag); z += 5 works).
Expected behavior
Int(flag) has type Int in either branch, so all of these compile, as they do when the condition is a runtime value.
Possible cause
SIMD has an @implicit initializer from Bool whose result is not Self, def __init__(out self: SIMD[.bool, Self.length], value: Bool, /) (std/simd.mojo:862), next to the Intable initializer that Int(flag) normally resolves to (std/simd.mojo:691). For a constant condition, IfElseOpNode::emitIR (lib/MojoParser/ExprNodes.cpp, the hlcf.comptime.if path) emits each branch with emitBranchUnderAssumption(..., buildBranchAssumption(cond, invert)), so the dead branch is checked under the assumption not True or False, a contradiction. Overload resolution under that assumption appears to pick the SIMD[.bool] initializer. The parametric case works, perhaps because its assumption isn't a literal contradiction.
Steps to reproduce
def f(v1: Int, flag: Bool) -> Int:
return v1 if True else Int(flag)
def main():
print(f(1, True))
mojo run t.mojo
t.mojo:2:15: error: value of type 'Int' is not compatible with value of type 'Scalar[DType.bool]'
Found by a program generator that emits conditional expressions with constant conditions.
System information
- Mojo 1.3.0.dev2026111705 (298d4f45), also 1.3.0.dev2026111216 (0a766ce7) and 1.3.0.dev2026110505 (602ae551), from the
max-nightlyconda channel via pixi 0.81.0 - Pop!_OS 24.04 LTS, Linux 7.0.11, x86_64
- Langage dominant
- Mojo
- Étoiles
- 30.2k
- Forks
- 3.2k
- Merge moyen
- 5 j 18 h
- PR mergées (30 j)
- 27
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de modular/modular
-
accepted bug bug_feature_triaged good first issue mojo Team: Mojo Libraries
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
Les mainteneurs répondent en général sous 2 jours
-
accepted bug bug_feature_triaged good first issue mojo Team: Mojo Libraries
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
Les mainteneurs répondent en général sous 2 jours
-
[BUG] `tanh(NaN)` returns 1.0 instead of NaN for Float64, Float32, BFloat16 and Float16 on CPUOuvertebug_feature_triaged Team: Mojo Libraries
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
Les mainteneurs répondent en général sous 2 jours
-
bug_feature_triaged Team: Mojo Libraries
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
Les mainteneurs répondent en général sous 2 jours
-
bug_feature_triaged
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
Les mainteneurs répondent en général sous 2 jours
Toutes les issues de modular/modular
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
Deltakit/deltakit-compile#27 ·
-
sema: dynamic member keys print numbers with Rust's formatter, not JS's (1658206780088562.25)Ouvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
Les mainteneurs répondent en général sous 1 jour
-
new issue
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
llvm/llvm-project#245835 ·
Les mainteneurs répondent en général sous 1 jour
-
enhancement needs triage
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
Les mainteneurs répondent en général sous 1 jour