Skip to content

Commit 0b481ce

Browse files
authored
Merge pull request #2113 from topecongiro/issue-2110
Use correct span for tuple struct's body
2 parents 51b03c3 + 16302d3 commit 0b481ce

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

src/items.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,25 @@ pub fn format_struct_struct(
11551155
}
11561156
}
11571157

1158+
/// Returns a bytepos that is after that of `(` in `pub(..)`. If the given visibility does not
1159+
/// contain `pub(..)`, then return the `lo` of the `defualt_span`. Yeah, but for what? Well, we need
1160+
/// to bypass the `(` in the visibility when creating a span of tuple's body or fn's args.
1161+
fn get_bytepos_after_visibility(
1162+
context: &RewriteContext,
1163+
vis: &ast::Visibility,
1164+
default_span: Span,
1165+
terminator: &str,
1166+
) -> BytePos {
1167+
match *vis {
1168+
ast::Visibility::Crate(s, CrateSugar::PubCrate) => context
1169+
.codemap
1170+
.span_after(mk_sp(s.hi(), default_span.hi()), terminator),
1171+
ast::Visibility::Crate(s, CrateSugar::JustCrate) => s.hi(),
1172+
ast::Visibility::Restricted { ref path, .. } => path.span.hi(),
1173+
_ => default_span.lo(),
1174+
}
1175+
}
1176+
11581177
fn format_tuple_struct(
11591178
context: &RewriteContext,
11601179
item_name: &str,
@@ -1171,12 +1190,13 @@ fn format_tuple_struct(
11711190
result.push_str(&header_str);
11721191

11731192
let body_lo = if fields.is_empty() {
1174-
context.codemap.span_after(span, "(")
1193+
let lo = get_bytepos_after_visibility(context, vis, span, ")");
1194+
context.codemap.span_after(mk_sp(lo, span.hi()), "(")
11751195
} else {
11761196
fields[0].span.lo()
11771197
};
11781198
let body_hi = if fields.is_empty() {
1179-
context.codemap.span_after(span, ")")
1199+
context.codemap.span_after(mk_sp(body_lo, span.hi()), ")")
11801200
} else {
11811201
// This is a dirty hack to work around a missing `)` from the span of the last field.
11821202
let last_arg_span = fields[fields.len() - 1].span;
@@ -1224,7 +1244,10 @@ fn format_tuple_struct(
12241244
.to_string(context.config))
12251245
}
12261246
result.push('(');
1227-
let snippet = context.snippet(mk_sp(body_lo, context.codemap.span_before(span, ")")));
1247+
let snippet = context.snippet(mk_sp(
1248+
body_lo,
1249+
context.codemap.span_before(mk_sp(body_lo, span.hi()), ")"),
1250+
));
12281251
if snippet.is_empty() {
12291252
// `struct S ()`
12301253
} else if snippet.trim_right_matches(&[' ', '\t'][..]).ends_with('\n') {
@@ -1766,13 +1789,7 @@ fn rewrite_fn_base(
17661789
}
17671790

17681791
// Skip `pub(crate)`.
1769-
let lo_after_visibility = match fn_sig.visibility {
1770-
ast::Visibility::Crate(s, CrateSugar::PubCrate) => {
1771-
context.codemap.span_after(mk_sp(s.hi(), span.hi()), ")")
1772-
}
1773-
ast::Visibility::Crate(s, CrateSugar::JustCrate) => s.hi(),
1774-
_ => span.lo(),
1775-
};
1792+
let lo_after_visibility = get_bytepos_after_visibility(context, &fn_sig.visibility, span, ")");
17761793
// A conservative estimation, to goal is to be over all parens in generics
17771794
let args_start = fn_sig
17781795
.generics

tests/source/structs.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,11 @@ struct Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
258258
struct Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong {}
259259
struct Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong {}
260260
struct Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong { x: i32 }
261+
262+
// structs with visibility, do not duplicate visibility (#2110).
263+
pub(in self) struct Foo{}
264+
pub(super) struct Foo{}
265+
pub(crate) struct Foo{}
266+
pub(in self) struct Foo();
267+
pub(super) struct Foo();
268+
pub(crate) struct Foo();

tests/target/structs.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,11 @@ struct Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
300300
{
301301
x: i32,
302302
}
303+
304+
// structs with visibility, do not duplicate visibility (#2110).
305+
pub(self) struct Foo {}
306+
pub(super) struct Foo {}
307+
pub(crate) struct Foo {}
308+
pub(self) struct Foo();
309+
pub(super) struct Foo();
310+
pub(crate) struct Foo();

0 commit comments

Comments
 (0)