[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
メンテナーはふだん 2 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 領域
- compilers
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- Mojo
- スター
- 30.2k
- フォーク
- 3.2k
- 平均マージ
- 5日 18時間
- マージ済み PR(30日)
- 27
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
modular/modular のほかの issue
-
accepted bug bug_feature_triaged good first issue mojo Team: Mojo Libraries
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 2 日以内に返信
-
accepted bug bug_feature_triaged good first issue mojo Team: Mojo Libraries
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
-
bug_feature_triaged Team: Mojo Libraries
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
-
bug_feature_triaged Team: Mojo Libraries
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 2 日以内に返信
-
bug_feature_triaged
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
modular/modular の issue をすべて見る
似ている issue
-
new issue
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
llvm/llvm-project#245835 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
triton-lang/triton#12945 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
JulianKemmerer/PipelineC#409 ·
-
`trawl build --help` doesn't say what `--keep-c` or `-o` do対応中かも @siddesh-nexus が今日担当しました。 オープンgood first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
メンテナーはふだん 1 日以内に返信