Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix span in proc macro #4265

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions diesel_compile_tests/tests/fail/derive/bad_insertable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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<diesel::sql_types::Integer>`
|
Expand All @@ -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<diesel::sql_types::Integer>`
= 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<diesel::sql_types::Text>`
|
Expand All @@ -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<diesel::sql_types::Text>`
= 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<diesel::sql_types::Integer>`
|
Expand All @@ -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<diesel::sql_types::Integer>`
= 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<diesel::sql_types::Text>`
|
Expand All @@ -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<diesel::sql_types::Text>`
= note: this error originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info)
4 changes: 2 additions & 2 deletions diesel_derives/src/as_changeset.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -172,7 +172,7 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {

fn field_changeset_ty_embed(field: &Field, lifetime: Option<TokenStream>) -> 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)
}

Expand Down
11 changes: 6 additions & 5 deletions diesel_derives/src/field.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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(),
Expand Down
8 changes: 4 additions & 4 deletions diesel_derives/src/insertable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -177,7 +177,7 @@ fn derive_into_single_table(

fn field_ty_embed(field: &Field, lifetime: Option<TokenStream>) -> 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)
}

Expand All @@ -193,7 +193,7 @@ fn field_ty_serialize_as(
treat_none_as_default_value: bool,
) -> Result<TokenStream> {
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);

Expand Down Expand Up @@ -242,7 +242,7 @@ fn field_ty(
treat_none_as_default_value: bool,
) -> Result<TokenStream> {
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);

Expand Down
22 changes: 13 additions & 9 deletions diesel_derives/src/queryable_by_name.rs
Original file line number Diff line number Diff line change
@@ -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<TokenStream> {
let model = Model::from_item(&item, false, false)?;
Expand Down Expand Up @@ -45,7 +49,7 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {

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
Expand All @@ -63,7 +67,7 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {
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>
Expand Down
16 changes: 7 additions & 9 deletions diesel_derives/src/selectable.rs
Original file line number Diff line number Diff line change
@@ -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<TokenStream> {
let model = Model::from_item(&item, false, false)?;
Expand Down Expand Up @@ -51,7 +49,7 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {
.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<diesel::dsl::SqlTypeOf<#ty>, #b>
Expand Down
8 changes: 4 additions & 4 deletions diesel_derives/src/table.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion diesel_derives/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather prefer auditing the derive crate to ensure all usages of spans are encoded correctly. Yes that's a bit of work but it should fix all these issues at once.
The alternative is to remove this allow and start searching again if something pops up in some code.

// checked by CI. See https://github.com/rust-lang/rust/issues/130277 for more details.
quote! {
#[allow(unused_imports)]
#[allow(unused_qualifications)]
Expand Down
Loading