Skip to content

Dedup add_lower_bound/add_upper_bound into add_var_bound - #4408

Open
nitishagar wants to merge 1 commit into
facebook:mainfrom
nitishagar:nitishagar/4292-dedup-add-var-bound
Open

Dedup add_lower_bound/add_upper_bound into add_var_bound#4408
nitishagar wants to merge 1 commit into
facebook:mainfrom
nitishagar:nitishagar/4292-dedup-add-var-bound

Conversation

@nitishagar

Copy link
Copy Markdown
Contributor

Summary

add_lower_bound and add_upper_bound were near-perfect mirrors: identical two-phase lock structure, identical (res.is_ok(), quantified_kind) match (including the IntVar arm), and identical fallbacks. They differed only in ways fully derivable from a single is_upper bool — which Bounds field is the bound-side vs the opposite-side, the is_subset argument order, and the is_upper value threaded through get_new_bound.

This extracts a private add_var_bound(v, bound, is_upper, is_subset) holding the shared body, and reduces the two public functions to one-line delegations (false / true). This mirrors the established get_new_bound(..., is_upper: bool, ...) idiom already in this file, keeping the change idiomatic rather than introducing a one-use enum.

Behavior preservation

The refactor is behavior-preserving: the helper is a pure relocation of the existing add_lower_bound statements with every divergence resolved by an is_upper branch that reproduces the original add_upper_bound text. Concretely preserved:

  • The two-phase lock pattern (read under lock, drop guard, validate without lock so the is_subset callback can safely recurse into is_subset_eq, re-lock to write) — holding the lock across is_subset would deadlock.
  • The IntVar normalization (type_as_intvar_solution(&bound).expect(...)).
  • The (false, IntVar) => None drop and the // TODO(issues/105) any-error fallback.
  • Public signatures are unchanged, so all five callers need no edits.

The full cargo test -p pyrefly --lib suite (7462 tests) passes unchanged.

Fixes #4292


AI-generated: This PR was prepared by an AI agent (ZCode). The code has been reviewed and tested; the diff is a pure refactor with no intended behavior change, disclosed per the repository's AI-contribution guidelines.

`add_lower_bound` and `add_upper_bound` were near-perfect mirrors:
identical two-phase lock structure, identical `(res.is_ok(),
quantified_kind)` match (including the IntVar arm), and identical
fallbacks. They differed only in ways fully derivable from a single
`is_upper` bool: which `Bounds` field is the bound-side vs the
opposite-side, the `is_subset` argument order, and the `is_upper` value
threaded through `get_new_bound`.

Extract a private `add_var_bound(v, bound, is_upper, is_subset)` holding
the shared body, and reduce the two public functions to one-line
delegations (`false` / `true`). This mirrors the established
`get_new_bound(..., is_upper: bool, ...)` idiom already accepted in this
file, keeping the change idiomatic rather than introducing a one-use
enum.

The refactor is behavior-preserving: the helper is a pure relocation of
the existing `add_lower_bound` statements with every divergence
resolved by an `is_upper` branch that reproduces the original
`add_upper_bound` text. The two-phase lock pattern (read under lock,
drop guard, validate without lock so the `is_subset` callback can safely
recurse into `is_subset_eq`, re-lock to write) is preserved verbatim, as
are the IntVar normalization expect, the `(false, IntVar) => None` drop,
and the `// TODO(issues/105)` any-error fallback. Public signatures are
unchanged, so all five callers need no edits.

Fixes facebook#4292
@meta-cla meta-cla Bot added the cla signed label Aug 2, 2026
@github-actions github-actions Bot added the size/s label Aug 2, 2026
@meta-codesync

meta-codesync Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D114527565. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dedup add_lower_bound and add_upper_bound

1 participant