Repository: launchbadge/sqlx

Issue #1515: Upgrading from 0.5.5 -> 0.5.9 causes proc macro panics.
Created: 2021-10-26T11:45:35Z
Hi, I've got queries like these that fail to compile:
SELECT id, library_id, name, description, rating, year, added, poster_path, backdrop_path, media_type as "media_type: _" 
FROM media 
WHERE library_id = ? AND NOT media_type = "episode"

The query proc macro seems to panic with the message:
error: proc macro panicked
  --> database/src/media.rs:59:12
   |
59 |   ...   Ok(sqlx::query_as!(
   |  __________^
60 | | ...           Media,
61 | | ...           r#"SELECT id, library_id, name, description, rating, year, added, poster_path, backdrop_path, media_type as "media_type: _"...
62 | | ...           library_id
63 | | ...       )
   | |___________^
   |
   = help: message: no entry found for key
   = note: this error originates in the macro `sqlx::query_as` (in Nightly builds, run with -Z macro-backtrace for more info)


These queries compile fine on 0.5.5 though.

Duplicate issue #1642: Compilation fails with “attempted to communicate with a crashed background worker”
Created: 2022-01-19T23:34:09Z
Under certain conditions an sqlx::query! macro causes a compilation error with the message “attempted to communicate with a crashed background worker” (see steps to reproduce below).
Basic information

SQLx version is 0.5.10
Enabled features are runtime-tokio-native-tls, sqlite, and macros
sqlite3 --version is 3.37.0 2021-11-27 14:13:22 bd41822c7424d393a30e92ff6cb254d25c26769889c1499a18a0b9 f5dalt1

Steps to reproduce
First, create an SQLite database with the following schema using sqlite3 test.db < schema.sql:
CREATE TABLE things(
    thing_id INTEGER PRIMARY KEY,
    thing_name TEXT NOT NULL UNIQUE
);
CREATE TABLE thing_widgets(
    thing_id INTEGER NOT NULL UNIQUE,
    thing_widget INTEGER NOT NULL UNIQUE
);
Then, et the DATABASE_URL environment variable:
export DATABASE_URL=sqlite:test.db
Finally, try to compile the following code:
use sqlx::SqlitePool;

#[tokio::main]
async fn main() -> sqlx::Result<()> {
    let pool = SqlitePool::connect("sqlite:test.db").await?;

    let data = sqlx::query!(r"
        SELECT
            things.thing_id, thing_widget
            FROM things
            LEFT OUTER JOIN thing_widgets
                ON things.thing_id = thing_widgets.thing_id
            WHERE thing_name = ?
    ", "bob").fetch_optional(&pool).await?;
    
    Ok(())
}
Compilation should fail with the following error message (or something similar):
The error message
thread 'sqlx-sqlite-worker-0' panicked at 'no entry found for key', /home/arm32x/.ca
rgo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.10/src/sqlite/connection/
explain.rs:217:34
stack backtrace:
   0: rust_begin_unwind
             at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/pani
cking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/pan
icking.rs:107:14
   2: core::panicking::panic_display
             at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/pan
icking.rs:63:5
   3: core::option::expect_failed
             at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/opt
ion.rs:1637:5
   4: core::option::Option<T>::expect
             at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/core/src/opt
ion.rs:709:21
   5: <std::collections::hash::map::HashMap<K,V,S> as core::ops::index::Index<&Q>>::
index
             at /rustc/02072b482a8b5357f7fb5e5637444ae30e423c40/library/std/src/coll
ections/hash/map.rs:1158:9
   6: <ahash::hash_map::AHashMap<K,V,S> as core::ops::index::Index<&Q>>::index
             at /home/arm32x/.cargo/registry/src/github.com-1ecc6299db9ec823/ahash-0
.7.6/src/hash_map.rs:249:9
   7: sqlx_core::sqlite::connection::explain::explain
             at /home/arm32x/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-co
re-0.5.10/src/sqlite/connection/explain.rs:217:34
   8: sqlx_core::sqlite::connection::describe::describe
             at /home/arm32x/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-co
re-0.5.10/src/sqlite/connection/describe.rs:41:51
   9: sqlx_core::sqlite::connection::worker::ConnectionWorker::establish::{{closure}
}::{{closure}}
             at /home/arm32x/.cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-co
re-0.5.10/src/sqlite/connection/worker.rs:132:37
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtra
ce.

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new
?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.58.0 (02072b482 2022-01-11) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type
 bin

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
error: attempted to communicate with a crashed background worker
  --> src/main.rs:7:16
   |
7  |       let data = sqlx::query!(r"
   |  ________________^
8  | |         SELECT
9  | |             things.thing_id, thing_widget
10 | |             FROM things
...  |
13 | |             WHERE thing_name = ?
14 | |     ", "bob").fetch_optional(&pool).await?;
   | |_____________^
   |
   = note: this error originates in the macro `$crate::sqlx_macros::expand_query` (i
n Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `sqlx-bug` due to previous error

Closing comment (2022-01-20T23:55:19Z): The first error in the stack backtrace is the actual error. I suspect this is related to or a duplicate of #1515