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 22 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)
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
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/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 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.

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
Loading