Repository navigation
Conversation
This was referenced Oct 31, 2026
ubelai
force-pushed
the
fix/730-hoist-exclusive-range-bound
branch
5 times, most recently
from
November 7, 2026 18:27
682539b to
8dbb659
Compare
ubelai
force-pushed
the
fix/730-hoist-exclusive-range-bound
branch
from
November 9, 2026 23:06
8dbb659 to
912cfe6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #730
While inclusive range loops (
for v in lo..=hi) already hoist both bounds (hyLoandhyHi) before declaring the loop variablev, exclusive range loops (for v in lo..hi) previously emitted:This caused two bugs:
for n in 0..n): In Go'sforheader scoping rules,n := 0in the init statement is in scope for the conditionn < n, sofor n in 0..nlowered tofor n := 0; n < n; n++(0 iterations instead of reading the outern).hiexpressions (for i in 0..expensive()) were re-evaluated on every iteration instead of once at loop entry.Changes
compiler/crates/hy-emit/src/stmt.rs:hi_tinto a freshhyHitemporary (let hi_once = self.next_temp("hyHi"); self.push(&format!("{} := {}", hi_once, hi_t));) before emittingfor {} := {}; {} < {}; {}++ {{.compiler/crates/hy-emit/src/rangebind.rs:ForKind::Rangeloops now evaluateloandhiin the enclosing scope before bindingvar,stmt_readsuniformly treatsexpr_reads(lo, target) || expr_reads(hi, target)as reading the outertargeteven whenvar == target.compiler/crates/hy-emit/src/tests.rs&compiler/crates/hy-emit/tests/rangebind_match_soundness.rs):exclusive_range_hoists_upper_bound_once_in_outer_scopeverifyingfor n in 0..nlowers tohyHi0 := nfollowed byfor n := 0; n < hyHi0; n++.rangebind_match_soundness.rsand golden emission assertions for the hoistedhyHibinding.Verification
cargo test -p hy-emit -p hy-check: 977 passed; 0 failed.