Skip to content

Commit 66cac1f

Browse files
kamalmarhubimarcusklaas
authored andcommitted
Handle pub(restricted) (#1013)
* Handle pub(restricted) This commit properly handles pub(restricted) as introduced in RFC 1422 [0]. The syntax support was added in #971, but they were not correctly formatted. [0] https://github.com/rust-lang/rfcs/blob/master/text/1422-pub-restricted.md Fixes #970 * Drop #[inline] attribute on format_visibility * Make newly non-failing functions return String The change to `format_visibiilty` means that `format_header` and `format_unit_struct` can no longer fail. Their return type is updated to reflect that.
1 parent 6e01fac commit 66cac1f

File tree

8 files changed

+147
-43
lines changed

8 files changed

+147
-43
lines changed

Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ syntex_syntax = "0.32"
2525
log = "0.3"
2626
env_logger = "0.3"
2727
getopts = "0.2"
28+
itertools = "0.4.15"

src/items.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ impl<'a> FmtVisitor<'a> {
139139
ast::ForeignItemKind::Static(ref ty, is_mutable) => {
140140
// FIXME(#21): we're dropping potential comments in between the
141141
// function keywords here.
142-
let vis = match format_visibility(&item.vis) {
143-
Some(s) => s,
144-
None => return,
145-
};
142+
let vis = format_visibility(&item.vis);
146143
let mut_str = if is_mutable {
147144
"mut "
148145
} else {
@@ -305,11 +302,7 @@ impl<'a> FmtVisitor<'a> {
305302
enum_def: &ast::EnumDef,
306303
generics: &ast::Generics,
307304
span: Span) {
308-
let header_str = match format_header("enum ", ident, vis) {
309-
Some(s) => s,
310-
None => return,
311-
};
312-
self.buffer.push_str(&header_str);
305+
self.buffer.push_str(&format_header("enum ", ident, vis));
313306

314307
let enum_snippet = self.snippet(span);
315308
let body_start = span.lo + BytePos(enum_snippet.find_uncommented("{").unwrap() as u32 + 1);
@@ -453,7 +446,7 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
453446
ref self_ty,
454447
ref items) = item.node {
455448
let mut result = String::new();
456-
result.push_str(try_opt!(format_visibility(&item.vis)));
449+
result.push_str(&*format_visibility(&item.vis));
457450
result.push_str(format_unsafety(unsafety));
458451
result.push_str("impl");
459452

@@ -593,7 +586,7 @@ pub fn format_struct(context: &RewriteContext,
593586
one_line_width: Option<usize>)
594587
-> Option<String> {
595588
match *struct_def {
596-
ast::VariantData::Unit(..) => format_unit_struct(item_name, ident, vis),
589+
ast::VariantData::Unit(..) => Some(format_unit_struct(item_name, ident, vis)),
597590
ast::VariantData::Tuple(ref fields, _) => {
598591
format_tuple_struct(context,
599592
item_name,
@@ -623,7 +616,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
623616
item.node {
624617
let mut result = String::new();
625618
let header = format!("{}{}trait {}",
626-
try_opt!(format_visibility(&item.vis)),
619+
format_visibility(&item.vis),
627620
format_unsafety(unsafety),
628621
item.ident);
629622

@@ -744,14 +737,8 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
744737
}
745738
}
746739

747-
fn format_unit_struct(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> Option<String> {
748-
let mut result = String::with_capacity(1024);
749-
750-
let header_str = try_opt!(format_header(item_name, ident, vis));
751-
result.push_str(&header_str);
752-
result.push(';');
753-
754-
Some(result)
740+
fn format_unit_struct(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> String {
741+
format!("{};", format_header(item_name, ident, vis))
755742
}
756743

757744
fn format_struct_struct(context: &RewriteContext,
@@ -766,7 +753,7 @@ fn format_struct_struct(context: &RewriteContext,
766753
-> Option<String> {
767754
let mut result = String::with_capacity(1024);
768755

769-
let header_str = try_opt!(format_header(item_name, ident, vis));
756+
let header_str = format_header(item_name, ident, vis);
770757
result.push_str(&header_str);
771758

772759
let body_lo = context.codemap.span_after(span, "{");
@@ -859,7 +846,7 @@ fn format_tuple_struct(context: &RewriteContext,
859846
-> Option<String> {
860847
let mut result = String::with_capacity(1024);
861848

862-
let header_str = try_opt!(format_header(item_name, ident, vis));
849+
let header_str = format_header(item_name, ident, vis);
863850
result.push_str(&header_str);
864851

865852
// FIXME(#919): don't lose comments on empty tuple structs.
@@ -945,7 +932,7 @@ pub fn rewrite_type_alias(context: &RewriteContext,
945932
-> Option<String> {
946933
let mut result = String::new();
947934

948-
result.push_str(&try_opt!(format_visibility(&vis)));
935+
result.push_str(&format_visibility(&vis));
949936
result.push_str("type ");
950937
result.push_str(&ident.to_string());
951938

@@ -1013,7 +1000,7 @@ impl Rewrite for ast::StructField {
10131000
}
10141001

10151002
let name = self.ident;
1016-
let vis = try_opt!(format_visibility(&self.vis));
1003+
let vis = format_visibility(&self.vis);
10171004
let mut attr_str = try_opt!(self.attrs
10181005
.rewrite(context, context.config.max_width - offset.width(), offset));
10191006
if !attr_str.is_empty() {
@@ -1042,7 +1029,7 @@ pub fn rewrite_static(prefix: &str,
10421029
context: &RewriteContext)
10431030
-> Option<String> {
10441031
let prefix = format!("{}{} {}{}: ",
1045-
try_opt!(format_visibility(vis)),
1032+
format_visibility(vis),
10461033
prefix,
10471034
format_mutability(mutability),
10481035
ident);
@@ -1260,7 +1247,7 @@ fn rewrite_fn_base(context: &RewriteContext,
12601247

12611248
let mut result = String::with_capacity(1024);
12621249
// Vis unsafety abi.
1263-
result.push_str(try_opt!(format_visibility(vis)));
1250+
result.push_str(&*format_visibility(vis));
12641251

12651252
if let ast::Constness::Const = constness {
12661253
result.push_str("const ");
@@ -1816,8 +1803,8 @@ fn rewrite_where_clause(context: &RewriteContext,
18161803
}
18171804
}
18181805

1819-
fn format_header(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> Option<String> {
1820-
Some(format!("{}{}{}", try_opt!(format_visibility(vis)), item_name, ident))
1806+
fn format_header(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> String {
1807+
format!("{}{}{}", format_visibility(vis), item_name, ident)
18211808
}
18221809

18231810
fn format_generics(context: &RewriteContext,

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern crate unicode_segmentation;
2424
extern crate regex;
2525
extern crate diff;
2626
extern crate term;
27+
extern crate itertools;
2728

2829
use syntax::ast;
2930
use syntax::codemap::{mk_sp, CodeMap, Span};

src/utils.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::borrow::Cow;
1112
use std::cmp::Ordering;
1213

13-
use syntax::ast::{self, Visibility, Attribute, MetaItem, MetaItemKind};
14+
use itertools::Itertools;
15+
16+
use syntax::ast::{self, Visibility, Attribute, MetaItem, MetaItemKind, Path};
1417
use syntax::codemap::{CodeMap, Span, BytePos};
1518
use syntax::abi;
1619

@@ -66,14 +69,23 @@ pub fn extra_offset(text: &str, offset: Indent) -> usize {
6669
}
6770
}
6871

69-
#[inline]
70-
pub fn format_visibility(vis: &Visibility) -> Option<&'static str> {
72+
// Uses Cow to avoid allocating in the common cases.
73+
pub fn format_visibility(vis: &Visibility) -> Cow<'static, str> {
7174
match *vis {
72-
Visibility::Public => Some("pub "),
73-
Visibility::Inherited => Some(""),
74-
// FIXME(#970): Handle new visibility types.
75-
Visibility::Crate(_) => None,
76-
Visibility::Restricted { .. } => None,
75+
Visibility::Public => Cow::from("pub "),
76+
Visibility::Inherited => Cow::from(""),
77+
Visibility::Crate(_) => Cow::from("pub(crate) "),
78+
Visibility::Restricted { ref path, .. } => {
79+
let Path { global, ref segments, .. } = **path;
80+
let prefix = if global {
81+
"::"
82+
} else {
83+
""
84+
};
85+
let mut segments_iter = segments.iter().map(|seg| seg.identifier.name.as_str());
86+
87+
Cow::from(format!("pub({}{}) ", prefix, segments_iter.join("::")))
88+
}
7789
}
7890
}
7991

src/visitor.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,7 @@ impl<'a> FmtVisitor<'a> {
502502
let local_file_name = self.codemap.span_to_filename(s);
503503
let is_internal = local_file_name == self.codemap.span_to_filename(source!(self, m.inner));
504504

505-
if let Some(vis) = utils::format_visibility(vis) {
506-
self.buffer.push_str(vis);
507-
}
505+
self.buffer.push_str(&*utils::format_visibility(vis));
508506
self.buffer.push_str("mod ");
509507
self.buffer.push_str(&ident.to_string());
510508

@@ -540,10 +538,7 @@ impl<'a> FmtVisitor<'a> {
540538
}
541539

542540
fn format_import(&mut self, vis: &ast::Visibility, vp: &ast::ViewPath, span: Span) {
543-
let vis = match utils::format_visibility(vis) {
544-
Some(s) => s,
545-
None => return,
546-
};
541+
let vis = utils::format_visibility(vis);
547542
let mut offset = self.block_indent;
548543
offset.alignment += vis.len() + "use ".len();
549544
// 1 = ";"

tests/source/pub-restricted.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
pub( super ) enum WriteState<D> {
2+
WriteId {
3+
id: U64Writer,
4+
size: U64Writer,
5+
payload: Option<Writer<D>>,
6+
},
7+
WriteSize {
8+
size: U64Writer,
9+
payload: Option<Writer<D>>,
10+
},
11+
WriteData(Writer<D>),
12+
}
13+
14+
pub( crate ) enum WriteState<D> {
15+
WriteId {
16+
id: U64Writer,
17+
size: U64Writer,
18+
payload: Option<Writer<D>>,
19+
},
20+
WriteSize {
21+
size: U64Writer,
22+
payload: Option<Writer<D>>,
23+
},
24+
WriteData(Writer<D>),
25+
}
26+
27+
pub( ::global:: path :: to::some_mod ) enum WriteState<D> {
28+
WriteId {
29+
id: U64Writer,
30+
size: U64Writer,
31+
payload: Option<Writer<D>>,
32+
},
33+
WriteSize {
34+
size: U64Writer,
35+
payload: Option<Writer<D>>,
36+
},
37+
WriteData(Writer<D>),
38+
}
39+
40+
pub( local:: path :: to::some_mod ) enum WriteState<D> {
41+
WriteId {
42+
id: U64Writer,
43+
size: U64Writer,
44+
payload: Option<Writer<D>>,
45+
},
46+
WriteSize {
47+
size: U64Writer,
48+
payload: Option<Writer<D>>,
49+
},
50+
WriteData(Writer<D>),
51+
}

tests/target/pub-restricted.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
pub(super) enum WriteState<D> {
2+
WriteId {
3+
id: U64Writer,
4+
size: U64Writer,
5+
payload: Option<Writer<D>>,
6+
},
7+
WriteSize {
8+
size: U64Writer,
9+
payload: Option<Writer<D>>,
10+
},
11+
WriteData(Writer<D>),
12+
}
13+
14+
pub(crate) enum WriteState<D> {
15+
WriteId {
16+
id: U64Writer,
17+
size: U64Writer,
18+
payload: Option<Writer<D>>,
19+
},
20+
WriteSize {
21+
size: U64Writer,
22+
payload: Option<Writer<D>>,
23+
},
24+
WriteData(Writer<D>),
25+
}
26+
27+
pub(::global::path::to::some_mod) enum WriteState<D> {
28+
WriteId {
29+
id: U64Writer,
30+
size: U64Writer,
31+
payload: Option<Writer<D>>,
32+
},
33+
WriteSize {
34+
size: U64Writer,
35+
payload: Option<Writer<D>>,
36+
},
37+
WriteData(Writer<D>),
38+
}
39+
40+
pub(local::path::to::some_mod) enum WriteState<D> {
41+
WriteId {
42+
id: U64Writer,
43+
size: U64Writer,
44+
payload: Option<Writer<D>>,
45+
},
46+
WriteSize {
47+
size: U64Writer,
48+
payload: Option<Writer<D>>,
49+
},
50+
WriteData(Writer<D>),
51+
}

0 commit comments

Comments
 (0)