Skip to content

Commit e775dd6

Browse files
committed
Auto merge of #18166 - ChayimFriedman2:dollar-crate-root, r=Veykril
fix: Fix a bug in span map merge, and add explanations of how span maps are stored Because it took me hours to figure out that contrary to common sense, the offset stored is the *end* of the node, and we search by the *start*. Which is why we need a convoluted `partition_point()` instead of a simple `binary_search()`. And this was not documented at all. Which made me make mistakes with my implementation of `SpanMap::merge()`. The other bug fixed about span map merging is correctly keeping track of the current offset in presence of multiple sibling macro invocations. Unrelated, but because of the previous issue it took me hours to debug, so I figured out I'll put them together for posterity. Fixes #18163.
2 parents f30d4ea + b275f37 commit e775dd6

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

crates/ide/src/expand_macro.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ fn expand_macro_recur(
149149
expanded.text_range().len(),
150150
&expansion_span_map,
151151
);
152-
Some(expand(sema, expanded, result_span_map, offset_in_original_node))
152+
Some(expand(sema, expanded, result_span_map, u32::from(offset_in_original_node) as i32))
153153
}
154154

155155
fn expand(
156156
sema: &Semantics<'_, RootDatabase>,
157157
expanded: SyntaxNode,
158158
result_span_map: &mut SpanMap<SyntaxContextId>,
159-
offset_in_original_node: TextSize,
159+
mut offset_in_original_node: i32,
160160
) -> SyntaxNode {
161161
let children = expanded.descendants().filter_map(ast::Item::cast);
162162
let mut replacements = Vec::new();
@@ -166,8 +166,14 @@ fn expand(
166166
sema,
167167
&child,
168168
result_span_map,
169-
offset_in_original_node + child.syntax().text_range().start(),
169+
TextSize::new(
170+
(offset_in_original_node + (u32::from(child.syntax().text_range().start()) as i32))
171+
as u32,
172+
),
170173
) {
174+
offset_in_original_node = offset_in_original_node
175+
+ (u32::from(new_node.text_range().len()) as i32)
176+
- (u32::from(child.syntax().text_range().len()) as i32);
171177
// check if the whole original syntax is replaced
172178
if expanded == *child.syntax() {
173179
return new_node;

crates/span/src/map.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313
/// Maps absolute text ranges for the corresponding file to the relevant span data.
1414
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
1515
pub struct SpanMap<S> {
16+
/// The offset stored here is the *end* of the node.
1617
spans: Vec<(TextSize, SpanData<S>)>,
1718
/// Index of the matched macro arm on successful expansion for declarative macros.
1819
// FIXME: Does it make sense to have this here?
@@ -109,11 +110,32 @@ where
109110
///
110111
/// The length of the replacement node needs to be `other_size`.
111112
pub fn merge(&mut self, other_range: TextRange, other_size: TextSize, other: &SpanMap<S>) {
113+
// I find the following diagram helpful to illustrate the bounds and why we use `<` or `<=`:
114+
// --------------------------------------------------------------------
115+
// 1 3 5 6 7 10 11 <-- offsets we store
116+
// 0-1 1-3 3-5 5-6 6-7 7-10 10-11 <-- ranges these offsets refer to
117+
// 3 .. 7 <-- other_range
118+
// 3-5 5-6 6-7 <-- ranges we replace (len = 7-3 = 4)
119+
// ^^^^^^^^^^^ ^^^^^^^^^^
120+
// remove shift
121+
// 2 3 5 9 <-- offsets we insert
122+
// 0-2 2-3 3-5 5-9 <-- ranges we insert (other_size = 9-0 = 9)
123+
// ------------------------------------
124+
// 1 3
125+
// 0-1 1-3 <-- these remain intact
126+
// 5 6 8 12
127+
// 3-5 5-6 6-8 8-12 <-- we shift these by other_range.start() and insert them
128+
// 15 16
129+
// 12-15 15-16 <-- we shift these by other_size-other_range.len() = 9-4 = 5
130+
// ------------------------------------
131+
// 1 3 5 6 8 12 15 16 <-- final offsets we store
132+
// 0-1 1-3 3-5 5-6 6-8 8-12 12-15 15-16 <-- final ranges
133+
112134
self.spans.retain_mut(|(offset, _)| {
113-
if other_range.contains(*offset) {
135+
if other_range.start() < *offset && *offset <= other_range.end() {
114136
false
115137
} else {
116-
if *offset >= other_range.end() {
138+
if *offset > other_range.end() {
117139
*offset += other_size;
118140
*offset -= other_range.len();
119141
}

0 commit comments

Comments
 (0)