[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

Ouverte
#7,664 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

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_feature_triaged Team: Mojo Compiler
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-nightly conda 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

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de modular/modular

Toutes les issues de modular/modular

Issues similaires

Plus d'issues Compilers

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.