Repository: diesel-rs/diesel (language: rust)

Issue:
derive(Queryable) doesn't work with a field of `Field` type
<!--
If you want to report a bug, we added some points below which help us track down the problem faster.
-->

## Setup

### Versions

- **Rust:** 1.78.0
- **Diesel:** 2.2.0
- **Database:** PostgreSQL
- **Operating System** Linux

### Feature Flags

- **diesel:** none

## Problem Description


### What are you trying to accomplish?

I'm trying use a type called `Field` in my `Queryable` data structure. In my case it has `FromSql` implementation from `Jsonb`, but in a minimal example for reproduction below I aliased `i32` to make things as simple as possible.

### What is the expected output?

It should work.

### What is the actual output?

```
error[E0106]: missing lifetime specifier
 --> src/main.rs:7:12
  |
7 |     field: Field,
  |            ^^^^^ expected named lifetime parameter
  |
  = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html

error[E0106]: missing lifetime specifier
 --> src/main.rs:7:12
  |
7 |     field: Field,
  |            ^^^^^ expected named lifetime parameter

error[E0107]: missing generics for trait `diesel::row::Field`
   --> src/main.rs:7:12
    |
7   |     field: Field,
    |            ^^^^^ expected 1 generic argument
    |
note: trait defined here, with 1 generic parameter: `DB`
   --> /home/xfix/.cargo/registry/src/index.crates.io-6f17d22bba15001f/diesel-2.2.0/src/row.rs:100:11
    |
100 | pub trait Field<'a, DB: Backend> {
    |           ^^^^^     --
help: add missing generic argument
    |
7   |     field: Field<DB>,
    |                 ++++

error[E0782]: trait objects must include the `dyn` keyword
 --> src/main.rs:7:12
  |
7 |     field: Field,
  |            ^^^^^
  |
help: add `dyn` keyword before this trait
  |
7 |     field: dyn Field,
  |            +++

Some errors have detailed explanations: E0106, E0107, E0782.
For more information about an error, try `rustc --explain E0106`.
```

### Are you seeing any additional errors?


### Steps to reproduce

<!--
Please include as much of your codebase as needed to reproduce the error.  If the relevant files are large, please consider linking to a public repository or a [Gist](https://gist.github.com/). This includes normally the following parts:

* The exact code where your hit the problem
* Relevant parts your schema, so any `table!` macro calls required for
* Any other type definitions involved in the code, which produces your problem
-->

Compile the following code.

```rust
use diesel::Queryable;

type Field = i32;

#[derive(Queryable)]
pub struct Example {
    field: Field,
}

fn main() {}
```

## Checklist

- [x] I have already looked over the [issue tracker](https://github.com/diesel-rs/diesel/issues) and the [discussion forum](https://github.com/diesel-rs/diesel/discussions) for similar possible closed issues.
<!--
If you are unsure if your issue is a duplicate of an existing issue please link the issue in question here
--> 
- [x] This issue can be reproduced on Rust's stable channel. (Your issue will be
  closed if this is not the case)
- [x] This issue can be reproduced without requiring a third party crate

<!--
Thank you for your submission!  You're helping make Diesel more robust 🎉

We'll try to respond as quickly as possible.
-->



Discussion / hints:
Thanks for reporting. This is caused by a import to the `Field` trait in the relevant code generated by the `Queryable` derive here:

https://github.com/diesel-rs/diesel/blob/cc8a997ff58cd7917ba6d2c8498dcb0a7b84aec1/diesel_derives/src/queryable.rs#L49

The fix for that issue is to replace this (and all the other trait imports there) with `use diesel::row::Field as _;` to avoid the name collision. 

A fix should also add this example (maybe extended with other fields to match the other traits) as compile pass test there:

https://github.com/diesel-rs/diesel/blob/master/diesel_derives/tests/queryable.rs   

(Just having the structs + derive is sufficient for that)

It would be great to receive a PR for this.

Fix patch:
diff --git a/diesel_derives/src/queryable.rs b/diesel_derives/src/queryable.rs
index ac421365e8a4..72903241dedf 100644
--- a/diesel_derives/src/queryable.rs
+++ b/diesel_derives/src/queryable.rs
@@ -46,7 +46,7 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {
 
     Ok(wrap_in_dummy_mod(quote! {
         use diesel::deserialize::{self, FromStaticSqlRow, Queryable};
-        use diesel::row::{Row, Field};
+        use diesel::row::{Row as _, Field as _};
         use std::convert::TryInto;
 
         impl #impl_generics Queryable<(#(#sql_type,)*), __DB> for #struct_name #ty_generics


Test patch:
diff --git a/diesel_derives/tests/queryable.rs b/diesel_derives/tests/queryable.rs
index f2b11713b992..3c17d2ecc107 100644
--- a/diesel_derives/tests/queryable.rs
+++ b/diesel_derives/tests/queryable.rs
@@ -87,3 +87,16 @@ fn multiple_tables() {
         data
     );
 }
+
+#[test]
+fn name_conflict() {
+    type Field = i32;
+    type Record = i32;
+
+    #[derive(Debug, Clone, PartialEq, Eq, Queryable)]
+    struct MyStruct(Field, Record);
+
+    let conn = &mut connection();
+    let data = select(sql::<(Integer, Integer)>("1, 2")).get_result(conn);
+    assert_eq!(Ok(MyStruct(1, 2)), data);
+}