From 1217a2f925da0e4c78ca82ae60f69a431e0c181b Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Thu, 20 Sep 2018 23:57:52 +0200 Subject: [PATCH 1/4] Replace `crate` with `pub(crate)` --- src/analyze.rs | 2 +- src/attr.rs | 8 ++++---- src/diag.rs | 14 +++++++------- src/gen.rs | 2 +- src/proxy.rs | 6 +++--- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/analyze.rs b/src/analyze.rs index d8d44b8..2ca4d8a 100644 --- a/src/analyze.rs +++ b/src/analyze.rs @@ -36,7 +36,7 @@ const PROXY_LT_PARAM_NAME: &str = "'__auto_impl_proxy_lifetime"; /// name, we'll use the ugly `PROXY_TY_PARAM_NAME` and `PROXY_LT_PARAM_NAME`. /// /// This method returns two idents: (type_parameter, lifetime_parameter). -crate fn find_suitable_param_names(trait_def: &ItemTrait) -> (Ident, Lifetime) { +pub(crate) fn find_suitable_param_names(trait_def: &ItemTrait) -> (Ident, Lifetime) { // Define the visitor that just collects names struct IdentCollector<'ast> { ty_names: HashSet<&'ast Ident>, diff --git a/src/attr.rs b/src/attr.rs index a350d97..98e3845 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -16,7 +16,7 @@ use crate::{ /// Removes all `#[auto_impl]` attributes that are attached to methods of the /// given trait. -crate fn remove_our_attrs(trait_def: &mut syn::ItemTrait) { +pub(crate) fn remove_our_attrs(trait_def: &mut syn::ItemTrait) { struct AttrRemover; impl VisitMut for AttrRemover { fn visit_trait_item_method_mut(&mut self, m: &mut TraitItemMethod) { @@ -29,7 +29,7 @@ crate fn remove_our_attrs(trait_def: &mut syn::ItemTrait) { /// Checks if the given attribute is "our" attribute. That means that it's path /// is `auto_impl`. -crate fn is_our_attr(attr: &Attribute) -> bool { +pub(crate) fn is_our_attr(attr: &Attribute) -> bool { attr.path.segments.len() == 1 && attr.path.segments.iter().next().map(|seg| { seg.ident == "auto_impl" && seg.arguments.is_empty() @@ -40,7 +40,7 @@ crate fn is_our_attr(attr: &Attribute) -> bool { /// attributes. If it's invalid, an error is emitted and `Err(())` is returned. /// You have to make sure that `attr` is one of our attrs with `is_our_attr` /// before calling this function! -crate fn parse_our_attr(attr: &Attribute) -> Result { +pub(crate) fn parse_our_attr(attr: &Attribute) -> Result { assert!(is_our_attr(attr)); // Get the body of the attribute (which has to be a ground, because we @@ -119,6 +119,6 @@ crate fn parse_our_attr(attr: &Attribute) -> Result { /// Attributes of the form `#[auto_impl(...)]` that can be attached to items of /// the trait. #[derive(Clone, PartialEq, Debug)] -crate enum OurAttr { +pub(crate) enum OurAttr { KeepDefaultFor(Vec), } diff --git a/src/diag.rs b/src/diag.rs index 1ffb0de..ba02e20 100644 --- a/src/diag.rs +++ b/src/diag.rs @@ -47,7 +47,7 @@ impl DiagnosticExt for Diagnostic { crate type Diagnostic = ::proc_macro::Diagnostic; #[cfg(not(feature = "nightly"))] -crate struct Diagnostic { +pub(crate) struct Diagnostic { span: Span, msg: String, } @@ -86,19 +86,19 @@ crate struct Diagnostic { // required. #[cfg(not(feature = "nightly"))] impl Diagnostic { - crate fn note(mut self, msg: impl Into) -> Diagnostic { + pub(crate) fn note(mut self, msg: impl Into) -> Diagnostic { self.msg += &format!("\n\nnote: {}", msg.into()); self } - crate fn span_note(mut self, _: Span, msg: impl Into) -> Diagnostic { + pub(crate) fn span_note(mut self, _: Span, msg: impl Into) -> Diagnostic { // With out span fake method, we can only handle one span. We take the // one of the original error and ignore additional ones. self.msg += &format!("\n\nnote: {}", msg.into()); self } - crate fn emit(self) { + pub(crate) fn emit(self) { // Create the error token stream that contains the `compile_error!()` // invocation. let msg = &self.msg; @@ -149,7 +149,7 @@ thread_local! { /// On stable, we just copy the error token streams from the global variable. #[cfg(not(feature = "nightly"))] -crate fn error_tokens() -> TokenStream { +pub(crate) fn error_tokens() -> TokenStream { ERROR_TOKENS.with(|toks| toks.borrow().iter().cloned().collect()) } @@ -157,13 +157,13 @@ crate fn error_tokens() -> TokenStream { /// we just return an empty token stream. That's not a problem because all of /// our errors were already printed. #[cfg(feature = "nightly")] -crate fn error_tokens() -> TokenStream { +pub(crate) fn error_tokens() -> TokenStream { TokenStream::new() } /// Extension trait to add the `err()` method to `Span`. This makes it easy to /// start a `Diagnostic` from a span. -crate trait SpanExt { +pub(crate) trait SpanExt { fn err(self, msg: impl Into) -> Diagnostic; } diff --git a/src/gen.rs b/src/gen.rs index 17fa298..f45ca15 100644 --- a/src/gen.rs +++ b/src/gen.rs @@ -18,7 +18,7 @@ use crate::{ /// Generates one complete impl of the given trait for each of the given proxy /// types. All impls are returned as token stream. -crate fn gen_impls( +pub(crate) fn gen_impls( proxy_types: &[ProxyType], trait_def: &syn::ItemTrait, ) -> Result { diff --git a/src/proxy.rs b/src/proxy.rs index 7ac47ae..feb2930 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -6,7 +6,7 @@ use crate::diag::SpanExt; /// Types for which a trait can automatically be implemented. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -crate enum ProxyType { +pub(crate) enum ProxyType { Ref, RefMut, Arc, @@ -18,7 +18,7 @@ crate enum ProxyType { } impl ProxyType { - crate fn is_fn(&self) -> bool { + pub(crate) fn is_fn(&self) -> bool { match *self { ProxyType::Fn | ProxyType::FnMut | ProxyType::FnOnce => true, _ => false, @@ -34,7 +34,7 @@ impl ProxyType { /// /// If the given TokenStream is not valid, errors are emitted as appropriate /// and `Err(())` is returned. -crate fn parse_types(args: TokenStream) -> Result, ()> { +pub(crate) fn parse_types(args: TokenStream) -> Result, ()> { let mut out = Vec::new(); let mut iter = args.into_iter().peekable(); From 4c0c0ca3192888bfba8b1bf199ae8283a5af9511 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Fri, 21 Sep 2018 00:08:09 +0200 Subject: [PATCH 2/4] Remove edition-flag from Cargo.toml (it's stabilized now) --- Cargo.toml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f733d79..ae0fafe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,3 @@ -cargo-features = ["edition"] - [package] name = "auto_impl" version = "0.2.0" @@ -15,7 +13,7 @@ keywords = ["plugin"] categories = ["development-tools"] readme = "README.md" autotests = true -edition = '2018' +edition = "2018" [badges] travis-ci = { repository = "KodrAus/auto_impl" } From e1cfab2a0c234dc35187b4c43d9eb2aa09581d03 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Fri, 21 Sep 2018 15:25:39 +0200 Subject: [PATCH 3/4] Fix `use proc_macro` statements for newest nightly/beta See this for more information: https://github.com/rust-lang/rust/issues/54418 --- src/diag.rs | 2 +- src/gen.rs | 2 +- src/lib.rs | 3 ++- src/proxy.rs | 4 ++-- src/spanned.rs | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/diag.rs b/src/diag.rs index ba02e20..c7b7097 100644 --- a/src/diag.rs +++ b/src/diag.rs @@ -12,7 +12,7 @@ //! `.err()` on spans. //! -use proc_macro::{Span, TokenStream}; +use crate::proc_macro::{Span, TokenStream}; /// Extension trait that adds a convenience method to `Diagnostic`. This is diff --git a/src/gen.rs b/src/gen.rs index f45ca15..e768a90 100644 --- a/src/gen.rs +++ b/src/gen.rs @@ -1,4 +1,4 @@ -use proc_macro::Span; +use crate::proc_macro::Span; use proc_macro2::TokenStream as TokenStream2; use quote::{ToTokens, TokenStreamExt}; use syn::{ diff --git a/src/lib.rs b/src/lib.rs index db2b794..109b025 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,10 +4,11 @@ #![cfg_attr(feature = "nightly", feature(proc_macro_diagnostic, proc_macro_span))] +extern crate proc_macro; #[macro_use] extern crate quote; -use proc_macro::TokenStream; +use crate::proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; use quote::ToTokens; diff --git a/src/proxy.rs b/src/proxy.rs index feb2930..16dfaab 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -1,6 +1,6 @@ use std::iter::Peekable; -use proc_macro::{token_stream, TokenStream, TokenTree}; +use crate::proc_macro::{token_stream, TokenStream, TokenTree}; use crate::diag::SpanExt; @@ -141,7 +141,7 @@ fn eat_type(iter: &mut Peekable) -> Result Date: Fri, 21 Sep 2018 15:50:59 +0200 Subject: [PATCH 4/4] Fix crate->pub(crate) and proc_macro->crate::proc_macro for nightly feature --- src/analyze.rs | 2 +- src/diag.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/analyze.rs b/src/analyze.rs index 2ca4d8a..afc83c4 100644 --- a/src/analyze.rs +++ b/src/analyze.rs @@ -101,7 +101,7 @@ pub(crate) fn find_suitable_param_names(trait_def: &ItemTrait) -> (Ident, Lifeti /// but this is cleaner and just the correct thing to do. #[cfg(feature = "nightly")] fn param_span() -> Span2 { - ::proc_macro::Span::def_site().into() + crate::proc_macro::Span::def_site().into() } /// On stable, we use `call_site()` hygiene. That means that our names could diff --git a/src/diag.rs b/src/diag.rs index c7b7097..20cf06c 100644 --- a/src/diag.rs +++ b/src/diag.rs @@ -44,7 +44,7 @@ impl DiagnosticExt for Diagnostic { // `Diagnostic`. #[cfg(feature = "nightly")] -crate type Diagnostic = ::proc_macro::Diagnostic; +pub(crate) type Diagnostic = crate::proc_macro::Diagnostic; #[cfg(not(feature = "nightly"))] pub(crate) struct Diagnostic { @@ -170,7 +170,7 @@ pub(crate) trait SpanExt { impl SpanExt for Span { #[cfg(feature = "nightly")] fn err(self, msg: impl Into) -> Diagnostic { - Diagnostic::spanned(self, ::proc_macro::Level::Error, msg) + Diagnostic::spanned(self, crate::proc_macro::Level::Error, msg) } #[cfg(not(feature = "nightly"))]