diff --git a/diesel_compile_tests/tests/fail/derive/bad_insertable.stderr b/diesel_compile_tests/tests/fail/derive/bad_insertable.stderr index da908ef0f94b..34d625522c72 100644 --- a/diesel_compile_tests/tests/fail/derive/bad_insertable.stderr +++ b/diesel_compile_tests/tests/fail/derive/bad_insertable.stderr @@ -79,6 +79,9 @@ error[E0277]: the trait bound `i32: diesel::Expression` is not satisfied error[E0277]: the trait bound `std::string::String: diesel::Expression` is not satisfied --> tests/fail/derive/bad_insertable.rs:12:5 | +10 | #[derive(Insertable)] + | ---------- in this derive macro expansion +11 | struct User { 12 | id: String, | ^^ the trait `diesel::Expression` is not implemented for `std::string::String`, which is required by `std::string::String: AsExpression` | @@ -93,10 +96,14 @@ error[E0277]: the trait bound `std::string::String: diesel::Expression` is not s (T0, T1, T2, T3, T4, T5, T6, T7) and $N others = note: required for `std::string::String` to implement `AsExpression` + = note: this error originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0277]: the trait bound `i32: diesel::Expression` is not satisfied --> tests/fail/derive/bad_insertable.rs:13:5 | +10 | #[derive(Insertable)] + | ---------- in this derive macro expansion +... 13 | name: i32, | ^^^^ the trait `diesel::Expression` is not implemented for `i32`, which is required by `i32: AsExpression` | @@ -111,10 +118,14 @@ error[E0277]: the trait bound `i32: diesel::Expression` is not satisfied (T0, T1, T2, T3, T4, T5, T6, T7) and $N others = note: required for `i32` to implement `AsExpression` + = note: this error originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0277]: the trait bound `std::string::String: diesel::Expression` is not satisfied --> tests/fail/derive/bad_insertable.rs:12:5 | +10 | #[derive(Insertable)] + | ---------- in this derive macro expansion +11 | struct User { 12 | id: String, | ^^ the trait `diesel::Expression` is not implemented for `std::string::String`, which is required by `&'insert std::string::String: AsExpression` | @@ -130,10 +141,14 @@ error[E0277]: the trait bound `std::string::String: diesel::Expression` is not s and $N others = note: required for `&'insert std::string::String` to implement `diesel::Expression` = note: required for `&'insert std::string::String` to implement `AsExpression` + = note: this error originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0277]: the trait bound `i32: diesel::Expression` is not satisfied --> tests/fail/derive/bad_insertable.rs:13:5 | +10 | #[derive(Insertable)] + | ---------- in this derive macro expansion +... 13 | name: i32, | ^^^^ the trait `diesel::Expression` is not implemented for `i32`, which is required by `&'insert i32: AsExpression` | @@ -149,3 +164,4 @@ error[E0277]: the trait bound `i32: diesel::Expression` is not satisfied and $N others = note: required for `&'insert i32` to implement `diesel::Expression` = note: required for `&'insert i32` to implement `AsExpression` + = note: this error originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/diesel_derives/src/as_changeset.rs b/diesel_derives/src/as_changeset.rs index 44ba64c6cb04..a18da437ce27 100644 --- a/diesel_derives/src/as_changeset.rs +++ b/diesel_derives/src/as_changeset.rs @@ -1,4 +1,4 @@ -use proc_macro2::TokenStream; +use proc_macro2::{Span, TokenStream}; use quote::{quote, quote_spanned}; use syn::spanned::Spanned as _; use syn::{parse_quote, DeriveInput, Expr, Path, Result, Type}; @@ -172,7 +172,7 @@ pub fn derive(item: DeriveInput) -> Result { fn field_changeset_ty_embed(field: &Field, lifetime: Option) -> TokenStream { let field_ty = &field.ty; - let span = field.span; + let span = Span::mixed_site().located_at(field.span); quote_spanned!(span=> #lifetime #field_ty) } diff --git a/diesel_derives/src/field.rs b/diesel_derives/src/field.rs index 492bc9f5cfcb..f5f8c192483b 100644 --- a/diesel_derives/src/field.rs +++ b/diesel_derives/src/field.rs @@ -1,6 +1,7 @@ -use proc_macro2::{Span, TokenStream}; -use syn::spanned::Spanned; -use syn::{Expr, Field as SynField, Ident, Index, Result, Type}; +use { + proc_macro2::{Span, TokenStream}, + syn::{spanned::Spanned, Expr, Field as SynField, Ident, Index, Result, Type}, +}; use crate::attrs::{parse_attributes, AttributeSpanWrapper, FieldAttr, SqlIdentifier}; @@ -119,10 +120,10 @@ impl Field { None => FieldName::Unnamed(index.into()), }; - let span = match name { + let span = Span::mixed_site().located_at(match name { FieldName::Named(ref ident) => ident.span(), FieldName::Unnamed(_) => ty.span(), - }; + }); Ok(Self { ty: ty.clone(), diff --git a/diesel_derives/src/insertable.rs b/diesel_derives/src/insertable.rs index 44af578a38d4..16623dce4158 100644 --- a/diesel_derives/src/insertable.rs +++ b/diesel_derives/src/insertable.rs @@ -2,7 +2,7 @@ use crate::attrs::AttributeSpanWrapper; use crate::field::Field; use crate::model::Model; use crate::util::{inner_of_option_ty, is_option_ty, wrap_in_dummy_mod}; -use proc_macro2::TokenStream; +use proc_macro2::{Span, TokenStream}; use quote::quote; use quote::quote_spanned; use syn::parse_quote; @@ -177,7 +177,7 @@ fn derive_into_single_table( fn field_ty_embed(field: &Field, lifetime: Option) -> TokenStream { let field_ty = &field.ty; - let span = field.span; + let span = Span::mixed_site().located_at(field.span); quote_spanned!(span=> #lifetime #field_ty) } @@ -193,7 +193,7 @@ fn field_ty_serialize_as( treat_none_as_default_value: bool, ) -> Result { let column_name = field.column_name()?.to_ident()?; - let span = field.span; + let span = Span::mixed_site().located_at(field.span); if treat_none_as_default_value { let inner_ty = inner_of_option_ty(ty); @@ -242,7 +242,7 @@ fn field_ty( treat_none_as_default_value: bool, ) -> Result { let column_name = field.column_name()?.to_ident()?; - let span = field.span; + let span = Span::mixed_site().located_at(field.span); if treat_none_as_default_value { let inner_ty = inner_of_option_ty(&field.ty); diff --git a/diesel_derives/src/queryable_by_name.rs b/diesel_derives/src/queryable_by_name.rs index 7848e20ed58c..3bfdb340442c 100644 --- a/diesel_derives/src/queryable_by_name.rs +++ b/diesel_derives/src/queryable_by_name.rs @@ -1,11 +1,15 @@ -use proc_macro2::TokenStream; -use quote::quote; -use syn::{parse_quote, parse_quote_spanned, DeriveInput, Ident, LitStr, Result, Type}; +use { + proc_macro2::{Span, TokenStream}, + quote::quote, + syn::{parse_quote, parse_quote_spanned, DeriveInput, Ident, LitStr, Result, Type}, +}; -use crate::attrs::AttributeSpanWrapper; -use crate::field::{Field, FieldName}; -use crate::model::Model; -use crate::util::wrap_in_dummy_mod; +use crate::{ + attrs::AttributeSpanWrapper, + field::{Field, FieldName}, + model::Model, + util::wrap_in_dummy_mod, +}; pub fn derive(item: DeriveInput) -> Result { let model = Model::from_item(&item, false, false)?; @@ -45,7 +49,7 @@ pub fn derive(item: DeriveInput) -> Result { for field in model.fields() { let where_clause = generics.where_clause.get_or_insert(parse_quote!(where)); - let span = field.span; + let span = Span::mixed_site().located_at(field.span); let field_ty = field.ty_for_deserialize(); if field.embed() { where_clause @@ -63,7 +67,7 @@ pub fn derive(item: DeriveInput) -> Result { let field_check_bound = model.fields().iter().filter(|f| !f.embed()).flat_map(|f| { backends.iter().map(move |b| { let field_ty = f.ty_for_deserialize(); - let span = f.span; + let span = Span::mixed_site().located_at(f.span); let ty = sql_type(f, model).unwrap(); quote::quote_spanned! {span => #field_ty: diesel::deserialize::FromSqlRow<#ty, #b> diff --git a/diesel_derives/src/selectable.rs b/diesel_derives/src/selectable.rs index 7f85863b1121..22bf04057aff 100644 --- a/diesel_derives/src/selectable.rs +++ b/diesel_derives/src/selectable.rs @@ -1,12 +1,10 @@ -use proc_macro2::TokenStream; -use quote::quote; -use syn::spanned::Spanned; -use syn::DeriveInput; -use syn::{parse_quote, Result}; +use { + proc_macro2::{Span, TokenStream}, + quote::quote, + syn::{parse_quote, spanned::Spanned, DeriveInput, Result}, +}; -use crate::field::Field; -use crate::model::Model; -use crate::util::wrap_in_dummy_mod; +use crate::{field::Field, model::Model, util::wrap_in_dummy_mod}; pub fn derive(item: DeriveInput) -> Result { let model = Model::from_item(&item, false, false)?; @@ -51,7 +49,7 @@ pub fn derive(item: DeriveInput) -> Result { .filter(|(f, _)| !f.embed()) .flat_map(|(f, ty)| { backends.iter().map(move |b| { - let span = f.ty.span(); + let span = Span::mixed_site().located_at(f.ty.span()); let field_ty = to_field_ty_bound(f.ty_for_deserialize())?; Ok(syn::parse_quote_spanned! {span => #field_ty: diesel::deserialize::FromSqlRow, #b> diff --git a/diesel_derives/src/table.rs b/diesel_derives/src/table.rs index 90ee6d161290..9a46b84bbea1 100644 --- a/diesel_derives/src/table.rs +++ b/diesel_derives/src/table.rs @@ -1,5 +1,5 @@ use diesel_table_macro_syntax::{ColumnDef, TableDecl}; -use proc_macro2::TokenStream; +use proc_macro2::{Span, TokenStream}; use syn::parse_quote; use syn::Ident; @@ -76,7 +76,7 @@ pub(crate) fn expand(input: TableDecl) -> TokenStream { } message += "\t}\n}"; - let span = input.table_name.span(); + let span = Span::mixed_site().located_at(input.table_name.span()); return quote::quote_spanned! {span=> compile_error!(#message); }; @@ -129,7 +129,7 @@ pub(crate) fn expand(input: TableDecl) -> TokenStream { let reexport_column_from_dsl = input.column_defs.iter().map(|c| { let column_name = &c.column_name; if c.column_name == *table_name { - let span = c.column_name.span(); + let span = Span::mixed_site().located_at(c.column_name.span()); let message = format!( "Column `{column_name}` cannot be named the same as it's table.\n\ You may use `#[sql_name = \"{column_name}\"]` to reference the table's \ @@ -684,7 +684,7 @@ fn generate_op_impl(op: &str, tpe: &syn::Ident) -> TokenStream { fn expand_column_def(column_def: &ColumnDef) -> TokenStream { // TODO get a better span here as soon as that's // possible using stable rust - let span = column_def.column_name.span(); + let span = Span::mixed_site().located_at(column_def.column_name.span()); let meta = &column_def.meta; let column_name = &column_def.column_name; let sql_name = &column_def.sql_name; diff --git a/diesel_derives/src/util.rs b/diesel_derives/src/util.rs index 3fb06b246435..51e243a9990b 100644 --- a/diesel_derives/src/util.rs +++ b/diesel_derives/src/util.rs @@ -93,7 +93,8 @@ where } pub fn wrap_in_dummy_mod(item: TokenStream) -> TokenStream { - // #[allow(unused_qualifications)] can be removed if https://github.com/rust-lang/rust/issues/130277 gets done + // allow(unused_qualifications) is here as it hard to unsure the span is correctly set. Should stay until it is + // checked by CI. See https://github.com/rust-lang/rust/issues/130277 for more details. quote! { #[allow(unused_imports)] #[allow(unused_qualifications)]