From cee2938af2cd098e7033a16067a3346a4b7e78c3 Mon Sep 17 00:00:00 2001 From: Galoretka Date: Tue, 21 Oct 2025 12:23:22 +0300 Subject: [PATCH 1/2] fix(fmt): correct peek_comment_between to scan comments within range --- crates/fmt/src/state/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/fmt/src/state/mod.rs b/crates/fmt/src/state/mod.rs index 4dbcbd37a0273..52e491e666871 100644 --- a/crates/fmt/src/state/mod.rs +++ b/crates/fmt/src/state/mod.rs @@ -944,7 +944,8 @@ impl<'sess> State<'sess, '_> { { self.comments .iter() - .take_while(|c| pos_lo < c.pos() && c.pos() < pos_hi) + .skip_while(|c| c.pos() <= pos_lo) + .take_while(|c| c.pos() < pos_hi) .find(|c| !c.style.is_blank()) } From c3b8bba21e0d112842d0b4df91c6c1dbecc27b56 Mon Sep 17 00:00:00 2001 From: Galoretka Date: Wed, 22 Oct 2025 11:32:44 +0300 Subject: [PATCH 2/2] add test --- crates/fmt/src/state/mod.rs | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/crates/fmt/src/state/mod.rs b/crates/fmt/src/state/mod.rs index 52e491e666871..147281a855d72 100644 --- a/crates/fmt/src/state/mod.rs +++ b/crates/fmt/src/state/mod.rs @@ -1190,3 +1190,47 @@ fn style_doc_comment(style: DocCommentStyle, mut cmnt: Comment) -> Comment { _ => cmnt, } } + +#[cfg(test)] +mod tests { + use super::*; + use solar::parse::interface::source_map::FileName; + + // Repro: when there is a comment before `pos_lo`, the buggy take_while-based + // implementation of `peek_comment_between` fails to find a later comment + // that is actually within (pos_lo, pos_hi). + #[test] + fn peek_comment_between_finds_comment_after_prior_comment() { + // Source with two block comments; we will search between the end of the first + // comment and near the end of the second comment. + let src = "x /*c1*/ y /*c2*/ z"; + + let sess = + solar::interface::Session::builder().with_buffer_emitter(Default::default()).build(); + let sm = sess.source_map(); + let file = sm + .new_source_file(FileName::Custom("peek_between_test".to_string()), src.to_string()) + .expect("source file"); + + // Gather comments from the source. + let comments = Comments::new(&file, sm, true, /* group */ true, None); + + // Sanity: ensure we actually have at least two comments in order. + let mut it = comments.iter(); + let first_span = it.next().expect("first comment").span; + let second_span = it.next().expect("second comment").span; + drop(it); + assert!(first_span.lo() < second_span.lo(), "comments should be ordered by position"); + + let pos_lo = first_span.hi(); + let pos_hi = second_span.hi(); + + let config = Arc::new(FormatterConfig::default()); + let inline = InlineConfig::default(); + let state = State::new(sm, config, inline, comments); + + // Expected: we should detect `second` as a comment strictly between boundaries. + // Buggy (take_while-only) implementation returns None here. + assert!(state.peek_comment_between(pos_lo, pos_hi).is_some()); + } +}