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

Fix inconsistent identifier spans in route codegen #2919

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

JOT85
Copy link

@JOT85 JOT85 commented Mar 12, 2025

Previously, identifiers such as __req were output with inconsistent spans. For example, in query_decls, it was defined with Span::call_site(), but in request_guard_decl as guard.ty.span(). The problem is, when used with macros, these spans can be different and cause the identifiers to fail to resolve.

A simple failure example is:

macro_rules! empty_get {
    ($name:ident) => {
        #[get("/")]
        async fn $name() {}
    }
}

empty_get!(test);

In this case, the inconsistency arises because:

  • In codegen_route, no span for __req is specified, which I believe means it defaults to Span::call_site(), so inside macro_rules!, then
  • in responder_outcome_expr, the span used is from the identifier of the function, which is outside of macro_rules!.

This causes the __req in responder_outcome not to resolve to the __req defined in the monomorphized_function arguments.

The span is now always Span::call_site(), which resolves these issues.

I believe call_site is acceptable, since it was already used as the span for these variables elsewhere. In addition, I didn't spot anywhere in the code where this could cause a problem.

Previously, identifiers such as `__req` were output with inconsistent
spans. For example, in
[`query_decls`](https://github.com/rwf2/Rocket/blob/28891e8072136f4641a33fb8c3f2aafce9d88d5b/core/codegen/src/attribute/route/mod.rs#L43),
it was defined with `Span::call_site()`, but in
[`request_guard_decl`](https://github.com/rwf2/Rocket/blob/28891e8072136f4641a33fb8c3f2aafce9d88d5b/core/codegen/src/attribute/route/mod.rs#L127)
as `guard.ty.span()`. The problem is, when used with macros, these spans
can be different and cause the identifiers to fail to resolve.

A simple failure example is:

```rust
macro_rules! empty_get {
    ($name:ident) => {
        #[get("/")]
        async fn $name() {}
    }
}

empty_get!(test);
```

In this case, the inconsistency arises because:

- In
  [`codegen_route`](https://github.com/rwf2/Rocket/blob/28891e8072136f4641a33fb8c3f2aafce9d88d5b/core/codegen/src/attribute/route/mod.rs#L380),
  no span for `__req` is specified, which I believe means it defaults to
  `Span::call_site()`, so inside `macro_rules!`, then
- in
  [`responder_outcome_expr`](https://github.com/rwf2/Rocket/blob/28891e8072136f4641a33fb8c3f2aafce9d88d5b/core/codegen/src/attribute/route/mod.rs#L299),
  the span used is from the identifier of the function, which is outside
  of `macro_rules!`.

This causes the `__req` in `responder_outcome` not to resolve to the
`__req` defined in the `monomorphized_function` arguments.

The span is now always `Span::call_site()`, which resolves these issues.

I believe `call_site` is acceptable, since it was already used as the
span for these variables elsewhere. In addition, I didn't spot anywhere
in the code where this could cause a problem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant