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 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8522e36
Change span
Sindbad-Walter Sep 16, 2024
5bcad57
Removes allow unused_qualifications to see if change works
Sindbad-Walter Sep 16, 2024
8545b03
Testing
Sindbad-Walter Sep 16, 2024
fac96f6
Testing seems to positive
Sindbad-Walter Sep 16, 2024
a7b7751
Puts back allow unused_qualifications as per discussion with Ten0
Sindbad-Walter Sep 16, 2024
491a00d
Adds comments to explain the allow
Sindbad-Walter Sep 16, 2024
f6cba14
Fix added space in multi replace
Sindbad-Walter Sep 16, 2024
1573b6f
come on...
Sindbad-Walter Sep 16, 2024
8f624fc
Orders the imports
Sindbad-Walter Sep 16, 2024
4fc94ac
Trying to see what breaks the tests
Sindbad-Walter Sep 16, 2024
3cc8e26
dumb-dumb
Sindbad-Walter Sep 16, 2024
c06a29e
Does this work ?
Sindbad-Walter Sep 16, 2024
42a6fb9
How about... this !
Sindbad-Walter Sep 16, 2024
f51f54e
This should fail
Sindbad-Walter Sep 17, 2024
4a0134a
Better error message
Sindbad-Walter Sep 17, 2024
098ecf4
Use previous formating
Sindbad-Walter Sep 17, 2024
4102157
Too many spaces
Sindbad-Walter Sep 17, 2024
fe3570e
formating
Sindbad-Walter Sep 17, 2024
9d276d0
Merge remote-tracking branch 'upstream/master' into fix_span_in_proc_…
Sindbad-Walter Sep 17, 2024
95aaeb1
Trying to understand the error formatting wanted
Sindbad-Walter Sep 17, 2024
4d918cc
last change seemed to have no effect
Sindbad-Walter Sep 17, 2024
6a97ede
trial and error, but mostly error
Sindbad-Walter Sep 17, 2024
b9fcc6b
Tried to put mixed_span everywhere
Sindbad-Walter Sep 18, 2024
57c2db2
updating queryable
Sindbad-Walter Oct 4, 2024
9269b2a
wip
Sindbad-Walter Oct 4, 2024
d0b5658
fix auto formatting
Sindbad-Walter Oct 4, 2024
32bc3b9
oops
Sindbad-Walter Oct 4, 2024
0878050
unused
Sindbad-Walter Oct 4, 2024
acd1e81
wip
Sindbad-Walter Oct 4, 2024
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)
6 changes: 3 additions & 3 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 @@ -27,7 +27,7 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {

if fields_for_update.is_empty() {
return Err(syn::Error::new(
proc_macro2::Span::call_site(),
proc_macro2::Span::mixed_site(),
"Deriving `AsChangeset` on a structure that only contains primary keys isn't supported.\n\
help: If you want to change the primary key of a row, you should do so with `.set(table::id.eq(new_id))`.\n\
note: `#[derive(AsChangeset)]` never changes the primary key of a row."
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
2 changes: 1 addition & 1 deletion diesel_derives/src/as_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {

if model.sql_types.is_empty() {
return Err(syn::Error::new(
proc_macro2::Span::call_site(),
proc_macro2::Span::mixed_site(),
"At least one `sql_type` is needed for deriving `AsExpression` on a structure.",
));
}
Expand Down
2 changes: 1 addition & 1 deletion diesel_derives/src/associations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {

if model.belongs_to.is_empty() {
return Err(syn::Error::new(
proc_macro2::Span::call_site(),
proc_macro2::Span::mixed_site(),
"At least one `belongs_to` is needed for deriving `Associations` on a structure.",
));
}
Expand Down
4 changes: 2 additions & 2 deletions diesel_derives/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,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
4 changes: 2 additions & 2 deletions diesel_derives/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ impl Model {
}) => Some(unnamed),
_ if !allow_unit_structs => {
return Err(syn::Error::new(
proc_macro2::Span::call_site(),
proc_macro2::Span::mixed_site(),
"This derive can only be used on non-unit structs",
));
}
_ => None,
};

let mut table_names = vec![];
let mut primary_key_names = vec![Ident::new("id", Span::call_site())];
let mut primary_key_names = vec![Ident::new("id", Span::mixed_site())];
let mut treat_none_as_default_value = None;
let mut treat_none_as_null = None;
let mut belongs_to = vec![];
Expand Down
4 changes: 2 additions & 2 deletions diesel_derives/src/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {
});
let sql_type = &(0..model.fields().len())
.map(|i| {
let i = Ident::new(&format!("__ST{i}"), Span::call_site());
let i = Ident::new(&format!("__ST{i}"), Span::mixed_site());
quote!(#i)
})
.collect::<Vec<_>>();
Expand All @@ -33,7 +33,7 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {
.params
.push(parse_quote!(__DB: diesel::backend::Backend));
for id in 0..model.fields().len() {
let ident = Ident::new(&format!("__ST{id}"), Span::call_site());
let ident = Ident::new(&format!("__ST{id}"), Span::mixed_site());
generics.params.push(parse_quote!(#ident));
}
{
Expand Down
6 changes: 3 additions & 3 deletions diesel_derives/src/queryable_by_name.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;
use syn::{parse_quote, parse_quote_spanned, DeriveInput, Ident, LitStr, Result, Type};

Expand Down Expand Up @@ -45,7 +45,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 +63,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
4 changes: 2 additions & 2 deletions diesel_derives/src/selectable.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;
use syn::spanned::Spanned;
use syn::DeriveInput;
Expand Down Expand Up @@ -51,7 +51,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
4 changes: 2 additions & 2 deletions diesel_derives/src/sql_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn sqlite_tokens(item: &DeriveInput, model: &Model) -> Option<TokenStream> {
model
.sqlite_type
.as_ref()
.map(|sqlite_type| Ident::new(&sqlite_type.name.value(), Span::call_site()))
.map(|sqlite_type| Ident::new(&sqlite_type.name.value(), Span::mixed_site()))
.and_then(|ty| {
if cfg!(not(feature = "sqlite")) {
return None;
Expand All @@ -67,7 +67,7 @@ fn mysql_tokens(item: &DeriveInput, model: &Model) -> Option<TokenStream> {
model
.mysql_type
.as_ref()
.map(|mysql_type| Ident::new(&mysql_type.name.value(), Span::call_site()))
.map(|mysql_type| Ident::new(&mysql_type.name.value(), Span::mixed_site()))
.and_then(|ty| {
if cfg!(not(feature = "mysql")) {
return None;
Expand Down
20 changes: 10 additions & 10 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 @@ -46,10 +46,10 @@ pub(crate) fn expand(input: TableDecl) -> TokenStream {
let primary_key: TokenStream = match input.primary_keys.as_ref() {
None if column_names.contains(&&syn::Ident::new(
DEFAULT_PRIMARY_KEY_NAME,
proc_macro2::Span::call_site(),
proc_macro2::Span::mixed_site(),
)) =>
{
let id = syn::Ident::new(DEFAULT_PRIMARY_KEY_NAME, proc_macro2::Span::call_site());
let id = syn::Ident::new(DEFAULT_PRIMARY_KEY_NAME, proc_macro2::Span::mixed_site());
parse_quote! {
#id
}
Expand All @@ -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 @@ -527,15 +527,15 @@ fn generate_valid_grouping_for_table_columns(table: &TableDecl) -> Vec<TokenStre
for (id, right_col) in table.column_defs.iter().enumerate() {
for left_col in table.column_defs.iter().skip(id) {
let right_to_left = if Some(left_col.column_name.to_string()) == primary_key {
Ident::new("Yes", proc_macro2::Span::call_site())
Ident::new("Yes", proc_macro2::Span::mixed_site())
} else {
Ident::new("No", proc_macro2::Span::call_site())
Ident::new("No", proc_macro2::Span::mixed_site())
};

let left_to_right = if Some(right_col.column_name.to_string()) == primary_key {
Ident::new("Yes", proc_macro2::Span::call_site())
Ident::new("Yes", proc_macro2::Span::mixed_site())
} else {
Ident::new("No", proc_macro2::Span::call_site())
Ident::new("No", proc_macro2::Span::mixed_site())
};

let left_col = &left_col.column_name;
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
7 changes: 4 additions & 3 deletions 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.

Copy link
Member

Choose a reason for hiding this comment

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

That seems like a lot of work for something that's seldom going to be noticed considering the warning is not enabled by default. It looks like it's going to be hard to maintain if we don't have it in our CI.

To make that practical to fix and maintain, it looks like syn's Spanned trait should have a span_mixed method that constructs this.

// checked by CI. See https://github.com/rust-lang/rust/issues/130277 for more details.
quote! {
#[allow(unused_imports)]
#[allow(unused_qualifications)]
Expand Down Expand Up @@ -154,12 +155,12 @@ pub fn ty_for_foreign_derive(item: &DeriveInput, model: &Model) -> Result<Type>
Data::Struct(ref body) => match body.fields.iter().next() {
Some(field) => Ok(field.ty.clone()),
None => Err(syn::Error::new(
proc_macro2::Span::call_site(),
proc_macro2::Span::mixed_site(),
"foreign_derive requires at least one field",
)),
},
_ => Err(syn::Error::new(
proc_macro2::Span::call_site(),
proc_macro2::Span::mixed_site(),
"foreign_derive can only be used with structs",
)),
}
Expand Down
2 changes: 1 addition & 1 deletion dsl_auto_type/src/auto_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(crate) fn auto_type_impl(
(false, true, _) => false,
(true, true, _) => {
return Err(syn::Error::new(
Span::call_site(),
Span::mixed_site(),
"type_alias and no_type_alias are mutually exclusive",
)
.into())
Expand Down
Loading