Skip to content

Commit

Permalink
Future proof us against upcoming changes to derives
Browse files Browse the repository at this point in the history
Macro hygiene used to ignore module scope, which meant that we could
reference a type from outside our wrapper module even if we didn't
import it. That is changing in an upcoming version of Rust.

Unfortunately, we can't just do either of the recommended solutions
(either adding `use super::*` or changing to use a function instead of a
module). `use super::*` doesn't work for types defined inside of a
function, and changing ot use a function instead of a module broke our
workaround to pretend we had `$crate` when we really didn't.

With this change, an item named `diesel` must be present at the crate
root. In the case of Diesel itself, this is just a module, but for all
users they will now have to put `extern crate diesel` at the crate root.
It's unlikely folks were renaming it, but we will no longer work if they
do (the import was already located at the crate root, since you cant do
`#[macro_use] extern crate` anywhere else).

Fixes #1785.
  • Loading branch information
sgrif committed Jul 17, 2018
1 parent 1f45786 commit 1fdad5b
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 50 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/

[write-tuple-1-4-0]: docs.diesel.rs/diesel/serialize/trait.WriteTuple.html

### Changed

* Diesel's derives now require that `extern crate diesel;` be at your crate root
(e.g. `src/lib.rs` or `src/main.rs`)

## [1.3.2] - 2018-06-13

### Fixed
Expand Down
4 changes: 4 additions & 0 deletions diesel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,7 @@ pub use query_builder::debug_query;
pub use query_builder::functions::{delete, insert_into, insert_or_ignore_into, replace_into,
select, sql_query, update};
pub use result::Error::NotFound;

pub(crate) mod diesel {
pub use super::*;
}
9 changes: 0 additions & 9 deletions diesel/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,15 +1042,6 @@ macro_rules! allow_tables_to_appear_in_same_query {
() => {};
}

#[macro_export]
#[doc(hidden)]
/// Used by `diesel_derives`, which can't access `$crate`
macro_rules! __diesel_use_everything {
() => {
pub use $crate::*;
};
}

/// Gets the value out of an option, or returns an error.
///
/// This is used by `FromSql` implementations.
Expand Down
4 changes: 2 additions & 2 deletions diesel_derives/src/as_changeset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
Ok(wrap_in_dummy_mod(
model.dummy_mod_name("as_changeset"),
quote!(
use self::diesel::query_builder::AsChangeset;
use self::diesel::prelude::*;
use diesel::query_builder::AsChangeset;
use diesel::prelude::*;

impl #impl_generics AsChangeset for &'update #struct_name #ty_generics
#where_clause
Expand Down
8 changes: 4 additions & 4 deletions diesel_derives/src/as_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
Ok(wrap_in_dummy_mod(
dummy_mod.into(),
quote! {
use self::diesel::expression::AsExpression;
use self::diesel::expression::bound::Bound;
use self::diesel::sql_types::Nullable;
use self::diesel::serialize::{self, ToSql, Output};
use diesel::expression::AsExpression;
use diesel::expression::bound::Bound;
use diesel::sql_types::Nullable;
use diesel::serialize::{self, ToSql, Output};

#(#tokens)*
},
Expand Down
12 changes: 6 additions & 6 deletions diesel_derives/src/diesel_numeric_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ pub fn derive(mut item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
Ok(wrap_in_dummy_mod(
dummy_name.to_lowercase().into(),
quote! {
use self::diesel::expression::{ops, Expression, AsExpression};
use self::diesel::sql_types::ops::{Add, Sub, Mul, Div};
use diesel::expression::{ops, Expression, AsExpression};
use diesel::sql_types::ops::{Add, Sub, Mul, Div};

impl #impl_generics self::std::ops::Add<__Rhs> for #struct_name #ty_generics
impl #impl_generics ::std::ops::Add<__Rhs> for #struct_name #ty_generics
#where_clause
<Self as Expression>::SqlType: Add,
__Rhs: AsExpression<<<Self as Expression>::SqlType as Add>::Rhs>,
Expand All @@ -38,7 +38,7 @@ pub fn derive(mut item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
}
}

impl #impl_generics self::std::ops::Sub<__Rhs> for #struct_name #ty_generics
impl #impl_generics ::std::ops::Sub<__Rhs> for #struct_name #ty_generics
#where_clause
<Self as Expression>::SqlType: Sub,
__Rhs: AsExpression<<<Self as Expression>::SqlType as Sub>::Rhs>,
Expand All @@ -50,7 +50,7 @@ pub fn derive(mut item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
}
}

impl #impl_generics self::std::ops::Mul<__Rhs> for #struct_name #ty_generics
impl #impl_generics ::std::ops::Mul<__Rhs> for #struct_name #ty_generics
#where_clause
<Self as Expression>::SqlType: Mul,
__Rhs: AsExpression<<<Self as Expression>::SqlType as Mul>::Rhs>,
Expand All @@ -62,7 +62,7 @@ pub fn derive(mut item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
}
}

impl #impl_generics self::std::ops::Div<__Rhs> for #struct_name #ty_generics
impl #impl_generics ::std::ops::Div<__Rhs> for #struct_name #ty_generics
#where_clause
<Self as Expression>::SqlType: Div,
__Rhs: AsExpression<<<Self as Expression>::SqlType as Div>::Rhs>,
Expand Down
2 changes: 1 addition & 1 deletion diesel_derives/src/from_sql_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn derive(mut item: syn::DeriveInput) -> Result<Tokens, Diagnostic> {
Ok(wrap_in_dummy_mod(
dummy_mod,
quote! {
use self::diesel::deserialize::{self, FromSql, FromSqlRow, Queryable};
use diesel::deserialize::{self, FromSql, FromSqlRow, Queryable};

impl #impl_generics FromSqlRow<__ST, __DB> for #struct_ty
#where_clause
Expand Down
2 changes: 1 addition & 1 deletion diesel_derives/src/identifiable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
Ok(wrap_in_dummy_mod(
model.dummy_mod_name("identifiable"),
quote! {
use self::diesel::associations::{HasTable, Identifiable};
use diesel::associations::{HasTable, Identifiable};

impl #impl_generics HasTable for #struct_name #ty_generics
#where_clause
Expand Down
6 changes: 3 additions & 3 deletions diesel_derives/src/insertable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
Ok(wrap_in_dummy_mod(
model.dummy_mod_name("insertable"),
quote! {
use self::diesel::insertable::Insertable;
use self::diesel::query_builder::UndecoratedInsertRecord;
use self::diesel::prelude::*;
use diesel::insertable::Insertable;
use diesel::query_builder::UndecoratedInsertRecord;
use diesel::prelude::*;

impl #impl_generics Insertable<#table_name::table> for #struct_name #ty_generics
#where_clause
Expand Down
2 changes: 1 addition & 1 deletion diesel_derives/src/query_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn derive(mut item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
Ok(wrap_in_dummy_mod(
dummy_mod.into(),
quote! {
use self::diesel::query_builder::QueryId;
use diesel::query_builder::QueryId;

#[allow(non_camel_case_types)]
impl #impl_generics QueryId for #struct_name #ty_generics
Expand Down
2 changes: 1 addition & 1 deletion diesel_derives/src/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
Ok(wrap_in_dummy_mod(
model.dummy_mod_name("queryable"),
quote! {
use self::diesel::Queryable;
use diesel::Queryable;

impl #impl_generics Queryable<__ST, __DB> for #struct_name #ty_generics
#where_clause
Expand Down
4 changes: 2 additions & 2 deletions diesel_derives/src/queryable_by_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub fn derive(item: syn::DeriveInput) -> Result<quote::Tokens, Diagnostic> {
Ok(wrap_in_dummy_mod(
model.dummy_mod_name("queryable_by_name"),
quote! {
use self::diesel::deserialize::{self, QueryableByName};
use self::diesel::row::NamedRow;
use diesel::deserialize::{self, QueryableByName};
use diesel::row::NamedRow;

impl #impl_generics QueryableByName<__DB>
for #struct_name #ty_generics
Expand Down
2 changes: 1 addition & 1 deletion diesel_derives/src/sql_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn pg_tokens(item: &syn::DeriveInput) -> Option<quote::Tokens> {
};

Some(quote! {
use self::diesel::pg::{PgMetadataLookup, PgTypeMetadata};
use diesel::pg::{PgMetadataLookup, PgTypeMetadata};

impl #impl_generics diesel::sql_types::HasSqlType<#struct_name #ty_generics>
for diesel::pg::Pg
Expand Down
21 changes: 2 additions & 19 deletions diesel_derives/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ pub use diagnostic_shim::*;
use meta::*;

pub fn wrap_in_dummy_mod(const_name: Ident, item: Tokens) -> Tokens {
let call_site = root_span(Span::call_site());
let use_everything = quote_spanned!(call_site=> __diesel_use_everything!());
quote! {
#[allow(non_snake_case, unused_extern_crates)]
#[allow(non_snake_case, unused_extern_crates, unused_imports)]
mod #const_name {
// https://github.com/rust-lang/rust/issues/47314
extern crate std;
use diesel;

mod diesel {
#use_everything;
}
#item
}
}
Expand Down Expand Up @@ -78,16 +74,3 @@ pub fn fix_span(maybe_bad_span: Span, fallback: Span) -> Span {
maybe_bad_span
}
}

#[cfg(not(feature = "nightly"))]
fn root_span(span: Span) -> Span {
span
}

#[cfg(feature = "nightly")]
/// There's an issue with the resolution of `__diesel_use_everything` if the
/// derive itself was generated from within a macro. This is a shitty workaround
/// until we figure out the expected behavior.
fn root_span(span: Span) -> Span {
span.unstable().source().into()
}

0 comments on commit 1fdad5b

Please sign in to comment.