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

Add a flag to help users better debug bad Queryable impls #1615

Closed
wants to merge 1 commit into from

Conversation

sgrif
Copy link
Member

@sgrif sgrif commented Apr 5, 2018

Since the impl we generate is so generic, when a bad Queryable impl is
given, the error doesn't occur until they actually try to load a query.
This ends up being really non-local, and gives little to no feedback
about which field is actually the problem.

There is a new option called #[check_types] which completely changes
how the impl is generated, in such a way that when the unstable
feature is enabled the error will point at the specific field that is
the problem. The actual message is the same, but where it points makes
debugging much easier.

This flag also generates code which validates that your fields are in
the right order to match your columns.

Unfortunately, we cannot have this generate an actual usable impl.
Trait bounds not matching in types or where clauses will always result
in an error pointing at the derive itself, which is no more helpful than
where we started. For that reason, we can't actually have any usable
Row type. So this flag is for debugging purposes only.

This does not help with cases where the number of fields is incorrect.

@sgrif sgrif requested a review from a team April 5, 2018 15:09
/// name -> Text,
/// hair_color -> Nullable<Text>,
/// created_at -> Timestamp,
/// updated_at -> Timestamp,
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be Nullable<Timestamp>

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes good catch

/// |
/// 24| / updated_at: SystemTime,
/// |
/// = note: expected type `users::columns::created_at`
Copy link
Member

Choose a reason for hiding this comment

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

what's going on here ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Some generated code to assert order like this:

let users::updated_at = users::all_columns.4

That code gets a span which points at the field definition as its source location

Copy link
Member

Choose a reason for hiding this comment

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

Ohhh I didn't see the order was wrong :D

use self::std::result::Result::Err;
use self::std::convert::Into;

Err("`#[check_types]` is only for debugging purposes".into())
Copy link
Member

Choose a reason for hiding this comment

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

I think that should rather panic than return a Err, because this is clearly a programmer mistake.
(We could even think about using compile_error! if this typechecks correctly.)

Copy link
Member

Choose a reason for hiding this comment

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

Nah, because you could have lots of code, add a column to you Queryable struct and still want the project to compile instead of throwing 20 000 compile errors telling you that #[check_types] is for debugging only.

/// found type `users::columns::created_at`
/// ```
///
/// Keep in mind that `#[check_types]` is only for debugging purposes. The impl
Copy link
Member

Choose a reason for hiding this comment

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

We should mention somewhere here that this feature needs the unstable feature and a nightly compiler.

Copy link
Member

Choose a reason for hiding this comment

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

@sgrif I do not see this anywhere mentioned.

Since the impl we generate is so generic, when a bad `Queryable` impl is
given, the error doesn't occur until they actually try to load a query.
This ends up being really non-local, and gives little to no feedback
about which field is actually the problem.

There is a new option called `#[check_types]` which completely changes
how the impl is generated, in such a way that when the `unstable`
feature is enabled the error will point at the specific field that is
the problem. The actual message is the same, but where it points makes
debugging much easier.

This flag also generates code which validates that your fields are in
the right order to match your columns.

Unfortunately, we cannot have this generate an actual usable impl.
Trait bounds not matching in types or where clauses will always result
in an error pointing at the derive itself, which is no more helpful than
where we started. For that reason, we can't actually have any usable
`Row` type. So this flag is for debugging purposes only.

This does not help with cases where the number of fields is incorrect.
@@ -82,6 +82,11 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
rounding issues. This is primarily to support things like `avg(int_col)`,
which we define as returning `Numeric`

* `#[check_types]` has been added to help debug missing `Queryable` impls. See
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is right

Copy link
Member Author

Choose a reason for hiding this comment

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

What's wrong with it?

Copy link
Member

Choose a reason for hiding this comment

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

They're not missing, just wrong impls

Copy link
Member Author

Choose a reason for hiding this comment

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

I worded it this way because the message the user gets is literally "Queryable<...> is not implemented"

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense I guess

@weiznich
Copy link
Member

weiznich commented Mar 1, 2023

That's now basically implemented by #3359

@weiznich weiznich closed this Mar 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants