Repository navigation
Closed
Description
Hi!
I noticed a naming collision when using actix-web and SQLx together. Basically, if I have an endpoint password, the various routing macros will create a struct with name password. In Rust, a structure name cannot be used as an identifier for a variable, which is exactly what happens when using the query! macro with for example 'password' as a parameter. I don't exactly know how the macro works, so I sadly cannot give any suggestions on how to fix this.
Here's an example of the error I am getting when compiling:
error[E0308]: mismatched types
--> backend/src/auth.rs:11:8
|
11 | if sqlx::query!(
| ________^
12 | | r#"
13 | | SELECT * FROM users WHERE
14 | | uname = $1 AND
15 | | password = $2;
16 | | "#,
17 | | &payload.identifier,
18 | | &payload.password,
19 | | )
| | ^
| | |
| | expected struct `String`, found struct `password`
| | this expression has type `String`
| |_____`password` is interpreted as a unit struct, not a new binding
| help: introduce a new binding instead: `other_password`
...
63 | #[post("/password")]
| -------------------- unit struct defined here
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0308]: mismatched types
--> backend/src/auth.rs:11:8
|
11 | if sqlx::query!(
| ________^
12 | | r#"
13 | | SELECT * FROM users WHERE
14 | | uname = $1 AND
15 | | password = $2;
16 | | "#,
17 | | &payload.identifier,
18 | | &payload.password,
19 | | )
| |_____^ expected struct `String`, found struct `password`I'm not looking for a workaround, just thought I'd let you know this was an issue.
Reacted by Jonas Platte
Metadata
Metadata
Assignees
Labels
No labels
Activity
abonander commentedon Jul 11, 2021
The name of the actual function you apply
#[post]to doesn't matter for routing purposes, so you can just rename that. It's just what the macro uses to name the struct. I'd recommend something like:abonander commentedon Jul 11, 2021
Otherwise I'm closing this since I don't really see anything actionable on our end. You could also rename the column in the query, see the manual for your SQL database for how to do that.
jplatte commentedon Jul 11, 2021
Why would this not be actionable? The macros can just avoid using column names for match patterns, at least that's what it sounds like is happening.
jplatte commentedon Jul 11, 2021
… or alternatively, the new
Span::mixed_sitemight be a solution too.mehcode commentedon Jul 11, 2021
What I think is happening is we are making local bindings for each field in the final structure.
I agree with @abonander that this is way easier to fix from the app side and just rename the route, but I do also agree with @jplatte here and think there might be something we can do to avoid any conflicts as these aren't public identifiers but macro-generated ones. I think even without macro hygiene we could do:
Which would fix this in a way that it won't likely happen again.
fix(macros): prefix generated variable names in `query_as!()`
fix(macros): prefix generated variable names in `query_as!()`
fix(macros): prefix generated variable names in `query_as!()` (#1336)
Merge launchbadge/sqlx master into JetASAP/sqlx master (#12)
Update ws-support (#15)