Skip to content

Commit b9d8798

Browse files
bors[bot]Veykril
andauthored
Merge #9979
9979: fix: Incorrect up-mapping for tokens in derive attributes r=Veykril a=Veykril Merely detaching the attributes causes incorrect spans to appear when mapping tokens up as the token ids resolve to the ranges of the stripped item so all the text ranges of its tokens are actually lower than the non-stripped ones. Same fix as with attributes can be applied here, just replace the derive attribute with an equal amount of whitespace. Fixes #9387 Co-authored-by: Lukas Wirth <[email protected]>
2 parents e535791 + 5507512 commit b9d8798

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

crates/hir_expand/src/input.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn remove_derives_up_to(item: ast::Item, attr_index: usize) -> ast::Item {
4040
attr.path().and_then(|path| path.as_single_segment()).and_then(|seg| seg.name_ref())
4141
{
4242
if name.as_name() == name![derive] {
43-
attr.syntax().detach();
43+
replace_attr(&item, &attr);
4444
}
4545
}
4646
}
@@ -54,10 +54,14 @@ fn remove_attr_invoc(item: ast::Item, attr_index: usize) -> ast::Item {
5454
.attrs()
5555
.nth(attr_index)
5656
.unwrap_or_else(|| panic!("cannot find attribute #{}", attr_index));
57+
replace_attr(&item, &attr);
58+
item
59+
}
60+
61+
fn replace_attr(item: &ast::Item, attr: &ast::Attr) {
5762
let syntax_index = attr.syntax().index();
5863
let ws = make::tokens::whitespace(&" ".repeat(u32::from(attr.syntax().text().len()) as usize));
5964
item.syntax().splice_children(syntax_index..syntax_index + 1, vec![ws.into()]);
60-
item
6165
}
6266

6367
#[cfg(test)]
@@ -78,7 +82,9 @@ mod tests {
7882
assert_eq!(items.len(), 1);
7983

8084
let item = remove_derives_up_to(items.pop().unwrap(), attr);
81-
expect.assert_eq(&item.to_string());
85+
let res: String =
86+
item.syntax().children_with_tokens().map(|e| format!("{:?}\n", e)).collect();
87+
expect.assert_eq(&res);
8288
}
8389

8490
#[test]
@@ -95,13 +101,20 @@ struct A {
95101
}
96102
"#,
97103
expect![[r#"
98-
#[allow(unused)]
99-
100-
101-
#[derive(Clone)]
102-
struct A {
103-
bar: u32
104-
}"#]],
104+
105+
Token([email protected] "\n")
106+
Token([email protected] " ")
107+
Token([email protected] "\n")
108+
Token([email protected] " ")
109+
Token([email protected] "\n")
110+
111+
Token([email protected] "\n")
112+
Token([email protected] "struct")
113+
Token([email protected] " ")
114+
115+
Token([email protected] " ")
116+
117+
"#]],
105118
);
106119
}
107120
}

0 commit comments

Comments
 (0)