Skip to content

feat: add ConditionalExpr support to constant_fold_expr [2/2] - #19984

Draft
BobTheBuidler wants to merge 6 commits into
python:masterfrom
BobTheBuidler:patch-14
Draft

BobTheBuidler wants to merge 6 commits into
python:masterfrom
BobTheBuidler:patch-14

Conversation

@BobTheBuidler

Copy link
Copy Markdown
Contributor

This PR adds ConditionalExpr support to constant_fold_expr

@BobTheBuidler BobTheBuidler changed the title [mypyc] feat: add ConditionalExpr support to constant_fold_expr Oct 2, 2025
@github-actions

This comment has been minimized.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an irbuild test and a run test?

@BobTheBuidler

Copy link
Copy Markdown
Contributor Author

Hmm that's weird, it looks like the constant for the base folded correctly, as did the 2 branch values, but not the overall expression

________________________ testConditionalConstantFolding ________________________
[gw3] linux -- Python 3.10.18 /home/runner/work/mypy/mypy/.tox/py/bin/python
data: /home/runner/work/mypy/mypy/mypyc/test-data/irbuild-constant-fold.test:482:
Failed: Invalid source code output (/home/runner/work/mypy/mypy/mypyc/test-data/irbuild-constant-fold.test, line 482)
----------------------------- Captured stderr call -----------------------------
Expected:
  def f():
      a :: str (diff)
  L0:
      a = "t" (diff)
Actual:
  def f():
      r0 :: bit (diff)
      r1, r2, r3, a :: str (diff)
  L0:
      r0 = 2 != 0 (diff)
      if r0 goto L1 else goto L2 :: bool (diff)
  L1: (diff)
      r1 = 't' (diff)
      r2 = r1 (diff)
      goto L3 (diff)
  L2: (diff)
      r3 = 'f' (diff)
      r2 = r3 (diff)
  L3: (diff)
      a = r2 (diff)
      return 1 (diff)

Alignment of first line difference:
  E:     a :: str
  A:     r0 :: bit
         ^
@BobTheBuidler

Copy link
Copy Markdown
Contributor Author

I mean... I guess it did constant fold all of the necessary values... And I think with the code like this the C compiler can optimize away the if check in some cases (ex: ints), but not all (ex: strings)

@github-actions

This comment has been minimized.

@BobTheBuidler
BobTheBuidler marked this pull request as draft October 2, 2025 19:50
@BobTheBuidler

Copy link
Copy Markdown
Contributor Author

Oh, I know why! Let's put this on hold until #19982 is merged, so I can use the @folding_candidate decorator

@BobTheBuidler BobTheBuidler changed the title feat: add ConditionalExpr support to constant_fold_expr [1/1] Oct 3, 2025
@github-actions

github-actions Bot commented Oct 3, 2025

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants