From 5233f9cde0aa234090662b9a2e3bca99924b6af4 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Sat, 28 Oct 2017 18:41:58 +1300 Subject: [PATCH] Add support for `crate` shorthand for `pub(crate)` --- Cargo.lock | 42 +++++++++++++++++----------------- src/items.rs | 12 ++++++---- src/utils.rs | 7 +++--- tests/source/fn-simple.rs | 2 ++ tests/source/pub-restricted.rs | 13 +++++++++++ tests/target/fn-simple.rs | 2 ++ tests/target/pub-restricted.rs | 13 +++++++++++ 7 files changed, 62 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2ece75165b3..f2041542cc4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/src/items.rs b/src/items.rs index b6dbb9af1d2..562986da6a9 100644 --- a/src/items.rs +++ b/src/items.rs @@ -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 diff --git a/src/utils.rs b/src/utils.rs index 3106df9ed8b..3bdadf460e2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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()); diff --git a/tests/source/fn-simple.rs b/tests/source/fn-simple.rs index 88eb9c36892..ff08b04c704 100644 --- a/tests/source/fn-simple.rs +++ b/tests/source/fn-simple.rs @@ -62,3 +62,5 @@ mod foo { // #2082 pub(crate) fn init() {} + +crate fn init() {} diff --git a/tests/source/pub-restricted.rs b/tests/source/pub-restricted.rs index 5683acbf3aa..30051fa72ee 100644 --- a/tests/source/pub-restricted.rs +++ b/tests/source/pub-restricted.rs @@ -24,6 +24,19 @@ pub( crate ) enum WriteState { WriteData(Writer), } + crate enum WriteState { + WriteId { + id: U64Writer, + size: U64Writer, + payload: Option>, + }, + WriteSize { + size: U64Writer, + payload: Option>, + }, + WriteData(Writer), +} + pub(in ::global:: path :: to::some_mod ) enum WriteState { WriteId { id: U64Writer, diff --git a/tests/target/fn-simple.rs b/tests/target/fn-simple.rs index 909985454aa..6bbdf4be95d 100644 --- a/tests/target/fn-simple.rs +++ b/tests/target/fn-simple.rs @@ -104,3 +104,5 @@ mod foo { // #2082 pub(crate) fn init() {} + +crate fn init() {} diff --git a/tests/target/pub-restricted.rs b/tests/target/pub-restricted.rs index 0e178ef1013..8cc2ade612a 100644 --- a/tests/target/pub-restricted.rs +++ b/tests/target/pub-restricted.rs @@ -24,6 +24,19 @@ pub(crate) enum WriteState { WriteData(Writer), } +crate enum WriteState { + WriteId { + id: U64Writer, + size: U64Writer, + payload: Option>, + }, + WriteSize { + size: U64Writer, + payload: Option>, + }, + WriteData(Writer), +} + pub(in global::path::to::some_mod) enum WriteState { WriteId { id: U64Writer,