added byte position range for proc_macro::Span

This commit is contained in:
Michael van Straten 2023-03-10 21:16:35 +01:00
parent 35a0961bbc
commit b8c6d2211e
3 changed files with 19 additions and 2 deletions

View File

@ -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() }

View File

@ -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 }
);

View File

@ -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 {