Skip to content

Commit

Permalink
Re-add updated Span::start()/end()/line()/column() span-locations met…
Browse files Browse the repository at this point in the history
…hods

Updated for rust-lang/rust#111571
  • Loading branch information
MingweiSamuel committed Jun 30, 2023
1 parent 10f5dc3 commit d7f8ce8
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
**/*.rs.bk
Cargo.lock
/.vscode
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ rustversion = "1"
proc-macro = []
default = ["proc-macro"]

# Expose methods Span::start and Span::end which give the line/column location
# Expose the old Span::start and Span::end methods which give the line/column location
# of a token.
span-locations = []

Expand Down
51 changes: 29 additions & 22 deletions src/fallback.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(span_locations)]
use crate::location::LineColumn;
use crate::parse::{self, Cursor};
use crate::rcvec::{RcVec, RcVecBuilder, RcVecIntoIter, RcVecMut};
use crate::{Delimiter, Spacing, TokenTree};
Expand Down Expand Up @@ -357,21 +355,16 @@ struct FileInfo {

#[cfg(all(span_locations, not(fuzzing)))]
impl FileInfo {
fn offset_line_column(&self, offset: usize) -> LineColumn {
/// Returns `(line, column)`.
fn offset_line_column(&self, offset: usize) -> (usize, usize) {
assert!(self.span_within(Span {
lo: offset as u32,
hi: offset as u32
}));
let offset = offset - self.span.lo as usize;
match self.lines.binary_search(&offset) {
Ok(found) => LineColumn {
line: found + 1,
column: 0,
},
Err(idx) => LineColumn {
line: idx,
column: offset - self.lines[idx - 1],
},
Ok(found) => (found + 1, 0),
Err(idx) => (idx, offset - self.lines[idx - 1]),
}
}

Expand Down Expand Up @@ -516,9 +509,26 @@ impl Span {
}

#[cfg(span_locations)]
pub fn start(&self) -> LineColumn {
pub fn start(&self) -> Self {
Self {
lo: self.lo,
hi: self.lo,
}
}

#[cfg(span_locations)]
pub fn end(&self) -> Self {
Self {
lo: self.hi,
hi: self.hi,
}
}

/// Helper: returns `(line, column)`.
#[cfg(span_locations)]
fn line_column(&self) -> (usize, usize) {
#[cfg(fuzzing)]
return LineColumn { line: 0, column: 0 };
return (0, 0);

#[cfg(not(fuzzing))]
SOURCE_MAP.with(|cm| {
Expand All @@ -529,16 +539,13 @@ impl Span {
}

#[cfg(span_locations)]
pub fn end(&self) -> LineColumn {
#[cfg(fuzzing)]
return LineColumn { line: 0, column: 0 };
pub fn line(&self) -> usize {
self.line_column().0
}

#[cfg(not(fuzzing))]
SOURCE_MAP.with(|cm| {
let cm = cm.borrow();
let fi = cm.fileinfo(*self);
fi.offset_line_column(self.hi as usize)
})
#[cfg(span_locations)]
pub fn column(&self) -> usize {
self.line_column().1
}

#[cfg(not(span_locations))]
Expand Down
39 changes: 28 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ mod imp;

#[cfg(span_locations)]
mod convert;
#[cfg(span_locations)]
mod location;

use crate::extra::DelimSpan;
use crate::marker::Marker;
Expand All @@ -157,9 +155,6 @@ use std::error::Error;
#[cfg(procmacro2_semver_exempt)]
use std::path::PathBuf;

#[cfg(span_locations)]
pub use crate::location::LineColumn;

/// An abstract stream of tokens, or more concretely a sequence of token trees.
///
/// This type provides interfaces for iterating over token trees and for
Expand Down Expand Up @@ -461,7 +456,27 @@ impl Span {
SourceFile::_new(self.inner.source_file())
}

/// Get the starting line/column in the source file for this span.
/// Creates an empty span pointing to directly before this span.
///
/// This method requires the `"span-locations"` feature to be enabled.
#[cfg(span_locations)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
pub fn start(&self) -> Self {
Self::_new(self.inner.start())
}

/// Creates an empty span pointing to directly after this span.
///
/// This method requires the `"span-locations"` feature to be enabled.
#[cfg(span_locations)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
pub fn end(&self) -> Self {
Self::_new(self.inner.end())
}

/// The one-indexed line of the source file where the span starts.
///
/// To obtain the line of the span's end, use `span.end().line()`.
///
/// This method requires the `"span-locations"` feature to be enabled.
///
Expand All @@ -472,11 +487,13 @@ impl Span {
/// line/column are always meaningful regardless of toolchain.
#[cfg(span_locations)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
pub fn start(&self) -> LineColumn {
self.inner.start()
pub fn line(&self) -> usize {
self.inner.line()
}

/// Get the ending line/column in the source file for this span.
/// The one-indexed column of the source file where the span starts.
///
/// To obtain the column of the span's end, use `span.end().column()`.
///
/// This method requires the `"span-locations"` feature to be enabled.
///
Expand All @@ -487,8 +504,8 @@ impl Span {
/// line/column are always meaningful regardless of toolchain.
#[cfg(span_locations)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
pub fn end(&self) -> LineColumn {
self.inner.end()
pub fn column(&self) -> usize {
self.inner.column()
}

/// Create a new span encompassing `self` and `other`.
Expand Down
29 changes: 0 additions & 29 deletions src/location.rs

This file was deleted.

32 changes: 23 additions & 9 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::detection::inside_proc_macro;
#[cfg(span_locations)]
use crate::location::LineColumn;
use crate::{fallback, Delimiter, Punct, Spacing, TokenTree};
use core::fmt::{self, Debug, Display};
use core::iter::FromIterator;
Expand Down Expand Up @@ -468,22 +466,38 @@ impl Span {
}

#[cfg(span_locations)]
pub fn start(&self) -> LineColumn {
pub fn start(&self) -> Self {
match self {
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
Span::Fallback(s) => s.start(),
Self::Compiler(s) => Self::Compiler(s.start()),
Self::Fallback(s) => Self::Fallback(s.start()),
}
}

#[cfg(span_locations)]
pub fn end(&self) -> LineColumn {
pub fn end(&self) -> Self {
match self {
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
Span::Fallback(s) => s.end(),
Self::Compiler(s) => Self::Compiler(s.end()),
Self::Fallback(s) => Self::Fallback(s.end()),
}
}

pub fn join(&self, other: Span) -> Option<Span> {
#[cfg(span_locations)]
pub fn line(&self) -> usize {
match self {
Self::Compiler(s) => s.line(),
Self::Fallback(s) => s.line(),
}
}

#[cfg(span_locations)]
pub fn column(&self) -> usize {
match self {
Self::Compiler(s) => s.column(),
Self::Fallback(s) => s.column(),
}
}

pub fn join(&self, other: Self) -> Option<Span> {
let ret = match (self, other) {
#[cfg(proc_macro_span)]
(Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.join(b)?),
Expand Down
7 changes: 2 additions & 5 deletions tests/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ assert_impl!(TokenTree is not Send or Sync);

#[cfg(procmacro2_semver_exempt)]
mod semver_exempt {
use proc_macro2::{LineColumn, SourceFile};

assert_impl!(LineColumn is Send and Sync);
use proc_macro2::SourceFile;

assert_impl!(SourceFile is not Send or Sync);
}
Expand All @@ -68,7 +66,7 @@ mod unwind_safe {
Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
};
#[cfg(procmacro2_semver_exempt)]
use proc_macro2::{LineColumn, SourceFile};
use proc_macro2::SourceFile;
use std::panic::{RefUnwindSafe, UnwindSafe};

macro_rules! assert_unwind_safe {
Expand All @@ -94,7 +92,6 @@ mod unwind_safe {

#[cfg(procmacro2_semver_exempt)]
assert_unwind_safe! {
LineColumn
SourceFile
}
}
32 changes: 16 additions & 16 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,10 @@ fn literal_span() {

#[cfg(span_locations)]
{
assert_eq!(positive.span().start().column, 0);
assert_eq!(positive.span().end().column, 3);
assert_eq!(negative.span().start().column, 0);
assert_eq!(negative.span().end().column, 4);
assert_eq!(positive.span().start().column(), 0);
assert_eq!(positive.span().end().column(), 3);
assert_eq!(negative.span().start().column(), 0);
assert_eq!(negative.span().end().column(), 4);
assert_eq!(subspan.unwrap().source_text().unwrap(), ".");
}

Expand Down Expand Up @@ -432,11 +432,11 @@ testing 123
#[test]
fn default_span() {
let start = Span::call_site().start();
assert_eq!(start.line, 1);
assert_eq!(start.column, 0);
assert_eq!(start.line(), 1);
assert_eq!(start.column(), 0);
let end = Span::call_site().end();
assert_eq!(end.line, 1);
assert_eq!(end.column, 0);
assert_eq!(end.line(), 1);
assert_eq!(end.column(), 0);
let source_file = Span::call_site().source_file();
assert_eq!(source_file.path().to_string_lossy(), "<unspecified>");
assert!(!source_file.is_real());
Expand Down Expand Up @@ -469,10 +469,10 @@ fn span_join() {

let start = joined1.unwrap().start();
let end = joined1.unwrap().end();
assert_eq!(start.line, 1);
assert_eq!(start.column, 0);
assert_eq!(end.line, 2);
assert_eq!(end.column, 3);
assert_eq!(start.line(), 1);
assert_eq!(start.column(), 0);
assert_eq!(end.line(), 2);
assert_eq!(end.column(), 3);

assert_eq!(
joined1.unwrap().source_file(),
Expand Down Expand Up @@ -717,12 +717,12 @@ fn check_spans_internal(ts: TokenStream, lines: &mut &[(usize, usize, usize, usi
*lines = rest;

let start = i.span().start();
assert_eq!(start.line, sline, "sline did not match for {}", i);
assert_eq!(start.column, scol, "scol did not match for {}", i);
assert_eq!(start.line(), sline, "sline did not match for {}", i);
assert_eq!(start.column(), scol, "scol did not match for {}", i);

let end = i.span().end();
assert_eq!(end.line, eline, "eline did not match for {}", i);
assert_eq!(end.column, ecol, "ecol did not match for {}", i);
assert_eq!(end.line(), eline, "eline did not match for {}", i);
assert_eq!(end.column(), ecol, "ecol did not match for {}", i);

if let TokenTree::Group(g) = i {
check_spans_internal(g.stream().clone(), lines);
Expand Down

0 comments on commit d7f8ce8

Please sign in to comment.