Skip to content

Commit ef99b15

Browse files
added byte position range for proc_macro::Span
1 parent a60c309 commit ef99b15

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

proc_macro/src/bridge/mod.rs

Lines changed: 7 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 position($self: $S::Span) -> Range<u32>;
9698
fn start($self: $S::Span) -> LineColumn;
9799
fn end($self: $S::Span) -> LineColumn;
98100
fn before($self: $S::Span) -> $S::Span;
@@ -293,6 +295,7 @@ mark_noop! {
293295
&'_ str,
294296
String,
295297
u8,
298+
u32,
296299
usize,
297300
Delimiter,
298301
LitKind,
@@ -519,3 +522,7 @@ pub struct ExpnGlobals<Span> {
519522
compound_traits!(
520523
struct ExpnGlobals<Span> { def_site, call_site, mixed_site }
521524
);
525+
526+
compound_traits!(
527+
struct Range<T> { start, end }
528+
);

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, iter};
@@ -488,6 +488,12 @@ impl Span {
488488
Span(self.0.source())
489489
}
490490

491+
/// Returns the spans byte position range in the source file.
492+
#[unstable(feature = "proc_macro_span", issue = "54725")]
493+
pub fn position(&self) -> Range<u32> {
494+
self.0.position()
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)