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

#[sqlx::test] imported with #[macro_use] causes a stack overflow in rustc #2017

Closed
moubctez opened this issue Aug 3, 2022 · 1 comment · Fixed by #2028
Closed

#[sqlx::test] imported with #[macro_use] causes a stack overflow in rustc #2017

moubctez opened this issue Aug 3, 2022 · 1 comment · Fixed by #2028

Comments

@moubctez
Copy link

moubctez commented Aug 3, 2022

My minimal setup:

cargo new --lib --vcs none testsqlx
cd testsqlx
cargo add sqlx -Fruntime-tokio-native-tls

Contents of src/lib.rs

#[macro_use]
extern crate sqlx;

#[cfg(test)]
mod tests {
    #[test]
    fn test() {}
}

Then

cargo test --lib

Ends with

thread 'rustc' has overflowed its stack
fatal runtime error: stack overflow
error: could not compile `testsqlx`

This is Rust 1.62.1. It fails on both macOS and FreeBSD, so I guess it is not platform related.

@abonander
Copy link
Collaborator

This is happening because #[sqlx::test] emits code with the #[test] attribute in it (expecting the #[test] from std) but because you've shadowed that with #[macro_use] extern crate sqlx; it just recurses into itself instead.

It looks like #[tokio::test] avoids this problem by referencing the #[test] attribute with its fully qualified path: https://github.com/tokio-rs/tokio/blob/master/tokio-macros/src/entry.rs#L378

So the fix would be to just do the same thing here and here.

In the meantime, I would recommend just using #[sqlx::test] fully qualified like the examples show.

@abonander abonander changed the title Sqlx 0.6.1: cargo test causes rustc to overflow the stack #[sqlx::test] imported with #[macro_use] causes a stack overflow in rustc Aug 4, 2022
alexander-jackson added a commit to alexander-jackson/sqlx that referenced this issue Aug 6, 2022
When the `#[sqlx::test]` macro is imported using `#[macro_use]` such as
in the following example:

```rust
extern crate sqlx;

mod tests {
    #[test]
    fn something() {}
}
```

then the `#[test]` generated by the macro will refer to itself instead
of the standard Rust `#[test]` macro. This will cause `rustc` to
recursively expand it and produce the following error message:

```
thread 'rustc' has overflowed its stack
fatal runtime error: stack overflow
```

Instead, we can just refer to the standard macro by using its fully
qualified path.

This PR:
* Swaps `#[test]` usages in `#[sqlx::test]` for their hygenic path to
  prevent recursive expansion alongside `#[macro_use]`

Closes launchbadge#2017.
abonander pushed a commit that referenced this issue Aug 10, 2022
When the `#[sqlx::test]` macro is imported using `#[macro_use]` such as
in the following example:

```rust
extern crate sqlx;

mod tests {
    #[test]
    fn something() {}
}
```

then the `#[test]` generated by the macro will refer to itself instead
of the standard Rust `#[test]` macro. This will cause `rustc` to
recursively expand it and produce the following error message:

```
thread 'rustc' has overflowed its stack
fatal runtime error: stack overflow
```

Instead, we can just refer to the standard macro by using its fully
qualified path.

This PR:
* Swaps `#[test]` usages in `#[sqlx::test]` for their hygenic path to
  prevent recursive expansion alongside `#[macro_use]`

Closes #2017.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants