Skip to content

Name conflict when using actix-web #1322

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.

Activity

  1. abonander commented on Jul 11, 2021

    @abonander
    Collaborator

    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:

    #[post("/password")]
    async fn set_user_password(...) -> _ { ... }
  2. abonander commented on Jul 11, 2021

    @abonander
    Collaborator

    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.

  3. jplatte commented on Jul 11, 2021

    @jplatte
    Contributor

    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.

  4. jplatte commented on Jul 11, 2021

    @jplatte
    Contributor

    … or alternatively, the new Span::mixed_site might be a solution too.

  5. mehcode commented on Jul 11, 2021

    @mehcode
    Contributor

    What I think is happening is we are making local bindings for each field in the final structure.

    let password: Type = row.try_get_unchecked(0)?;
    // [...]
    
    let record = Record { password };

    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:

    let __sqlx_password: Type = row.try_get_unchecked(0)?;
    // [...]
    
    let record = Record { password: __sqlx_password };

    Which would fix this in a way that it won't likely happen again.

  6. added 2 commits that reference this issue on Jul 21, 2021
    f881faa
    9474a30
  7. added a commit that references this issue on Jul 22, 2021
    b3ae6e5
  8. added 2 commits that reference this issue on Nov 19, 2021
    6cdb35d
    b5a95c4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions