-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
proc_macro: Stabilize Span::resolved_at
and Span::located_at
#69041
Conversation
Span::resolve_at
and Span::located_at
Span::resolved_at
and Span::located_at
@SergioBenitez & @jebrosen may be interested in this as well. |
Yes, I suspect |
There are a few alternatives in terms of naming and the function signatures here. The primary alternative is some API looking like,
, which has some issues.
This kind of promises that
So, let span = input_subspan.resolved_at(Span::def_site()); // Nice |
ping @dtolnay, this is waiting on you. |
@rust-lang/libs @rust-lang/lang: This PR stabilizes these two methods of impl Span {
/// Creates a new span with the same line/column information as `self` but
/// that resolves symbols as though it were at `other`.
pub fn resolved_at(&self, other: Span) -> Span;
/// Creates a new span with the same name resolution behavior as `self` but
/// with the line/column information of `other`.
pub fn located_at(&self, other: Span) -> Span;
} These are for mixing and matching the two pieces of information held by Span, source location and hygiene (≈ name resolution) context. We have experience with these methods in Serde, e.g. serde-rs/serde@6457331. To take a look at one of the diffs from that commit: - quote!(try!(_serde::de::SeqAccess::next_element::<#field_ty>(&mut __seq)))
+ let span = Span::def_site().located_at(field.original.span());
+ let func = quote_spanned!(span=> _serde::de::SeqAccess::next_element::<#field_ty>);
+ quote!(try!(#func(&mut __seq))) In this code:
The green implementation uses Span::located_at to combine def_site's hygiene context with the caller's field's source location to get compilable code that produces good error messages. @rfcbot fcp merge |
Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members:
No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Reviewed, but do we have relevant tests for these methods? (i.e. as exposed, and not via tests exercising the deeper internal mechanism) |
No, we don't. I'll add a test. |
@rfcbot reviewed My feelings here are similar to my comments from |
45a5f04
to
bc548ed
Compare
Test is added. |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. The RFC will be merged soon. |
bc548ed
to
966a295
Compare
@bors r+ |
📌 Commit 966a295 has been approved by |
Rollup of 7 pull requests Successful merges: - rust-lang#69041 (proc_macro: Stabilize `Span::resolved_at` and `Span::located_at`) - rust-lang#69813 (Implement BitOr and BitOrAssign for the NonZero integer types) - rust-lang#70712 (stabilize BTreeMap::remove_entry) - rust-lang#71168 (Deprecate `{Box,Rc,Arc}::into_raw_non_null`) - rust-lang#71544 (Replace filter_map().next() calls with find_map()) - rust-lang#71545 (Fix comment in docstring example for Error::kind) - rust-lang#71548 (Add missing Send and Sync impls for linked list Cursor and CursorMut.) Failed merges: r? @ghost
proc_macro: Fix since attributes for new Span methods Added in rust-lang#69041, took a while to merge so the since attributes went out of date.
@@ -352,14 +352,14 @@ impl Span { | |||
|
|||
/// Creates a new span with the same line/column information as `self` but | |||
/// that resolves symbols as though it were at `other`. | |||
#[unstable(feature = "proc_macro_span", issue = "54725")] | |||
#[stable(feature = "proc_macro_span_resolved_at", since = "1.43.0")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So maybe I'm being stupid but since this was merged on the 25th April shouldn't this be since = "1.44.0"
what with 1.43.0 being released on the 23rd April
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With 1.43 released before this was merged, it's actually going to be stabilized in 1.45 (the GitHub milestone this PR is assigned to is also the one for 1.45). I've already opened #71574 to fix the attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I'm not crazy? Good to know :-)
These were stabilized in Rust in rust-lang/rust#69041
On compilers prior to 1.45 where this is not stable, these fall back to returning the span associated with resolution behavior, since the source location is only cosmetic. This is a reversal of the previous fallback implementation, which preserved source location because it does not track resolution location. The differnce is only observable with `span_locations` enabled. These methods were stabilized in Rust in rust-lang/rust#69041
On compilers prior to 1.45 where this is not stable, these fall back to returning the span associated with resolution behavior, since the source location is only cosmetic. This is a reversal of the previous fallback implementation, which preserved source location because it does not track resolution location. The differnce is only observable with `span_locations` enabled. These methods were stabilized in Rust in rust-lang/rust#69041
On compilers prior to 1.45 where this is not stable, these fall back to returning the span associated with resolution behavior, since the source location is only cosmetic. This is a reversal of the previous fallback implementation, which preserved source location because it does not track resolution location. The differnce is only observable with `span_locations` enabled. These methods were stabilized in Rust in rust-lang/rust#69041
Introduced in #47149.
Part of #54725.
Motivation: #68716 (comment).
Identifiers in proc macros may want to inherit span locations for diagnostics from one tokens (e.g. some tokens from the macro input), but resolve those identifiers from some different location (e.g. from the macro's definition site).
This becomes especially important when multiple resolution locations become available with stabilization of
Span::mixed_site
.Why I think this is the right API for setting span's location and hygiene - #69041 (comment).
r? @dtolnay