Fix inconsistent identifier spans in route codegen#2919
Open
JOT85 wants to merge 2 commits intorwf2:masterfrom
Open
Fix inconsistent identifier spans in route codegen#2919JOT85 wants to merge 2 commits intorwf2:masterfrom
JOT85 wants to merge 2 commits intorwf2:masterfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously, identifiers such as
__reqwere output with inconsistent spans. For example, inquery_decls, it was defined withSpan::call_site(), but inrequest_guard_declasguard.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:
In this case, the inconsistency arises because:
codegen_route, no span for__reqis specified, which I believe means it defaults toSpan::call_site(), so insidemacro_rules!, thenresponder_outcome_expr, the span used is from the identifier of the function, which is outside ofmacro_rules!.This causes the
__reqinresponder_outcomenot to resolve to the__reqdefined in themonomorphized_functionarguments.The span is now always
Span::call_site(), which resolves these issues.I believe
call_siteis 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.