Skip to content

Commit b8c6d22

Browse files
added byte position range for proc_macro::Span
1 parent 35a0961 commit b8c6d22

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_span::def_id::CrateNum;
1818
use rustc_span::symbol::{self, sym, Symbol};
1919
use rustc_span::{BytePos, FileName, Pos, SourceFile, Span};
2020
use smallvec::{smallvec, SmallVec};
21-
use std::ops::Bound;
21+
use std::ops::{Bound, Range};
2222

2323
trait FromInternal<T> {
2424
fn from_internal(x: T) -> Self;
@@ -634,6 +634,10 @@ impl server::Span for Rustc<'_, '_> {
634634
span.source_callsite()
635635
}
636636

637+
fn position(&mut self, span: Self::Span) -> Range<u32> {
638+
Range { start: span.lo().0, end: span.lo().0 }
639+
}
640+
637641
fn start(&mut self, span: Self::Span) -> LineColumn {
638642
let loc = self.sess().source_map().lookup_char_pos(span.lo());
639643
LineColumn { line: loc.line, column: loc.col.to_usize() }

library/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+
);

library/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)