Add support for crate shorthand for pub(crate)

This commit is contained in:
Nick Cameron 2017-10-28 18:41:58 +13:00
parent e2a5c78e4b
commit 5233f9cde0
7 changed files with 62 additions and 29 deletions

42
Cargo.lock generated
View file

@ -1,24 +1,3 @@
[root]
name = "rustfmt-nightly"
version = "0.2.9"
dependencies = [
"diff 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)",
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"strings 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "aho-corasick"
version = "0.6.3"
@ -115,6 +94,27 @@ name = "regex-syntax"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rustfmt-nightly"
version = "0.2.9"
dependencies = [
"diff 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)",
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"strings 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde"
version = "1.0.15"

View file

@ -14,7 +14,7 @@ use std::borrow::Cow;
use std::cmp::min;
use syntax::{abi, ast, ptr, symbol};
use syntax::ast::ImplItem;
use syntax::ast::{CrateSugar, ImplItem};
use syntax::codemap::{BytePos, Span};
use syntax::visit;
@ -1765,10 +1765,12 @@ fn rewrite_fn_base(
}
// Skip `pub(crate)`.
let lo_after_visibility = if let ast::Visibility::Crate(s) = fn_sig.visibility {
context.codemap.span_after(mk_sp(s.hi(), span.hi()), ")")
} else {
span.lo()
let lo_after_visibility = match fn_sig.visibility {
ast::Visibility::Crate(s, CrateSugar::PubCrate) => {
context.codemap.span_after(mk_sp(s.hi(), span.hi()), ")")
}
ast::Visibility::Crate(s, CrateSugar::JustCrate) => s.hi(),
_ => span.lo(),
};
// A conservative estimation, to goal is to be over all parens in generics
let args_start = fn_sig

View file

@ -11,8 +11,8 @@
use std::borrow::Cow;
use syntax::{abi, ptr};
use syntax::ast::{self, Attribute, MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind,
Path, Visibility};
use syntax::ast::{self, Attribute, CrateSugar, MetaItem, MetaItemKind, NestedMetaItem,
NestedMetaItemKind, Path, Visibility};
use syntax::codemap::{BytePos, Span, NO_EXPANSION};
use rewrite::RewriteContext;
@ -37,7 +37,8 @@ pub fn format_visibility(vis: &Visibility) -> Cow<'static, str> {
match *vis {
Visibility::Public => Cow::from("pub "),
Visibility::Inherited => Cow::from(""),
Visibility::Crate(_) => Cow::from("pub(crate) "),
Visibility::Crate(_, CrateSugar::PubCrate) => Cow::from("pub(crate) "),
Visibility::Crate(_, CrateSugar::JustCrate) => Cow::from("crate "),
Visibility::Restricted { ref path, .. } => {
let Path { ref segments, .. } = **path;
let mut segments_iter = segments.iter().map(|seg| seg.identifier.name.to_string());

View file

@ -62,3 +62,5 @@ mod foo {
// #2082
pub(crate) fn init() {}
crate fn init() {}

View file

@ -24,6 +24,19 @@ pub( crate ) enum WriteState<D> {
WriteData(Writer<D>),
}
crate enum WriteState<D> {
WriteId {
id: U64Writer,
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteSize {
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteData(Writer<D>),
}
pub(in ::global:: path :: to::some_mod ) enum WriteState<D> {
WriteId {
id: U64Writer,

View file

@ -104,3 +104,5 @@ mod foo {
// #2082
pub(crate) fn init() {}
crate fn init() {}

View file

@ -24,6 +24,19 @@ pub(crate) enum WriteState<D> {
WriteData(Writer<D>),
}
crate enum WriteState<D> {
WriteId {
id: U64Writer,
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteSize {
size: U64Writer,
payload: Option<Writer<D>>,
},
WriteData(Writer<D>),
}
pub(in global::path::to::some_mod) enum WriteState<D> {
WriteId {
id: U64Writer,