Skip to content

Commit 9a951b0

Browse files
committed
fix first_comment_between
previously this could have returned any comment in the span rather than guaranteeing the first
1 parent 9c69083 commit 9a951b0

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

compiler-core/src/parse/extra.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,34 @@ impl ModuleExtra {
4040
self.first_comment_between(start, end).is_some()
4141
}
4242

43-
// TODO: this won't necessarily find the first comment, just any comment
43+
/// Returns the first comment overlapping the given source locations (inclusive)
44+
/// Note that the returned span covers the text of the comment, not the `//`
4445
pub(crate) fn first_comment_between(&self, start: u32, end: u32) -> Option<SrcSpan> {
45-
self.comments
46-
.binary_search_by(|comment| {
47-
if comment.end < start {
48-
Ordering::Less
49-
} else if comment.start > end {
50-
Ordering::Greater
51-
} else {
52-
Ordering::Equal
53-
}
54-
})
55-
.ok()
56-
.and_then(|index| self.comments.get(index).copied())
46+
let inner = |comments: &[SrcSpan], start, end| {
47+
if comments.is_empty() {
48+
return None;
49+
}
50+
51+
comments
52+
.binary_search_by(|comment| {
53+
if comment.end < start {
54+
Ordering::Less
55+
} else if comment.start > end {
56+
Ordering::Greater
57+
} else {
58+
Ordering::Equal
59+
}
60+
})
61+
.ok()
62+
};
63+
64+
let mut best = None;
65+
let mut search_list = &self.comments[..];
66+
while let Some(index) = inner(search_list, start, end) {
67+
best = self.comments.get(index).copied();
68+
search_list = &search_list[0..index];
69+
}
70+
best
5771
}
5872
}
5973

@@ -130,6 +144,15 @@ mod tests {
130144
));
131145
}
132146

147+
#[test]
148+
fn first_comment_between_overlapping_end_of_range() {
149+
let extra = set_up_extra();
150+
assert!(matches!(
151+
extra.first_comment_between(35, 45),
152+
Some(SrcSpan { start: 40, end: 50 })
153+
));
154+
}
155+
133156
#[test]
134157
fn first_comment_between_at_end_of_range() {
135158
let extra = set_up_extra();

0 commit comments

Comments
 (0)