Skip to content

Commit d6404af

Browse files
committed
Auto merge of rust-lang#109002 - michaelvanstraten:master, r=petrochenkov
Added byte position range for `proc_macro::Span` Currently, the [`Debug`](https://doc.rust-lang.org/beta/proc_macro/struct.Span.html#impl-Debug-for-Span) implementation for [`proc_macro::Span`](https://doc.rust-lang.org/beta/proc_macro/struct.Span.html#) calls the debug function implemented in the trait implementation of `server::Span` for the type `Rustc` in the `rustc-expand` crate. The current implementation, of the referenced function, looks something like this: ```rust fn debug(&mut self, span: Self::Span) -> String { if self.ecx.ecfg.span_debug { format!("{:?}", span) } else { format!("{:?} bytes({}..{})", span.ctxt(), span.lo().0, span.hi().0) } } ``` It returns the byte position of the [`Span`](https://doc.rust-lang.org/beta/proc_macro/struct.Span.html#) as an interpolated string. Because this is currently the only way to get a spans position in the file, I might lead someone, who is interested in this information, to parsing this interpolated string back into a range of bytes, which I think is a very non-rusty way. The proposed `position()`, method implemented in this PR, gives the ability to directly get this info. It returns a [`std::ops::Range`](https://doc.rust-lang.org/std/ops/struct.Range.html#) wrapping the lowest and highest byte of the [`Span`](https://doc.rust-lang.org/beta/proc_macro/struct.Span.html#). I put it behind the `proc_macro_span` feature flag because many of the other functions that have a similar footprint also are annotated with it, I don't actually know if this is right. It would be great if somebody could take a look at this, thank you very much in advanced.
2 parents 852fbc0 + c60814a commit d6404af

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

proc_macro/src/bridge/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::hash::Hash;
1414
use std::marker;
1515
use std::mem;
1616
use std::ops::Bound;
17+
use std::ops::Range;
1718
use std::panic;
1819
use std::sync::atomic::AtomicUsize;
1920
use std::sync::Once;
@@ -93,6 +94,7 @@ macro_rules! with_api {
9394
fn source_file($self: $S::Span) -> $S::SourceFile;
9495
fn parent($self: $S::Span) -> Option<$S::Span>;
9596
fn source($self: $S::Span) -> $S::Span;
97+
fn byte_range($self: $S::Span) -> Range<usize>;
9698
fn start($self: $S::Span) -> LineColumn;
9799
fn end($self: $S::Span) -> LineColumn;
98100
fn before($self: $S::Span) -> $S::Span;
@@ -519,3 +521,7 @@ pub struct ExpnGlobals<Span> {
519521
compound_traits!(
520522
struct ExpnGlobals<Span> { def_site, call_site, mixed_site }
521523
);
524+
525+
compound_traits!(
526+
struct Range<T> { start, end }
527+
);

proc_macro/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ mod diagnostic;
4444
pub use diagnostic::{Diagnostic, Level, MultiSpan};
4545

4646
use std::cmp::Ordering;
47-
use std::ops::RangeBounds;
47+
use std::ops::{Range, RangeBounds};
4848
use std::path::PathBuf;
4949
use std::str::FromStr;
5050
use std::{error, fmt};
@@ -488,6 +488,12 @@ impl Span {
488488
Span(self.0.source())
489489
}
490490

491+
/// Returns the span's byte position range in the source file.
492+
#[unstable(feature = "proc_macro_span", issue = "54725")]
493+
pub fn byte_range(&self) -> Range<usize> {
494+
self.0.byte_range()
495+
}
496+
491497
/// Gets the starting line/column in the source file for this span.
492498
#[unstable(feature = "proc_macro_span", issue = "54725")]
493499
pub fn start(&self) -> LineColumn {

0 commit comments

Comments
 (0)