Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a09ca68

Browse files
rchaser53topecongiro
authored andcommitted
fix the bug removing attrs (rust-lang#3760)
1 parent ef00f74 commit a09ca68

File tree

2 files changed

+71
-13
lines changed

2 files changed

+71
-13
lines changed

src/patterns.rs

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use syntax::ast::{self, BindingMode, FieldPat, Pat, PatKind, RangeEnd, RangeSynt
22
use syntax::ptr;
33
use syntax::source_map::{self, BytePos, Span};
44

5-
use crate::comment::FindUncommented;
5+
use crate::comment::{combine_strs_with_missing_comments, FindUncommented};
66
use crate::config::lists::*;
77
use crate::expr::{can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field};
88
use crate::lists::{
@@ -179,7 +179,13 @@ fn rewrite_struct_pat(
179179
fields.iter(),
180180
terminator,
181181
",",
182-
|f| f.span.lo(),
182+
|f| {
183+
if f.node.attrs.is_empty() {
184+
f.span.lo()
185+
} else {
186+
f.node.attrs.first().unwrap().span.lo()
187+
}
188+
},
183189
|f| f.span.hi(),
184190
|f| f.node.rewrite(context, v_shape),
185191
context.snippet_provider.span_after(span, "{"),
@@ -225,25 +231,50 @@ fn rewrite_struct_pat(
225231

226232
impl Rewrite for FieldPat {
227233
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
228-
let pat = self.pat.rewrite(context, shape);
234+
let hi_pos = if let Some(last) = self.attrs.last() {
235+
last.span.hi()
236+
} else {
237+
self.pat.span.lo()
238+
};
239+
240+
let attrs_str = if self.attrs.is_empty() {
241+
String::from("")
242+
} else {
243+
self.attrs.rewrite(context, shape)?
244+
};
245+
246+
let pat_str = self.pat.rewrite(context, shape)?;
229247
if self.is_shorthand {
230-
pat
248+
combine_strs_with_missing_comments(
249+
context,
250+
&attrs_str,
251+
&pat_str,
252+
mk_sp(hi_pos, self.pat.span.lo()),
253+
shape,
254+
false,
255+
)
231256
} else {
232-
let pat_str = pat?;
257+
let nested_shape = shape.block_indent(context.config.tab_spaces());
233258
let id_str = rewrite_ident(context, self.ident);
234259
let one_line_width = id_str.len() + 2 + pat_str.len();
235-
if one_line_width <= shape.width {
236-
Some(format!("{}: {}", id_str, pat_str))
260+
let pat_and_id_str = if one_line_width <= shape.width {
261+
format!("{}: {}", id_str, pat_str)
237262
} else {
238-
let nested_shape = shape.block_indent(context.config.tab_spaces());
239-
let pat_str = self.pat.rewrite(context, nested_shape)?;
240-
Some(format!(
263+
format!(
241264
"{}:\n{}{}",
242265
id_str,
243266
nested_shape.indent.to_string(context.config),
244-
pat_str,
245-
))
246-
}
267+
self.pat.rewrite(context, nested_shape)?
268+
)
269+
};
270+
combine_strs_with_missing_comments(
271+
context,
272+
&attrs_str,
273+
&pat_and_id_str,
274+
mk_sp(hi_pos, self.pat.span.lo()),
275+
nested_shape,
276+
false,
277+
)
247278
}
248279
}
249280
}

tests/target/issue-3759.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
fn main() {
2+
let Test {
3+
#[cfg(feature = "test")]
4+
x,
5+
} = Test {
6+
#[cfg(feature = "test")]
7+
x: 1,
8+
};
9+
10+
let Test {
11+
#[cfg(feature = "test")]
12+
// comment
13+
x,
14+
} = Test {
15+
#[cfg(feature = "test")]
16+
x: 1,
17+
};
18+
19+
let Test {
20+
// comment
21+
#[cfg(feature = "test")]
22+
x,
23+
} = Test {
24+
#[cfg(feature = "test")]
25+
x: 1,
26+
};
27+
}

0 commit comments

Comments
 (0)