about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael van Straten <michael@vanstraten.de>2023-03-10 21:16:35 +0100
committerMichael van Straten <michael@vanstraten.de>2023-03-10 21:19:25 +0100
commitb8c6d2211eb059c3f4ddb77d1cc0ae7278c792fc (patch)
tree59ce34444b2463775de5758ae5dc729b553fe129
parent35a0961bbc8fba75bb863c7835b39d431ad9fc5d (diff)
downloadrust-b8c6d2211eb059c3f4ddb77d1cc0ae7278c792fc.tar.gz
rust-b8c6d2211eb059c3f4ddb77d1cc0ae7278c792fc.zip
added byte position range for proc_macro::Span
-rw-r--r--compiler/rustc_expand/src/proc_macro_server.rs6
-rw-r--r--library/proc_macro/src/bridge/mod.rs7
-rw-r--r--library/proc_macro/src/lib.rs8
3 files changed, 19 insertions, 2 deletions
diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs
index 341ae18541b..a4578bd5835 100644
--- a/compiler/rustc_expand/src/proc_macro_server.rs
+++ b/compiler/rustc_expand/src/proc_macro_server.rs
@@ -18,7 +18,7 @@ use rustc_span::def_id::CrateNum;
 use rustc_span::symbol::{self, sym, Symbol};
 use rustc_span::{BytePos, FileName, Pos, SourceFile, Span};
 use smallvec::{smallvec, SmallVec};
-use std::ops::Bound;
+use std::ops::{Bound, Range};
 
 trait FromInternal<T> {
     fn from_internal(x: T) -> Self;
@@ -634,6 +634,10 @@ impl server::Span for Rustc<'_, '_> {
         span.source_callsite()
     }
 
+    fn position(&mut self, span: Self::Span) -> Range<u32> {
+        Range { start: span.lo().0, end: span.lo().0 }
+    }
+
     fn start(&mut self, span: Self::Span) -> LineColumn {
         let loc = self.sess().source_map().lookup_char_pos(span.lo());
         LineColumn { line: loc.line, column: loc.col.to_usize() }
diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs
index 4c1e196b5ad..aedfecf51a5 100644
--- a/library/proc_macro/src/bridge/mod.rs
+++ b/library/proc_macro/src/bridge/mod.rs
@@ -14,6 +14,7 @@ use std::hash::Hash;
 use std::marker;
 use std::mem;
 use std::ops::Bound;
+use std::ops::Range;
 use std::panic;
 use std::sync::atomic::AtomicUsize;
 use std::sync::Once;
@@ -93,6 +94,7 @@ macro_rules! with_api {
                 fn source_file($self: $S::Span) -> $S::SourceFile;
                 fn parent($self: $S::Span) -> Option<$S::Span>;
                 fn source($self: $S::Span) -> $S::Span;
+                fn position($self: $S::Span) -> Range<u32>;
                 fn start($self: $S::Span) -> LineColumn;
                 fn end($self: $S::Span) -> LineColumn;
                 fn before($self: $S::Span) -> $S::Span;
@@ -293,6 +295,7 @@ mark_noop! {
     &'_ str,
     String,
     u8,
+    u32,
     usize,
     Delimiter,
     LitKind,
@@ -519,3 +522,7 @@ pub struct ExpnGlobals<Span> {
 compound_traits!(
     struct ExpnGlobals<Span> { def_site, call_site, mixed_site }
 );
+
+compound_traits!(
+    struct Range<T> { start, end }
+);
diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs
index 938935771d6..e37015f2c3b 100644
--- a/library/proc_macro/src/lib.rs
+++ b/library/proc_macro/src/lib.rs
@@ -44,7 +44,7 @@ mod diagnostic;
 pub use diagnostic::{Diagnostic, Level, MultiSpan};
 
 use std::cmp::Ordering;
-use std::ops::RangeBounds;
+use std::ops::{Range, RangeBounds};
 use std::path::PathBuf;
 use std::str::FromStr;
 use std::{error, fmt, iter};
@@ -488,6 +488,12 @@ impl Span {
         Span(self.0.source())
     }
 
+    /// Returns the spans byte position range in the source file.
+    #[unstable(feature = "proc_macro_span", issue = "54725")]
+    pub fn position(&self) -> Range<u32> {
+        self.0.position()
+    }
+
     /// Gets the starting line/column in the source file for this span.
     #[unstable(feature = "proc_macro_span", issue = "54725")]
     pub fn start(&self) -> LineColumn {