Skip to content

Commit 7eec4b2

Browse files
Manishearthgitbot
authored and
gitbot
committed
Rollup merge of rust-lang#86165 - m-ou-se:proc-macro-span-shrink, r=dtolnay
Add proc_macro::Span::{before, after}. This adds `proc_macro::Span::before()` and `proc_macro::Span::after()` to get a zero width span at the start or end of the span. These are equivalent to rustc's `Span::shrink_to_lo()` and `Span::shrink_to_hi()` but with a less cryptic name. They are useful when generating diagnostlics like "missing \<thing\> after \<thing\>". E.g. ```rust syn::Error::new(ident.span().after(), "missing `:` after field name").into_compile_error() ```
2 parents fa05853 + cdc2c7b commit 7eec4b2

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

proc_macro/src/bridge/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ macro_rules! with_api {
160160
fn source($self: $S::Span) -> $S::Span;
161161
fn start($self: $S::Span) -> LineColumn;
162162
fn end($self: $S::Span) -> LineColumn;
163+
fn before($self: $S::Span) -> $S::Span;
164+
fn after($self: $S::Span) -> $S::Span;
163165
fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
164166
fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
165167
fn source_text($self: $S::Span) -> Option<String>;

proc_macro/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,18 @@ impl Span {
359359
self.0.end()
360360
}
361361

362+
/// Creates an empty span pointing to directly before this span.
363+
#[unstable(feature = "proc_macro_span_shrink", issue = "87552")]
364+
pub fn before(&self) -> Span {
365+
Span(self.0.before())
366+
}
367+
368+
/// Creates an empty span pointing to directly after this span.
369+
#[unstable(feature = "proc_macro_span_shrink", issue = "87552")]
370+
pub fn after(&self) -> Span {
371+
Span(self.0.after())
372+
}
373+
362374
/// Creates a new span encompassing `self` and `other`.
363375
///
364376
/// Returns `None` if `self` and `other` are from different files.

0 commit comments

Comments
 (0)