Skip to content

Commit 1151ea6

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 1f5768b + 342c5fb commit 1151ea6

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+10-1
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,15 @@ impl server::Span for Rustc<'_, '_> {
634634
span.source_callsite()
635635
}
636636

637+
fn byte_range(&mut self, span: Self::Span) -> Range<usize> {
638+
let source_map = self.sess().source_map();
639+
640+
let relative_start_pos = source_map.lookup_byte_offset(span.lo()).pos;
641+
let relative_end_pos = source_map.lookup_byte_offset(span.hi()).pos;
642+
643+
Range { start: relative_start_pos.0 as usize, end: relative_end_pos.0 as usize }
644+
}
645+
637646
fn start(&mut self, span: Self::Span) -> LineColumn {
638647
let loc = self.sess().source_map().lookup_char_pos(span.lo());
639648
LineColumn { line: loc.line, column: loc.col.to_usize() }

library/proc_macro/src/bridge/mod.rs

+6
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+
);

library/proc_macro/src/lib.rs

+7-1
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 {

src/tools/rust-analyzer/crates/proc-macro-srv/src/abis/abi_sysroot/ra_server.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use token_stream::TokenStreamBuilder;
2020
mod symbol;
2121
pub use symbol::*;
2222

23-
use std::ops::Bound;
23+
use std::ops::{Bound, Range};
2424

2525
use crate::tt;
2626

@@ -298,6 +298,10 @@ impl server::Span for RustAnalyzer {
298298
// FIXME handle span
299299
span
300300
}
301+
fn byte_range(&mut self, _span: Self::Span) -> Range<usize> {
302+
// FIXME handle span
303+
Range { start: 0, end: 0 }
304+
}
301305
fn start(&mut self, _span: Self::Span) -> LineColumn {
302306
// FIXME handle span
303307
LineColumn { line: 0, column: 0 }

0 commit comments

Comments
 (0)