Skip to content

Commit

Permalink
Merge pull request #442 from dtolnay/byterange
Browse files Browse the repository at this point in the history
Expose Span::byte_range
  • Loading branch information
dtolnay authored Jan 21, 2024
2 parents da1be4d + 1082767 commit 6cefaec
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
6 changes: 5 additions & 1 deletion build/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

extern crate proc_macro;

use core::ops::RangeBounds;
use core::ops::{Range, RangeBounds};
use proc_macro::{Literal, Span};

pub fn byte_range(this: &Span) -> Range<usize> {
this.byte_range()
}

pub fn join(this: &Span, other: Span) -> Option<Span> {
this.join(other)
}
Expand Down
33 changes: 27 additions & 6 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use core::cell::RefCell;
use core::cmp;
use core::fmt::{self, Debug, Display, Write};
use core::mem::ManuallyDrop;
#[cfg(span_locations)]
use core::ops::Range;
use core::ops::RangeBounds;
use core::ptr;
use core::str::FromStr;
Expand Down Expand Up @@ -372,7 +374,7 @@ impl FileInfo {
span.lo >= self.span.lo && span.hi <= self.span.hi
}

fn source_text(&mut self, span: Span) -> String {
fn byte_range(&mut self, span: Span) -> Range<usize> {
let lo_char = (span.lo - self.span.lo) as usize;

// Look up offset of the largest already-computed char index that is
Expand Down Expand Up @@ -401,11 +403,15 @@ impl FileInfo {

let trunc_lo = &self.source_text[lo_byte..];
let char_len = (span.hi - span.lo) as usize;
let source_text = match trunc_lo.char_indices().nth(char_len) {
Some((offset, _ch)) => &trunc_lo[..offset],
None => trunc_lo,
};
source_text.to_owned()
lo_byte..match trunc_lo.char_indices().nth(char_len) {
Some((offset, _ch)) => lo_byte + offset,
None => self.source_text.len(),
}
}

fn source_text(&mut self, span: Span) -> String {
let byte_range = self.byte_range(span);
self.source_text[byte_range].to_owned()
}
}

Expand Down Expand Up @@ -547,6 +553,21 @@ impl Span {
})
}

#[cfg(span_locations)]
pub fn byte_range(&self) -> Range<usize> {
#[cfg(fuzzing)]
return 0..0;

#[cfg(not(fuzzing))]
{
if self.is_call_site() {
0..0
} else {
SOURCE_MAP.with(|sm| sm.borrow_mut().fileinfo_mut(*self).byte_range(*self))
}
}
}

#[cfg(span_locations)]
pub fn start(&self) -> LineColumn {
#[cfg(fuzzing)]
Expand Down
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ use crate::marker::{ProcMacroAutoTraits, MARKER};
use core::cmp::Ordering;
use core::fmt::{self, Debug, Display};
use core::hash::{Hash, Hasher};
#[cfg(span_locations)]
use core::ops::Range;
use core::ops::RangeBounds;
use core::str::FromStr;
use std::error::Error;
Expand Down Expand Up @@ -472,6 +474,21 @@ impl Span {
SourceFile::_new(self.inner.source_file())
}

/// Returns the span's byte position range in the source file.
///
/// This method requires the `"span-locations"` feature to be enabled.
///
/// When executing in a procedural macro context, the returned range is only
/// accurate if compiled with a nightly toolchain. The stable toolchain does
/// not have this information available. When executing outside of a
/// procedural macro, such as main.rs or build.rs, the byte range is always
/// accurate regardless of toolchain.
#[cfg(span_locations)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
pub fn byte_range(&self) -> Range<usize> {
self.inner.byte_range()
}

/// Get the starting line/column in the source file for this span.
///
/// This method requires the `"span-locations"` feature to be enabled.
Expand Down
13 changes: 13 additions & 0 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::detection::inside_proc_macro;
use crate::location::LineColumn;
use crate::{fallback, Delimiter, Punct, Spacing, TokenTree};
use core::fmt::{self, Debug, Display};
#[cfg(span_locations)]
use core::ops::Range;
use core::ops::RangeBounds;
use core::str::FromStr;
use std::panic;
Expand Down Expand Up @@ -460,6 +462,17 @@ impl Span {
}
}

#[cfg(span_locations)]
pub fn byte_range(&self) -> Range<usize> {
match self {
#[cfg(proc_macro_span)]
Span::Compiler(s) => s.byte_range(),
#[cfg(not(proc_macro_span))]
Span::Compiler(_) => 0..0,
Span::Fallback(s) => s.byte_range(),
}
}

#[cfg(span_locations)]
pub fn start(&self) -> LineColumn {
match self {
Expand Down

0 comments on commit 6cefaec

Please sign in to comment.