Skip to content

Commit b558a74

Browse files
topecongiroytmimi
authored andcommitted
Backport: fix to extract comments to stop internal error (#3857)
1 parent f2c31ba commit b558a74

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

src/comment.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,16 @@ mod test {
19881988
check("\"/* abc", "abc", Some(4));
19891989
}
19901990

1991+
#[test]
1992+
fn test_find_last_uncommented() {
1993+
fn check(haystack: &str, needle: &str, expected: Option<usize>) {
1994+
assert_eq!(expected, haystack.find_last_uncommented(needle));
1995+
}
1996+
check("foo test bar test", "test", Some(13));
1997+
check("test,", "test", Some(0));
1998+
check("nothing", "test", None);
1999+
}
2000+
19912001
#[test]
19922002
fn test_filter_normal_code() {
19932003
let s = r#"

src/items.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,57 @@ impl Rewrite for ast::Local {
113113
result.push_str(&infix);
114114

115115
if let Some((init, _els)) = self.kind.init_else_opt() {
116+
let base_span = if let Some(ref ty) = self.ty {
117+
mk_sp(ty.span.hi(), self.span.hi())
118+
} else {
119+
mk_sp(self.pat.span.hi(), self.span.hi())
120+
};
121+
122+
if let Some(offset) = context.snippet(base_span).find_uncommented("=") {
123+
let base_span_lo = base_span.lo();
124+
125+
let assign_lo = base_span_lo + BytePos(offset as u32);
126+
let comment_start_pos = if let Some(ref ty) = self.ty {
127+
ty.span.hi()
128+
} else {
129+
self.pat.span.hi()
130+
};
131+
let comment_before_assign =
132+
context.snippet(mk_sp(comment_start_pos, assign_lo)).trim();
133+
134+
let assign_hi = base_span_lo + BytePos((offset + 1) as u32);
135+
let rhs_span_lo = init.span.lo();
136+
let comment_end_pos = if init.attrs.is_empty() {
137+
rhs_span_lo
138+
} else {
139+
let attr_span_lo = init.attrs.first().unwrap().span.lo();
140+
// for the case using block
141+
// ex. let x = { #![my_attr]do_something(); }
142+
if rhs_span_lo < attr_span_lo {
143+
rhs_span_lo
144+
} else {
145+
attr_span_lo
146+
}
147+
};
148+
let comment_after_assign =
149+
context.snippet(mk_sp(assign_hi, comment_end_pos)).trim();
150+
151+
if !comment_before_assign.is_empty() {
152+
let new_indent_str = &pat_shape
153+
.block_indent(0)
154+
.to_string_with_newline(context.config);
155+
result = format!("{}{}{}", comment_before_assign, new_indent_str, result);
156+
}
157+
158+
if !comment_after_assign.is_empty() {
159+
let new_indent_str =
160+
&shape.block_indent(0).to_string_with_newline(context.config);
161+
result.push_str(new_indent_str);
162+
result.push_str(comment_after_assign);
163+
result.push_str(new_indent_str);
164+
}
165+
}
166+
116167
// 1 = trailing semicolon;
117168
let nested_shape = shape.sub_width(1)?;
118169

tests/source/issue-3851.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
fn main() {
2+
let before
3+
// before_comment
4+
=
5+
if true {
6+
1.0
7+
};
8+
9+
let after =
10+
// after_comment
11+
if true {
12+
1.0
13+
};
14+
15+
let both
16+
// before_comment
17+
=
18+
// after_comment
19+
if true {
20+
1.0
21+
};
22+
}

tests/target/issue-3851.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
fn main() {
2+
// before_comment
3+
let before = if true {
4+
1.0
5+
};
6+
7+
let after =
8+
// after_comment
9+
if true {
10+
1.0
11+
};
12+
13+
// before_comment
14+
let both =
15+
// after_comment
16+
if true {
17+
1.0
18+
};
19+
}

0 commit comments

Comments
 (0)